TypeError: __init__() takes from 3 to 4 positional arguments but 5 were given

Traceback (most recent call last):
File "D:/ProgramData/workspace/tt/yolov5-5.0/models/yolo.py", line 265, in
model = Model(opt.cfg).to(device)
File "D:/ProgramData/workspace/tt/yolov5-5.0/models/yolo.py", line 85, in init
self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
File "D:/ProgramData/workspace/tt/yolov5-5.0/models/yolo.py", line 242, in parse_model
m_ = nn.Sequential(*[m(*args) for _ in range(n)]) if n > 1 else m(*args) # module
TypeError: init() takes from 3 to 4 positional arguments but 5 were given
说是需要传3或4个参数,我传了5个,应该在哪里改参数呀

emmm,你代码呢

类的init函数传递参数的数目不对
你发下代码看看

class SE(nn.Module):
def init(self, c1, c2, r=16):
super(SE, self).init()
self.avgpool = nn.AdaptiveAvgPool2d(1)
self.l1 = nn.Linear(c1, c1 // r, bias=False)
self.relu = nn.ReLU(inplace=True)
self.l2 = nn.Linear(c1 // r, c1, bias=False)
self.sig = nn.Sigmoid()

def forward(self, x):
    print(x.size())
    b, c, _, _ = x.size()
    y = self.avgpool(x).view(b, c)
    y = self.l1(y)
    y = self.relu(y)
    y = self.l2(y)
    y = self.sig(y)
    y = y.view(b, c, 1, 1)

上面这是在yolov5中添加的注意力机制代码。。
backbone:

[from, number, module, args]

[[-1, 1, Focus, [64, 3]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 9, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 1, SPP, [1024, [5, 9, 13]]],
[-1, 3, C3, [1024, False]], # 9
[-1, 1, SE, [1024, 4]],
]

YOLOv5 head

head:
[[-1, 1, Conv, [512, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 6], 1, Concat, [1]], # cat backbone P4
[-1, 3, C3, [512, False]], # 13

[-1, 1, Conv, [256, 1, 1]],
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
[[-1, 4], 1, Concat, [1]], # cat backbone P3
[-1, 3, C3, [256, False]], # 17 (P3/8-small)

[-1, 1, Conv, [256, 3, 2]],
[[-1, 15], 1, Concat, [1]], # cat head P4
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)

[-1, 1, Conv, [512, 3, 2]],
[[-1, 11], 1, Concat, [1]], # cat head P5
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)

[[18, 21, 24], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
]

发一下代码

看下这篇博客,也许你就懂了,链接:TypeError: __init__() takes from 1 to 2 positional arguments but 3 were given错误原因