Search code examples
pythondeep-learningpytorchdata-augmentationimage-augmentation

'Image' object has no attribute 'shape'


I am trying to implement prime augmentation on my dataset. Previously I was getting error raise ValueError('pic should be 2/3 dimensional. Got {} dimensions.'.format(pic.ndim)) ValueError: pic should be 2/3 dimensional. Got 4 dimensions. The first print gives the shape: img_prime shape: torch.Size([3, 3, 320, 320]), so I removed the first element which is probably the batch size, Then I converted the tensor array to numpy and transposed so that the shape becomes (320,320,3) i.e in a (H,W,C) format.

class PrimeAugment:
    def __init__(self, prime_module):
        self.prime_module = prime_module

    def __call__(self, img, mask):
        img = transforms.ToTensor()(img).unsqueeze(0)  # Convert PIL image to tensor and add batch dimension
        img_prime = self.prime_module(img)  # Apply PRIME augmentations
        print("img_prime shape:", img_prime.shape)  # Print the shape of img_prime
        img_prime = img_prime[0, :, :, :]
        img_prime = img_prime.detach().cpu().numpy().transpose(1, 2, 0)
        img_prime = (img_prime * 255).astype(np.uint8)  # Convert data type to uint8
        print("shape after remove:", img_prime.shape)
        img_prime = Image.fromarray(img_prime)  # Convert back to PIL image
        return img_prime, mask

But now I am getting an error:

    params.update({"cols": kwargs["image"].shape[1], "rows": kwargs["image"].shape[0]})
AttributeError: 'Image' object has no attribute 'shape'

Whole trace of the error:

Original Traceback (most recent call last):
  File "/home/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/utils/data/_utils/worker.py", line 308, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 51, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/anaconda3/envs/myenv/lib/python3.9/site-packages/torch/utils/data/_utils/fetch.py", line 51, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "/home/Crack-PRIME-final3/tool/dataset.py", line 225, in __getitem__
    image_store, mask_store = image_mask_transformation(image, mask, self.img_trans, self.aug_trans)
  File "/home/Crack-PRIME-final3/tool/dataset.py", line 180, in image_mask_transformation
    transformed = img_norm(image=image)
  File "/home/anaconda3/envs/myenv/lib/python3.9/site-packages/albumentations/core/transforms_interface.py", line 118, in __call__
    return self.apply_with_params(params, **kwargs)
  File "/home/anaconda3/envs/myenv/lib/python3.9/site-packages/albumentations/core/transforms_interface.py", line 125, in apply_with_params
    params = self.update_params(params, **kwargs)
  File "/home/anaconda3/envs/myenv/lib/python3.9/site-packages/albumentations/core/transforms_interface.py", line 175, in update_params
    params.update({"cols": kwargs["image"].shape[1], "rows": kwargs["image"].shape[0]})
AttributeError: 'Image' object has no attribute 'shape'

from dataset.py:

179     img_norm = A.Normalize(config.MEAN, config.STD,  p=1.0)
180     transformed = img_norm(image=image)
181     image = transformed["image"]
182

Solution

  • The error message indicates that the object you are interacting with does not have a shape attribute and is an instance of PIL.Image. What you can do is either avoiding to convert by to image with Image.from_array and convert directly to Tensor (check the array has the correct type though), or you can use ToTensor or tf.to_tensor:

    img_prime = tf.to_tensor(img_prime)