Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras flow_from_directory() read only from selected sub-directories

I have a directory structure as follows.

train
|- dog --> contains image files of dogs
|- cat --> contains image files of cats
|- elephant --> contains image files of elephants

I want to train a CNN to identify animals, but only for cats and dogs and not elephants.

I want to use keras ImageDataGenerator class to augment data and flow_from_directory() method to read the image files.

train_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(directory='train', class_mode='categorical', target_size=(64,64), batch_size=16, shuffle=True)

The above code will read data from all sub-directories of 'train', which I don't want. One option is to create a new directory and copy of 'dog' and 'cat' sub-directories along with the files inside it. But is there a way to control it from flow_from_directory() method itself?

like image 570
Supratim Haldar Avatar asked Mar 24 '19 20:03

Supratim Haldar


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 target_size is the size of your input images, every image will be resized to this size. color_mode: if the image is either black and white or grayscale set “grayscale” or if the image has three color channels, set “rgb”. batch_size: No. of images to be yielded from the generator per batch.

How does Keras ImageDataGenerator work?

Keras ImageDataGenerator is used for getting the input of the original data and further, it makes the transformation of this data on a random basis and gives the output resultant containing only the data that is newly transformed. It does not add the data.


1 Answers

Assuming that I understood your question in the right way, this should help you:

train_generator = train_datagen.flow_from_directory(directory='train', class_mode='categorical', target_size=(64,64), batch_size=16, shuffle=True, classes=["dog", "cat"])

This will read only the images from the directories dog and cat, leave out the elephant directory and provide distinct categorical labels for them.

like image 142
gorjan Avatar answered Oct 13 '22 01:10

gorjan