Reputation: 19
How to combine two Keras models using functional API. I mean, I have two models (a, which is pretrained with freezer weights, and b). I want to create a c model by adding the b model to the bottom of the frozen model.
In detail, I have the following two models:
def define_neural_network_model_1st(input_shape, initializer, outputs = 1):
........
return model
def define_neural_network_model_2st(input_shape, initializer, outputs = 1):
........
return model
Since the first one is trained I am loading the weights and freezing the model.
neural_network_model_1st.load_weights('./../some_path.hdf5')
neural_network_model_1st.trainable = False
When I am trying to merge both blocks in the following way
merge_interpretation = Model(inputs=[neural_network_model_1st.inputs], outputs=neural_network_model_2st(neural_network_model_1st.inputs))
I am receiving:
What I am doing wrong? I am waiting to have 1 layer from the frozen model plus all layers in the second one.
Upvotes: 0
Views: 533
Reputation: 1634
Let suppose I have two models,
resnet_50 = tf.keras.applications.ResNet50(weights=None,
input_shape=(224 , 224 , 3),
classes = 2)
vgg_16 = tf.keras.applications.VGG16(
weights=None,
input_shape=(224,224,3),
classes=2,
include_top=False)
Now I want to merge these two models, first of all, I will make sure the output of the first model should be the same shape as the input of the second model, so for that first I have to do some pre-processing.
model = tf.keras.Model(vgg_16.inputs , vgg_16.layers[-2].output)
model2 = tf.keras.Model(resnet_50.get_layer('conv4_block6_out').input , resnet_50.output)
input = tf.keras.layers.Input(shape=(224 , 224 , 3))
out1 = model(input)
intermediate_layer = tf.keras.layers.Conv2D(model2.inputs[0][0].shape[2] , 1 , 1 , padding='same')(out1)
out2 = model2(intermediate_layer)
f_model = tf.keras.Model(input , out2)
tf.keras.utils.plot_model(
f_model,
show_shapes=True)
Now this is the output shape of the two models [The Architecture of the combined two models][1] [1]: https://i.sstatic.net/F7H8d.png
You can see the individual summary of the models by doing this
f_model.layers[1].summary() #This will show the summary of the first model VGG16
f_model.layers[3].summary() #This will show the summary of the second model Resnet18
But if you run the f_model.summary()
this will not show the summary of the combined two models as one, because in the backend Keras take model one as a functional graph Node so it acts as a Node of the graph.
Upvotes: 1