Skip to content
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

Open
nikhil8786 opened this issue Mar 26, 2018 · 9 comments

Comments

@nikhil8786
Copy link

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

@asharsid
Copy link

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
import h5py
filename = 'vgg16.h5'
f = h5py.File(filename, 'r')
list(f.attrs)
output -> ['keras_version', 'backend', 'model_config']
As you see, it do not have the attribute nb_layers.

When I use a weight file downloaded from net:
File : vgg16_weights_tf_dim_ordering_tf_kernels.h5

import h5py
filename = 'vgg16_weights_tf_dim_ordering_tf_kernels.h5'
f = h5py.File(filename, 'r')
list(f.attrs)
Output -> ['layer_names']

So as you see, I cant see any attribute by name "nb_layers" in either files.
Author has used weights_path='../vgg16_weights.h5 but its not mentioned where he got this vgg16_weights.h5 file from Thats where the confusion is.

I am trying to do some workaround but will appreciate if anyone has a solution and can share.

Thanks!

@yuyifan1991
Copy link

@asharsid Have you solved the problem? I have the same situation! Could you tell me how to teal?

@kibeomKim
Copy link

in my case, my vgg16_weights.h5 file has just 95 bytes.

so I downloaded new one from here.
https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3

@asharsid
Copy link

@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 :
model.get_layer(layername).output function

Hope that helps. Thanks!

@emadeldeen24
Copy link

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"])

@vriyal
Copy link

vriyal commented Aug 14, 2019

@Emadeldeen-24 - the work around above is not working. Throws me below error:
KeyError: "Can't open attribute (can't locate attribute: 'layer_names')".

Has anyone resolved this error by any other method/approach? If yes, please share insights. Thanks in advance.

@asharsid
Copy link

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.
If you want to use the VGG16 model just for feature extraction, use the 13 convolution layers and maybe one dense layer. Pop out the last two layer or just the topmost layer. Once you extract the features, you can use any classifier(SVM or Logistic) to make predictions.
There are multiple ways of removing the top layers from VGG16. Search google.
Hope that helps.

@nageshkatna
Copy link

Does anyone found a proper way to do it?

@nageshkatna
Copy link

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.

def finetune_binary_model():
    nb_epoch = 25
    img_width, img_height = 256, 256
#     model = load_vgg16()
    # build the VGG16 network
    base_model = 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=base_model.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=base_model.input, outputs=top_model(base_model.output))

    
    # set the first 25 layers (up to the last conv block)
    # to non-trainable - weights will not be updated
    for layer in model.layers[:25]:
        layer.trainable=False

    # compile the model with a SGD/momentum optimizer 
    # and a very slow learning rate
    model.compile(loss='binary_crossentropy',
                 optimizer = optimizers.SGD(lr=0.00001, momentum=0.9), # reduced learning rate by 1/10
                  metrics=['accuracy'])
    
    # prepare data augmentation configuration
    train_datagen = ImageDataGenerator(rescale=1./255,
                                       rotation_range=40,
                                       width_shift_range=0.2,
                                       height_shift_range=0.2,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True,
                                       fill_mode='nearest')

    test_datagen = ImageDataGenerator(rescale=1./255)

    train_generator= train_datagen.flow_from_directory(train_data_dir,
                                                     target_size=(img_height, img_width),
                                                     batch_size=8,
                                                     class_mode='binary')

    validation_generator = test_datagen.flow_from_directory(validation_data_dir,
                                                           target_size=(img_height, img_width),
                                                           batch_size=8,
                                                           class_mode='binary')
    
    
    checkpoint = ModelCheckpoint(fine_tuned_model_path, monitor='val_acc', 
                                 verbose=1, save_best_only=True, 
                                 save_weights_only=False, mode='auto')
    # fine-tune the model
    fit = model.fit_generator(train_generator,
                              samples_per_epoch=nb_training_samples,
                              nb_epoch=nb_epoch,
                              validation_data=validation_generator,
                              nb_val_samples=nb_validation_samples,
                              verbose=1,
                              callbacks=[checkpoint])
    
    with open(location+'/ft_history.txt', 'w') as f:
        json.dump(fit.history, f)
    
    return model, fit.history

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants