How to choose only spesific file format with flow_from_directory?

Question:

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/',
                                                  color_mode='grayscale',
                                                  # don't shuffle
                                                  shuffle=False,
                                                  # use same size as in training
                                                  target_size=(256, 256),
                                                  batch_size=1,
                                                  class_mode=None
                                                  )

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

Asked By: justRandomLearner

||

Answers:

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

For the multiple folders question, here’s a solution:

idg1 = ImageDataGenerator(**idg1_configs)
idg2 = ImageDataGenerator(**idg2_configs)

g1 = idg1.flow_from_directory('idg1_dir',...)
g2 = idg2.flow_from_directory('idg2_dir',...)

def combine_gen(*gens):
    while True:
        for g in gens:
            yield next(g)

# ...
model.fit_generator(combine_gen(g1, g2), steps_per_epoch=len(g1)+len(g2), ...)
Answered By: Serge