Hello fellow Pytorchers,
I am trying to add normalization to the custom Dataset class Pytorch provides inside this tutorial.
The problem is that it gives always the same error:
TypeError: tensor is not a torch image.
As you can see inside ToTensor() method it returns:
return {‘image’: torch.from_numpy(image),‘masks’: torch.from_numpy(landmarks)} so I think it returns a tensor already.
I give you my code:
class Rescale(object):
"""Rescale the image in a sample to a given size.
Args:
output_size (tuple or int): Desired output size. If tuple, output is
matched to output_size. If int, smaller of image edges is matched
to output_size keeping aspect ratio the same.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
self.output_size = output_size
def __call__(self, sample):
image, landmarks = sample['image'], sample['masks']
h, w = image.shape[:2]
if isinstance(self.output_size, int):
if h > w:
new_h, new_w = self.output_size * h / w, self.output_size
else:
new_h, new_w = self.output_size, self.output_size * w / h
else:
new_h, new_w = self.output_size
new_h, new_w = int(new_h), int(new_w)
img = transform.resize(image, (new_h, new_w))
# h and w are swapped for landmarks because for images,
# x and y axes are axis 1 and 0 respectively
#landmarks = landmarks
return {'image': img, 'masks': landmarks}
class ToTensor(object):
"""Convert ndarrays in sample to Tensors."""
def __call__(self, sample):
image, landmarks = sample['image'], np.array(sample['masks'])
# swap color axis because
# numpy image: H x W x C
# torch image: C X H X W
image = image.transpose(2,0,1)
return {'image': torch.from_numpy(image),'masks': torch.from_numpy(landmarks)}
transformed_train_dataset = MasksTrainDataset(csv_file='pneumo_input/train/train-rle.csv',
root_dir='pneumo_input/train/images/256/dicom/',
transform=transforms.Compose([
Rescale(224),
ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],std=[0.5, 0.5, 0.5])
]))
for i in range(len(transformed_train_dataset)):
sample = transformed_train_dataset[i]
print(i, sample['image'].size(), sample['masks'])
if i == 3:
break
train_dataloader = DataLoader(transformed_train_dataset, batch_size=4,
shuffle=True, num_workers=4)
I am using grayscale images converted to RGB.
Thank you in advance!