我使用Keras(tensorflow)实现了以下模型:
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, None, None, 32) 896
_________________________________________________________________
conv2d_2 (Conv2D) (None, None, None, 32) 9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, None, None, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, None, None, 32) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, None, None, 64) 18496
_________________________________________________________________
conv2d_4 (Conv2D) (None, None, None, 64) 36928
_________________________________________________________________
dropout_2 (Dropout) (None, None, None, 64) 0
_________________________________________________________________
global_average_pooling2d_1 ( (None, 64) 0
_________________________________________________________________
dense_1 (Dense) (None, 256) 16640
_________________________________________________________________
dropout_3 (Dropout) (None, 256) 0
_________________________________________________________________
dense_2 (Dense) (None, 14) 3598
=================================================================
Total params: 85,806
Trainable params: 85,806
Non-trainable params: 0
我的数据集是Leed Sports Set,其中包含可变宽度和高度的图像。根据Keras documentation并跟随github issue,我只需要将输入形状设置为(None, None, Num_Channels)
。为了准备我的数据集,我将注释和图像加载为numpy数组,如下所示:
# Train Input, contains paths of images first
x_train = image_list[train_indexes]
print("Converting x_train images to numpy...")
x_train = np.array([misc.imread(path) for path in x_train])
print(x_train.shape)
print(x_train[0].shape)
print(x_train[1].shape)
打印电话的输出是:
(9600,) # x_train
(188, 282, 3) # x_train first image
(686, 1024, 3) # x_train second image
如果我现在将x_train
提供给我的模型,则会抛出以下错误:
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (9600, 1)
我如何塑造我的训练集才能被我的模特接受?
答案 0 :(得分:0)
Keras希望您为x_train
输入一个与输入形状相匹配的张量(一个4维张量)。你在x_train
中创建的是一个不同形状的三维数组对象的一维数组,而不是四维数组。这不是一个张量。张量不能是锯齿状的,它不能有各种形状的行。
由于矢量化,Keras要求您输入张量。为了获得矢量化的好处,所有输入必须是张量。 Keras不能处理锯齿状的张量,也不能处理numpy。要运行不同形状的输入,使用Keras矢量化所要做的就是填充每个图像(在所有3个通道中都有0' s),使它们的形状一致。或者,您可以编写一个for循环,一次一个地通过模型运行输入。这基本上会失去矢量化的所有好处,并且可能需要很长时间来训练您的模型。