Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras flow_from_directory class index

Tags:

keras

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

like image 847
Diogo Aleixo Avatar asked May 05 '17 20:05

Diogo Aleixo


People also ask

What is flow_from_directory in keras?

According the Keras documentation. flow_from_directory(directory) , Description:Takes the path to a directory, and generates batches of augmented/normalized data. Yields batches indefinitely, in an infinite loop. With shuffle = False , it takes the same batch indefinitely.

What is flow_from_directory?

The flow_from_directory() method allows you to read the images directly from the directory and augment them while the neural network model is learning on the training data. The method expects that images belonging to different classes are present in different folders but are inside the same parent folder.

What is Batch_size in flow_from_directory?

The syntax to call flow_from_directory() function is as follows: flow_from_directory(directory, target_size=(256, 256), color_mode='rgb', classes=None, class_mode='categorical', batch_size=32, shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png', follow_links=False, subset=None, interpolation=' ...

What is class mode categorical keras?

Class modes: "categorical" : 2D output (aka. list of numbers of length N), [0, 0, 1, 0], which is a one-hot encoding (only one number is 1/ "hot") representing the donkey. This is for mutually exclusive labels. A dog cannot be a cat, a human is not a dog.


2 Answers

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

print(validation_generator.class_indices)

Simple...

like image 141
Diogo Aleixo Avatar answered Oct 16 '22 09:10

Diogo Aleixo


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)

like image 31
Daniel Möller Avatar answered Oct 16 '22 09:10

Daniel Möller