【Python】TypeError: __init__() takes 1 positional argument but 2 were given

2023-05-16

问题描述

        以下代码使用关键词参数时出现错误:

TypeError: __init__() takes 1 positional argument but 2 were given

class DAFormerHead(BaseDecodeHead): 
    def __init__(self, **kwargs):
        super(DAFormerHead, self).__init__(**kwargs)

norm_cfg = dict(type='BN', requires_grad=True)

decode_head = dict(
    # type='DAFormerHead',
    in_channels=[64, 128, 320, 512],
    in_index=[0, 1, 2, 3],
    channels=256,
    dropout_ratio=0.1,
    num_classes=1,
    norm_cfg=norm_cfg,
    align_corners=False,
    decoder_params=dict(
        embed_dims=256,
        embed_cfg=dict(type='mlp', act_cfg=None, norm_cfg=None),
        embed_neck_cfg=dict(type='mlp', act_cfg=None, norm_cfg=None),
        fusion_cfg=dict(
            # _delete_=True,
            type='aspp',
            sep=True,
            dilations=(1, 6, 12, 18),
            pool=False,
            act_cfg=dict(type='ReLU'),
            norm_cfg=norm_cfg),
    ),
    loss_decode=dict(
        type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0)),

self.decode_head = DAFormerHead(**decode_head)

分析解决

        看代码感觉定义了dict参数后,使用**传参,感觉没问题但却报错了。跟代码发现定义的dict传参时实际是一个tuple,tuple里的元素是dict,因此修改以上代码传参部分。

self.decode_head = DAFormerHead(**decode_head[0])

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Python】TypeError: __init__() takes 1 positional argument but 2 were given 的相关文章

随机推荐