Search code examples
pythonmachine-learningkerasdeep-learningdata-augmentation

Adding AdditiveGaussianNoise to a single image - AssertionError: Expected boolean as argument for 'return_batch'


I would like to add AdditiveGaussianNoise (link: https://imgaug.readthedocs.io/en/latest/source/overview/arithmetic.html#additivegaussiannoise) to a single image which I resized before.

This is my code:

from skimage.io import imread
from skimage.transform import resize
import imgaug.augmenters as iaa

file_name = "path/to/image.jpg"
resized_img = resize(imread(file_name), (224, 224))

aug = iaa.AdditiveGaussianNoise(scale=(0, 0.2*255))
augmented_image = aug(resized_img)

And I get this error message:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-20-e4a0b17d4ac4> in <module>()
----> 1 augmented_image =aug(resized_img)

1 frames
/usr/local/lib/python3.6/dist-packages/imgaug/augmenters/meta.py in augment(self, return_batch, hooks, **kwargs)
   1782             ("Expected boolean as argument for 'return_batch', got type %s. "
   1783              + "Call augment() only with named arguments, e.g. "
-> 1784              + "augment(images=<array>).") % (str(type(return_batch)),)
   1785         )
   1786 

AssertionError: Expected boolean as argument for 'return_batch', got type <class 'numpy.ndarray'>. Call augment() only with named arguments, e.g. augment(images=<array>).

How do I have to amend my code?

Thank you very much!


Solution

  • this is the solution using a random image... you need to specify the images argument

    import numpy as np
    import imgaug.augmenters as iaa
    
    img = np.random.randint(0,256, (1,224,224,3)).astype('float32')
    
    aug = iaa.AdditiveGaussianNoise(scale=(0, 0.2*255))
    augmented_image = aug(images=img)