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

时间:2020-08-22 13:54:21

标签: python keras vgg-net

我正在尝试通过使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,3), activation='relu', strides=1, padding='same', input_shape=(32,32,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, kernel_size=(3, 3), activation='relu', strides=1, padding='same', 
                 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, 32, 32]

我在做什么错了?

0 个答案:

没有答案