error occurs when loading data in pytorch: 'Image' object has no attribute 'shape'
I am finetuning resnet152 using the code based on ImageNet training in PyTorch, and error occurs when I load the data, and it occured only after handling several batches of images. How can I solve the problem.
Following code is  the simplified code that produces the same error :

code

# Data loading code
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])

train_loader = torch.utils.data.DataLoader(
    datasets.ImageFolder(train_img_dir, transforms.Compose([
        transforms.RandomSizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        normalize,
    ])),
    batch_size=256, shuffle=True,
    num_workers=1, pin_memory=True)
for i, (input_x, target) in enumerate(train_loader):
    if i % 10 == 0:
        print(i)
        print(input_x.shape)
        print(target.shape)


error

0
torch.Size([256, 3, 224, 224])
torch.Size([256])
10
torch.Size([256, 3, 224, 224])
torch.Size([256])
20
torch.Size([256, 3, 224, 224])
torch.Size([256])
30
torch.Size([256, 3, 224, 224])
torch.Size([256])
----------------------------------------------------------------------
AttributeError                       Traceback (most recent call last)
&lt;ipython-input-48-792d6ca206df&gt; in &lt;module&gt;()
----&gt; 1 for i, (input_x, target) in enumerate(train_loader):
      2     if i % 10 == 0:
      3 #     sample_img = input_x[0]
      4         print(i)
      5         print(input_x.shape)

/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    200                 self.reorder_dict[idx] = batch
    201                 continue
--&gt; 202             return self._process_next_batch(batch)
    203 
    204     next = __next__  # Python 2 compatibility

/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
    220         self._put_indices()
    221         if isinstance(batch, ExceptionWrapper):
--&gt; 222             raise batch.exc_type(batch.exc_msg)
    223         return batch
    224 

AttributeError: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 41, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/usr/local/lib/python3.5/dist-packages/torch/utils/data/dataloader.py", line 41, in &lt;listcomp&gt;
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "/usr/local/lib/python3.5/dist-packages/torchvision-0.1.9-py3.5.egg/torchvision/datasets/folder.py", line 118, in __getitem__
    img = self.transform(img)
  File "/usr/local/lib/python3.5/dist-packages/torchvision-0.1.9-py3.5.egg/torchvision/transforms.py", line 369, in __call__
    img = t(img)
  File "/usr/local/lib/python3.5/dist-packages/torchvision-0.1.9-py3.5.egg/torchvision/transforms.py", line 706, in __call__
    i, j, h, w = self.get_params(img)
  File "/usr/local/lib/python3.5/dist-packages/torchvision-0.1.9-py3.5.egg/torchvision/transforms.py", line 693, in get_params
    w = min(img.size[0], img.shape[1])
AttributeError: 'Image' object has no attribute 'shape'


There's a bug in transforms.RandomSizedCrop.get_params(). In the last line of your error message, it should be img.size instead of img.shape.

The lines containing the bug will only be executed if the cropping failed for 10 consecutive times (where it falls back to central crop). That's why this error does not occur for every batch of images.

I've submitted a PR to fix it. For a quick fix, you can edit your /usr/local/lib/python3.5/dist-packages/torchvision-0.1.9-py3.5.egg/torchvision/transforms.py file and change all img.shape to img.size.

EDIT: the PR is merged. You can install the latest torchvision on GitHub to fix it.
