Keras flowFromDirectory获取正在生成的文件名 [英] Keras flowFromDirectory get file names as they are being generated

查看:266
本文介绍了Keras flowFromDirectory获取正在生成的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获得使用flow_from_directory加载的文件名? 我有:

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

datagen = ImageDataGenerator(
    rotation_range=3,
#     featurewise_std_normalization=True,
    fill_mode='nearest',
    width_shift_range=0.2,
    height_shift_range=0.2,
    horizontal_flip=True
)

train_generator = datagen.flow_from_directory(
        path+'/train',
        target_size=(224, 224),
        batch_size=batch_size,)

我有一个用于多输出模型的自定义生成器,例如:

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

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

print(train_generator.filenames)

def generate():
    while 1:
        x,y = train_generator.next()
        yield [x] ,[a,y]

节点,目前我正在为a生成随机数,但为了进行实际训练,我希望加载一个json文件,其中包含图像的边界框坐标.为此,我需要获取使用train_generator.next()方法生成的文件名.安装完之后,我可以加载文件,解析json并将其传递给a而不是. x变量的顺序和我得到的文件名列表也必须相同.

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 train_generator.next() 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 is the same.

推荐答案

是的,至少在版本2.0.4(不知道较早的版本)中是可能的.

Yes is it possible, at least with version 2.0.4 (don't know about earlier version).

ImageDataGenerator().flow_from_directory(...)的实例具有一个filenames属性,该属性按生成器生成文件的顺序列出了所有文件,并且还具有一个batch_index属性.因此,您可以这样做:

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. So you can do it like this:

datagen = ImageDataGenerator()
gen = datagen.flow_from_directory(...)

生成器上的每次迭代都可以获取相应的文件名,如下所示:

And every iteration on generator you can get the corresponding filenames like this:

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.

这篇关于Keras flowFromDirectory获取正在生成的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆