Keras: Unable to load vgg16 weights

Created on 8 Jan 2018  路  5Comments  路  Source: keras-team/keras

I am loading the vgg16 weights for imageNet using
lossModel=vgg16.VGG16(include_top=False,weights='imagenet',input_tensor=mainModel.output,input_shape=(None,None,3))

where
mainModel.output is
"Tensor("conv2d_55/Sigmoid:0", shape=(?, ?, ?, 3), dtype=float32)"

I am getting the following error
ValueError: You are trying to load a weight file containing 13 layers into a model with 18 layers.

Most helpful comment

Hi! Thank you for reporting that! I found this issue. Please next time, give the complete traceback, it saves some time :) I'll do a pull request, but it'll take some time to get into keras.

What you can do in the meantime is to apply the fix yourself to your keras file. Go to line 176 of keras/applications/vgg16.py in your keras installation directory,
and replace model.load_weights(weights_path) by model.load_weights(weights_path, by_name=True).

All 5 comments

Hi! Thank you for reporting that! I found this issue. Please next time, give the complete traceback, it saves some time :) I'll do a pull request, but it'll take some time to get into keras.

What you can do in the meantime is to apply the fix yourself to your keras file. Go to line 176 of keras/applications/vgg16.py in your keras installation directory,
and replace model.load_weights(weights_path) by model.load_weights(weights_path, by_name=True).

Hi!
So I get back what I said, don't modify the file.
Fran莽ois Chollet proposed to fix the issue manually, rather than modifying the codebase.
You can see the right way (less error prone) to do this in the thread of the PR. (link just above)

In the link that you mentioned,
model = VGG16(include_top=False, weights='imagenet')
throws the same issue

@sandeepnmenon , "You are trying to load a weight file containing 13 layers into a model with 18 layers. This is because the last 3 layers are the dense layers of 4096 , 4096 and 1000 neurons respectively.
So the weight file which you are trying to load are for till the last convolutional layer block which is till 5th block and 3rd layer. So better copy the architecture of VGG16 except the 3 FC layers and then load the weights , you won't find any difficulty..... ! Keep going !

@sandeepnmenon , "You are trying to load a weight file containing 13 layers into a model with 18 layers. This is because the last 3 layers are the dense layers of 4096 , 4096 and 1000 neurons respectively.
So the weight file which you are trying to load are for till the last convolutional layer block which is till 5th block and 3rd layer. So better copy the architecture of VGG16 except the 3 FC layers and then load the weights , you won't find any difficulty..... ! Keep going !

I am trying to do something similar, as in, trying to load a newly-trained VGG16 model to be used for training. I have the .h5 file, which has 3 layers, however, I can't for the life of me figure out how to copy the architecture of the original VGG16 (minus the last 3 layers), and append the new model onto that. Are there any pointers you could give as to how I could do this?

Was this page helpful?
0 / 5 - 0 ratings