Reputation: 89
I am working on a mixed-data type keras deep learning project. I use the following tutorial : https://heartbeat.fritz.ai/building-a-mixed-data-neural-network-in-keras-to-predict-accident-locations-d51a63b738cf
As explained there, I load my images via flowfromdirectory and I need to get the filenames and their exact order so that I can match it with the right metadata associated with it.
I've looked up the documentation from Keras concerning the function : https://keras.io/api/preprocessing/image/#flowfromdirectory-method
Whereas I put the shuffle
parameter on True
or Flase
, it doesn't seem to affect the order of the images I get from the ImageDataGenerator
, and it always show the alphanumeric order :
Loading Image with suffle=True
:
Loading Image with suffle=False
:
How can I be sure that my train_flow is effectively shuffled ?
How can I retrieve the exact order in which the files are loaded in train_flow ?
I'm quite new to Keras, this may be a stupid question, or a stupid error from my part, please answer kindly :) Thanks for all you help !
Upvotes: 0
Views: 424
Reputation:
The list of filenames from generator.filenames is indeed static. However the images coming through on each generated batch are being shuffled.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# directory contains two images. one back, one white
train_dir = 'C:/dev/test'
data_gen = ImageDataGenerator(rescale=1. / 255)
generator = data_gen.flow_from_directory(train_dir, target_size=(200, 200),
batch_size=2, class_mode='categorical', shuffle=True)
idx = 0
for batch in generator:
print(f'batch# {idx}')
# first pixel of images
print(batch[0][0][0][0])
print(batch[0][1][0][0])
idx += 1
if idx == 5: exit()
gives
Found 2 images belonging to 1 classes.
batch# 0
[0. 0. 0.]
[1. 1. 1.]
batch# 1
[1. 1. 1.]
[0. 0. 0.]
batch# 2
[1. 1. 1.]
[0. 0. 0.]
batch# 3
[1. 1. 1.]
[0. 0. 0.]
batch# 4
[0. 0. 0.]
[1. 1. 1.]
Upvotes: 0