0
$\begingroup$

I have downloaded .h5 files from qubvel/resnet and qubvel/efficientnet. I was trying to use some models as a backbone for my model but I'm getting the following error:

ValueError: No model found in the config file.

As explained here this is because the .h5 file contains only weights, not a model.

So those .h5 files are only weights. What's the purpose of having only weights without architecture?

I was trying to do following code:

resnet18_path_to_file = "models/resnet18.h5"
resnet18 = tf.keras.models.load_model(resnet18_path_to_file)
resnet18.compile()

inputs = resnet18.input
outputs = resnet18.layers[-2].output

return tf.keras.models.Model(inputs=inputs, outputs=outputs, name="custom_resnet18")
$\endgroup$
1
$\begingroup$

There are several options when saving and loading a keras model, as explained at https://www.tensorflow.org/guide/keras/save_and_serialize:

  • save the whole configuration, including the architecture, weights and even the last training state
  • but also the model architecture and the weights can be saved as independent files, and that is what you might have loaded: an .h5 with only the weights, and you also need the .json with the model architecture, based on the snippet below:

-model architecture and model weights to disk as separate files, which is what it might be done by the user who trained the model:

json_config = model.to_json()
with open('model_config.json', 'w') as json_file:
    json_file.write(json_config)
# weights saving to disk
model.save_weights('path_to_my_weights.h5')

So you need to do the following:

-load the model as follows (you need the model json also):

with open('model_config.json') as json_file:
    json_config = json_file.read()
new_model = keras.models.model_from_json(json_config)
new_model.load_weights('path_to_my_weights.h5')
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.