如何在预训练的 MobileNetV3 模型之上添加额外的层?

How to add extra layer on the top of pretrained MobileNetV3 model?

我有一个模型,它使用预训练的 MobileNetV3Large 模型并连接类似的 U-net 架构。那不是问题。但是我想用这个model2添加model1。在 model2 中,我只有批量归一化和丢失,我想将其添加到这个 model2 的顶部。我尝试了很多东西,但它不能正常工作。有什么想法吗?!

模型 2

inputs = Input((256,256,3))

# MobileNetV3
mobilenet = MobileNetV3Large(include_top=False, weights="imagenet", input_tensor=inputs)

mobilenet.layers[89]._name = "relu_3"
mobilenet.layers[196]._name = "relu_4"

l4 = mobilenet.get_layer("relu_3").output
b_layer = mobilenet.get_layer("relu_4").output   

up = Conv2DTranspose(256, (2, 2), strides=2, padding="same")(b_layer)
up = Concatenate()([up, l4])
conv = Conv2D(256, (3, 3), activation='relu', padding="same")(up)
conv = Conv2D(256, (3, 3), activation='relu', padding="same")(conv)

#output
outputs = Conv2D(1, 1, padding="same", activation="sigmoid")(conv)

model2 = Model(inputs , outputs)
model2.summary()

模型 1

inputs = Input((256,256,3))

x = Sequential()
x = inputs
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
outputs1 = x

model1 = Model(inputs , outputs1)
model1.summary()

您可以按功能堆叠模型:

model2 = Model(inputs)
model1 = Model(model2)
model3 = Model(inputs, model1)

您可以按顺序堆叠模型:

model3 = Sequential()
for layer in model2.layers:
    model3.add(layer)

for layer in model1.layers:
    model3.add(layer)