AttributeError: 'MpoImageFile' object has no attribute 'shape'
images, labels = next(iter(self.loader))
grid = torchvision.utils.make_grid(images)



  images, labels = next(iter(self.loader))


triggers the error.

I have a custom dataset class where I load each image (RGB) from an url :


  image = Image.open(urllib.request.urlopen(URL))


and I apply some albumentations transforms.

The code works when I read an image for which I have a path using cv2.
However, it doesn't work when I read an image from the url. 
Note that I verified that the urls aren't broken. 

Here's the traceback:

/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    344     def __next__(self):
    345         index = self._next_index()  # may raise StopIteration
--&gt; 346         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    347         if self._pin_memory:
    348             data = _utils.pin_memory.pin_memory(data)

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py in &lt;listcomp&gt;(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

/content/transform_dataset.py in __getitem__(self, idx)
     49     labels = torch.from_numpy(item[2:].values.astype("float32"))
     50     #print("self.root,item,self.image_transform,self.transform,self.size", self.root,item,self.image_transform,self.transform,self.size)
---&gt; 51     image = load_image(self.root,item.ID,item.URL,self.image_transform)
     52     return image, labels
     53 

/content/transform_dataset.py in load_image(root, ID, URL, image_transform)
     81     print(image.shape)
     82     image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
---&gt; 83   image = image_transform(image=image)["image"]
     84   return image

/usr/local/lib/python3.6/dist-packages/albumentations/core/composition.py in __call__(self, **data)
    169                                               convert_keypoints_to_albumentations, data)
    170 
--&gt; 171             data = t(**data)
    172 
    173             if dual_start_end is not None and idx == dual_start_end[1]:

/usr/local/lib/python3.6/dist-packages/albumentations/core/transforms_interface.py in __call__(self, **kwargs)
     26         if (random.random() &lt; self.p) or self.always_apply:
     27             params = self.get_params()
---&gt; 28             params = self.update_params(params, **kwargs)
     29             if self.targets_as_params:
     30                 targets_as_params = {k: kwargs[k] for k in self.targets_as_params}

/usr/local/lib/python3.6/dist-packages/albumentations/core/transforms_interface.py in update_params(self, params, **kwargs)
     66         if hasattr(self, 'interpolation'):
     67             params['interpolation'] = self.interpolation
---&gt; 68         params.update({'cols': kwargs['image'].shape[1], 'rows': kwargs['image'].shape[0]})
     69         return params
     70 

AttributeError: 'MpoImageFile' object has no attribute 'shape'


In order to work with albumentations, you must pass a numpy array to the transforms not a PIL image. So:

image = Image.open(urllib.request.urlopen(URL))
image = np.array(image)

