ValueError: Unknown activation function:swish_activation

ValueError: Unknown activation function:swish_activation

我正在尝试使用 keras load_model() 加载保存权重。

from keras.models import load_model

model=load_model("weights.hdf5")

这是我遇到的错误。

ValueError                                Traceback (most recent call last)
<ipython-input-34-52d6983dfc34> in <module>()
      1 from keras.models import load_model
----> 2 model=load_model("weights.hdf5")

14 frames
/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    165             if fn is None:
    166                 raise ValueError('Unknown ' + printable_module_name +
--> 167                                  ':' + function_name)
    168         return fn
    169     else:

ValueError: Unknown activation function:swish_activation

Swish Keras默认不提供激活。相反,添加:

from keras.utils.generic_utils import get_custom_objects
from keras import backend as K
from keras.layers import Activation

def swish_activation(x):
        return (K.sigmoid(x) * x)

get_custom_objects().update({'swish_activation': Activation(swish_activation)})
# model use some custom objects, so before loading saved model
# import module your network was build with
# e.g. import efficientnet.keras / import efficientnet.tfkeras
import efficientnet.tfkeras
from tensorflow.keras.models import load_model
model = load_model('path/to/model.h5')