keras_deeplearning_example
keras_deeplearning_example copied to clipboard
KeyError: "Can't open attribute (Can't locate attribute: 'nb_layers')"在预初始化某些层权重
那个地方?你发个详细点的截图看看?
我不知道怎么发截图到issue上. 我finetuing VGG16,图片尺寸64*64,想要用keras model 部分初始化VGG16的前13层,就出错. 环境:ubuntu16.04+keras2.0+tensorflow1.3+python2.7 model name--vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5 ` **def loadWeightsPartial( model , weights_path , n_layers ):
f = h5py.File(weights_path)
# for k in range(f.attrs['nb_layers']):
# for k in range(f.attrs['layer_names']):
for k in range(f.keys()):
if k >= n_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()**
attrs:<Attributes of HDF5 object at 140348330607152> _MutableMapping__marker:<object object at 0x7fa5f69f8030> _abc_cache:<_weakrefset.WeakSet object at 0x7fa5ab53fd90> _abc_negative_cache:<_weakrefset.WeakSet object at 0x7fa5ab53fdd0> _abc_negative_cache_version:39 _abc_registry:<_weakrefset.WeakSet object at 0x7fa5ab53fd50> _id:<GroupID, len() = 18> _lapl:<h5py.h5p.PropLAID object at 0x7fa5ac82cb40> _lcpl:<h5py.h5p.PropLCID object at 0x7fa5ac822d40> **# [0]:u'layer_names'**
我发现f.attrs里不是nb_layer,而是layer_names
可能是不是你的版本的问题,我用的是2.0.8的版本,2.08和2有些调整,你可以留意下是不是这个问题,还有一个需要注意的是,keras的backend(如果用tensorflow,或theano的话,是不一样的,tensorflow 是channel last, 而theano是相反的,我之前好像是用的theano)。 我过几天有时间后会上传一个新的这个代码上去,到时应该不会有这种问题了!
@rockywind 我有同样的问题,你是怎样解决的?可一告诉我吗?