YOLOv5模型介绍
Ultralytics YOLOv5 🚀 是由 Ultralytics 开发的尖端、最先进(SOTA)计算机视觉模型。YOLOv5 基于 PyTorch 框架构建,以其易用性、速度和准确性而闻名。它融合了广泛研究与开发中的见解和最佳实践,使其成为各种视觉 AI 任务的热门选择,包括目标检测、图像分割和图像分类。

(以上内容取自 Ultralytics YOLOv5 Github 仓库:https://github.com/ultralytics/yolov5)
模型下载与导出
1.Git下载YOLOv5;
sudo git clone https://github.com/ultralytics/yolov5
Cloning into 'yolov5'...
remote: Enumerating objects: 17968, done.
remote: Counting objects: 100% (115/115), done.
remote: Compressing objects: 100% (66/66), done.
2.安装依赖;
pip install onnx onnxruntime
Collecting onnx
Downloading onnx-1.17.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.0 MB)
|████████████████████████████████| 16.0 MB 201 kB/s
Collecting onnxruntime
3.导出模型;
python3 export.py --weights yolov5n.pt --include onnx --opset 11
export: data=data/coco128.yaml, weights=['yolov5n.pt'], imgsz=[640, 640], batch_size=1, device=cpu, half=False, inplace=False, keras=False, optimize=False, int8=False, per_tensor=False, dynamic=False, cache=, simplify=False, mlmodel=False, opset=11, verbose=False, workspace=4, nms=False, agnostic_nms=False, topk_per_class=100, topk_all=100, iou_thres=0.45, conf_thres=0.25, include=['onnx']
fatal: detected dubious ownership in repository at '/home/openexplorer/Desktop/yolov5'
To add an exception for this directory, call:
git config --global --add safe.directory /home/openexplorer/Desktop/yolov5
4.拷贝模型至“Desktop/dataset/”文件夹下;

模型验证与转换
1.启动 Open Explorer Docker环境
sudo docker run -it --rm -v /open_explorer -v ./dataset:/data/horizon_j5/data openexplorer/ai_toolchain_ubuntu_20_j5_cpu:v1.1.77-py38
2.模型验证
cd ..
hb_mapper checker --model-type onnx --model /data/horizon_j5/data/yolov5n.onnx --march bayes
/usr/local/lib/python3.8/dist-packages/paramiko/pkey.py:100: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
"cipher": algorithms.TripleDES,
/usr/local/lib/python3.8/dist-packages/paramiko/transport.py:259: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.

3.图片转换
转换脚本如下:
import numpy as np
from PIL import Image
def generate_calib_data(img_path, output_path, target_size=(640, 640)):
img = Image.open(img_path).convert('RGB')
img_resized = img.resize(target_size, Image.BILINEAR)
img_np = np.array(img_resized)
img_np = img_np.transpose(2, 0, 1)
img_np = np.expand_dims(img_np, axis=0)
运行测试如下:
python3 test.py
test.py:6: DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.
img_resized = img.resize(target_size, Image.BILINEAR)
Shape: (1, 3, 640, 640), Size: 1228800

4.模型转换
4.1编写yolov5n.yaml文件“Desktop/dataset/”文件夹下;
cat > /data/horizon_j5/data/yolov5n.yaml
model_parameters:
onnx_model: '/data/horizon_j5/data/yolov5n.onnx'
march: "bayes"
layer_out_dump: False
working_dir: '../model_output'
output_model_file_prefix: 'yolov5n-det'
remove_node_type: "Dequantize"
node_info: {"/model.10/m/m.0/attn/Softmax": {'ON': 'BPU','InputType': 'int8','OutputType': 'int8'}}
4.2进行模型转换;
hb_mapper makertbin --config yolov5n.yaml --model-type onnx
/usr/local/lib/python3.8/dist-packages/paramiko/pkey.py:100: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
"cipher": algorithms.TripleDES,
/usr/local/lib/python3.8/dist-packages/paramiko/transport.py:259: CryptographyDeprecationWarning: TripleDES has been moved to cryptography.hazmat.decrepit.ciphers.algorithms.TripleDES and will be removed from this module in 48.0.0.
"class": algorithms.TripleDES,

文章转载地平线论坛:【地平线征程5域控试用】(三)
