打开微信,使用扫一扫进入页面后,点击右上角菜单,
点击“发送给朋友”或“分享到朋友圈”完成分享
【寒武纪硬件产品型号】必填*:MLU220
【使用操作系统】必填*:
例如:ubuntu
【使用驱动版本】必填*:
例如v4.20.6
【出错信息】必填*:移yolov5的时候出现警告提示
下面是我做的一些步骤(想将yolov5x6.pt移植到寒武纪MLU220上)
在高级的pytorch结构下先转成unzip格式(这个yolov5x6.pt是yolo官网下下载的pt文件)
因为 torch 1.3 版本没有 SiLU, Hardswish, 所以我在models文件中添加实现了
把common和yolo的代码改掉了,common代码有的没用的我就删掉了,yolo的代码是将寒武纪给的和yolo官网的合并了一下
下面的是yolo的代码
#基本上都是官网的代码就是在这个函数中添加了一些寒武纪的代码 class Detect(nn.Module): stride = None # strides computed during build export = False # onnx export dynamic = False # force grid reconstruction def __init__(self, nc=80, anchors=(), ch=()): # detection super(Detect, self).__init__() self.anchors_list = list(np.array(anchors).flatten()) self.num_anchors = len(self.anchors_list) self.nc = nc # number of classes self.no = nc + 5 # number of outputs per anchor self.nl = len(anchors) # number of detection s 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.register_buffer('anchors_cp', a.clone()) self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv #self.tmp_shape=[[1,255,80,64],[1,255,40,32],[1,255,20,16]] self.img_h = 640 self.img_w = 640 self.conf_thres = 0.4 self.iou_thres = 0.5 self.maxBoxNum = 1024 def forward(self, x): # x = x.copy() # for profiling output = [] # inference output self.training |= self.export if x[0].device.type == 'mlu': for i in range(self.nl): x[i] = self.m[i](x[i]) # conv y = x[i].sigmoid() print('y.shape: ',y.shape) output.append(y) detect_out = torch.ops.torch_mlu.yolov5_detection_output(output[0], output[1], output[2], self.anchors_list,self.nc, self.num_anchors, self.img_h, self.img_w, self.conf_thres, self.iou_thres, self.maxBoxNum) # [10, 13, 16, 30, 33, 23,30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326] return detect_out if x[0].device.type == 'cpu': 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() if not self.training: # inference if self.dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]: self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i) if isinstance(self, Segment): # (boxes + masks) xy, wh, conf, mask = x[i].split((2, 2, self.nc + 1, self.no - self.nc - 5), 4) xy = (xy.sigmoid() * 2 + self.grid[i]) * self.stride[i] # xy wh = (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i] # wh y = torch.cat((xy, wh, conf.sigmoid(), mask), 4) else: # Detect (boxes only) xy, wh, conf = x[i].sigmoid().split((2, 2, self.nc + 1), 4) xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh y = torch.cat((xy, wh, conf), 4) z.append(y.view(bs, self.na * nx * ny, self.no)) return x if self.training else (torch.cat(z, 1), ) if self.export else (torch.cat(z, 1), x) 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) # torch>=0.7 compatibility 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]).view((1, self.na, 1, 1, 2)).expand(shape) return grid, anchor_grid
_meshgrid函数因为寒武纪的那个容器版本低,没有这个函数,所以我就在function下写了个这个函数
def _meshgrid(*tensors): if has_torch_function(tensors): return handle_torch_function(meshgrid, tensors, *tensors) if len(tensors) == 1 and isinstance(tensors[0], (list, tuple)): # the old interface of passing the operands as one list argument tensors = tensors[0] # type: ignore return _VF.meshgrid(tensors) # type: ignore
然后测试了一下,能跑出结果也能画框,但是有一些警告
所以想问一下需要考虑这个警告问题吗?怎么解决这个警告?还是能跑就放着他不管?
热门帖子
精华帖子