公版链接:
https://github.com/WongKinYiu/yolov7
修改后处理(抄yolov5),使得decode部分能够顺利解析
+ def forward(self, x):
+ z = [] # inference output
+ for i in range(self.nl):
+ x[i] = self.m[i](x[i]) # conv
+ 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()
+
+ #return x
+ self.grid[i], anchor_grid = self._make_grid(nx, ny, i)
+
+ y = x[i].sigmoid()
+ xy, wh, conf = y.split((2, 2, self.nc + 1), 4) # y.tensor_split((2, 4, 5), 4) # torch 1.8.0
+ xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
+ wh = (wh * 2) ** 2 * anchor_grid # wh
+ y = torch.cat((xy, wh, conf), 4)
+ z.append(y.view(bs, -1, self.no))
+ return torch.cat(z, 1)
+
+ def _make_grid(self, nx=20, ny=20, i=0):
+ d = self.anchors[i].device
+ t = self.anchors[i].dtype
+ shape = 1, self.na, ny, nx, 2 # grid shape
+ y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t)
+ yv, xv = torch.meshgrid(y, x)
+ grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5
+ anchor_grid = (self.anchors[i] * self.stride[i])
+ anchor_grid = anchor_grid.view((1, self.na, 1, 1, 2)).expand(shape)
+
+ return grid, anchor_grid