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

问题描述 投票:0回答:2

我正在使用自定义的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)
pytorch
2个回答
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维张量。


0
投票

您可以使用无:

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

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

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

我从here得到了提示。

© www.soinside.com 2019 - 2024. All rights reserved.