在Keras ImageDataGenerator流方法中调整图像大小 [英] Resizing images in Keras ImageDataGenerator flow methods
问题描述
Keras ImageDataGenerator
类提供了两个流方法flow(X, y)
和flow_from_directory(directory)
( https://keras.io/preprocessing/image/).
The Keras ImageDataGenerator
class provides the two flow methods flow(X, y)
and flow_from_directory(directory)
(https://keras.io/preprocessing/image/).
为什么是参数
target_size:整数元组,默认值:(256,256).找到的所有图像的尺寸将被调整为
target_size: tuple of integers, default: (256, 256). The dimensions to which all images found will be resized
仅由 flow_from_directory(directory)提供?使用 flow(X,y)将图像的重塑添加到预处理管道中的最简洁方法是什么?
Only provided by flow_from_directory(directory) ? And what is the most concise way to add reshaping of images to the preprocessing pipeline using flow(X, y) ?
推荐答案
flow_from_directory(directory)
从带有任意图像集合的目录中生成增强图像.因此,需要参数target_size
来制作所有具有相同形状的图像.
flow_from_directory(directory)
generates augmented images from directory with arbitrary collection of images. So there is need of parameter target_size
to make all images of same shape.
flow(X, y)
会增强已经按顺序存储在X中的图像,这些图像不过是numpy矩阵,在传递给flow
之前可以很容易地进行预处理/调整大小.因此,不需要target_size
参数.至于调整大小,我更喜欢使用scipy.misc.imresize
而不是PIL.Image resize
或cv2.resize,因为它可以处理numpy图片数据.
While flow(X, y)
augments images which are already stored in a sequence in X which is nothing but numpy matrix and can be easily preprocessed/resized before passing to flow
. So no need for target_size
parameter. As for resizing I prefer using scipy.misc.imresize
over PIL.Image resize
, or cv2.resize as it can operate on numpy image data.
import scipy
new_shape = (28,28,3)
X_train_new = np.empty(shape=(X_train.shape[0],)+new_shape)
for idx in xrange(X_train.shape[0]):
X_train_new[idx] = scipy.misc.imresize(X_train[idx], new_shape)
这篇关于在Keras ImageDataGenerator流方法中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!