使用VGG进行迁移学习时如何“拼接”图层?

问题描述

我正在尝试通过使VGG16更“深”来提高模型的准确性;

#@title Model 2.3
#@markdown Add some more layers between the blocks of VGG16
vgg16_full = VGG16(include_top=False,weights='imagenet',input_tensor=None,input_shape=(32,32,3),pooling='max',classes=10)

model = Sequential()
i = 0

model.add(Conv2D(filters = 32,kernel_size=(3,activation='relu',strides=1,padding='same',3)))

for layer in vgg16_full.layers:
  layer.trainable = False # Setting the trainable flag to false makes sure that the imported weights are used
  i+=1
  if i < 12:
    model.add(layer)

model.add(Conv2D(filters=128,data_format='channels_last'))
model.add(Batchnormalization())
model.add(Dropout(0.25))

model.add(Flatten()) # Without this layer we get a dimension-error
model.add(Dense(512,activation='relu'))
model.add(Batchnormalization())
model.add(Dropout(0.5))
model.add(Dense(128,activation='relu'))
model.add(Batchnormalization())
model.add(Dropout(0.5))
model.add(Dense(10,activation='softmax')) # This is the only layer we will be training.

model22 = model
# model2.summary()
model22.compile(loss="sparse_categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
hist22 = model22.fit(x_train,y_train,batch_size=32,epochs=50,validation_data=(x_validate,y_validate),callbacks=[monitor])

在VGG层工作正常之后添加额外的层,但是当我尝试将它们放在块之前或块之间时,会出现此错误

ValueError: Input 0 of layer block1_conv1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape [None,32]

我在做什么错了?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)