Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use to_categorical when using ImageDataGenerator

I am using Keras for classifying images (multiple classes) and I'm using ImageDataGenerator. It automatically finds all of classes, and it doesn't seem to write labels in any variable. I figured I need to use to_categorical to store my labels in matrix form, but I just don't know where to use it.

Here is a snippet of my code:

...
datagen = ImageDataGenerator(
    rotation_range=40,
    width_shift_range=0.2,
    height_shift_range=0.2,
    rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest')

# generator for training
train_generator = datagen.flow_from_directory(
train_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')

# generator for validation
val_generator = datagen.flow_from_directory(
val_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')

# generator for testing
test_generator = datagen.flow_from_directory(
test_dir,
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical')

# train
model.fit_generator(
train_generator,
steps_per_epoch=nb_train_samples // batch_size,
epochs=epochs,
validation_data=val_generator,
validation_steps=nb_validation_samples // batch_size)

Generators just say "Found 442 images belonging to 5 classes." or smth like that. How can I use to_categorical on my labels?

like image 731
Kamil Saitov Avatar asked Jul 12 '18 12:07

Kamil Saitov


People also ask

What is fill mode in ImageDataGenerator?

You can fill this in different ways like a constant value or nearest pixel values, etc. This is specified in the fill_mode argument and the default value is “nearest” which simply replaces the empty area with the nearest pixel values. # ImageDataGenerator rotation.

What is Batch_size in ImageDataGenerator?

For example, if you have 1000 images in your dataset and the batch size is defined as 10. Then the "ImageDataGenerator" will produce 10 images in each iteration of the training. An iteration is defined as steps per epoch i.e. the total number of samples / batch_size.

What is zoom range in ImageDataGenerator?

This method uses the zoom_range argument of the ImageDataGenerator class. We can specify the percentage value of the zooms either in a float, range in the form of an array, or python tuple. If we specify the value of the zoom-in using float value then it will be [1-floatValue, 1+floatValue].

Why do we use ImageDataGenerator?

What is keras ImageDataGenerator? Keras image data generator is used for the generation of the batches containing the data of tensor images and is used in the domain of real-time data augmentation. We can loop over the data in batches when we make use of the image data generator in Keras.


2 Answers

It might be useful (even after two years) to also mention that if you want specific order for one-hot vectors, you can feed that through classes argument.
For example if you want "dog"=[1,0] and "cat"=[0,1], then explicitly set:
classes=["dog", "cat"].

like image 197
Ala Tarighati Avatar answered Oct 19 '22 15:10

Ala Tarighati


Since you are passing class_mode='categorical' you dont have to manually convert the labels to one hot encoded vectors using to_categorical().

The Generator will return labels as categorical.

like image 30
Sreeram TP Avatar answered Oct 19 '22 16:10

Sreeram TP