Search code examples
tensorflowkeras

Using different backbone network in keras gets error


I am using several backbone network for transfer learning.

However, I got an error as below.

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

How could I make all backbone network has different layer name?

I call backbone network such as below.

model_input = keras.Input(shape=(image_size, image_size, 3))

densenet121 = keras.applications.DenseNet121(
    weights="imagenet", include_top=False, input_tensor=model_input 
)

resnet50 = keras.applications.ResNet50(
    weights="imagenet", include_top=False, input_tensor=model_input 
)

input_b = resnet50.get_layer("conv2_block3_2_relu").output
input_d = densenet121.get_layer("conv2_block6_1_relu").output

model_output = layers.Concatenate(axis=-1)([input_b, input_d])

keras.Model(inputs=model_input, outputs=model_output)

Solution

  • One possibility is to take advantage of the fact that a Model is composable like a Layer using the Keras Functional API. All the layers in a Model are prefixed with the name of the model, so that should solve the issue of layer names conflict.

    Using your example, you could do the following:

    model_input = keras.Input(shape=(image_size, image_size, 3))
    
    densenet121 = keras.applications.DenseNet121(
        weights="imagenet", include_top=False, input_tensor=model_input 
    )
    
    resnet50 = keras.applications.ResNet50(
        weights="imagenet", include_top=False, input_tensor=model_input 
    )
    input_b = resnet50.get_layer("conv2_block3_2_relu").output
    input_d = densenet121.get_layer("conv2_block6_1_relu").output
    
    backbone_resnet = keras.Model(inputs=resnet50.inputs, outputs=input_b, name="resnet50")
    backbone_densenet = keras.Model(inputs=densenet121.inputs, outputs=input_d, name="densenet121")
    
    # using Model as Layer using the Functional API
    out_resnet = backbone_resnet(model_input)
    out_densenet = backbone_densenet(model_input)
    
    model_output = layers.Concatenate(axis=-1)([out_resnet, out_densenet])
    
    keras.Model(inputs=model_input, outputs=model_output, name="multi_backbone_model")