KeyError:“无法打开属性(无法找到属性:'nb_params')”

时间:2019-11-11 18:23:10

标签: python keras transfer-learning

我为Keras的VGG-16预训练模型使用了相同的代码,但使用了自己的重量。这是我的代码:

f = h5py.File(weights_path)
    nb_layers = len(f.attrs["layer_names"])
    for k in range(nb_layers):
        if k >= len(model_MS.layers):
            # without 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)
        model.layers[k].trainable = False
    f.close()
    print

f具有以下属性:['后端','keras_version','layer_names']

这是我遇到的第一个错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File ".../python3.6/site-packages/h5py/_hl/group.py", line 264, in __getitem__
    oid = h5o.open(self.id, self._e(name), lapl=self._lapl)
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5o.pyx", line 190, in h5py.h5o.open
KeyError: "Unable to open object (object 'layer_0' doesn't exist)"

手动设置图层名称时出现第二个错误:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File ".../python3.6/site-packages/h5py/_hl/attrs.py", line 60, in __getitem__
    attr = h5a.open(self._id, self._e(name))
  File "h5py/_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py/_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py/h5a.pyx", line 77, in h5py.h5a.open
KeyError: "Can't open attribute (can't locate attribute: 'nb_params')"

g具有以下属性:['weight_names']

我的问题是,为什么我的权重对象包含['backend','keras_version','layer_names'],而VGG16中的对象却包含['nb_layers']

0 个答案:

没有答案