81

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.

Ryan
  • 8,459
  • 14
  • 40
  • 66

5 Answers5

133

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)
blackHoleDetector
  • 2,975
  • 2
  • 13
  • 13
  • 3
    If I save the weights on python 3.6 is it possible to load them on python 2.7? – Rtucan Dec 05 '18 at 19:44
  • 2
    @Rtucan I think Yes. You can try it. – Michael Heidelberg Feb 20 '19 at 15:46
  • 2
    Is it possible to load weights from the saved model from model.save() , not model.save_weights? If so how to do it? – TeenyTinySparkles Jul 31 '19 at 18:22
  • I have the same curiosity (and really a necessity from my side) as @TeenyTinySparkles . I'm having issues loading the full model and I need to at least try to recover the weights (since I can easily recreate the model). – Leonardo TM Oct 23 '22 at 21:07
  • For those who may need it, I found it! It can be done just call a `model.load_weights("model_saved_path")` If you need or want to get to the source of this answer you can check the documentation for the load_weights on [Keras Docs - Load_Weights](https://keras.io/api/models/model_saving_apis/#loadweights-method) – Leonardo TM Oct 23 '22 at 21:18
22

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')
Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
  • by model if you mean all the layers,the i have all that i just havent posted it – Ryan Nov 13 '17 at 14:29
  • 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 Feb 21 '19 at 13:28
  • @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. – Daniel Möller Feb 21 '19 at 14:04
  • @DanielMöller Yea, I am using ```load_model```. The ```Keras``` version I use is ```2.2.4```. – KK2491 Feb 28 '19 at 11:33
  • @Jubick Actually there is a simpler method. You can directly save the model and load it. (.model extension) – KK2491 Nov 04 '19 at 16:55
  • @KK2491 actually there's a lack of documentation. I faced this problem as i trained model in google collab and then saved it localy with ```save_model()``` but when i tried to use ```load_model(path)``` on my pc it showed this error. Keras' documentation doesn't say that you have to build a model it says just use ```load_model(path)``` but in fact you have to define it first (make all layers). I solved this but what's the purpose of ```load_model``` if you have to manually recreate whole architecture then. – Jubick Nov 04 '19 at 20:02
  • @Jubick, this is most certainly a version specific bug. `load_model` definitely loads the entire model, compiles it with the previous optimizer states and gives it to you ready to use. – Daniel Möller Nov 05 '19 at 01:37
  • I use `model.save(path)` and `model = keras.models.load_model(path)`. It's really this simple. – Daniel Möller Nov 05 '19 at 01:43
  • @Jubick There are 2 ways. 1. Save the model weights (.h5). In this case, before loading the weights you need to rebuild the entire network architecture as the saved file doesnt have any information about the network. 2. Save the model along with weights (.model). In this, saved file contains the model architecture + model weights. You dont have to rebuild the network architecture. – KK2491 Nov 05 '19 at 04:03
  • @DanielMöller Nice hat – OverLordGoldDragon Dec 14 '19 at 00:07
7

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 :

enter image description here

Sam Vanhoutte
  • 3,247
  • 27
  • 48
  • After loading the saved model (weights) how can i predict unseen data? can someone provide any sample code for predicting with siamese nets? – Lakwin Chandula Jul 17 '20 at 03:59
  • 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: https://stackoverflow.com/questions/37891954/keras-how-do-i-predict-after-i-trained-a-model – Sam Vanhoutte Jul 29 '20 at 10:44
  • or just model(X) as .predict can be slow – Peter Cotton Oct 22 '21 at 03:19
4

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)
ElvisM89
  • 47
  • 2
0
checkpoint_path = save_train_data + "/" + "model.{epoch:02d}-" + ".h5"
save_weight = MultiGPUCheckpointCallback(filepath=checkpoint_path,
                                         base_model=model,
                                         save_weights_only=True)

   history = model.fit(x=train_dataset, validation_data=valid_dataset,
                        steps_per_epoch=int(np.ceil(training_step_nums. / BATCH_SIZE)),
                        validation_steps=int(np.ceil(validation_nums / BATCH_SIZE)),
                        epochs=EPOCHES, verbose="auto", callbacks=[save_weight])

Saving weight callback function is shown in follow:

import warnings
import numpy as np
from keras.callbacks import Callback


class MultiGPUCheckpointCallback(Callback):
    def __init__(self, filepath, base_model, monitor='val_loss', verbose=0,
                 save_best_only=False, save_weights_only=False,
                 mode='auto', period=1):
        super(MultiGPUCheckpointCallback, self).__init__()
        self.base_model = base_model
        self.monitor = monitor
        self.verbose = verbose
        self.filepath = filepath
        self.save_best_only = save_best_only
        self.save_weights_only = save_weights_only
        self.period = period
        self.epochs_since_last_save = 0

        if mode not in ['auto', 'min', 'max']:
            warnings.warn('ModelCheckpoint mode %s is unknown, '
                          'fallback to auto mode.' % (mode),
                          RuntimeWarning)
            mode = 'auto'

        if mode == 'min':
            self.monitor_op = np.less
            self.best = np.Inf
        elif mode == 'max':
            self.monitor_op = np.greater
            self.best = -np.Inf
        else:
            if 'acc' in self.monitor or self.monitor.startswith('fmeasure'):
                self.monitor_op = np.greater
                self.best = -np.Inf
            else:
                self.monitor_op = np.less
                self.best = np.Inf

    def on_epoch_end(self, epoch, logs=None):
        logs = logs or {}
        self.epochs_since_last_save += 1
        if self.epochs_since_last_save >= self.period:
            self.epochs_since_last_save = 0
            filepath = self.filepath.format(epoch=epoch + 1, **logs)
            if self.save_best_only:
                current = logs.get(self.monitor)
                if current is None:
                    warnings.warn('Can save best model only with %s available, '
                                  'skipping.' % (self.monitor), RuntimeWarning)
                else:
                    if self.monitor_op(current, self.best):
                        if self.verbose > 0:
                            print('Epoch %05d: %s improved from %0.5f to %0.5f,'
                                  ' saving model to %s'
                                  % (epoch + 1, self.monitor, self.best,
                                     current, filepath))
                        self.best = current
                        if self.save_weights_only:
                            self.base_model.save_weights(filepath, overwrite=True)
                        else:
                            self.base_model.save(filepath, overwrite=True)
                    else:
                        if self.verbose > 0:
                            print('Epoch %05d: %s did not improve' %
                                  (epoch + 1, self.monitor))
            else:
                if self.verbose > 0:
                    print('Epoch %05d: saving model to %s' % (epoch + 1, filepath))
                if self.save_weights_only:
                    self.base_model.save_weights(filepath, overwrite=True)
                else:
                    self.base_model.save(filepath, overwrite=True)
samzhang
  • 11
  • 1
  • 5