我有格式为 .h5 的Keras模型。我想用以下代码阅读它:
#model.h5 is name of mode, tf and modellib is read
custom_objects={'tf': tf,'BatchNorm':modellib.BatchNorm,'ProposalLayer':
modellib.ProposalLayer}
model=tf.keras.models.load_model("model.h5")
但是我得到一个错误
TypeError:init()缺少2个必需的位置参数:“ proposal_count”和“ nms_threshold”
我拥有TensorFlow(2.2)的最新版本。更改TensorFlow的版本无济于事。
答案 0 :(得分:1)
根据错误和自定义对象,我认为您正在尝试保存 matterport/MaskRcnn 的模型,我遇到了同样的问题。 问题是自定义层 ProposalLayer 未正确定义。
自定义层init的参数要默认初始化,否则可能报错:
TypeError: init() missing 2 required positional arguments: 'proposal_count' and 'nms_threshold'
我是这样解决的:
首先我以这种方式加载标准配置:
from mrcnn import config as config_std
然后我修改自定义层“ProposalLayer”的初始化
def __init__(self, proposal_count = config_std.Config.POST_NMS_ROIS_TRAINING,
nms_threshold = config_std.Config.RPN_NMS_THRESHOLD,
config=None, **kwargs):
我只是将在框架的构建函数中传递的标准配置直接作为 keras 需要的默认参数。