load_model加载使用'leaky_relu'激活bug处理

关于tensorflow,load_model函数加载带有'leaky_relu'激活函数bug处理

今天在使用tensorflow2.5(其他版本这个问题有没有不知道)的load_model函数加载之前保存的模型时发生报错,报错详情如下:

问题展示

Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\save.py", line 202, in load_model
    compile)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 181, in load_model_from_hdf5
    custom_objects=custom_objects)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\saving\model_config.py", line 59, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\layers\serialization.py", line 163, in deserialize
    printable_module_name='layer')
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 672, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\sequential.py", line 498, in from_config
    custom_objects=custom_objects)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\layers\serialization.py", line 163, in deserialize
    printable_module_name='layer')
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 675, in deserialize_keras_object
    deserialized_obj = cls.from_config(cls_config)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\engine\base_layer.py", line 740, in from_config
    return cls(**config)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\layers\convolutional.py", line 669, in __init__
    activation=activations.get(activation),
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\activations.py", line 587, in get
    return deserialize(identifier)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\util\dispatch.py", line 206, in wrapper
    return target(*args, **kwargs)
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\activations.py", line 550, in deserialize
    printable_module_name='activation function')
  File "C:\Users\Administrator\AppData\Roaming\Python\Python36\site-packages\tensorflow\python\keras\utils\generic_utils.py", line 703, in deserialize_keras_object
    .format(printable_module_name, object_name))
ValueError: Unknown activation function: leaky_relu. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

其中最关键的信息就是ValueError: Unknown activation function: leaky_relu. Please ensure this object is passed to the 'custom_objects' argument,意思是: ValueError: Unknown activation function: leaky_relu。请确保将此对象传递给'custom objects'参数


这里开启调试模式重新运行看看


tensorflow.python.keras.utils.generic_utils.deserialize_keras_object这个函数中将字符串leaky_relu转为对应的tensorflow.python.ops.nn_ops.leaky_relu,首先它判断是否是自定义对象,然后判断是否是全局自定义对象,最后才在module_objects字典对象中找,我们看看这个dict有什么。

module_objects字典对象中没有leaky_relukey,所以找不到obj就会赋值为None,后面就报错了。

解决方法

从源代码中可以发现解决方法有多种:

  • 直接使用module_object字典有的做激活函数,如:relu。这样就是要重新训练模型。
  • leaky_relu当作自定义的函数,然后通过load_modelcustom_objects参数传入。
  • 使用LeakyRelu层,不使用层内激活函数,直接使用层。

对于上面第二种解决方法,可以直接看源码:

image.png

对于上面最后一种解决方法,可以直接看源码:


以上就是最后一种解决方法的由来。
如果模型训练费时很久,不想重新训练,就可以使用第二种解决方法。

你可能感兴趣的:(load_model加载使用'leaky_relu'激活bug处理)