Keras Nested Models save and load weights separately or view Summary of all nested models

Issue

I am trying to train a Keras Model which include two nested models, and I want to save the weights of both inner models separately. Right now I am able to save weights of the whole model, but I am unable to load the weights of nested models within big model.

Output of Big_model.summary looks like this

Model: "model_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_4 (InputLayer)         [(None, 128, 128, 1)]     0         
_________________________________________________________________
model (Model)                (None, 16, 16, 512)       170369024 
_________________________________________________________________
model_1 (Model)              (None, 128, 128, 1)       15342209  
=================================================================
Total params: 185,711,233
Trainable params: 185,711,233
Non-trainable params: 0

How can I even see the summary of both inner models, e.g Big_model.inner_Model1.summary() something like that, Or save the weights of both inner models separately after training using Big_model.inner_Model1.save_weights() and Big_model.inner_Model2.save_weights()or callbacks during model.fit.

What I am getting is Big_model has no module as inner_Model1, Any help Please ??

PS: There is no problem with training or anything, I can run the training, also I am using Tensorflow version tf.keras.models.Model for models.

This is how I am creating models

inner_Model1 = tf.keras.models.Model()
inner_Model2 = tf.keras.models.Model()

x = tf.keras.layers.Input(shape=IMAGE_SHAPE)
Big_model = tf.keras.models.Model(x, inner_model2(inner_model1(x)))
Big_model.compile(optimizer=optimizer, loss='mean_absolute_error')

Solution

In that summary you posted, model is layer 1, and model_1 is layer 2:

Big_model.layers[1].summary()   #this is inner_Model1.summary()
Big_model.layers[2].summary()   #this is inner_Model2.summary()

Do whatever you want with them.


If you created the model like you did, there is nothing wrong with simply doing:

inner_Model1.save_weights(...)
inner_Model2.save_weights(...)

It will also work OK if you load the weights outside the big model, it will see the changes.

inner_Model1.load_weights(...)
inner_Model2.load_weights(...)

Answered By – Daniel Möller

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published