Keras: Visualize ImageDataGenerator Output

2020-08-10 07:02发布

问题:

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?

回答1:

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



回答2:

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:

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



回答3:

keras uses the image operation with Pillow backend, which comes with 'RGB' format default. So you don't need to reverse the channel axis in your plt.imshow().

Only in cv2.imread() (which is 'BGR' default), you may need plt.imshow(img[:, :, ::-1]) to display the right image.

BR