keras load_model在加载包含自定义层时会出现unknown layer的错误
解决方法:
1.在自定义层的定义文件YourLayer.py中添加如下代码:
def create_custom_objects():
instance_holder = {"instance": None}
class ClassWrapper(YourLayerName):
def __init__(self, *args, **kwargs):
instance_holder["instance"] = self
super(ClassWrapper, self).__init__(*args, **kwargs)
return {"ClassWrapper": ClassWrapper ,"YourLayerName": ClassWrapper}
2.然后加载模型
from YourLayer import create_custom_objects
model = keras.models.load_model(model_path, custom_objects=create_custom_objects())
参阅链接
python - Not able to load keras trained model - Stack Overflow
网友评论