我正在使用Keras 2进行vgg16微调。在实际细化顶层的权重之前,我需要加载VGG16模型权重以避免长时间训练。
我从this link下载了VGG16重量。我试图权重包括“vgg16_weights_tf_dim_ordering_tf_kernels.h5”和“vgg16_weights_th_dim_ordering_th_kernels.h5”但都失败了,错误如下:
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')"
下面列出了用于加载权重文件的代码:
# 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()
实际上我在StackOverflow中找到了similar question,但它没有解决我的问题。那么,你遇到同样的问题还是可以帮我解决这个问题?