Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get labels ids in Keras when training on multiple classes?

I'm using flow_from_directory to get the training set from a folder with the following structure:

train
  class1
  class2
  class3
  ...

The generator is called as it follows:

train_generator = train_datagen.flow_from_directory( 
        train_data_dir,                              
        target_size=(img_height, img_width),         
        batch_size=32,                               
        class_mode='categorical')  

I am not setting the argument classes, but I was expecting to get the labels in alphabetical order.

classes: optional list of class subdirectories (e.g. ['dogs', 'cats']). Default: None. If not provided, the list of classes will be automatically inferred (and the order of the classes, which will map to the label indices, will be alphanumeric).

However, when I classify the training images (for checking which labels are being returned), I'm don't get any specific ordering. The training goes well (accuracy of ~85%), and there is a consistency with the output labels when classifying images from the same class.

How can I infer the labels numbers generated by flow_from_directory and map them to the classes?

like image 845
Fábio Perez Avatar asked Jun 23 '16 19:06

Fábio Perez


1 Answers

You can see which class correspond to which integer looking at the variable ImageDataGenerator.class_indices

Here is an example on how to use it

    def build(source=None):
        datagen = ImageDataGenerator(rescale=1. / 255)
        data_generator = datagen.flow_from_directory(
        source,  # this is the target directory
        target_size=(150, 150),  # all images will be resized to 150x150
        batch_size=11,
        class_mode='sparse')
        class_dictionary = data_generator.class_indices
    return data_generator, class_dictionary
like image 61
emanuele Avatar answered Oct 12 '22 00:10

emanuele