I have modified the custom RandomCrop transform given in this tutorial - https://pytorch.org/tutorials/beginner/data_loading_tutorial.html as follows:
class RandomCrop(object):
"""Crop randomly the image in a sample.
Args:
output_size (tuple or int): Desired output size. If int, square crop
is made.
"""
def __init__(self, output_size):
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
image,image_id,label_id = sample['image'], sample['image_id'], sample['label_id']
h, w = image.shape[:2]
new_h, new_w = self.output_size
top = np.random.randint(0, h - new_h)
left = np.random.randint(0, w - new_w)
image = image[top: top + new_h,
left: left + new_w]
return {'image': image, 'image_id':image_id,'label_id':label_id}
When I run this, I get the following Value error after a few iterations:
ValueError: Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 55, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 55, in <listcomp>
samples = collate_fn([dataset[i] for i in batch_indices])
File "<ipython-input-20-17b45e03499d>", line 39, in __getitem__
sample = self.transform(sample)
File "/usr/local/lib/python3.6/dist-packages/torchvision/transforms/transforms.py", line 42, in __call__
img = t(img)
File "<ipython-input-23-f3687176a1f2>", line 23, in __call__
top = np.random.randint(0, h - new_h)
File "mtrand.pyx", line 993, in mtrand.RandomState.randint
ValueError: low >= hig
Can anyone help me in identifying the issue and how I go about resolving it ?