Change Model/Layer name in TensorFlow Keras
Sometimes you need to change the name of one or more layers in your tf.keras model.
Q: Why one should do such a thing? Is there any reason for renaming layers/models?
A: Yes, of course there are reasons. For example, if you want to use a pre-trained model, let’s say MobileNet, twice and combine them into a single model, you will get an error saying something like “ The name is used 2 times … all layer names should be unique.”
You might think “That’s not even a problem. Renaming layers should not be that much of a trouble!”
Well! It depends on what your model is and how you get it.
When you design, build, and train your own model, you are a hundred percent right. Modification is simple. Especially renaming the model or some of its layers are surely easy and straight forward.
But what if you needed to do that with an existing model having pre-trained weights, e.g., those models that are available from tf.keras.applications?
Yeah! This is where things are going to get tricky 😄
First of all, I just want to make sure that you understand that the code snippet below would not work!
# Somethings like this
for layer in model.layers:
layer._name = "new_name"# Or this
model._name = "new_name"
Savvy?!
Here, I show you how to do this through an example in which I first load a pre-trained MobileNetV2 model and then, I will add a prefix to the model name and its layers name.
In the function “add_prefix”, I do the following things in order:
- Get model configurations by calling model.get_config()
A model config is a dictionary that has information about each layer (including its name, hyperparameters, parent layers, etc.), inputs and outputs of model, and name of the model. - In config, for each layer, add prefix to its name
- I keep a mapping from the old (original) names to new names and also a mapping from new ones to old ones. It is needed, because the parent(s) of each layer has to be renamed as well. And the parents of each layer are saved in “inbound_nodes”
3. Again, in config, change model name and the name of its input and output layers.
4. Create a new model from the modified config
5. For each layer, load pre-trained weights from the original model.
- In the step 5, the new names to old names mapping is needed.
Now, Let’s use this function:
Layer #2 name [before => after]: Conv1 => mobilenetv2_Conv1
That’s it. Actually, you were right. That was simple anyway 😃