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?
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.
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.
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].
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.
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"]
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With