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?