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

查看:50
本文介绍了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天全站免登陆