2

Recently I've learned that CuPy would utilize GPU to accelerate the computation in deep learning. However, after the following step I still got an unsolvable error:

  1. I installed CuPy correctly through https://docs.cupy.dev/en/stable/install.html. Other packages are also correctly installed.
  2. The original code is shown below, in file A:
import h5py
import torch
import numpy as np
import cupy as cp
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils


class cupy_Dataset(Dataset):

    def __init__(self, file_dir):

        super(cupy_Dataset, self).__init__()
        data = h5py.File(file_dir)

            self.ms = cp.array(data.get("ms"))
            self.lms = cp.array(data.get("lms"))
            self.pan = cp.array(data.get("pan"))
            self.gt = cp.array(data.get("gt"))

            self.ms = (self.ms[0:10, :, :, :])
            self.lms = (self.lms[0:10, :, :, :])
            self.pan = (self.pan[0:10, :, :])
            self.gt = (self.gt[0:10, :, :, :])

            self.ms = torch.utils.dlpack.from_dlpack(self.ms.toDLpack()).float()
            self.lms = torch.utils.dlpack.from_dlpack(self.lms.toDLpack()).float()
            self.pan = torch.utils.dlpack.from_dlpack(self.pan.toDLpack()).float()
            self.gt = torch.utils.dlpack.from_dlpack(self.gt.toDLpack()).float()

    def __len__(self):
        return len(self.gt.shape[0])

    def __getitem__(self, idx):

        ms = self.ms[idx, :, :, :]
        lms = self.lms[idx, :, :, :]
        pan = self.pan[idx, :, :, :]
        gt = self.gt[idx, :, :, :]

        return ms, lms, pan, gt

if __name__ == '__main__':
    train_dataset = cupy_WorldView3_Dataset(file_dir='./training_data/train.mat')
    print(train_dataset.gt.shape)

in file B:

import torch
import cupy
from {file_A} import cupy_Dataset

train_dataset = cupy_Dataset(file_dir='./training_data/train.mat')
gt = train_dataset.gt
pan = train_dataset.pan
lms = train_dataset.lms
ms = train_dataset.ms

print(type(gt))
  1. The outcome in file A is shown as:
Traceback (most recent call last):
  File "{file path}", line 94, in <module>
    train_dataset = cupy_WorldView3_Dataset(file_dir='./training_data/train.mat')
  File "{file path}", line 76, in __init__
    self.ms = torch.utils.dlpack.from_dlpack(self.ms.toDLpack()).float()
AttributeError: module 'torch.utils' has no attribute 'dlpack'

The outcome in file B is shown as:

Using backend: pytorch
Traceback (most recent call last):
  File "/home/office-desktop/.pycharm_helpers/pydev/pydevd.py", line 1477, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/office-desktop/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "{file path}", line 6, in <module>
    train_dataset = cupy_Dataset(file_dir='./training_data/train.mat')
  File "{file path}", line 76, in __init__
    self.ms = torch.utils.dlpack.from_dlpack(self.ms.toDLpack()).float()
AttributeError: 'cupy.core.core.ndarray' object has no attribute 'toDLpack'

Process finished with exit code 1

FYI, my basic thoughts are using DLpack as the interconnection between CuPy and PyTorch tensor. The initial usage and syntax can be found in https://docs.cupy.dev/en/stable/reference/interoperability.html and https://pytorch.org/docs/stable/dlpack.html.

Why is this? Thank you in advance. If you need any additional info, I am willing to provide :)

0 Answers0