用VGG预训练权重出错 KeyError: "Can't open attribute (can't locate attribute: 'layer_names')"

  File "vgg16.py", line 107, in 
    model = VGG16(include_top=True, classes=10, weights='../vgg16_weights.h5')
  File "/usr/local/lib/python3.5/dist-packages/keras/applications/vgg16.py", line 197, in VGG16
    model.load_weights(weights)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 2645, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 3127, in load_weights_from_hdf5_group
    layer_names = [n.decode('utf8') for n in f.attrs['layer_names']]
  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 "/usr/local/lib/python3.5/dist-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: 'layer_names')"

随着keras版本更新,预训练的h5文件会有变化,可以直接到keras.application里下载就好了,在其他地方下载的模型大小一样,但是可能是之前keras版本的,和你用的版本不同,就会出现上面的错误。而且预训练的代码都是比较久远了,基本上会报错。

你可能感兴趣的:(用VGG预训练权重出错 KeyError: "Can't open attribute (can't locate attribute: 'layer_names')")