Tauno
Tauno

Reputation: 197

How to call the Keras flow_from_directory() method on Test Dataset?

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?

Upvotes: 3

Views: 3287

Answers (3)

Andrea Ibba
Andrea Ibba

Reputation: 1

Another solution can be to set as a directory for the flow from directory function the parent folder of the test directory, and to set the classes attribute to the name of the test folder containing the images (here in the example 'test')

datagen = ImageDataGenerator()
# In ./dataset directory, there is `test` directory.
test_data = datagen.flow_from_directory('./dataset', classes=['test'])

Here's the reference: https://velog.io/@jhcho/Loading-Unlabeled-Images-with-ImageDataGenerator-flowfromdirectory-in-Keras

Upvotes: 0

Tushar Tiwari
Tushar Tiwari

Reputation: 161

If you do not want to change the structure of your test directory. Try this Directory structure

test_data_gen = test_image_generator.flow_from_directory(test_dir,
        target_size=(IMG_HEIGHT, IMG_WIDTH),
        batch_size= batch_size ,shuffle=False,
        class_mode= 'binary',classes=['.'])

classes=['.'] needs to be specified as the flow_from_directory method will search for folders.

In your Ver.3: you specified the class but there is no folder having test inside the directory so you got 0 images and 1 classes.

Upvotes: 1

Rick
Rick

Reputation: 150

If None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict_generator()). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.

Note that evaluation needs labels in order to determine how good the model is. Typically one only uses no labels in production, or real life testing. You seem to want to test your model, so you actually want to have labels. This means you should sort your testing data

train/
    label1/
        ...
        ...
    label2/
        ...
        ...
val/
    label1/
        ...
        ...
    label2/
        ...
        ...
test/
    label1/
        ...
        ...
    label2/

Reading documentation is often more helpful than those articles when you want to learn how to use a framework. Here is the link to flow_from_directory: https://keras.io/preprocessing/image/#flow_from_directory.


On another note, mc.ai is a website full of ripped Medium articles and should be avoided. Many articles are incomplete + it infringes the copyright of the authors.

Upvotes: 3

Related Questions