【问题标题】:How to combine two predefined models in Keras TensorFlow?如何在 Keras TensorFlow 中组合两个预定义模型?
【发布时间】:2020-09-27 06:49:17
【问题描述】:

我想构建一个有两个输入的神经网络,并使用 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.

那么我该如何构建我的模型呢?

【问题讨论】:

  • 好像有层命名冲突,因为你使用了相同的预定义模型两次,所以每个层都有相同的名称。可以在一种模式下重命名所有层,即。看这里:datascience.stackexchange.com/questions/40886

标签: python tensorflow keras deep-learning


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    • 2019-10-20
    • 2018-01-23
    • 2020-10-08
    • 2021-02-22
    相关资源
    最近更新 更多