将完全连接层转换为卷积层时出错

时间:2020-06-14 15:14:12

标签: python tensorflow keras conv-neural-network valueerror

我正在使用带有“ imagenet”权重的VGG-16进行迁移学习。而不是完全连接的层,我想使用卷积层。下面是代码:

IMG_WIDTH = 224
IMG_HEIGHT = 224
N_CHANNELS = 3

model = applications.VGG16(include_top=False, weights="imagenet", input_shape=(IMG_HEIGHT, IMG_WIDTH, 
                           N_CHANNELS))

for i in range(4) :
   model.layers.pop()

for layers in model.layers :
   layers.trainable = False

x = Conv2D(32, (7,7), padding = "valid", strides = (1,1), activation="relu", 
           kernel_initializer=he_normal(seed=42), bias_initializer=zeros())(model.layers[-1].output)
x = Conv2D(16, (1,1), padding = "valid", strides = (1,1), activation="relu", 
           kernel_initializer=he_normal(seed=42), bias_initializer=zeros())(x)

final_model = Model(model.input, x)

模型架构的最后几层:

enter image description here

错误: enter image description here enter image description here

此错误的可能原因是什么?

0 个答案:

没有答案