我是Keras的新手并尝试建立一个卷积神经网络。对于预处理,我在样本(批量大小)中拍摄了每张图片的32个补丁,大小为200 x 200(补丁大小)。我生成了它的numpy数组并保存了它们。 我尝试用这些批次喂养模型。
pg = PatchGenerator(image_folder, list_path, sheet_name, patch_size,
batch_size, split_distribution)
# Load batches and labels
X_train = []
Y_train = []
c = 0
for x in range(2):
X_train.append(np.load(train_batch_load_path + train_batch_name +
str(c+1) + '.npy'))
Y_train.append(pg.hd_train_list[c])
c = c + 1
X_train = np.array(X_train)
print(X_train.shape) # shape: (2, 32, 200, 200, 3)
# Model
model.add(Convolution2D(filters=16, kernel_size=3, strides=(1, 1),
bias_initializer='zeros', padding='same', data_format='channels_last',
activation=None, batch_size=32, input_shape=(patch_size, patch_size, 3)))
print(model.output_shape) # shape: (32, 200, 200, 16)
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='same',
data_format='channels_last'))
print(model.output_shape) #shape: (32, 100, 100, 16)
model.add(Convolution2D(filters=32, kernel_size=3, strides=(2, 2),
bias_initializer='zeros', padding='same', data_format='channels_last',
activation='relu'))
print(model.output_shape) # shape: (32, 50, 50, 32)
model.add(AveragePooling2D(pool_size=(2, 2), strides=(5, 5),
padding='same', data_format='channels_last'))
print(model.output_shape) # shape: (32, 10, 10, 32)
model.add(Convolution2D(filters=16, kernel_size=5, strides=(1, 1),
bias_initializer='zeros', padding='valid', data_format='channels_last',
activation='relu'))
print(model.output_shape) # shape: (32, 6, 6, 16)
model.add(MaxPooling2D(pool_size=(4, 4), strides=(1, 1), padding='valid',
data_format='channels_last'))
print(model.output_shape) # shape: (32, 3, 3, 16)
model.add(Convolution2D(filters=1, kernel_size=3, strides=(1, 1),
bias_initializer='zeros', padding='valid', data_format='channels_last',
activation='relu'))
print(model.output_shape) # shape: (32, 1, 1, 1)
model.add(Flatten())
print(model.output_shape) # shape: (32, 1)
model.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=1)
当我尝试运行代码时出现以下错误:
ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的数组有形状(2,32,200,200,3)
我知道它们的尺寸超过4个尺寸,但是当我只使用一个具有4个尺寸的形状的批次时,我得到了一个错误,我的尺寸太小了。我认为输出形状是运行模型所需的正确形状。
希望你能帮助我。
答案 0 :(得分:0)
不,您的数据应该是4D,因为错误提到了。您将输入形状等于(200,200,3),这意味着输入X的形状应为(样本,200,200,3)。样本可以是任意数字,但其他两个维度是固定的。
如果您在使用4D阵列时遇到其他错误,则还应将其包含在您的问题中。