【问题标题】:TypeError: __init__() missing 2 required positional argumentsTypeError: __init__() 缺少 2 个必需的位置参数
【发布时间】:2020-06-13 15:45:37
【问题描述】:

我有 .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 的版本没有帮助。

【问题讨论】:

  • 看来你使用的自定义层没有正确实现,很可能缺少get_config()方法。

标签: python tensorflow keras


【解决方案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需要的默认参数。

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2019-06-25
    • 2021-02-08
    • 1970-01-01
    • 2019-02-08
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    相关资源
    最近更新 更多