Add layer between two layers in saved model tensorflow

I have a h5 file of a model for which I don’t have the code used to build it. I want to load the model, select two layers and insert between these layers a custom one. I tried the following code

model = keras.models.load_model('weights.h5')
previous_layer = model.layers[index_of_previous_layer]
next_layer = model.layers[index_of_next_layer]
custom_layer = Custom(...)
previous_output = previous_layer.output
custom_output = custom_layer(previous_output)
next_layer.input = custom_output

But I get this error

AttributeError: Can't set the attribute "input", 
likely because it conflicts with an existing read-only @property of the object. 
Please choose a different name.

is there a way to add a layer between two layers of a saved model? Does it work even if the model has skip connections?

@zoythum Welcome to Tensorflow Forum !

Yes, it is possible to add a layer between two layers of a saved model. This can be done using the keras.models.Model class. The following code shows how to add a layer between the second and third layers of a saved model:

import keras

def add_layer(model, layer_name, new_layer):
  """Adds a layer between two layers of a saved model.

  Args:
    model: The saved model.
    layer_name: The name of the layer to insert the new layer after.
    new_layer: The new layer to insert.

  Returns:
    A new model with the new layer added.
  """

  input_tensor = model.input
  output_tensor = model.get_layer(layer_name).output
  new_output_tensor = new_layer(output_tensor)
  new_model = keras.models.Model(input_tensor, new_output_tensor)
  return new_model

model = keras.models.load_model('my_model.h5')
new_layer = keras.layers.Dense(10, activation='relu')
new_model = add_layer(model, 'layer2', new_layer)

This code will create a new model with the new layer added between the second and third layers of the original model. The new model will have the same weights as the original model, but the new layer will be added to the network.

If the model has skip connections, these will be preserved when the new layer is added. This means that the new layer will receive the output of both the second and third layers of the original model.

Let us know if this helps.

1 Like

Thaks for your help, unfortunately your solution does not work. You are creating a new model that ends with the new layer instead of adding it to the old layer. This might work if I want to replace the last layers of the model but not in my case :frowning:
Thank you anyway for your help!