Value error when using Dataloader

Recently, when I am using pytorch to build a simple image classifier, a value error occurred when using Dataloader. Here is the error message:

Epoch: 1/20

Train Loss 0.5450 Acc0.3640
Traceback (most recent call last):
  File "ms_resnet.py", line 135, in <module>
    optimizer_resnet,lr_scheduler_resnet,num_epochs=20)
  File "ms_resnet.py", line 82, in get_model
    for data in test_loader:
  File "/home/guest/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 259, in __next__
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/guest/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 259, in <listcomp>
    batch = self.collate_fn([self.dataset[i] for i in indices])
  File "/home/jiahao/anaconda3/envs/guest/lib/python3.6/site-packages/torchvision/datasets/folder.py", line 124, in __getitem__
    img = self.transform(img)
  File "/home/jiahao/anaconda3/envs/guest/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 42, in __call__
    img = t(img)
  File "/home/jiahao/anaconda3/envs/guest/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 286, in __call__
    i, j, h, w = self.get_params(img, self.size)
  File "/home/jiahao/anaconda3/envs/guest/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 271, in get_params
    i = random.randint(0, h - th)
  File "/home/jiahao/anaconda3/envs/guest/lib/python3.6/random.py", line 220, in randint
    return self.randrange(a, b+1)
  File "/home/jiahao/anaconda3/envs/guest/lib/python3.6/random.py", line 198, in randrange
    raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
ValueError: empty range for randrange() (0,-15, -15)

and here is my code:

train_set=datasets.ImageFolder(os.path.join(data_dir,'train'),train_transform)
test_set=datasets.ImageFolder(os.path.join(data_dir,'val'),test_transform)
train_loader=torch.utils.data.DataLoader(train_set,batch_size=5,shuffle=True,num_workers=5)
test_loader=torch.utils.data.DataLoader(test_set,batch_size=5,shuffle=True,num_workers=5)
...

for epoch in range(num_epochs):
    ...
    model.train(True)
    for data in train_loader:
    ...
    #training process
    loss.backward()
    optimizer.step()

    model.train(False)
    for data in test_loader:
    ...
    #validation process

As you can see, the DataLoader in the training process works well, but it fails in the validation. I’ve googled it and I think maybe the something is wrong in the random.py file in python. So can anyone give some suggestion on how to deal with this?

Could you share some information about test_transform?
It seems you would like to perform something like a random cropping on you images and self.size seems to be misdefined.

Here is my image transforms code:


train_transform=transforms.Compose([
		transforms.CenterCrop(224),
		transforms.RandomRotation(5),
		transforms.ToTensor(),
#for transfer learning 
		transforms.Normalize(mean = [0.485, 0.456, 0.406],std = [0.229, 0.224, 0.225])])
test_transform=transforms.Compose([
		transforms.RandomCrop(224),
		transforms.RandomRotation(3),
		transforms.ToTensor(),
		transforms.Normalize(mean = [0.485, 0.456, 0.406],std = [0.229, 0.224, 0.225])])

Ok, it seems some images are smaller than your desired crop size of 224.
Could you please check this? Appenrently some images are 208 x 208, since the error message states randrange is called with -15. You can see the line of code here.

Code to reproduce the error:

test_transform=transforms.Compose([
    transforms.ToPILImage(),
	transforms.RandomCrop(224),
	transforms.RandomRotation(3),
	transforms.ToTensor(),
	transforms.Normalize(
        mean = [0.485, 0.456, 0.406],
        std = [0.229, 0.224, 0.225])]
)

x = torch.randn(3, 208, 208)
x_ = test_transform(x)
1 Like

Just like @ptrblck said, usually we resize the image a bit larger than the network inputs then we could use the crop fun correctly.

Oh yes, you are correct. I check some images and they do appear to be smaller than my desired size. I’ve changed the code to RandomResizedCrop() and it worked.
Thank you again for your kindness help.