keras: Unable to load model from .h5 file

# Unable to load model
Using TensorFlow backend.
Traceback (most recent call last):
  File "ocv.py", line 7, in <module>
    model = load_model('bottleneck_fc_model.h5')
  File "/usr/local/lib/python2.7/dist-packages/keras/models.py", line 230, in load_model
    raise ValueError('No model found in config file.')
ValueError: No model found in config file.

I did not find a clear answer in other issues.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 20 (1 by maintainers)

Most upvoted comments

Did you saved with model.save or model.save_weights? Also, try to provide a working example so that we can see if it’s a bug.

I am using a pre-trained model such as Alexnet, In this case also, ending up with the same error.

I was downloaded the alexnet_weights from here–>https://github.com/heuritech/convnets-keras then I tried like this

from keras.models import load_model base_model=load_model(‘alexnet_weights.h5’)

I ended up with

ValueError: No model found in config file.

please help me to get rid out of it.

Thank You,
I was saving 'weights' but trying to call a 'model'.

use ‘model.save()’ will work

It seems like your only using only the weights of the model. In that case you cant use load_model method. You have to set and define the architecture of your model and then use model.load_weights(‘alexnet_weights.h5’). Take a look at this for example https://stackoverflow.com/questions/35074549/how-to-load-a-model-from-an-hdf5-file-in-keras

I have this issue (ValueError: No model found in config file.) with TF 2.4.1, tf.keras.callbacks.Callback.ModelCheckpoint and a custom network.
The reason of the issue is that the model was saved with model.save_weights despite having passed save_weights_only = False. I inspected tensorflow code and save_weights_only is forced to True in ModelCheckpoint in some specific case (which happens for me) even if the user passed save_weights_only = False to construct the ModelCheckpoint object.
Here is the code which causes the issue in the class ModelCheckpoint (tensorflow\python\keras\callbacks.py) :

  def set_model(self, model):
    self.model = model
    # Use name matching rather than `isinstance` to avoid circular dependencies.
    if (not self.save_weights_only and
        not model._is_graph_network and  # pylint: disable=protected-access
        model.__class__.__name__ != 'Sequential'):
      self.save_weights_only = True

We should show a warning to inform the user that save_weights_only is forced to True (with the reasons). It will save people hours of debugging as I did.
This is related to https://github.com/tensorflow/tensorflow/issues/37620 and https://github.com/tensorflow/tensorflow/issues/42741

Can anyone guide me regarding above issue while loading the .h5 model.

I have used google colab to save this model. On google colab, I am able to load this model. But, it I download that model and trying to load that, it gives above erroe.

I am also facing the same issue

.local/lib/python3.8/site-packages/keras/saving/hdf5_format.py", line 177, in load_model_from_hdf5
    raise ValueError(f'No model config found in the file at {filepath}.')

Any hopes?