为什么我们将Keras中的图像重塑为4d以进行图像分类

问题描述

from keras.preprocessing.image import ImageDataGenerator,array_to_img,img_to_array,load_img

datagen = ImageDataGenerator(
        rotation_range=40,width_shift_range=0.2,height_shift_range=0.2,shear_range=0.2,zoom_range=0.2,horizontal_flip=True,fill_mode='nearest')

img = load_img('data/train/cats/cat.0.jpg')  # this is a PIL image
x = img_to_array(img)  # this is a Numpy array with shape (3,150,150)
x = x.reshape((1,) + x.shape)  # this is a Numpy array with shape (1,3,150)

我不知道为什么我们要像X = x.reshape((1,) + x.shape)行那样重塑形状并将其形状做成(1、3、150、150),这里1代表什么,这有什么好处。 此示例来自https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html

解决方法

3是通道数(R,G和B),150是图像的宽度/高度,1是批次的大小

通常,机器学习方法(例如神经网络)一次处理多个图像。如果您一次处理n张图像,则n是您的批量大小,并且张量的形状将为( n ,3、150、150)。