我试图将具有以下形状的数据拟合到预训练的keras vgg19模型 .
图像输入形状是 (32383, 96, 96, 3)
标签形状是 (32383, 17)
我得到了这个错误
expected block5_pool to have 4 dimensions, but got array with shape (32383, 17)
在这条线上
model.fit(x = X_train, y= Y_train, validation_data=(X_valid, Y_valid),
batch_size=64,verbose=2, epochs=epochs,callbacks=callbacks,shuffle=True)
这是我定义模型的方式
model = VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(96,96,3),classes=17)
maxpool怎么给我2d张量而不是4D张量?我正在使用keras.applications.vgg16中的原始模型 . 我该如何解决这个错误?
1 回答
您的问题来自
VGG16(include_top=False,...)
,因为这使您的解决方案只加载VGG的卷积部分 . 这就是为什么Keras
抱怨它有二维输出的四维输出(4个维度来自卷积输出形状(nb_of_examples, width, height, channels)
的事实) . 为了克服这个问题,您需要设置include_top=True
或添加将压缩卷积部分的其他层 - 一个2d
(例如使用Flatten
,GlobalMaxPooling2D
,GlobalAveragePooling2D
和一组Dense
层 - 包括最后一层应该是aDense
,大小为17,softmax
激活功能) .