在Pytorch中转换自定义数据集时出错

时间:2020-04-10 18:28:46

标签: python python-3.x pytorch dataloader

我正在遵循本教程:https://pytorch.org/tutorials/beginner/data_loading_tutorial.html是为MuNuSeg数据集创建自己的自定义数据加载器,但我有一个重点。数据加载器工作正常,但是当我向其中添加转换时,会出现错误。

我面临的问题与这里提到的类似: Error Utilizing Pytorch Transforms and Custom Dataset
根据答案,我对每个内置转换都进行了自定义转换,以便可以同时转换整个样本。以下是我的自定义转换

class AffineTrans(object):
def __init__(self, degrees, translate):
    self.degrees = degrees
    self.translate = translate

def __call__(self, sample):
    image, contour, clrmask = sample['image'], sample['contour'], sample['clrmask']
    TF = transforms.RandomAffine(degrees = self.degrees, translate=self.translate)
    image = TF(image)
    contour = TF(contour)
    clrmask = (clrmask)

class Flip(object):   
def __call__(self, sample):
    image, contour, clrmask = sample['image'], sample['contour'], sample['clrmask']
    TF1 = transforms.RandomHorizontalFlip()
    TF2 = transforms.RandomVerticalFlip()
    image = TF1(image)
    contour = TF1(contour)
    clrmask = TF1(clrmask)
    image = TF2(image)
    contour = TF2(contour)
    clrmask = TF2(clrmask)

class ClrJitter(object):
def __init__(self, brightness, contrast, saturation, hue):
    self.brightness = brightness
    self.contrast = contrast
    self.saturation = saturation
    self.hue = hue

def __call__(self, sample):
    image, contour, clrmask = sample['image'], sample['contour'], sample['clrmask']
    TF = transforms.ColorJitter(self.brightness, self.contrast, self.saturation, self.hue)
    image = TF(image)
    contour = TF(contour)
    clrmask = TF(clrmask)

并通过以下方式组成它们

composed = transforms.Compose([RandomCrop(256),
                           AffineTrans(15.0,(0.1,0.1)),
                           Flip(),    
                           ClrJitter(10, 10, 10, 0.01),
                           ToTensor()])

这是trainLoader代码

class trainLoader(Dataset):
def __init__(self, transform=None):
    """
    Args:
        transform (callable, optional): Optional transform to be applied
            on a sample.
    """
    [self.train_data , self.test_data1, self.test_data2] = dirload()
    self.transform = transform

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

def __getitem__(self, idx):
    if torch.is_tensor(idx):
        idx = idx.tolist()
    img_name = self.train_data[0][idx]
    contour_name = self.train_data[1][idx]
    color_name = self.train_data[2][idx]
    image = cv2.imread(img_name)
    contour = cv2.imread(contour_name)
    clrmask = cv2.imread(color_name)
    sample = {'image': image, 'contour': contour, 'clrmask': clrmask}

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

    return sample

要检查上面的代码是否正常工作,我正在执行以下操作

    train_dat = trainLoader(composed)

for i in range(len(train_dat)):
    sample = train_dat[i]

    print(i, sample['image'].shape, sample['contour'].shape, sample['clrmask'].shape)
    cv2.imshow('sample',sample['image'])
    cv2.waitKey()
    if i == 3:
        break

但是我仍然一次又一次遇到以下错误

runcell(0, 'F:/Moodle/SEM 8/SRE/code/MoNuSeg/main.py')

runcell(1, 'F:/Moodle/SEM 8/SRE/code/MoNuSeg/main.py')
Traceback (most recent call last):

  File "F:\Moodle\SEM 8\SRE\code\MoNuSeg\main.py", line 212, in <module>
    sample = train_dat[i]

  File "F:\Moodle\SEM 8\SRE\code\MoNuSeg\main.py", line 109, in __getitem__
    sample = self.transform(sample)

  File "F:\Moodle\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 60, in __call__
    img = t(img)

  File "F:\Moodle\SEM 8\SRE\code\MoNuSeg\main.py", line 156, in __call__
    image = TF(image)

  File "F:\Moodle\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 1018, in __call__
    ret = self.get_params(self.degrees, self.translate, self.scale, self.shear, img.size)

  File "F:\Moodle\Anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 992, in get_params
    max_dx = translate[0] * img_size[0]

TypeError: 'int' object is not subscriptable

这是一个模棱两可的错误,因为我完全不知道错误是什么

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

问题是您要传递NumPy数组,而转换需要一个PIL图像。您可以通过添加transforms.ToPILImage()作为第一个转换来解决此问题:

composed = transforms.Compose([
    transforms.ToPILImage(),
    RandomCrop(256),
    AffineTrans(15.0,(0.1,0.1)),
    Flip(),    
    ClrJitter(10, 10, 10, 0.01),
    ToTensor()
])

假设您开头有一个from torchvision import transforms

问题的根源是您正在使用OpenCV加载图像:

def __getitem__(self, idx):
    # [...]
    image = cv2.imread(img_name)

,您也可以替换这些从OpenCV到PIL的加载调用,以解决该问题。


请注意,对于NumPy数组,.size()返回一个int,这将导致您遇到问题。检查以下代码的区别:

import numpy as np
from PIL import Image

# NumPy
img = np.zeros((30, 30))
print(img.size)  # output: 900

# PIL
pil_img = Image.fromarray(img)
print(pil_img.size)  # output: (30, 30)
相关问题