【问题标题】:AttributeError while loading a Keras model containing Lambda layers加载包含 Lambda 层的 Keras 模型时出现 AttributeError
【发布时间】:2019-08-06 06:25:04
【问题描述】:

我正在尝试加载经过训练的 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 层用作自定义层吗?

我怎样才能找到解决这个问题的方法?

P.S.:我在搜索后找到了这个(https://github.com/keras-team/keras/issues/4871),如何在这里使用自定义对象参数?

【问题讨论】:

    标签: python-3.x tensorflow keras deep-learning


    【解决方案1】:

    实际上 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)
    

    【讨论】:

    • 你能告诉我我应该通过什么函数来解决这个问题,我无法识别它!
    • 我实际上是在尝试用已经训练好的模型制作一个烧瓶 api,代码可以在这里找到pastebin.com/N4exqsfv,而模型的代码可以在这里找到(在 SeResnext 标题中)@ 987654322@
    • 可以使用笔记本中定义的SEResNeXt,使用SEResNeXt().model_senet.load_weights(path)
    猜你喜欢
    • 2021-01-05
    • 1970-01-01
    • 2019-03-26
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 2021-01-27
    • 2017-07-27
    相关资源
    最近更新 更多