How to choose only spesific file format with flow_from_directory?


I am using Keras for some DL experiments. After train my model, I would like to test my model with following code :

test_datagen = ImageDataGenerator(rescale=1 / 255.)

test_generator = test_datagen.flow_from_directory(directory='test/',
                                                  # don't shuffle
                                                  # use same size as in training
                                                  target_size=(256, 256),

preds = model.predict_generator(test_generator, steps=12)

The problem is test folder contains also subdirectories inside another subdirectories. (e.g. test/test2/test3/test4 …) and I would like to reach images inside test4 folder too but I got IsADirectoryError: [Errno 21] Is a directory: 'test/test2/test3' error.

My first question is: is there any possibility to search and use instead copy and paste all images into one folder?

Second : I want to use only .png formatted images. Can I do something like that? from_directory(directory='test/*.png') for only .png files?

Thank you in advance.
Updated : 24/02/20


For the second question, I managed to do that:

for i in range(len(test_generator)):
    if test_generator.filenames[i].find(".png") != -1:
        pred = model.predict(test_generator[i])

Better solutions are welcome. Cheers

Answered By – justRandomLearner

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published