'Error while importing VGG16 h5 file ValueError: No model found in config file

I tried to import vgg16 which I downloaded from google storage

    import keras
    import cv2
    from keras.models import Sequential, load_model

But I got that error

ValueError: No model found in config file.

enter image description here



Solution 1:[1]

I was able to recreate the issue using your code and downloaded weights file mentioned by you. I am not sure about the reason for the issue but I can offer an alternative way for you to use pretrained vgg16 model from keras.

You need to use model from keras.applications file

Here is the link for your reference https://keras.io/api/applications/

There are three ways to instantiate this model by using weights argument which takes any of following three values None/'imagenet'/filepathToWeightsFile. Since you have already downloaded the weights , I suggest that you use the filepath option like the below code but for first time usage I will suggest to use imagenet (option 3). It will download the weight file which can be saved and reused later.

You need to add the following lines of code.

Option 1:

    from keras.applications.vgg16 import VGG16
    model = VGG16(weights = 'vgg16_weights_tf_dim_ordering_tf_kernels.h5')

Option 2:

    from keras.applications.vgg16 import VGG16
    model = VGG16(weights = None)
    model.load_weights('vgg16_weights_tf_dim_ordering_tf_kernels.h5')

Option 3: for using pretrained imagenet weights

   from keras.applications.vgg16 import VGG16
   model = VGG16(weights = 'imagenet')

The constructor also takes other arguments like include_top etc which can be added as per requirement.

Solution 2:[2]

Thank you all I solved it by reconstructing the network's layers and then load the weights

Solution 3:[3]

The problem here is that you're trying to load a model that is not a model and probably are just weights: so the problem is not in the load of the model but in the save.

When you are saving the model try:

  1. If you are using callbacks then "save_weights_only"=False
  2. Else use the function tf.keras.models.save_model(model,filepath)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ahmed Mostafa
Solution 3 Diego Rando