WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.35k stars 4.21k forks source link

YOLOv7 train for object detection. Display:" 'list' object has no attribute 'shape'" #1973

Open Zhiying-Li-dot opened 9 months ago

Zhiying-Li-dot commented 9 months ago

`class IDetect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False concat = False

def __init__(self, nc=80, anchors=(), ch=()):  # detection layer
    super(IDetect, self).__init__()
    self.nc = nc  # number of classes
    self.no = nc + 5  # number of outputs per anchor
    self.nl = len(anchors)  # number of detection layers
    self.na = len(anchors[0]) // 2  # number of anchors
    self.grid = [torch.zeros(1)] * self.nl  # init grid
    a = torch.tensor(anchors).float().view(self.nl, -1, 2)
    self.register_buffer('anchors', a)  # shape(nl,na,2)
    self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2))  # shape(nl,1,na,1,1,2)
    self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch)  # output conv

    self.ia = nn.ModuleList(ImplicitA(x) for x in ch)
    self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch)

def forward(self, x):
    # x = x.copy()  # for profiling
    z = []  # inference output
    self.training |= self.export
    for i in range(self.nl):
        x[i] = self.m[i](self.ia[i](x[i]))  # conv
        x[i] = self.im[i](x[i])
        bs, _, ny, nx = x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)
        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

        if not self.training:  # inference
            if self.grid[i].shape[2:4] != x[i].shape[2:4]:
                self.grid[i] = self._make_grid(nx, ny).to(x[i].device)

            y = x[i].sigmoid()
            y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i]  # xy
            y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]  # wh
            z.append(y.view(bs, -1, self.no))

    return x if self.training else (torch.cat(z, 1), x)`

When I was run this code, there would dispaly the error. But I tried many method, It' s not OK. Please Hellp Me!

Traceback (most recent call last): File "train.py", line 674, in train(hyp, opt, device, tb_writer) File "train.py", line 89, in train model = Model(opt.cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create File "/data/zyli/YOLOv7/yolo-contrast/models/yolo.py", line 553, in init m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward File "/data/zyli/YOLOv7/yolo-contrast/models/yolo.py", line 553, in m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward AttributeError: 'list' object has no attribute 'shape'

z-w-wang commented 1 month ago

`class IDetect(nn.Module): stride = None # strides computed during build export = False # onnx export end2end = False include_nms = False concat = False

def __init__(self, nc=80, anchors=(), ch=()):  # detection layer
    super(IDetect, self).__init__()
    self.nc = nc  # number of classes
    self.no = nc + 5  # number of outputs per anchor
    self.nl = len(anchors)  # number of detection layers
    self.na = len(anchors[0]) // 2  # number of anchors
    self.grid = [torch.zeros(1)] * self.nl  # init grid
    a = torch.tensor(anchors).float().view(self.nl, -1, 2)
    self.register_buffer('anchors', a)  # shape(nl,na,2)
    self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2))  # shape(nl,1,na,1,1,2)
    self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch)  # output conv

    self.ia = nn.ModuleList(ImplicitA(x) for x in ch)
    self.im = nn.ModuleList(ImplicitM(self.no * self.na) for _ in ch)

def forward(self, x):
    # x = x.copy()  # for profiling
    z = []  # inference output
    self.training |= self.export
    for i in range(self.nl):
        x[i] = self.m[i](self.ia[i](x[i]))  # conv
        x[i] = self.im[i](x[i])
        bs, _, ny, nx = x[i].shape  # x(bs,255,20,20) to x(bs,3,20,20,85)
        x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()

        if not self.training:  # inference
            if self.grid[i].shape[2:4] != x[i].shape[2:4]:
                self.grid[i] = self._make_grid(nx, ny).to(x[i].device)

            y = x[i].sigmoid()
            y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i]  # xy
            y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i]  # wh
            z.append(y.view(bs, -1, self.no))

    return x if self.training else (torch.cat(z, 1), x)`

When I was run this code, there would dispaly the error. But I tried many method, It' s not OK. Please Hellp Me!

Traceback (most recent call last): File "train.py", line 674, in train(hyp, opt, device, tb_writer) File "train.py", line 89, in train model = Model(opt.cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create File "/data/zyli/YOLOv7/yolo-contrast/models/yolo.py", line 553, in init m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward File "/data/zyli/YOLOv7/yolo-contrast/models/yolo.py", line 553, in m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward AttributeError: 'list' object has no attribute 'shape'

I have the same problem. Have you solved this?