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) ?
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With