第一步下载模型
yolov8 https://github.com/ultralytics/ultralytics
第二步训练模型
yolo train data=自定义.yaml model=yolov8s.pt pretrained=True project=Train_640 name=n_pretrain epochs=1000 batch=4 device=0 patience=0 lr0=0.001 lrf=0.001第三步转换模型onnx
1、修改head.py
class Detect(nn.Module):
...
#新增forward
def forward(self, x):
bboxes = [self.cv2[i](x[i]).permute(0, 2, 3, 1).contiguous() for i in range(self.nl)]
clses = [self.cv3[i](x[i]).permute(0, 2, 3, 1).contiguous() for i in range(self.nl)]
return (bboxes, clses)
#重命名 forward -> forward_
def forward_(self, x):
"""Concatenates and returns predicted bounding boxes and class probabilities."""
if self.end2end:
return self.forward_end2end(x)
for i in range(self.nl):
x[i] = torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)
if self.training: # Training path
return x
y = self._inference(x)
return y if self.export else (y, x)
...
class Segment(Detect)
#新增forward
def forward(self, x):
p = self.proto(x[0]).permute(0, 2, 3, 1).contiguous() # mask protos
results = []
for i in range(self.nl):
box = self.cv2[i](x[i]).permute(0, 2, 3, 1).contiguous()
cls = self.cv3[i](x[i]).permute(0, 2, 3, 1).contiguous()
mask = self.cv4[i](x[i]).permute(0, 2, 3, 1).contiguous()
results.append(cls)
results.append(box)
results.append(mask)
results.append(p)
return tuple(results)
#重命名 forward -> forward_
def forward_(self, x):
"""Return model outputs and mask coefficients if training, otherwise return outputs and mask coefficients."""
p = self.proto(x[0]) # mask protos
bs = p.shape[0] # batch size
mc = torch.cat([self.cv4[i](x[i]).view(bs, self.nm, -1) for i in range(self.nl)], 2) # mask coefficients
x = Detect.forward(self, x)
if self.training:
return x, mc, p
return (torch.cat([x, mc], 1), p) if self.export else (torch.cat([x[0], mc], 1), (x[1], mc, p))
2、导出ONNX
from ultralytics import YOLO
YOLO('H:/git/ultralytics/volleyball/best.pt').export(imgsz=640, format='onnx', simplify=True, opset=11)第四步Docker环境准备 参考:
RDK X5 算法工具链社区手册:https://developer.d-robotics.cc/api/v1/fileData/x5_doc-v126cn/index.html
1、下载
wget -c ftp://x5ftp@vrftp.horizon.ai/OpenExplorer/v1.2.6_release/docker_openexplorer_ubuntu_20_x5_cpu_v1.2.6.tar.gz --ftp-password=x5ftp@123$%
2、加载
docker load --input docker_openexplorer_ubuntu_20_x5_cpu_v1.2.6.tar.gz
3、查看
docker image list
4、挂载
docker run -it -v "H:\horizonx5\codes":/data/horizon_x5/codes -v "H:\horizonx5\data":/data/horizon_x5/data openexplorer/ai_toolchain_ubuntu_20_x5_cpu:v1.2.6第五步转换模型onnx
1、检查模型
hb_mapper checker --model-type onnx --march bayes-e --model yolov8.onnx
2、验证集准备 生成校准数据集 使用 generate_calibration_data.py
https://github.com/D-Robotics/rdk_model_zoo/tree/main/demos/tools/generate_calibration_data

3、转换模型
转换配置文件 yolov8_onnx_convert.yaml
# 模型转化相关的参数
model_parameters:
# 必选参数
# Onnx浮点网络数据模型文件, 例如:onnx_model: './horizon_x3_onnx.onnx'
onnx_model: './yolov8.onnx'
march: "bayes-e"
layer_out_dump: False
working_dir: 'model_output'
output_model_file_prefix: 'horizon_x5'
node_info: {"/model.10/m/m.0/attn/Softmax": {'ON': 'BPU','InputType': 'int16','OutputType': 'int16'}}
# 模型输入相关参数
input_parameters:
input_name: ""
input_type_rt: 'nv12'
input_type_train: 'rgb'
input_layout_train: 'NCHW'
norm_type: 'data_scale'
scale_value: 0.003921568627451
# 模型量化相关参数
calibration_parameters:
# 必选参数, 使用2.1中的图片路径
cal_data_dir: './calibration_data_rgb_f32_640'
cal_data_type: 'float32'
# 编译器相关参数
compiler_parameters:
compile_mode: 'latency'
debug: False
# core_num: 2
optimize_level: 'O3'转换命令执行
hb_mapper makertbin --config yolov8_onnx_convert.yaml --model-type onnx

此处报错:
2025-01-16 10:25:03,245 ERROR load cal data for input 'images' error
2025-01-16 10:25:03,249 ERROR cannot reshape array of size 307200 into shape (1,3,640,640)