Home > OS >  timm: typeError: 'builtin_function_or_method' object is not subscriptable
timm: typeError: 'builtin_function_or_method' object is not subscriptable

Time:06-17

I am implementing timm tutorials on data augmentation to increase the number of images of my dataset. According to their tutorials, I have implemented same code but it did not work.

Code

import numpy as np
import torch
from PIL import Image
from timm.data.transforms_factory import create_transform
a = create_transform(224, is_training=True)
print(a)
pets_image_paths = './download.png'
image = Image.open(pets_image_paths)
# We can convert this into a tensor, and transpose the channels into the format that PyTorch expects:
np_image = np.array(image, dtype=np.float32)
image = torch.as_tensor(np_image).transpose(2, 0)[None]
from timm.data.transforms import RandomResizedCropAndInterpolation
tfm = RandomResizedCropAndInterpolation(size=350, interpolation='random')
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 4, figsize=(10, 5))
for idx, im in enumerate([tfm(image) for i in range(4)]):
    ax[0, idx].imshow(im)
for idx, im in enumerate([tfm(image) for i in range(4)]):
    ax[1, idx].imshow(im)
fig.tight_layout()
plt.show()

Traceback

Traceback (most recent call last):
  File "/home/cvpr/PycharmProjects/timm_tutorials/9_augmentation.py", line 24, in <module>
    for idx, im in enumerate([tfm(image) for i in range(4)]):
  File "/home/cvpr/PycharmProjects/timm_tutorials/9_augmentation.py", line 24, in <listcomp>
    for idx, im in enumerate([tfm(image) for i in range(4)]):
  File "/home/cvpr/anaconda3/envs/timm_tutorials/lib/python3.8/site-packages/timm/data/transforms.py", line 181, in __call__
    i, j, h, w = self.get_params(img, self.scale, self.ratio)
  File "/home/cvpr/anaconda3/envs/timm_tutorials/lib/python3.8/site-packages/timm/data/transforms.py", line 143, in get_params
    area = img.size[0] * img.size[1]
TypeError: 'builtin_function_or_method' object is not subscriptable

CodePudding user response:

RandomResizedCropAndInterpolation expects the input to be an instance of PIL.Image and not torch.tensor. If you need to convert it to tensor, it has to be done later.

CodePudding user response:

As the previous answer mentions RandomResizedCropAndInterpolation expects PIL.Image.

You can look at timm docs in the Note:

Note: RandomResizedCropAndInterpolation expects the input to be an instance of PIL.Image and not torch.tensor.

So you can remove the Tensor convert:

import numpy as np
import torch
from PIL import Image
from timm.data.transforms_factory import create_transform
a = create_transform(224, is_training=True)
print(a)
pets_image_paths = './download.png'
image = Image.open(pets_image_paths)

from timm.data.transforms import RandomResizedCropAndInterpolation
tfm = RandomResizedCropAndInterpolation(size=350, interpolation='random')
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2, 4, figsize=(10, 5))
for idx, im in enumerate([tfm(image) for i in range(4)]):
    ax[0, idx].imshow(im)
for idx, im in enumerate([tfm(image) for i in range(4)]):
    ax[1, idx].imshow(im)
fig.tight_layout()
plt.show()


  • Related