0

I am doing vgg16 finetuning using Keras 2. Before actually refine the weights of the top layers, I need to load the VGG16 model weights to avoid long-time training.

I downloaded the VGG16 weights from this link. The weights I tried included 'vgg16_weights_tf_dim_ordering_tf_kernels.h5' and 'vgg16_weights_th_dim_ordering_th_kernels.h5' but all failed with the following error:

   File "h5py/h5a.pyx", line 77, in h5py.h5a.open (/tmp/pip-nCYoKW-build/h5py/h5a.c:2337)
KeyError: "Can't open attribute (Can't locate attribute: 'nb_layers')"

The code used to load the weights file is listed below:

# loading the weights of the pre-trained VGG16:
assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
f = h5py.File(weights_path, 'r')
for k in range(f.attrs['nb_layers']):
    if k >= len(model.layers):
        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()

Actually I found a similar question in StackOverflow but it didn't solve my problem. So, do you encounter the same problem or can you help me with this?

  • Why don't you use the version available from application. https://keras.io/applications/#vgg16 – J.Down May 27 '17 at 08:49
  • Thx. I can now work with it using that version. But here comes another question. The top layers are created by myself and I wanna load weights from file other than created them with initial random weights. [Here](https://gist.github.com/fchollet/7eb39b44eb9e16e59632d25fb3119975) he used Sequential() to create top layers using .load_weights() method to load weight file and then using .add() method to concatenate top layers and VGG-16's layers. But in Keras 2 it's impossible since VGG16 is of type 'Model' and it doesn't have .add() method. So, how can you load a weight file in this situation? – zhangjun-xyz May 28 '17 at 08:00
  • Again it is possible with the application. just set the `weight` parameter to `imagenet` and it the model will be preloaded with those weights. – J.Down May 28 '17 at 08:06
  • What I meant was to load top layers' weights. VGG16's weights are easy to load using the way you mentioned. But I need to fix VGG16's weights and learn top layers' weights. But I don't want to learn them from zero but from a pretrained weights file. – zhangjun-xyz May 29 '17 at 01:30
  • I finally found a solution. Model() class have a method .load_weights(). Then use Model() to connect two models: `input_tensor = Input(shape=(150,150,3)) base_model = applications.VGG16(weights='imagenet',include_top= False,input_tensor=input_tensor) 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')) top_model.load_weights(top_model_weights_path) model = Model(input= base_model.input, output= top_model(base_model.output))` – zhangjun-xyz May 29 '17 at 08:30
  • @J.Down Thank you very much! – zhangjun-xyz May 29 '17 at 08:33

0 Answers0