☰
解决Ultralytics-YOLOv8-seg 由pt文件导出onnx后精度损失
2026/10/3 15:40:37 网站建设 项目流程

前言:使用Ultralytics 8.1.34中yolov8n-seg进行训练,并到处onnx模型后,onnx模型精度对比pt文件有明显损失,具体表现为检测到的分割区域会出现缺少情况。如下图所示:

pt文件推理正常分割图片

onnx模型推理分割图片

产生该问题原因(参考注释):

classDetect(nn.Module):"""YOLOv8 Detect head for detection models."""dynamic=False# force grid reconstructionexport=False# export modeshape=Noneanchors=torch.empty(0)# initstrides=torch.empty(0)# initdef__init__(self,nc=80,ch=()):"""Initializes the YOLOv8 detection layer with specified number of classes and channels."""super().__init__()self.nc=nc# number of classesself.nl=len(ch)# number of detection layersself.reg_max=16# DFL channels (ch[0] // 16 to scale 4/8/12/16/20 for n/s/m/l/x)self.no=nc+self.reg_max*4# number of outputs per anchorself.stride=torch.zeros(self.nl)# strides computed during buildc2,c3=max((16,ch[0]//4,self.reg_max*4)),max(ch[0],min(self.nc,100))# channelsself.cv2=nn.ModuleList(nn.Sequential(Conv(x,c2,3),Conv(c2,c2,3),nn.Conv2d(c2,4*self.reg_max,1))forxinch)self.cv3=nn.ModuleList(nn.Sequential(Conv(x,c3,3),Conv(c3,c3,3),nn.Conv2d(c3,self.nc,1))forxinch)self.dfl=DFL(self.reg_max)ifself.reg_max>1elsenn.Identity()......defdecode_bboxes(self,bboxes,anchors):"""Decode bounding boxes."""ifself.export:returndist2bbox(bboxes,anchors,xywh=False,dim=1)# 导出onnx时xywh入参为Falsereturndist2bbox(bboxes,anchors,xywh=True,dim=1)# pt训练推理时xywh入参为Truedefdist2bbox(distance,anchor_points,xywh=True,dim=-1):"""Transform distance(ltrb) to box(xywh or xyxy)."""assert(distance.shape[dim]==4)lt,rb=distance.split([2,2],dim)x1y1=anchor_points-lt x2y2=anchor_points+rbifxywh:# pt训练推理时的代码实现c_xy=(x1y1+x2y2)/2wh=x2y2-x1y1returntorch.cat((c_xy,wh),dim)# xywh bboxreturntorch.cat((x1y1,x2y2),dim)# 导出onnx时代码实现

xywh的入参不同导致模型结构出现不同,需要将入参部分全部置为True即可。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询