Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the filename of an image with keras flow_from_directory shuffled method?

If I don't shuffle my files, I can get the file names with generator.filenames. But when the generator shuffles the images, filenames isn't shuffled, so I don't know how to get the file names back.

like image 603
Borbag Avatar asked Feb 08 '17 16:02

Borbag


1 Answers

Internally, the DirectoryIterator will iterate over an index_arrray which is shuffled when you ask it to.

You just need to index the filename array using the current indexes of the batch:

it = ImageDataGenerator().flow_from_directory(shuffle=True,...)
for img in it:
    idx = (it.batch_index - 1) * it.batch_size
    fnames = [it.filenames[it.index_array[i]] for i in range(idx, idx + it.batch_size)]
like image 118
Jonno_FTW Avatar answered Oct 30 '22 05:10

Jonno_FTW