keras flow_from_directory returns 0 images

When I try to use the following snippet of code to try to predict on a batch of images, I get a message saying that no image were found.

test_datagen = ImageDataGenerator()

test_generator = test_datagen.flow_from_directory(
directory='test/',
target_size=(300, 300),
color_mode="rgb",
shuffle = False,
class_mode='binary',
batch_size=1)

filenames = test_generator.filenames


My directory structure is as follows. I first have a main directory named dogs_vs_cats in which I have two sub directories train and test containing the respective images and also the notebook which contains this code.

• Could you post the exact ErrorMessage? – Danny May 9 '19 at 14:40
• I actually solved the issue just now. I forgot to create 2 subdirectories inside the respective train and test folders and put the images in them. Thanks anyway – Jitesh Malipeddi May 9 '19 at 14:45
• The output actually was 'Found 0 images belonging to 0 classes' – Jitesh Malipeddi May 9 '19 at 14:47

Keras generator alway looks for subfolders (representing the classes). Images insight the subfolders are associated with a class.

So whan you work on C:\images\ and you have two classes, say C1, C2, you need to create subfolders C:\images\C1\ and C:\images\C2\. The directory insight the generator function should point to C:\images\.

See this post for the case of image prediction: https://stackoverflow.com/a/55991598/9524424

• Thanks a lot. This worked for me – Jitesh Malipeddi May 12 '19 at 12:07

There could be two situations when you run ImageDataGenerator on test set of images.

Case #1: Test folder has subfolders representing the classes. Peter has answered this part.

Case #2: There are no labelled test images. In this case, you will have a single test folder which contains all the images that you want to classify.

Kyle Banks has written a tip to handle this in his blog: https://kylewbanks.com/blog/loading-unlabeled-images-with-imagedatagenerator-flowfromdirectory-keras