Pytorch 无法转换 np.ndarray 类型 numpy.object

Pytorch can't convert np.ndarray of type numpy.object

我正在尝试创建一个具有可变图像大小的 PyTorch 数据加载器。这是我的代码片段

def get_imgs(path_to_imgs):

    imgs = []
    for path in path_to_imgs:

        imgs.append(cv2.imread(path))

    imgs = np.asarray(imgs)    

    return imgs   

上面的函数获取路径列表并将图像从路径加载到列表 'imgs'。顺便说一句,图像的大小不相等。该列表看起来像 imgs = [NumPy array, NumPy array ....]。但是,当我将列表转换为 np.asarray 时,它会将列表转换为 dtype = object.

这是我的数据加载器class

class Dataset(torch.utils.data.Dataset):

  def __init__(self, path_to_imgs, path_to_label):
        'Initialization'
        self.path_to_imgs = path_to_imgs
        self.path_to_label = path_to_label

        self.imgs = get_imgs(path_to_imgs)
        self.label = get_pts(path_to_label)

        self.imgs = torch.Tensor(self.imgs)             **Error here
        # self.imgs = torch.from_numpy(self.imgs)       ** I tried this as well. Same error

        self.label = torch.Tensor(self.label)

        self.len = len(self.imgs)

  def __len__(self):
        'Denotes the total number of samples'
        return self.len

  def __getitem__(self, index):

        return self.imgs, self.label

当我尝试将图像列表转换为张量时** 它失败并给出以下错误

can't convert np.ndarray of type numpy.object_. The only supported types are: float64, float32, float16, int64, int32, int16, int8, uint8, and bool.

我看过类似的问题 and 但它们没有帮助。

def get_imgs(path_to_imgs):

    imgs = []
    for path in path_to_imgs:
        imgs.append(torch.Tensor(cv2.imread(path)))

    return imgs
class Dataset(torch.utils.data.Dataset):
    def __init__(self, path_to_imgs, path_to_label):
        'Initialization'
        self.path_to_imgs = path_to_imgs
        self.path_to_label = path_to_label

        self.imgs = get_imgs(path_to_imgs)
        self.label = get_pts(path_to_label)

        # padding ops here (https://pytorch.org/docs/stable/nn.html#padding-layers)
        # for img in self.imgs:
        #     ...

        self.label = torch.Tensor(self.label)

        self.len = len(self.imgs)

    def __len__(self):
        'Denotes the total number of samples'
        return self.len

    def __getitem__(self, index):

        return self.imgs, self.label