Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Machine Learning by (19k points)

Is it possible to get the file names that were loaded using flow_from_directory? I have :

datagen = ImageDataGenerator(


#     featurewise_std_normalization=True,






train_generator = datagen.flow_from_directory(


        target_size=(224, 224),


I have a custom generator for my multi-output model like:

a = np.arange(8).reshape(2, 4)

# print(a)


def generate():

    while 1:

        x,y =

        yield [x] ,[a,y]

The node that at the moment I am generating random numbers for a but for real training, I wish to load up a JSON file that contains the bounding box coordinates for my images. For that, I will need to get the file names that were generated using method. After I have that, I can load the file, parse the JSON and pass it instead of a. It is also necessary that the ordering of the x variable and the list of the file names that I get are the same.

1 Answer

0 votes
by (33.1k points)

Your problem can be easily solved by the following method.

The instance of ImageDataGenerator().flow_from_directory(...) has an attribute with filenames which is a list of all the files in the order the generator yields them and also an attribute batch_index. 

For example:

datagen = ImageDataGenerator()

gen = datagen.flow_from_directory(...)

And every iteration, outputs can be like:

for i in gen:

    idx = (gen.batch_index - 1) * gen.batch_size

    print(gen.filenames[idx : idx + gen.batch_size])

This will give you the filenames of the images in the current batch.

Better and more details on Keras would be given through Machine Learning Algorithms. Also, one can study Machine Learning Course for a better grasp on the topic. 

Hope this answer helps you!

Browse Categories