-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KeyError: "Can't open attribute (can't locate attribute: 'nb_layers')" #6
Comments
Looks like there is come problem with the attributes of the h5 file. If you print the attributes of the h5 file that we are using, it do not seem to have the attribute nb_layers: When i use the vgg16.h5 file created When I use a weight file downloaded from net: import h5py So as you see, I cant see any attribute by name "nb_layers" in either files. I am trying to do some workaround but will appreciate if anyone has a solution and can share. Thanks! |
@asharsid Have you solved the problem? I have the same situation! Could you tell me how to teal? |
in my case, my vgg16_weights.h5 file has just 95 bytes. so I downloaded new one from here. |
@yuyifan1991 No, i was not able to find out the solution taking the "nb_layers" route. i ended up using a different approach to pop out the last layer of vgg16 and then inserting my own classifier. To get a layer of a pretrained network as an input to your own model, use something like : Hope that helps. Thanks! |
Apparently "nb_layers" refers to the number of layers, so instead you can use a work around. f = h5py.File(filename, 'r') nb_layers = len(f.attrs["layer_names"]) |
@Emadeldeen-24 - the work around above is not working. Throws me below error: Has anyone resolved this error by any other method/approach? If yes, please share insights. Thanks in advance. |
nb_layers is not going to work. VGG!6 model has 16 layers in total, 13 convolution and 3 dense. The last dense layer in default trained VGG16 model has 1000 categories. |
Does anyone found a proper way to do it? |
This is how I made it work. Please let me know if you still have any problems. I am basically not loading the weights into the load_vgg16() because of Keras 2, I am building the vgg16 network in the finetune_binary _model(). Check the following code.
|
In this code from file 02-second_gate-damaged_or_whole :
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):
if k >= len(model.layers):
# we don't look at the last (fully-connected) layers in the savefile
break
g = f['layer_{}'.format(k)]
weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
model.layers[k].set_weights(weights)
f.close()
print('VGG16 Model with partial weights loaded.')
else:
print('VGG16 Model with no weights Loaded.')
This error is occurring
The text was updated successfully, but these errors were encountered: