检查输入时出错:期望input_1具有4个维度,但数组的形状为(224,224,3)

时间:2019-04-12 19:41:28

标签: python tensorflow keras deep-learning conv-neural-network

我正在尝试使用自己的数据来训练CNN,以解决二进制分类问题。但是我输入的预期大小有一个问题,我认为是(224,224,3)。我搜索了此案例,发现有人说可以通过将图像大小从(224,224,3)调整为(1,224,224,3)来解决此问题,但此方法无效。

这是我的代码:

import scipy.io
import tensorflow as tf
import cv2

# Parameters
img_height = 224
img_width = 224
img_depth = 3
classes = 2

# Load Data
db_name = 'polo'
db_path = 'D:/databases/' + db_name + '/'
db_data = scipy.io.loadmat(db_path + 'db_py.mat')
db_size = len(db_data['db']['images'][0][0][0])
faces_path = 'data/' + db_name + '/faces/'
images = []
labels = [0] * db_size
for i in range(0,db_size):
    filename = 'data/' + db_name + '/faces/' + db_data['db']['images'][0][0][0][i][2][0]
    image = cv2.imread(filename)
    image = cv2.resize(image, (img_height, img_width))
    images.append(image)
    labels[i] = db_data['db']['subjects'][0][0][0][i][4][0][0][0][0][0]

inputs = tf.keras.layers.Input(shape=(img_height,img_width,img_depth))
layers = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(inputs)
layers = tf.keras.layers.Activation("relu")(layers)
layers = tf.keras.layers.BatchNormalization(axis=-1)(layers)
layers = tf.keras.layers.Conv2D(32, (3, 3), padding="same")(layers)
layers = tf.keras.layers.Activation("relu")(layers)
layers = tf.keras.layers.BatchNormalization(axis=-1)(layers)
layers = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(layers)
layers = tf.keras.layers.Dropout(0.25)(layers)
layers = tf.keras.layers.Conv2D(64, (3, 3), padding="same")(layers)
layers = tf.keras.layers.Activation("relu")(layers)
layers = tf.keras.layers.BatchNormalization(axis=-1)(layers)
layers = tf.keras.layers.Conv2D(64, (3, 3), padding="same")(layers)
layers = tf.keras.layers.Activation("relu")(layers)
layers = tf.keras.layers.BatchNormalization(axis=-1)(layers)
layers = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(layers)
layers = tf.keras.layers.Dropout(0.25)(layers)
layers = tf.keras.layers.Flatten()(layers)
layers = tf.keras.layers.Dense(512)(layers)
layers = tf.keras.layers.Activation("relu")(layers)
layers = tf.keras.layers.BatchNormalization()(layers)
layers = tf.keras.layers.Dropout(0.5)(layers)
layers = tf.keras.layers.Dense(classes)(layers)
layers = tf.keras.layers.Activation("softmax")(layers)

InitialLearnRate = 0.03
MaxEpochs = 30
MiniBatchSize = 32
opt = tf.keras.optimizers.SGD(lr=InitialLearnRate, decay=InitialLearnRate / MaxEpochs)
model = tf.keras.Model(inputs, layers , name="net")
model.compile(loss="categorical_crossentropy", optimizer=opt,
    metrics=["accuracy"])
model.summary()
H = model.fit(images, labels,
    batch_size=MiniBatchSize, epochs=MaxEpochs, verbose=1,steps_per_epoch=10)

1 个答案:

答案 0 :(得分:2)

如果您去官方documentation并搜索conv2d输入形状,则会看到:

  

具有以下形状的4D张量:(批,通道,行,列),如果data_format为“ channels_first”或   如果data_format为“ channels_last”,则形状为(批,行,列,通道)的4D张量

或者,这是有关输入格式的详细answer

如果您有多张图片,则输入的大小为(batch_size, 244,244, 3)。我看到您正在做的是创建一个包含所有这些图像的列表。我会尝试:

images = np.empty(batch_size, 244, 244, 3)

for i in range(0,db_size):
    filename = ('data/'
                + db_name
                + '/faces/'
                + db_data['db']['images'][0][0][0][i][2][0])
    image = cv2.imread(filename)
    images[i]  = cv2.resize(image, (img_height, img_width))

如果这没有帮助,您收到的错误消息可能会帮助其他人回答您的问题。