在模型中添加新图层后,加载预训练的权重

时间:2018-08-06 18:15:50

标签: keras hdf5 keras-layer

我想增加输出的维数,以便还预测回归估计的方差(除均值外)。问题在于,由于出现以下错误,我无法再重新加载原始体系结构的预训练模型(请参见下面的追溯):

 /lib/python3.5/site-packages/keras/engine/saving.py", line 673, in preprocess_weights_for_loading 
 elif layer_weights_shape != weights[0].shape:
 IndexError: list index out of range

我无法弄清楚为什么旧层的权重加载不起作用,因为它们没有更改名称,并且我用

加载了权重
 model.load_weights(weights, by_name=True, skip_mismatch=True)

其中“权重”对应于以前保存的快照(我相信是hdf5文件)。 下面定义了输出中的更改:

# ORIGINAL
#outputs = keras.layers.Conv2D(num_anchors * 4, name='pyramid_regression', **options)(outputs)   #y afaik: (None, None, None, num_anchors * 4)
#outputs = keras.layers.Reshape((-1, 4), name='pyramid_regression_reshape')(outputs)             #y afaik: (None, total_#anchors, 4)
# Including additional outputs:
outputs      = keras.layers.Conv2D(num_anchors * 4, name='pyramid_regression', **options)(outputs)
extra_output = keras.layers.Conv2D(num_anchors * 4, name = 'extra_output', **options)(outputs)
extra_output = keras.layers.Lambda(lambda x: keras.backend.exp(x))(extra_output)
outputs      = keras.layers.Reshape((-1, 4), name='pyramid_regression_reshape')(outputs)
extra_output = keras.layers.Reshape((-1, 4), name = 'extra_output_reshape')(extra_output)
outputs      = keras.layers.Concatenate(axis = 2, name='extra_output_concat')([outputs, extra_output])

return keras.models.Model(inputs=inputs, outputs=outputs, name=name)

这是错误的回溯:

Traceback (most recent call last):
  File "my_project/keras_retinanet/bin/train.py", line 473, in <module>
    main()
  File "my_project/keras_retinanet/bin/train.py", line 439, in main
    freeze_backbone=args.freeze_backbone
  File "my_project/keras_retinanet/bin/train.py", line 89, in create_models
    model = model_with_weights(backbone_retinanet(num_classes, modifier=modifier), weights=weights, skip_mismatch=True)
  File "my_project/keras_retinanet/bin/train.py", line 75, in model_with_weights
    model.load_weights(weights, by_name=True, skip_mismatch=skip_mismatch)
  File "/user/.local/lib/python3.5/site-packages/keras/engine/network.py", line 1158, in load_weights
    reshape=reshape)
  File "/user/.local/lib/python3.5/site-packages/keras/engine/saving.py", line 985, in load_weights_from_hdf5_group_by_name
    reshape=reshape)
  File "/user/.local/lib/python3.5/site-packages/keras/engine/saving.py", line 556, in preprocess_weights_for_loading
    weights = convert_nested_model(weights)
  File "/user/.local/lib/python3.5/site-packages/keras/engine/saving.py", line 532, in convert_nested_model
    original_backend=original_backend))
  File "/user/.local/lib/python3.5/site-packages/keras/engine/saving.py", line 673, in preprocess_weights_for_loading
    elif layer_weights_shape != weights[0].shape:
IndexError: list index out of range

请注意,对架构进行的其他小更改(例如添加退出层)也不会导致相同的问题。我感谢任何建议!由于该模型非常复杂,因此我并未包含所有代码。

编辑:我在TensorFlow 1.9.0中使用Keras 2.2.2。

0 个答案:

没有答案