Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras: Visualize ImageDataGenerator Output

I would like to see what my ImageDataGenerator yields to my network.

Edit:
removed the channel_shift_range, accidently left it in the code

Generator

genNorm = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, 
       height_shift_range=0.1, zoom_range=0.1, horizontal_flip=True)

Get Batches

batches = genNorm.flow_from_directory(path+'train', target_size=(224,224),
      class_mode='categorical', batch_size=64)

x_batch, y_batch = next(batches)

Plot Images

for i in range (0,32):
    image = x_batch[i]
    plt.imshow(image.transpose(2,1,0))
    plt.show()

Result

Generator Output

Is this normal or am I doing something wrong here?

like image 747
Kay Avatar asked Aug 21 '17 13:08

Kay


People also ask

What does ImageDataGenerator return?

ImageDataGenerator class ensures that the model receives new variations of the images at each epoch. But it only returns the transformed images and does not add it to the original corpus of images.

How does keras ImageDataGenerator work?

Keras ImageDataGenerator is used for getting the input of the original data and further, it makes the transformation of this data on a random basis and gives the output resultant containing only the data that is newly transformed. It does not add the data.

How many images does ImageDataGenerator generate?

Then the "ImageDataGenerator" will produce 10 images in each iteration of the training. An iteration is defined as steps per epoch i.e. the total number of samples / batch_size. In above case, in each epoch of training there will be 100 iterations.

What is Class_mode =' categorical?

Class modes: "categorical" : 2D output (aka. list of numbers of length N), [0, 0, 1, 0], which is a one-hot encoding (only one number is 1/ "hot") representing the donkey. This is for mutually exclusive labels.


2 Answers

The strange colors result from your channel shift. Do you really need that to augment your samples? Is a value of 10 (=very high) really what you want?

In addition to that: Another and likely more efficient way of checking what your generator yields is to set a directory with save_to_dir (parameter of flow/flow from directory function). In that you´ll find all the images that have been transformed and been delivered to your fit/flow function.

Edit:

You still somehow seem to invert your images during processing or while displaying them. I assume the original images look more like this:

enter image description here

Save the results of your ImageDataGenerator to a directory and compare these with the results that you see with plt.show.

like image 80
petezurich Avatar answered Sep 20 '22 05:09

petezurich


Try this; change the generator as follow:

import numpy as np

def my_preprocessing_func(img):
    image = np.array(img)
    return image / 255

genNorm = ImageDataGenerator(rotation_range=10, width_shift_range=0.1, 
       height_shift_range=0.1, zoom_range=0.1, horizontal_flip=True, 
       preprocessing_function=my_preprocessing_func)

That worked for me,

Bruno

like image 40
user3452266 Avatar answered Sep 20 '22 05:09

user3452266