Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing images in Keras ImageDataGenerator flow methods

Tags:

The Keras ImageDataGenerator class provides the two flow methods flow(X, y) and flow_from_directory(directory) (https://keras.io/preprocessing/image/).

Why is the parameter

target_size: tuple of integers, default: (256, 256). The dimensions to which all images found will be resized

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) ?

like image 280
user1934212 Avatar asked Jan 20 '17 07:01

user1934212


People also ask

Which parameter of ImageDataGenerator flow_from_directory is for resizing image?

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.

What is rescale in ImageDataGenerator?

The ImageDataGenerator class can be used to rescale pixel values from the range of 0-255 to the range 0-1 preferred for neural network models. Scaling data to the range of 0-1 is traditionally referred to as normalization.

What is target size in ImageDataGenerator?

target_size: Size of the input image. color_mode: Set to rgb for colored images otherwise grayscale if the images are black and white. batch_size: Size of the batches of data. class_mode: Set to binary is for 1-D binary labels whereas categorical is for 2-D one-hot encoded labels.


1 Answers

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.

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) 
like image 195
indraforyou Avatar answered Sep 23 '22 03:09

indraforyou