How to merge two different models in Keras
Overview
In the Keras, the user can design a model in two ways. Each method has its own use cases. These methods are:
- Sequential API: We use this method when the objective is to write the code in a linear fashion. This method makes debugging easier because of its readability.
- Functional API: It follows a non-linear topology for designing models. As a user, we have the flexibilty to join different layers of different networks.
We use the functional APIs usually when we’re working with more than two models simultaneously.
In this shot, we’ll discuss how a user can merge two separate models from a built in keras function; keras.layers.concatenate()
It is defined as follows:
merged_layer= keras.layers.concatenate(inputs, axix, name="")
-
inputs: The layers of two models at which we want to merge these models. -
axis: The axis along which we want to concatenate the two layers. -
name: The name of concatenated/merged layer.
Let’s look at the steps to merge the two models.
- For this, we’ll require the following libraries:
import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom keras import layersfrom keras.layers import Input, Dense, concatenatefrom keras.models import Modelfrom keras.utils import plot_model
- Model A and B are the sample models that we want to merge.
Note that functional API is used in this job. However, a sequential API (using the
add()function) would also have served the purpose.
# Model Aa_ip_img = Input(shape=(32,32,1), name="Input_a")al_1 = Dense(64, activation = "relu",name ="a_layer_1")(a_ip_img)al_2 = Dense(128, activation="relu",name ="a_layer_2")(al_1)al_3 = Dense(64, activation="relu",name ="a_layer_3")(al_2)al_4 = Dense(32, activation="sigmoid",name ="a_output_layer")(al_3)#Model Bb_ip_img = Input(shape=(32,32,1), name="Input_b")bl_1 = Dense(64, activation="relu",name ="b_layer_1")(b_ip_img)bl_2 = Dense(32, activation = "sigmoid",name ="b_output_layer")(bl_1)
- Now that we have our models, we can concatenate them using the
concatenate()function.
#Merging model A and Ba_b = concatenate([al_4,bl_2],name="concatenated_layer")
- We can opt to make further layers after the concatenated layer or call this layer
output_layer. Here, another dense layer has been added before model generation.
#Merging model A and Ba_b = concatenate([al_4,bl_2],name="concatenated_layer")#Final Layeroutput_layer = Dense(16, activation = "sigmoid", name = "output_layer")(a_b)#Model Definitionmerged = Model(inputs=[(a_ip_img,b_ip_img)],outputs=[output_layer], name = "merged model")
We need to be careful when initializing the final model using the keras.Model() function. We must mention the inputs of both models in the inputs field.
- We can analyze the resulting model using
keras.utilsorkeras.Model.summary().
#Model Detailsmerged.summary()keras.utils.plot_model(merged, "output/architecture.png", show_shapes=True)
The final is attached below. We can also run this and play around with different inputs and parameters.
import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom keras import layersfrom tensorflow.keras.layers import Input, Dense, concatenatefrom tensorflow.keras.models import Modelfrom tensorflow.keras.utils import plot_model# Model Aa_ip_img = Input(shape=(32,32,1), name="Input_a")al_1 = Dense(64, activation = "relu",name ="a_layer_1")(a_ip_img)al_2 = Dense(128, activation="relu",name ="a_layer_2")(al_1)al_3 = Dense(64, activation="relu",name ="a_layer_3")(al_2)al_4 = Dense(32, activation="sigmoid",name ="a_output_layer")(al_3)#Model Bb_ip_img = Input(shape=(32,32,1), name="Input_b")bl_1 = Dense(64, activation="relu",name ="b_layer_1")(b_ip_img)bl_2 = Dense(32, activation = "sigmoid",name ="b_output_layer")(bl_1)#Merging model A and Ba_b = concatenate([al_4,bl_2],name="concatenated_layer")#Final Layeroutput_layer = Dense(16, activation = "sigmoid", name = "output_layer")(a_b)#Model Definitionmerged = Model(inputs=[(a_ip_img,b_ip_img)],outputs=[output_layer], name = "merged model")#Model Detailsmerged.summary()keras.utils.plot_model(merged, "output/architecture.png", show_shapes=True)
Free Resources