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?