Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras Realtime Augmentation adding Noise and Contrast

Keras provides an ImageDataGenerator class for realtime augmentation, but it does not include contrast adjustment and addition of noise.

How can we apply a random level of noise and a random contrast adjustment during training? Could these functions be added to the 'preprocessing_function' parameter in the datagen?

Thank you.

like image 621
Chris Parry Avatar asked Apr 13 '17 01:04

Chris Parry


2 Answers

You could indeed add noise with preprocessing_function.

Example script:

import random
import numpy as np

def add_noise(img):
    '''Add random noise to an image'''
    VARIABILITY = 50
    deviation = VARIABILITY*random.random()
    noise = np.random.normal(0, deviation, img.shape)
    img += noise
    np.clip(img, 0., 255.)
    return img

# Prepare data-augmenting data generator
from keras.preprocessing.image import ImageDataGenerator
datagen = ImageDataGenerator(
        rescale=1./255,
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        zoom_range=0.2,
        preprocessing_function=add_noise,
    )

# Load a single image as our example
from keras.preprocessing import image
img_path = 'cat_by_irene_mei_flickr.png'
img = image.load_img(img_path, target_size=(299,299))

# Generate distorted images
images = [img]
img_arr = image.img_to_array(img)
img_arr = img_arr.reshape((1,) + img_arr.shape)
for batch in datagen.flow(img_arr, batch_size=1):
    images.append( image.array_to_img(batch[0]) )
    if len(images) >= 4:
        break

# Display
import matplotlib.pyplot as plt
f, xyarr = plt.subplots(2,2)
xyarr[0,0].imshow(images[0])
xyarr[0,1].imshow(images[1])
xyarr[1,0].imshow(images[2])
xyarr[1,1].imshow(images[3])
plt.show()

Example images generated by the script:

Four images of a cat with varying modification and levels of noise

like image 68
Andriy Makukha Avatar answered Nov 18 '22 16:11

Andriy Makukha


From the Keras docs:

preprocessing_function: function that will be implied on each input. The function will run before any other modification on it. The function should take one argument: one image (Numpy tensor with rank 3), and should output a Numpy tensor with the same shape.

So, I created a simple function and then used the image augmentation functions from the imgaug module. Note that imgaug requires images to be rank 4.

like image 2
Chris Parry Avatar answered Nov 18 '22 15:11

Chris Parry