Applying Transforms to a single image:

Following is my code:

from torchvision import datasets, models, transforms
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

import torch



data_transforms = transforms.Compose([
        transforms.RandomResizedCrop(256),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])

img = mpimg.imread('dataset/train/cats/cat.1.jpg')
print(type(img))
print(len(img))
# img = img.astype(float)
# img = img.float()
print(img)
img = data_transforms(img)

Error Message:

Traceback (most recent call last):
  File "Image_Preprocessing.py", line 47, in <module>
    img = data_transforms(img)
  File "/home/debayon/anaconda3/envs/ENV2/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 61, in __call__
    img = t(img)
  File "/home/debayon/anaconda3/envs/ENV2/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 676, in __call__
    i, j, h, w = self.get_params(img, self.scale, self.ratio)
  File "/home/debayon/anaconda3/envs/ENV2/lib/python3.7/site-packages/torchvision/transforms/transforms.py", line 638, in get_params
    area = img.size[0] * img.size[1]
TypeError: 'int' object is not subscriptable

I am unable to understand why is that occuring and please tell how can I solve it?
Thank You.

1 Like

Your image seems to be a numpy array.
torchvision transformations work on PIL.Images, so either load the image directly via Image.open or convert it to a PIL.Image before passing it to the transformations.

8 Likes

Oh. Thank You so much. Lesson Learnt: "torchvision transformations work on PIL.Image s "

4 Likes

To be a bit more specific: “a lot of” torchvision’s transformation use PIL, while e.g. Normalize will work on tensors. :wink:

5 Likes

Just wonder why? Isn’t numpy image more common?

I think the major advantage is to reuse a common library for loading and processing images.
PIL is one of these libs, but I don’t know which requirements were used to pick it.

Note however that as of torchvision 0.8 transformations are now supported on tensors and torchvision ships with a native image loading utility. More details can be found in the release notes.

Thank you very much for quick responses, and background info is very helpful.

Still not direct support Numpy as input data format is a little bit strange for transform functions. For example, a project deal with both images and videos a lot, we use OpenCV for preprocessing.