如何在自定义数据集中获得正确的张量形状

时间:2020-05-08 21:59:31

标签: pytorch

我正在使用自定义Dataset类,但是问题是,当我从Dataloader获取数据时,剩下的array的张量形状与我想要的张量形状不同。

我得到的

形状:torch.Size([1, 56, 128, 128]) 我想要的形状:torch.Size([1, 56, 1, 128, 128])

我的方法是:

1)在numpy.expand_dims上应用array并获得torch.Size([1, 1, 56, 128, 128])

2)然后转到np.transpose上的array得到我想要的形状torch.Size([1, 56, 1, 128, 128])

第一步后,我得到了错误:

raise ValueError('pic should be 2/3 dimensional. Got {} dimensions.'.format(pic.ndim))
ValueError: pic should be 2/3 dimensional. Got 4 dimensions.

如果我先进行转置,则np.transpose(array, axes=(1,2,0))的组合都不会产生形状torch.Size([56, 1, 128, 128])

如果我先将array转换为Tensor,然后再进行torch.unsqueeze,则会收到错误消息:


raise TypeError('pic should be PIL Image or ndarray. Got {}'.format(type(pic)))
TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>

这是我的代码:

class patientdataset(Dataset):
    def __init__(self, csv_file, root_dir, transform=None):
        self.annotations = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

    def __len__(self):
        return len(self.annotations)

    def __getitem__(self, index):
        img_path = os.path.join(self.root_dir, self.annotations.iloc[index,0])
        # np_load_old = np.load
        # np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)

        image= np.asarray(np.load(img_path))


        image= np.transpose(image, axes=(1,2,0))
        image = torch.Tensor (image)

        image = torch.unsqueeze(image, dim=1)



        y_label = torch.tensor(np.asarray(self.annotations.iloc[index,1]))

        if self.transform:
            image = self.transform(image)

        return (image, y_label)

2 个答案:

答案 0 :(得分:0)

不确定我的回答是否对您有帮助,但是您应该使用:

示例

import torch
import torchvision.transforms.functional as F
a = torch.FloatTensor(1, height, width)
a = F.to_pil_image(a)

注意。 torchvision中的图像必须表示为[Channel, Height, Width]形式的3维张量。

答案 1 :(得分:0)

您可以使用无:

x.shape
torch.Size([1, 56, 128, 128])

z = x[:,:,None,:,:]

z.shape
torch.Size([1, 56, 1, 128, 128])

我从here得到了提示。

相关问题