1

I have been trying to do image augmentation using a library called Albumentations. But I got some error from OpenCV while transforming the images. I ran the code below on Kaggle's notebook. The dataset is called "Intel image classification" on kaggle. It has 6 classes. Each image is 150 * 150 * 3.

import numpy as np
import tensorflow as tf
import albumentations as a

x_train_path = "../input/intel-image-classification/seg_train/seg_train"

train_data =  tf.keras.utils.image_dataset_from_directory(
  x_train_path,
  seed=123,
  image_size=(150, 150),
  batch_size=128)

transforms = Compose([
            a.Rotate(limit=40),
            a.HueSaturationValue(hue_shift_limit=20, sat_shift_limit=30, val_shift_limit=20, p=0.5),
            a.GaussianBlur(p=0.5),
            a.RandomContrast(limit=0.2, p=0.5),
            a.RandomBrightnessContrast(brightness_limit=0.3, contrast_limit=0.3),
            a.HorizontalFlip(),
        ])

#looping through the train_data generator
for image,label in train_data:

    #albumentations requires data to be numpy array and dtype uint8
    image = tf.cast(image,tf.uint8).numpy()
    
    aug_image = transforms(image=image)    # ERROR HERE
    print(aug_image)
    break

Here is the whole error:

error                                     Traceback (most recent call last)
/tmp/ipykernel_33/2678995032.py in <module>
      7 
      8 
----> 9     aug_image = transforms(image=image)
     10     print(aug_image)
     11     break

/opt/conda/lib/python3.7/site-packages/albumentations/core/composition.py in __call__(self, force_apply, *args, **data)
    208 
    209         for idx, t in enumerate(transforms):
--> 210             data = t(force_apply=force_apply, **data)
    211 
    212             if check_each_transform:

/opt/conda/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in __call__(self, force_apply, *args, **kwargs)
     95                     )
     96                 kwargs[self.save_key][id(self)] = deepcopy(params)
---> 97             return self.apply_with_params(params, **kwargs)
     98 
     99         return kwargs

/opt/conda/lib/python3.7/site-packages/albumentations/core/transforms_interface.py in apply_with_params(self, params, force_apply, **kwargs)
    110                 target_function = self._get_target_function(key)
    111                 target_dependencies = {k: kwargs[k] for k in self.target_dependence.get(key, [])}
--> 112                 res[key] = target_function(arg, **dict(params, **target_dependencies))
    113             else:
    114                 res[key] = None

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/geometric/rotate.py in apply(self, img, angle, interpolation, **params)
     86 
     87     def apply(self, img, angle=0, interpolation=cv2.INTER_LINEAR, **params):
---> 88         return F.rotate(img, angle, interpolation, self.border_mode, self.value)
     89 
     90     def apply_to_mask(self, img, angle=0, **params):

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/functional.py in wrapped_function(img, *args, **kwargs)
     68     def wrapped_function(img, *args, **kwargs):
     69         shape = img.shape
---> 70         result = func(img, *args, **kwargs)
     71         if len(shape) == 3 and shape[-1] == 1 and len(result.shape) == 2:
     72             result = np.expand_dims(result, axis=-1)

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/geometric/functional.py in rotate(img, angle, interpolation, border_mode, value)
     77         cv2.warpAffine, M=matrix, dsize=(width, height), flags=interpolation, borderMode=border_mode, borderValue=value
     78     )
---> 79     return warp_fn(img)
     80 
     81 

/opt/conda/lib/python3.7/site-packages/albumentations/augmentations/functional.py in __process_fn(img)
    187             img = np.dstack(chunks)
    188         else:
--> 189             img = process_fn(img, **kwargs)
    190         return img
    191 

error: OpenCV(4.5.4) /tmp/pip-req-build-21t5esfk/opencv/modules/imgproc/src/imgwarp.cpp:2595: error: (-215:Assertion failed) src.cols > 0 && src.rows > 0 in function 'warpAffine'
Tian
  • 31
  • 5
  • 1
    You are passing an empty array. Check the image path, that’s the common cause of this assertion. – stateMachine May 14 '22 at 23:41
  • I already checked. It is not empty – Tian May 14 '22 at 23:43
  • Maybe the image was casted incorrectly. – stateMachine May 14 '22 at 23:44
  • There is no problem with that either. The data after casting is numpy array, and dtype is uint8. Which is excally what Albumentations wants. – Tian May 14 '22 at 23:47
  • YOOOO. I am not getting an error after I ran it for >3 times. But if I ran it again after not getting the error, I will get the error again. The Albumentation library's code is so great!!!!!! – Tian May 15 '22 at 00:11

0 Answers0