Skip to content Skip to sidebar Skip to footer

Keyerror: "can't Open Attribute (can't Locate Attribute: 'nb_layers')"

I have a Python code that uses Keras. I didn't post the code because it is a bit long, and the issue seems not to be related to the code itself. This is the error I'm having: File

Solution 1:

Use the weights file vgg16_weights_th_dim_ordering_th_kernels.h5 from https://github.com/fchollet/deep-learning-models/releases

This file is in Keras 2 format.

Solution 2:

I had the same issue. I solved it by building the vgg16 network where I needed it by adding this line.

 Vmodel = applications.VGG16(weights='imagenet', include_top=False, input_shape=(3, img_width, img_height))
    print('Model loaded.')

    # build a classifier model to put on top of the convolutional model
    top_model = Sequential()
    top_model.add(Flatten(input_shape=Vmodel.output_shape[1:]))
    top_model.add(Dense(256, activation='relu'))
    top_model.add(Dropout(0.5))
    top_model.add(Dense(1, activation='sigmoid'))

    # note that it is necessary to start with a fully-trained# classifier, including the top classifier,# in order to successfully do fine-tuning
    top_model.load_weights(top_model_weights_path)

    # add the model on top of the convolutional base# model.add(top_model)
    model = Model(inputs=Vmodel.input, outputs=top_model(Vmodel.output))

So basically instead of creating a vgg16 Conv net of your own and loading the vgg16 weights into it. I created a vgg16 model and then added the last layers to the model. I hope this works for you.

Solution 3:

Apparently "nb_layers" refers to the number of layers, so instead you can use a work around. In this case:

f = h5py.File(filename, 'r')
nb_layers = len(f.attrs["layer_names"])

Post a Comment for "Keyerror: "can't Open Attribute (can't Locate Attribute: 'nb_layers')""