• YOLOV8-DET转ONNX和RKNN


    目录

    1. 前言

    2.环境配置

    (1) RK3588开发板Python环境

    (2) PC转onnx和rknn的环境

    3.PT模型转onnx

    4. ONNX模型转RKNN

    6.测试结果

    1. 前言

            yolov8就不介绍了,详细的请见YOLOV8详细对比,本文章注重实际的使用,从拿到yolov8的pt检测模型,怎样转onnx和rknn的使用,配置好相应的环境,就可以开心的做yolov8的检测啦!!!

    2.环境配置

    (1) RK3588开发板Python环境


            numpy ==1.26.0
            onnxruntime == 1.16.0
            opencv-python== 4.8.0.76
            rknn-toolkit-lite2 ==1.5.2

    (2) PC转onnx和rknn的环境

            Python == 3.10

            ultralytics ==8.0.147

            rknn_toolkit2 == 1.5.2

            torch ==2.0.1
            torchvision==0.15.2

    相关的库缺少什么就安装,很简单的

    3.PT模型转onnx

            根据自己模型路径设置model_path,输入的shape也自己设置,我这里设置的是640*640,这样就得到了onnx模型,按道理是可以做推理了

    1. from ultralytics import YOLO
    2. # pip install ultralytics
    3. input_width = 640
    4. input_height = 640
    5. model_path = "./models/yolov8x.pt"
    6. model = YOLO(model_path)
    7. model.export(format="onnx", imgsz=[input_height, input_width], opset=12)

    4. ONNX模型转RKNN

            rknn用FP16,用INT8设置了,但是量化效果精度有误差,用混合量化或者增加量化数据集,都可以试试(我偷懒就不尝试了)

    1. from rknn.api import RKNN
    2. ONNX_MODEL = 'yolov8n-sim.onnx'
    3. RKNN_MODEL = 'yolov8n-sim-fp16.rknn'
    4. IMG_PATH = './bus.jpg'
    5. DATASET = './dataset.txt'
    6. if __name__ == '__main__':
    7. # Create RKNN object
    8. rknn = RKNN()
    9. if not os.path.exists(ONNX_MODEL):
    10. print('model not exist')
    11. exit(-1)
    12. # pre-process config
    13. print('--> Config model')
    14. rknn.config(mean_values=[[0, 0, 0]], std_values=[[1, 1, 1]], target_platform='rk3588')
    15. print('done')
    16. # Load ONNX model
    17. print('--> Loading model')
    18. ret = rknn.load_onnx(model=ONNX_MODEL)
    19. if ret != 0:
    20. print('Load yolov8 failed!')
    21. exit(ret)
    22. print('done')
    23. # Build model
    24. print('--> Building model')
    25. ret = rknn.build(do_quantization=False, dataset=DATASET)
    26. if ret != 0:
    27. print('Build yolov8 failed!')
    28. exit(ret)
    29. print('done')
    30. # Export RKNN model
    31. print('--> Export RKNN model')
    32. ret = rknn.export_rknn(RKNN_MODEL)
    33. if ret != 0:
    34. print('Export yolov8rknn failed!')
    35. exit(ret)
    36. print('done')
    37. # Ret = rknn.accuracy_analysis(inputs=['./bus.jpg'])
    38. # if ret != 0:
    39. # print('Accuracy analysis failed!')
    40. # exit(ret)
    41. # print('done')

    5. RK3588推理

            代码使用的是https://github.com/ibaiGorordo/ONNX-YOLOv8-Object-DetectionibaiGorord博主的基础代码,在YOLOv8中集成了rknn模型的推理

    1. import cv2
    2. from yolov8 import YOLOv8
    3. # Initialize yolov8 object detector
    4. # model_path = "./models/yolov8n-sim.onnx"
    5. model_path = "./models/yolov8n-sim-fp16.rknn"
    6. yolov8_detector = YOLOv8(model_path, conf_thres=0.3, iou_thres=0.5)
    7. # Read image
    8. img = cv2.imread('./bus.jpg')
    9. # Detect Objects
    10. boxes, scores, class_ids = yolov8_detector(img)
    11. # Draw detections
    12. combined_img = yolov8_detector.draw_detections(img)
    13. cv2.imwrite("./detected_objects.png", combined_img)

    6.测试结果

    onnx推理结果

    rknn推理结果

            使用的是yolov8n模型,检测的速度加速了8倍,量化成int8的话应该还要更加快!!!

            完整的代码在资源中下载

  • 相关阅读:
    什么是回调函数?
    SpringCloud-2-入门概述
    thinkphp 生成邀请推广二维码,保存到服务器并接口返回给前端
    Azure Functions 支持 .NET 7 作为运行时
    OpenFeign简介,负载策略
    MySQL的下载、安装、配置
    javascript(2)高级
    7-Redis工具类
    YOLOv8改进有效涨点专栏目录——包含backbone、head、neck和损失函数上百余种改进技巧
    Android etc1tool之png图片转换pkm 和 zipalign简介
  • 原文地址:https://blog.csdn.net/weixin_43999691/article/details/133307741