将ImageDataGenerator结果分配给Numpy数组 [英] Assign ImageDataGenerator result to Numpy array

查看:298
本文介绍了将ImageDataGenerator结果分配给Numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Keras内使用ImageDataGenerator读取图像目录.我想将结果保存在一个numpy数组中,这样我就可以做进一步的操作,并将其保存到磁盘中的一个文件中.

I'm using the ImageDataGenerator inside Keras to read a directory of images. I'd like to save the result inside a numpy array, so I can do further manipulations and save it to disk in one file.

flow_from_directory()返回一个迭代器,这就是我尝试以下操作的原因

flow_from_directory() returns an iterator, which is why I tried the following

itr = gen.flow_from_directory('data/train/', batch_size=1, target_size=(32,32))
imgs = np.concatenate([itr.next() for i in range(itr.nb_sample)])

但是产生了

ValueError: could not broadcast input array from shape (32,32,3) into shape (1)

我认为我误用了concatenate()函数,但是我不知道失败的地方.

I think I'm misusing the concatenate() function, but I can't figure out where I fail.

推荐答案

我遇到了同样的问题,并通过以下方式解决了该问题: itr.next返回下一批图像作为两个numpy.ndarray对象:batch_x,batch_y. (来源: keras/preprocessing/image.py ) 因此,您可以将flow_from_directory的batch_size设置为整个火车数据集的大小.

I had the same problem and solved it the following way: itr.next returns the next batch of images as two numpy.ndarray objects: batch_x, batch_y. (Source: keras/preprocessing/image.py) So what you can do is set the batch_size for flow_from_directory to the size of your whole train dataset.

例如,我的整个训练集包含1481张图像:

Example, my whole training set consists of 1481 images:

train_datagen = ImageDataGenerator(rescale=1. / 255)
itr = train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_width, img_height),
batch_size=1481,
class_mode='categorical')

X, y = itr.next()

这篇关于将ImageDataGenerator结果分配给Numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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