# What is the correct way to call Keras flow_from_directory() method?

In the following article there is an instruction that dataset needs to be divided into train, validation and test folders where the test folder should not contain the labeled subfolders. Instead it should only contain a single folder (i.e. Test_folder).

When I use the following code, I get the output message refering that no image were found.

Ver.1:
test_generator = test_datagen.flow_from_directory(
"dataset\\test\\test_folder\\",
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=1,
class_mode=None,
shuffle=False,
seed=10)

Output message: "Found 0 images belonging to 0 classes.".


Instead, if I use the same folder structure (dataset\test\class_a\test_1.jpg etc) as in the train and validation folders, everything seems to be OK and I manage to evaluate my model.

Ver.2:
test_generator = test_datagen.flow_from_directory(
"dataset\\test\\",
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=32,
class_mode='categorical',
shuffle=False,
seed=10)

Output message: "Found 1500 images belonging to 3 classes.".


I also tried the recommendation where 'classes' attribute is specified but still 0 images were found.

Ver.3:
test_generator = test_datagen.flow_from_directory(
"dataset2\\test\\test_folder\\",
target_size=(IMG_WIDTH, IMG_HEIGHT),
batch_size=1,
classes=['test'],
class_mode=None,
shuffle=False,
seed=10)

Output message: Found 0 images belonging to 1 classes.


Thus, what is the correct way to call flow_from_directory() method and why am I getting the message that no files were found? Is my model not correctly evaluated when I use the Ver.2 solution?

Please find a working solution here.

The generators look like:

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

# Note that the validation data should not be augmented!
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
# This is the target directory
train_dir,
# All images will be resized to 150x150
target_size=(150, 150),
batch_size=batch_size,
# Since we use categorical_crossentropy loss, we need binary labels
class_mode='categorical')

validation_generator = test_datagen.flow_from_directory(
validation_dir,
target_size=(150, 150),
batch_size=batch_size,
class_mode='categorical')


Make sure you have your images stored in the correct way. E.g.

...images/train/class1/
...images/train/class2/
...images/val/class1/
...images/val/class2/


The generator function does need this structure. So make sure there are subfolders for each class in the train/test directories.

You can also use the data generator function for prediction as described in this post.

• Can I conclude from this that all folders (train, validation and test) should have the same structure and directory structure mentioned in the article is not correct - i.e. test folder should also contain class_a, class_b etc subfolders? – Tauno Jan 7 at 13:31
• Train and validation folder need to have the same substructure. – Peter Jan 7 at 13:44
• I'm not talking about train or validation folders. My question was how to construct the test folder and why am I getting the "Found 0 images" message when I implement this the way as described in the article. – Tauno Jan 7 at 14:20
• have you tried setting it up like described above in the test folder. The generator function always looks for subfolders – Peter Jan 7 at 14:22