ValueError:检查输入时出错:预期input_1具有5个维,但数组的形状为(1221,50,50,1)

时间:2020-01-21 11:54:31

标签: python opencv tensorflow keras deep-learning

我正在尝试使用xtrain=(1221,50,50,1)运行模型 在model.fit时显示此错误

ValueError:检查输入时出错:预期input_1具有5个维,但数组的形状为(1221,50,50,1)

我使用这些功能:

model.compile(loss=categorical_crossentropy,optimizer=Adadelta(lr=0.8), metrics=['acc'])
model.fit(x=ZZ, y=yyy, batch_size=128, epochs=1, validation_split=0.2)

当我将尺寸增加到(1221,50,50,1,1)时 使用expand_dims我收到此错误:

ValueError:检查输入时出错:预期input_1具有形状(16,50,50,1),但具有形状为(1221,50,50,1,1)的数组

Idk我在哪里弄错了

这是我的模特

input_layer = Input((16, 50, 50, 1))
## convolutional layers
conv_layer1 = Conv3D(filters=8, kernel_size=(3, 3, 3), activation='relu')(input_layer)
conv_layer2 = Conv3D(filters=16, kernel_size=(3, 3, 3), activation='relu')(conv_layer1)

## add max pooling to obtain the most imformatic features
pooling_layer1 = MaxPool3D(pool_size=(2, 2, 2))(conv_layer2)

conv_layer3 = Conv3D(filters=32, kernel_size=(3, 3, 3), activation='relu')(pooling_layer1)
conv_layer4 = Conv3D(filters=64, kernel_size=(3, 3, 3), activation='relu')(conv_layer3)
pooling_layer2 = MaxPool3D(pool_size=(2, 2, 2))(conv_layer4)

## perform batch normalization on the convolution outputs before feeding it to MLP architecture
pooling_layer2 = BatchNormalization()(pooling_layer2)
flatten_layer = Flatten()(pooling_layer2)

## create an MLP architecture with dense layers : 4096 -> 512 -> 10
## add dropouts to avoid overfitting / perform regularization
dense_layer1 = Dense(units=2048, activation='relu')(flatten_layer)
dense_layer1 = Dropout(0.4)(dense_layer1)
dense_layer2 = Dense(units=512, activation='relu')(dense_layer1)
dense_layer2 = Dropout(0.4)(dense_layer2)
output_layer = Dense(units=6, activation='softmax')(dense_layer2) #Use 5 instead of 1

## define the model with input layer and output layer
model = Model(inputs=input_layer, outputs=output_layer)```

3 个答案:

答案 0 :(得分:0)

input_layer = Input((16, 50, 50, 1))

此行指定输入的形状为(16, 50, 50 ,1)。您正在将整个(1221, 50, 50, 1)向量传递给网络。您需要重塑为(X, 16, 50, 50, 1),以使向量的元素具有所需的形状(16, 50, 50, 1)

答案 1 :(得分:0)

问题出在您的xtrain上,它包含1221个形状为(50,50,1)的图像,但是Conv3D希望使用形状为(16,50,50,1)的图像。

如果您感到困惑,则Conv2D的input_layer不需要图像总数。它只需要图像的形状即可。

在您的帮助下,您可以查看this示例如何传递和设置图像。

答案 2 :(得分:0)

我遇到了我的x_train应该犯的什么错误,该错误应包括(X,16,50,50,1) 但16个不在。 这是由于输入错误。

谢谢您的帮助 瑞沙(Rishabh Sahrawat) 奥菲·福柯兹 MH304