3
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Could you post the exact ErrorMessage? $\endgroup$
    – Danny
    May 9, 2019 at 14:40
  • $\begingroup$ 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 $\endgroup$ May 9, 2019 at 14:45
  • $\begingroup$ The output actually was 'Found 0 images belonging to 0 classes' $\endgroup$ May 9, 2019 at 14:47

3 Answers 3

4
$\begingroup$

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

So when 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

$\endgroup$
1
  • 1
    $\begingroup$ Thanks a lot. This worked for me $\endgroup$ May 12, 2019 at 12:07
3
$\begingroup$

There is another option where you do not have to copy the test file into another test file:

datagen = ImageDataGenerator()

test_data = datagen.flow_from_directory('.', classes=['test'])

This solved my problem. For more info see this.

$\endgroup$
1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.