SoFunction
Updated on 2024-11-12

keras weight saving and weight loading methods

If full weight loading is required, use the weight loading method directly

model.save_weights('./weigths.h5')
model2.load_weights('./weigths.h5')

But sometimes you only need to load some of the weights

So here's how you do it.

First, name all the layers and add the method name='layer1' directly in the layer

Second, use, change the names of the values you don't need to load weights.

Finally, load the weights.

x=BatchNormalization(axis=channel_axis,name='layer2')(x)
 
[-1].name='pred'
model2.load_weights('./weigths.h5',by_name=True)

The code above is the corresponding operation, here I have loaded the weights for all the layers except the last one, remember that by_name must be assigned to True so that the weights can be assigned according to the name counterpart.

Attention:The structure of the two models must be the same, otherwise something could go wrong

Additional knowledge:Saving and loading weights and model structure in Keras

1. Preservation and loading of model structures

(1) Save as JSON string

json_string = model.to_json()

(2) Reconstructing models from JSON strings

from import model_from_json
model = model_from_json(json_string)

(3) Save as YAML String

yaml_string = model.to_yaml()

(4) Reconstructing Models from YAML Strings

model = model_from_yaml(yaml_string)

2. Saving and loading model weights (parameters)

from  import load_model
 
# Create HDF5 file 'my_model.h5' to save model parameters
('my_model.h5')
 
# Load model parameters
load_model('my_model.h5')

2.1 Handling custom layers (or other custom objects) in saved models

If the model to be loaded contains custom layers or other custom classes or functions, they can be passed to the loading mechanism via the custom_objects parameter:

from  import load_model
 
# Suppose your model contains an instance of the AttentionLayer class.
model = load_model('my_model.h5', custom_objects={'AttentionLayer': AttentionLayer})

Alternatively, you can use custom object scopes:

from  import CustomObjectScope
 
with CustomObjectScope({'AttentionLayer': AttentionLayer}):
 model = load_model('my_model.h5')

Custom objects are handled in the same way as load_model, model_from_json, model_from_yaml work:

from import model_from_json

model = model_from_json(json_string, custom_objects={'AttentionLayer': AttentionLayer})

June 1, 2019 Updated:

More details on how to use it:

How do I save a Keras model?

(1) An HDF5 file holds both the structure and the weights of the model.

We do not recommend using pickle or cPickle to save Keras models.

You can use (filepath) to save the Keras model and weights in an HDF5 file that will contain:

structure of the model in order to reconstruct the model

Weights of the model

Training configuration (loss function, optimizer, etc.)

The state of the optimizer to pick up where the last training break left off

Use .load_model(filepath) to re-instantiate your model, and this function will also finish compiling the model at the same time if the training configuration is stored in the file.

Example:

from  import load_model
 
('my_model.h5') # creates a HDF5 file 'my_model.h5'
del model # deletes the existing model
 
# returns a compiled model
# identical to the previous one
model = load_model('my_model.h5')

(2) Only the structure of the model is preserved

Can be used if you just want to preserve the structure of the model without its weights or configuration information:

# save as JSON
json_string = model.to_json()
 
# save as YAML
yaml_string = model.to_yaml()

This operation will serialize the model into json or yaml files which are also human friendly, you can even open these files manually and edit them if needed.

Of course, you can also load the model from a saved json file or yaml file:

# model reconstruction from JSON:
from  import model_from_json
model = model_from_json(json_string)
 
# model reconstruction from YAML
model = model_from_yaml(yaml_string)

(3) Save only the weights of the model

If you need to save the weights of the model, you can do so using HDF5 with the code below. Note that you need to make sure you have HDF5 and its Python library h5py installed before using it.

model.save_weights('my_model_weights.h5')

If you need to initialize an identical model in code, use:

model.load_weights('my_model_weights.h5')

If you need to load weights into a different network structure (some layers are the same), such as fine-tune or transfer-learning, you can load the model by layer name:

model.load_weights('my_model_weights.h5', by_name=True)

Example:

"""
Suppose the original model is:
 model = Sequential()
 (Dense(2, input_dim=3, name="dense_1"))
 (Dense(3, name="dense_2"))
 ...
 model.save_weights(fname)
"""
# new model
model = Sequential()
(Dense(2, input_dim=3, name="dense_1")) # will be loaded
(Dense(10, name="new_dense")) # will not be loaded
 
# load weights from first model; will only affect the first layer, dense_1.
model.load_weights(fname, by_name=True)

This above keras weight saving and weight loading method is all that I have shared with you, I hope it will give you a reference and I hope you will support me more.