Hello everyone!
I am new in PyTorch and I tried to implement randomly scaling the input images (e.g. from 0.5 to 1.5) during training which is mentioned in the Deeplab paper. Here is the code.
class RandomScale(object):
def __init__(self, limit):
self.limit = limit
def __call__(self, sample):
img = sample['image']
mask = sample['label']
assert img.size == mask.size
scale = random.uniform(self.limit[0], self.limit[1])
w = int(scale * img.size[0])
h = int(scale * img.size[1])
img, mask = img.resize((w, h), Image.BILINEAR), mask.resize((w, h), Image.NEAREST)
return {'image': img, 'label': mask}
# Augmentation
composed_transforms_tr = transforms.Compose([
RandomScale((0.5, 1.5)),
RandomHorizontalFlip(),
ToTensor()])
However when just started to train there raised an error:
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 665 and 420 in dimension 2 at c:\new-builder_3\win-wheel\pytorch\aten\src\th\generic/THTensorMath.cpp:3616
This is because images in the same batch need to be the same size. 665 and 420 in the error above are heights of input images after randomly scaling.
So I wonder if there is a way to transform batch of images simultaneously. Or can someone help to provide a suggestion to implement randomly scaling inputs?
Thank you very much!