简体   繁体   English

Keras FlowFromDirectory 中的随机播放参数似乎不起作用

[英]Shuffle parameter in Keras FlowFromDirectory doesn't seem to work

I am working on a mixed-data type keras deep learning project.我正在研究一个混合数据类型 keras 深度学习项目。 I use the following tutorial: https://heartbeat.fritz.ai/building-a-mixed-data-neural-network-in-keras-to-predict-accident-locations-d51a63b738cf我使用以下教程: 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.正如那里所解释的,我通过 flowfromdirectory 加载我的图像,我需要获取文件名及其确切顺序,以便我可以将它与与之关联的正确元数据匹配。

I've looked up the documentation from Keras concerning the function: https://keras.io/api/preprocessing/image/#flowfromdirectory-method我查看了 Keras 关于 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:虽然我将shuffle参数放在TrueFlase上,但它似乎不会影响我从ImageDataGenerator获得的图像的顺序,并且它始终显示字母数字顺序:

Loading Image with suffle=True :使用suffle=True加载图像:

在此处输入图像描述

Loading Image with suffle=False :使用suffle=False加载图像:

在此处输入图像描述

How can I be sure that my train_flow is effectively shuffled?我怎样才能确定我的 train_flow 被有效地改组了?
How can I retrieve the exact order in which the files are loaded in train_flow?如何检索文件在 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 !我对 Keras 很陌生,这可能是一个愚蠢的问题,或者是我的愚蠢错误,请友好地回答:) 感谢您的帮助!

The list of filenames from generator.filenames is indeed static. generator.filenames 中的文件名列表确实是 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.]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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