-3

Suppose we have saved our model as h5 then we can load model using load_model function of keras.. I have gone through multiple github links where the author provides weights file (.h5 or .hdf5 file).

So can we use those pretrained weights/model using load_model?

what is difference between load_model and load_weights?

Can I load weight file with load_model (keras) or should I use (load_weight). If we are loading weight then we mush also load whole architecture of the model..

Please provide deeper understanding by taking some example..

YScharf
  • 1,638
  • 15
  • 20
user2986845
  • 75
  • 1
  • 2
  • 8

1 Answers1

4

Defining the model

model = ...

Training the model

model.fit...

Saving the model:

model.save('model_topology.h5) 

Saving the weights:

model.save_weights(`weights.h5`)

When calling load_model(), if you want to run inference, you have to call load weights to load the pretrained weights.

model = load_model('model_toplogy.h5)
model.load_weights('weights.h5)

This is true when dealing with models saved in the HDF5 format (.h5).

Tensorflow provides another format for saving models: the saved_model format. The save_model format saves the model topology (graph) and trained parameters (weights and biases) in a directory. This enables loading a model with one command.

model = tf.saved_model.load(path_to_dir)

About h5 and HDF5: h5 is just a shortened extension for files saved in the HDF5 format.

YScharf
  • 1,638
  • 15
  • 20
  • 1
    got your point. Upvoted. If we are using Modelcheckpoint then can we save weights in either format(.h5 or .hdf5)? Also, if I am not wrong.. then suppose if we are not saving the weights file instead saving model only then we can directly load model using load_model.. right – user2986845 May 19 '22 at 12:59
  • What is difference between .h5 and .hdf5 format? – Nike Apr 29 '23 at 15:03