ChatGPT解决这个技术问题 Extra ChatGPT

Save and load weights in keras

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.


b
blackHoleDetector

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:

The architecture of the model, allowing to re-create the model.

The weights of the model.

The training configuration (loss, optimizer).

The state of the optimizer, allowing to resume training exactly where you left off.

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)

If I save the weights on python 3.6 is it possible to load them on python 2.7?
@Rtucan I think Yes. You can try it.
Is it possible to load weights from the saved model from model.save() , not model.save_weights? If so how to do it?
D
Daniel Möller

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')

by model if you mean all the layers,the i have all that i just havent posted it
I am getting this error when I try to model the complete model using load_model(). Could you please let me know how to fix the below error: ValueError: You are trying to load a weight file containing 17 layers into a model with 0 layers
@KK2491 Are you really using load_model? This is an error for load_weights. If you're using load_model, it seems your file is corrupted, or your keras version is buggy.
@DanielMöller Yea, I am using load_model. The Keras version I use is 2.2.4.
@Jubick Actually there is a simpler method. You can directly save the model and load it. (.model extension)
S
Sam Vanhoutte

Since this question is quite old, but still comes up in google searches, I thought it would be good to point out the newer (and recommended) way to save Keras models. Instead of saving them using the older h5 format like has been shown before, it is now advised to use the SavedModel format, which is actually a dictionary that contains both the model configuration and the weights.

More information can be found here: https://www.tensorflow.org/guide/keras/save_and_serialize

The snippets to save & load can be found below:

model.fit(test_input, test_target)
# Calling save('my_model') creates a SavedModel folder 'my_model'.
model.save('my_model')

# It can be used to reconstruct the model identically.
reconstructed_model = keras.models.load_model('my_model')

A sample output of this :

https://i.stack.imgur.com/tllIL.png


After loading the saved model (weights) how can i predict unseen data? can someone provide any sample code for predicting with siamese nets?
Hello Lakwin, this can be done just the same as you would do, when building the model from scratch, by using model.predict(). that question was answered here: stackoverflow.com/questions/37891954/…
or just model(X) as .predict can be slow
E
ElvisM89

Loading model from scratch requires you to build model from scratch, so you can try saving your model architecture first using model.to_json()

model_architecture = model.to_json()

Save model weighs using

model.save_weights('model_weights.h5')
       

For loading the weights you need to reconstruct your model using the saved json file first.

from tensorflow.keras.models import model_from_json
model = model_from_json(model_architecture) 

Then load the weights using

model.load_weights('model_weights.h5') 

You can now Compile and test the model , No need to retrain eg

model.compile(loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
          optimizer=keras.optimizers.Adam(lr=0.001), metrics=["accuracy"])

model.evaluate(x_test, y_test, batch_size=32, verbose=2)