如何在Keras TensorFlow中结合两个预定义的模型?

问题描述

我想构建一个具有两个输入并使用EfficientNetB1提取特征并在新层上进行微调的神经网络,所以我编写了以下代码

def createNet(self,shape):
    FE1 = K.applications.EfficientNetB1(include_top=False,input_shape=shape)
    FE2 = K.applications.EfficientNetB1(include_top=False,input_shape=shape)
    inp1 = FE1.input
    out1 = FE1.layers[-1].output
    inp2 = FE2.input
    out2 = FE2.layers[-1].output

    merged_out = K.layers.concatenate((out1,out2))
    # .... other layers
    self.model = K.models.Model(inputs=[inp1,inp2],outputs=[merged_out])
    
    self.model.summary()

但是我得到了这个错误

ValueError: The name "stem_conv_pad" is used 2 times in the model. All layer names should be unique.

那我该如何建立我的模型?

解决方法

基于this,我添加了以下代码来解决它。

for layer in FE2.layers:
    layer._name = layer.name + str("_2")