赞
踩
wget https://github.com/ultralytics/yolov5/archive/refs/heads/master.zip
再进行解压,
unzip mater.zip
删除master压缩文件
rm mater.zip
进入到文件夹里面去
code yolov5-master/
激活环境,这里需要python版本>3.6的
conda activate torch1.8
执行export.py文件,导出ONNX,
python export.py
我们只需要输出ONNX,所以只需指定ONNX即可,
python export.py --include=onnx
int代替shape、size的返回值,这里使用了map函数,避免ONNX导出生成gather、shape等节点,
def forward(self, x):
z = [] # inference output
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv
bs, _, ny, nx = map(int, x[i].shape) # 原代码:x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
请看代码的最后两句,batch维度指定为-1,
def forward(self, x): z = [] # inference output for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = map(int, x[i].shape) # 原代码: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.onnx_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) y = x[i].sigmoid() if self.inplace: y[..., 0:2] = (y[..., 0:2] * 2 + self.grid[i]) * self.stride[i] # xy y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh else: # for YOLOv5 on AWS Inferentia https://github.com/ultralytics/yolov5/pull/2953 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 * self.anchor_grid[i] # wh y = torch.cat((xy, wh, conf), 4) # z.append(y.view(bs, -1, self.no)) z.append(y.view(-1, int(y.size(1) * y.size(2) * y.size(3), self.no)))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。