Resizing images in Keras ImageDataGenerator flow methods

前端 未结 4 1564
攒了一身酷
攒了一身酷 2020-12-16 10:56

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

4条回答
  •  眼角桃花
    2020-12-16 11:21

    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)
    

提交回复
热议问题