我知道此错误已多次发布,但我没有找到与我匹配的案例,我真的不明白为什么会出现这种错误。
所以我试图微调一个VGG16网络,而且如果我只是改变了输出层并使一些早期的层可训练,那么训练似乎不起作用,我只是想通过删除最后一层并添加新的。
具体来说,我将顶层删除到最后一个卷积层,因此网络如下所示:
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 224, 224, 3) 0
_________________________________________________________________
block1_conv1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
block1_conv2 (Conv2D) (None, 224, 224, 64) 36928
_________________________________________________________________
block1_pool (MaxPooling2D) (None, 112, 112, 64) 0
_________________________________________________________________
block2_conv1 (Conv2D) (None, 112, 112, 128) 73856
_________________________________________________________________
block2_conv2 (Conv2D) (None, 112, 112, 128) 147584
_________________________________________________________________
block2_pool (MaxPooling2D) (None, 56, 56, 128) 0
_________________________________________________________________
block3_conv1 (Conv2D) (None, 56, 56, 256) 295168
_________________________________________________________________
block3_conv2 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_conv3 (Conv2D) (None, 56, 56, 256) 590080
_________________________________________________________________
block3_pool (MaxPooling2D) (None, 28, 28, 256) 0
_________________________________________________________________
block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160
_________________________________________________________________
block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808
_________________________________________________________________
block4_pool (MaxPooling2D) (None, 14, 14, 512) 0
_________________________________________________________________
block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808
_________________________________________________________________
block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808
=================================================================
Total params: 12,354,880
Trainable params: 0
Non-trainable params: 12,354,880
_________________________________________________________________
然后我添加一个卷积层:
vgg16_model_ft.add(Conv2D(512, (3,3), padding='same', activation='relu'))
它引发了众所周知的错误:
ValueError: Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=2
我真的不明白为什么ndim = 2被发现用于那个新图层,它对我来说没有意义,即使这样做
vgg16_model_ft.add(Conv2D(512, (3,3), padding='same', activation='relu', input_shape=vgg16_model_ft.layers[-1].output_shape))
没有解决它。但是我对Keras来说仍然是一个新人,所以我必须要有一些微妙之处。 我使用带有Tensorflow后端的Keras 2.1.5。
答案 0 :(得分:1)
我不知道为什么会收到此错误,但您可以尝试使用Functional API
model_input=Input(shape=(224,224,3))
vgg16_model_ft=VGG16(include_top=False)(input)
new_model=Conv2D(512, (3,3), padding='same', activation='relu')(vgg_model_ft)
new_model=Model(input,new_model)
答案 1 :(得分:0)
感谢您的回答,我在切换到功能API时发现了它。显然,使用model.layers.pop()
弹出图层确实会更改model.summary()
,但不会更改模型本身(除非您编译我猜,但在添加我想要的所有图层之前我没有这样做)。
无论如何,我能够在没有功能API的情况下完成它。
现在我有另一个不相关的问题,但我不知道是否应该再发一个帖子......