Diogo Aleixo
Diogo Aleixo

Reputation: 871

Keras flow_from_directory class index

I used to make it manually, but i am using now flow_from_directory to train my network with my own data. I just have one question. When i make model.predict(), how can i know that my index 0 on predictions is for label category dog and index 1 is for category cats?

The code i am using is the following.

train_datagen = ImageDataGenerator(
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
        train_images_path,  
        target_size=(64, 64),  
        batch_size=batch_size)  


validation_generator = test_datagen.flow_from_directory(
        validate_images_path,
        target_size=(64, 64),
        batch_size=batch_size)
early_stopping = keras.callbacks.EarlyStopping(monitor='val_acc', min_delta=0, patience=3, verbose=1, mode='auto')
history = model.fit_generator(
        train_generator,
        steps_per_epoch=1700,
        epochs=epochs,
        verbose=1,
        callbacks=[early_stopping],
        validation_data=validation_generator,
        validation_steps=196
)

What i wanted to know is the pair images vs ground truth label.

Thank you

Upvotes: 13

Views: 16919

Answers (3)

Diogo Aleixo
Diogo Aleixo

Reputation: 871

You can have the the index of each class generated by the generator with class_indices property.

print(validation_generator.class_indices)

Simple...

Upvotes: 28

Daniel Möller
Daniel Möller

Reputation: 86650

When you gather data, you define that. There is no rule. But a simple way to check is:

  • see what your first training image is, look at it yourself: is it a cat or a dog?
  • then see the training Y (result/class/desired output), is it [0,1] or [1,0]?

This will answer your question.

For getting one sample from a generator, you can see this question: How to get one value from a generator in Python?

As defined in Keras documentation, the generator output is a tuple of (inputs, targets)

Upvotes: 1

Wasi Ahmad
Wasi Ahmad

Reputation: 37771

Its pretty simple. When you pre-process your data, just replace the class labels with some specific integers (you can call it id). So, when you compute the loss or accuracy from the model's output, just compare the prediction with the ground truth in terms of integer labels (id).

In case if you need the label text, you can get it back from the id (integer).

Upvotes: -1

Related Questions