pythonmachine-learningdeep-learningdatasetresnet

Ran into "TypeError: '<' not supported between instances of 'Tensor' and 'list'" when going through dataset


I am replicating ResNet (source: https://arxiv.org/abs/1512.03385).

I ran into the error "TypeError: '<' not supported between instances of 'Tensor' and 'list'" when trying to go through several different dataset in different sections of my code.

I tried different fixes but none worked: (i) I deleted enumerate cause I worried that using this may cause the problem (ii) I tried to go through dataloader rather than dataset but it didn't work

1st time: When I tried to view images:

for images, _ in train_loader:
    print('images.shape:', images.shape)
    plt.figure(figsize=(16,8))
    plt.axis('off')
    plt.imshow(torchvision.utils.make_grid(images, nrow=16).permute((1, 2, 0)))
    break

2nd/3rd time: when I tried to validate/test the resnet:

with torch.no_grad():
    for j, inputs, labels in enumerate(test_loader, start=0):
        outputs = resnet_models[i](inputs) 
        _, prediction = torch.max(outputs, dim=1)        

You may notice that I didn't run into this error when training the resnet, and the code is quite similar:

for batch, data in enumerate(train_dataloader, start=0): 
    inputs, labels = data
    inputs, labels = inputs.to(device), labels.to(device) 

Error message (taking the first error as an example. The rest is pretty much the same)

TypeError Traceback (most recent call last) Input In [38], in <cell line: 8>() 6 print("Images AFTER NORMALIZATION") 7 print("--------------------------") ----> 8 for images, _ in training_data: 9 sort=False 10 print('images.shape:', images.shape)

File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torch/utils/data/dataset.py:471, in Subset.getitem(self, idx) 469 if isinstance(idx, list): 470 return self.dataset[[self.indices[i] for i in idx]] --> 471 return self.dataset[self.indices[idx]]

File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torchvision/datasets/cifar.py:118, in CIFAR10.getitem(self, index) 115 img = Image.fromarray(img) 117 if self.transform is not None: --> 118 img = self.transform(img) 120 if self.target_transform is not None: 121 target = self.target_transform(target)

File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torchvision/transforms/transforms.py:95, in Compose.call(self, img) 93 def call(self, img): 94 for t in self.transforms: ---> 95 img = t(img) 96 return img

File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs) 1106 # If we don't have any hooks, we want to skip the rest of the logic in 1107 # this function, and just call forward. 1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks >or _global_backward_hooks 1109 or _global_forward_hooks or _global_forward_pre_hooks): -> 1110 return forward_call(*input, **kwargs) 1111 # Do not call functions when jit is used 1112 full_backward_hooks, non_full_backward_hooks = [], []

File ~/miniconda3/envs/resnet/lib/python3.9/site->packages/torchvision/transforms/transforms.py:707, in RandomHorizontalFlip.forward(self, >img) 699 def forward(self, img): 700 """ 701 Args: 702 img (PIL Image or Tensor): Image to be flipped. (...) 705 PIL Image or Tensor: Randomly flipped image. 706 """ --> 707 if torch.rand(1) < self.p: 708 return F.hflip(img) 709 return img

TypeError: '<' not supported between instances of 'Tensor' and 'list'


Solution

  • I was having the same error message, probably under different circumstances, but I just found my own bug and figured I would share it anyway for various readers. I was using a torchvision transformation in my dataset, which the dataloader was loading from. The transformation was

    torchvision.transforms.RandomHorizontalFlip([0.5]),

    and the error is that the input to this transformation should not be a list but should be

    torchvision.transforms.RandomHorizontalFlip(0.5),

    So if there is anything I can recommend, it's just that maybe there is some list argument being passed through that shouldn't be in some transformation or otherwise.