Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fetch the names of the classes scanned by .flow_from_directory function of ImageDataGenerator in Keras?

I want to make a user friendly GUI image classifier where the user would just need to point to the directory of the data set to get the model trained and then they can give any image to the program and it will display the probability and label of the object in the image. But how do I get the names of the classes scanned by .flow_from_directory function of ImageDataGenerator in Keras?

like image 306
Neeraj Kumar Avatar asked Jan 27 '23 02:01

Neeraj Kumar


1 Answers

From the documentation, "The dictionary containing the mapping from class names to class indices can be obtained via the attribute class_indices."

https://keras.io/preprocessing/image/#flow_from_directory

In the example below, the train_data_dir contains two sub-folders, cat and dog.

train_datagen = ImageDataGenerator(rescale=1. / 255)

train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    color_mode='grayscale',
    shuffle = True,
    batch_size=batch_size,
    class_mode='binary')

print(train_generator.class_indices)
{'cat': 0, 'dog': 1}

`

like image 115
Manoj Mohan Avatar answered Jan 28 '23 16:01

Manoj Mohan