尝试加载具有自定义对象的模型时出现“ValueError: Unknown activation: Activation”

Ben*_*nao 3 python keras tensorflow

我正在尝试加载具有两个自定义对象的模型,但在标题中出现此错误。

这是我导入/定义我的函数的地方,也是我允许 keras 按名称引用它们的地方。

from tensorflow.keras.utils import get_custom_objects
from tensorflow.python.keras.layers import LeakyReLU
from tensorflow.keras.layers import Activation
from tensorflow.keras.backend import sigmoid


def swish(x, beta=1):
    return x * sigmoid(beta * x)


get_custom_objects().update({'swish': Activation(swish)})
get_custom_objects().update({'lrelu': LeakyReLU()})
Run Code Online (Sandbox Code Playgroud)

我用这部分加载模型

from tensorflow.keras.models import load_model
model = load_model('model.h5', custom_objects={'swish': Activation(swish), 'lrelu': LeakyReLU()}, compile=False)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\Ben\PycharmProjects\untitled\trainer.py", line 102, in load_items
    model = load_model(data_loc + 'model.h5', custom_objects={'swish': Activation(swish), 'lrelu': LeakyReLU()}, compile=False)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\saving\save.py", line 146, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\saving\hdf5_format.py", line 168, in load_model_from_hdf5
    custom_objects=custom_objects)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\saving\model_config.py", line 55, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py", line 102, in deserialize
    printable_module_name='layer')
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 191, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\engine\sequential.py", line 369, in from_config
    custom_objects=custom_objects)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\layers\serialization.py", line 102, in deserialize
    printable_module_name='layer')
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 193, in deserialize_keras_object
    return cls.from_config(cls_config)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 594, in from_config
    return cls(**config)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\layers\core.py", line 361, in __init__
    self.activation = activations.get(activation)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\activations.py", line 321, in get
    identifier, printable_module_name='activation')
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 180, in deserialize_keras_object
    config, module_objects, custom_objects, printable_module_name)
  File "C:\Users\Ben\PycharmProjects\untitled\venv\lib\site-packages\tensorflow_core\python\keras\utils\generic_utils.py", line 165, in class_and_config_for_serialized_keras_object
    raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
ValueError: Unknown activation: Activation
Run Code Online (Sandbox Code Playgroud)

还可能值得注意的是,我正在尝试在不同环境的不同项目中保存和加载模型。两者都使用 tf 2.0.0 gpu。进口应该都是一样的。

Mat*_*gro 9

你不应该盲目相信互联网上的每一个教程。正如我在评论中所说,问题是将激活函数作为Layer(Activation准确地说)传递,这有效但不正确,因为您在模型保存/加载过程中遇到问题:

def swish(x, beta = 1):
    return (x * K.sigmoid(beta * x))

get_custom_objects().update({'swish': Activation(swish)})

model = Sequential()
model.add(Dense(10, input_shape=(1,), activation="swish"))
Run Code Online (Sandbox Code Playgroud)

上面的代码不是正确的方法,层内的激活不应该是另一层。使用此代码,我在model.save使用TensorFlow 1.14kerastf.keras使用 TensorFlow 1.14期间都会出错。正确的做法是:

def swish(x, beta = 1):
    return (x * K.sigmoid(beta * x))

get_custom_objects().update({'swish': swish})

model = Sequential()
model.add(Dense(10, input_shape=(1,), activation="swish"))
Run Code Online (Sandbox Code Playgroud)

然后您将能够正确加载和保存模型。如果您需要将激活添加为图层,您应该执行以下操作:

model.add(Activation("swish"))
Run Code Online (Sandbox Code Playgroud)

这也将允许模型保存/加载就好了。