我正在尝试加载经过训练的 keras 模型(SeResnext),并且该模型架构也包含“Lambda”层。
现在,当我尝试在脚本中加载模型时,出现此属性错误:
Traceback (most recent call last):
File "predict.py", line 9, in <module>
model = keras.models.load_model('mySeResnextModel.hdf5')
File "/usr/local/lib/python3.6/site-packages/keras/engine/saving.py", line 419, in load_model
model = _deserialize_model(f, custom_objects, compile)
File "/usr/local/lib/python3.6/site-packages/keras/engine/saving.py", line 225, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/usr/local/lib/python3.6/site-packages/keras/engine/saving.py", line 458, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/usr/local/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.6/site-packages/keras/engine/network.py", line 1022, in from_config
process_layer(layer_data)
File "/usr/local/lib/python3.6/site-packages/keras/engine/network.py", line 1008, in process_layer
custom_objects=custom_objects)
File "/usr/local/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
printable_module_name='layer')
File "/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
list(custom_objects.items())))
File "/usr/local/lib/python3.6/site-packages/keras/layers/core.py", line 732, in from_config
printable_module_name='function in Lambda layer')
File "/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 162, in deserialize_keras_object
fn = module_objects.get(function_name)
AttributeError: 'NoneType' object has no attribute 'get'
我认为这是因为 Keras 没有任何名为 Lambda 的内置层,所以它根本无法识别这一层。
现在,当我搜索这个问题时,我得到的唯一但不太有益的解决方案是删除 lambda 层,但在我的情况下,它们太多了。我也可以导入然后使用 Lambda 层作为自定义层吗?
请帮我找到这个问题的解决方案,任何指针将不胜感激!
提前致谢。
PS:我在搜索后找到了这个(https://github.com/keras-team/keras/issues/4871),我如何在这里使用自定义对象参数?
实际上 Keras 有 Lambda 层 ( keras.layers.Lambda
) 但问题是由它使用的函数引起的。
要解决它,您可以通过custom_objects
参数传递所需的函数,例如:
def channel_zeropad(x, channel_axis=3):
'''
Zero-padding for channle dimensions.
Note that padded channles are added like (Batch, H, W, 2/x + x + 2/x).
'''
shape = list(x.shape)
y = K.zeros_like(x)
if channel_axis == 3:
y = y[:, :, :, :shape[channel_axis] // 2]
else:
y = y[:, :shape[channel_axis] // 2, :, :]
return concatenate([y, x, y], channel_axis)
def channel_zeropad_output(input_shape, channel_axis=3):
'''
Function for setting a channel dimension for zero padding.
'''
shape = list(input_shape)
shape[channel_axis] *= 2
return tuple(shape)
model = keras.models.load_model('mySeResnextModel.hdf5',
custom_objects={'channel_zeropad': channel_zeropad,
'channel_zeropad_output': channel_zeropad_output})
或分别定义模型及其负载权重:
model = SEResNeXt().model # if you are using senet-keras
model.load_weights('mySeResnextModel.hdf5', by_name=True)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句