Ahmed Mostafa
Ahmed Mostafa

Reputation: 21

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

Upvotes: 2

Views: 16805

Answers (3)

sadegh khosravi
sadegh khosravi

Reputation: 31

A complete model has two parts: model architecture and weights. So if we just have weights, we must first load architecture(may be use python file or keras file ), and then load weights on the architecture. for example:

model = tf.keras.models.load_model("facenet_keras.h5")
model.load_weights("facenet_keras_weights.h5")

Upvotes: 3

Diego Rando
Diego Rando

Reputation: 93

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)

Upvotes: 1

Ankit Sharma
Ankit Sharma

Reputation: 75

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.

Upvotes: 2

Related Questions