Keras Image Data Generator show labels

14.7k views Asked by At

I am using an ImageDataGenerator to augment my images. I need to get the y labels from the generator.

Example : I have 10 training images, 7 are label 0 and 3 are label 1. I want to increase training set size to 100.

total_training_images = 100 total_val_images = 50

model.fit_generator(
    train_generator,
    steps_per_epoch= total_training_images // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps= total_val_images // batch_size)

By my understanding, this trains a model on 100 training images for each epoch, with each image being augmented in some way or the other according to my data generator, and then validates on 50 images.

If I do train_generator.classes, I get an output [0,0,0,0,0,0,0,1,1,1]. This corresponds to my 7 images of label 0 and 3 images of label 1.

For these new 100 images, how do I get the y-labels? Does this mean when I am augmenting this to 100 images, my new train_generator labels are the same thing, but repeated 10 times? Essentially np.append(train_generator.classes) 10 times?

I am following this tutorial, if that helps : https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

2

There are 2 answers

0
Dip On BEST ANSWER

The labels generate as one-hot-encoding with the images.Hope this helps !

training_set.class_indices

from keras.preprocessing import image
import matplotlib.pyplot as plt

x,y = train_generator.next()
for i in range(0,3):
    image = x[i]
    label = y[i]
    print (label)
    plt.imshow(image)
    plt.show()
0
Daniel Möller On

Based on what you're saying about the generator, yes.

It will replicate the same label for each augmented image. (Otherwise the model would not train properly).

One simple way to check what the generator is outputting is to get what it yields:

X,Y = train_generator.next() #or next(train_generator)

Just remember that this will place the generator in a position to yield the second element, not the first anymore. (This would make the fit method start from the second element).