Im trying to save and load weights from the model i have trained.
the code im using to save the model is.
TensorBoard(log_dir='/output') model.fit_generator(image_a_b_gen(batch_size), steps_per_epoch=1, epochs=1) model.save_weights('model.hdf5') model.save_weights('myModel.h5')
Let me know if this an incorrect way to do it,or if there is a better way to do it.
but when i try to load them,using this,
from keras.models import load_model model = load_model('myModel.h5')
but i get this error:
ValueError Traceback (most recent call last) <ipython-input-7-27d58dc8bb48> in <module>() 1 from keras.models import load_model ----> 2 model = load_model('myModel.h5') /home/decentmakeover2/anaconda3/lib/python3.5/site- packages/keras/models.py in load_model(filepath, custom_objects, compile) 235 model_config = f.attrs.get('model_config') 236 if model_config is None: --> 237 raise ValueError('No model found in config file.') 238 model_config = json.loads(model_config.decode('utf-8')) 239 model = model_from_config(model_config, custom_objects=custom_objects) ValueError: No model found in config file.
Any suggestions on what i may be doing wrong? Thank you in advance.
Using save_weights() method Now you can simply save the weights of all the layers using the save_weights() method. It saves the weights of the layers contained in the model. It is advised to use the save() method to save h5 models instead of save_weights() method for saving a model using tensorflow.
Now to save the weights only using the simple way, you just have to call the built-in function save_weights on your model. and train it for a few epochs. This will create a folder named weights_folder and save the weights in Tensorflow native format with the name of my_weights. It is going to have 3 files.
Here is a YouTube video that explains exactly what you're wanting to do: Save and load a Keras model
There are three different saving methods that Keras makes available. These are described in the video link above (with examples), as well as below.
First, the reason you're receiving the error is because you're calling load_model
incorrectly.
To save and load the weights of the model, you would first use
model.save_weights('my_model_weights.h5')
to save the weights, as you've displayed. To load the weights, you would first need to build your model, and then call load_weights
on the model, as in
model.load_weights('my_model_weights.h5')
Another saving technique is model.save(filepath)
. This save
function saves:
To load this saved model, you would use the following:
from keras.models import load_model new_model = load_model(filepath)'
Lastly, model.to_json()
, saves only the architecture of the model. To load the architecture, you would use
from keras.models import model_from_json model = model_from_json(json_string)
For loading weights, you need to have a model first. It must be:
existingModel.save_weights('weightsfile.h5') existingModel.load_weights('weightsfile.h5')
If you want to save and load the entire model (this includes the model's configuration, it's weights and the optimizer states for further training):
model.save_model('filename') model = load_model('filename')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With