简体   繁体   中英

Shuffle parameter in Keras FlowFromDirectory doesn't seem to work

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 !

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.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM