Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.46k stars 4.17k forks source link

Hope ncnn support yolov5-7.0 segmentation. #4475

Closed Digital2Slave closed 1 year ago

Digital2Slave commented 1 year ago

1. pt2onnx

I trained an image segment model tpadseg_v0.0.1.pt based on yolov5m-seg.pt by yolov5-7.0,and convert to onnx format through the following command :

$ python export.py --weights /home/tianzx/ai_model/seg/tpadseg_v0.0.1.pt --include onnx --inplace --simplify
  "categories": [
    {
      "id": 0,
      "name": "blur"
    },
    {
      "id": 1,
      "name": "phone"
    },
    {
      "id": 2,
      "name": "reflectLight"
    },
    {
      "id": 3,
      "name": "reflection"
    }
  ]

2. onnx2ncnn

I use out-of-the-box web model conversion to convert onnx format model to ncnn format.

image

3. modify ncnn model file *.param

I modified the tpadseg_v0.0.1-sim-opt-fp16.param file to support different resolution of image as input data.

239 Reshape                  Reshape_285              1 1 onnx::Reshape_466 onnx::Transpose_478 0=-1 1=41 2=3
253 Reshape                  Reshape_306              1 1 onnx::Reshape_507 onnx::Transpose_519 0=-1 1=41 2=3
267 Reshape                  Reshape_327              1 1 onnx::Reshape_548 onnx::Transpose_560 0=-1 1=41 2=3
$ ls -hl /home/tianzx/ai_model/seg/
total 167M
-rw-rw-r-- 1 tianzx tianzx 84M 1月  16 11:31 tpadseg_v0.0.1.onnx
-rw-rw-r-- 1 tianzx tianzx 42M 1月  14 11:16 tpadseg_v0.0.1.pt
-rw-rw-r-- 1 tianzx tianzx 42M 1月  16 11:32 tpadseg_v0.0.1-sim-opt-fp16.bin
-rw-rw-r-- 1 tianzx tianzx 27K 1月  16 11:44 tpadseg_v0.0.1-sim-opt-fp16.param
-rw-rw-r-- 1 tianzx tianzx 27K 1月  16 11:33 tpadseg_v0.0.1-sim-opt-fp16.param.bak

tpadseg_v0.0.1-sim-opt-fp16.param.bak is the default output file.

The files in seg folder can be find in the Google Drive or Baidu Disk.

4. ncnn demo [python/c++]

I search this repo issues and google some related blogs, but do not find any ncnn tutorial about yolov5-7.0 segmentation. Hope ncnn support yolov5-7.0 segmentation.

Thanks a lot!

Digital2Slave commented 1 year ago

I close this issue, cause it's similar to following issues:

glenn-jocher commented 1 year ago

@Digital2Slave I have great news 😃! I've recently added official support for Ultralytics YOLOv8 NCNN export ✅ in PR https://github.com/ultralytics/ultralytics/pull/3529 with the help of @nihui which is part of ultralytics==8.0.129. NCNN works for all tasks including Detect, Segment, Pose and Classify.

You can now export with CLI:

yolo export model=yolov8n.pt format=ncnn

or Python:

from ultralytics import YOLO

# Create a model
model = YOLO('yolov8n.pt')

# Export the model to NCNN with arguments
model.export(format='ncnn', half=True, imgsz=640)

Output is a yolov8n_ncnn_model/ directory containing model.bin, model.param and metadata.yaml, along with extra PNNX files. For details see https://github.com/pnnx/pnnx README.

To get this update:

Please let us know if NCNN export is working correctly for you, and don't hesitate to report any other issues you find or feature requests you may have. Happy training with YOLOv8 🚀!

Digital2Slave commented 1 year ago

@glenn-jocher @nihui Thanks a lot for the great work!

nihui commented 3 months ago

针对onnx模型转换的各种问题,推荐使用最新的pnnx工具转换到ncnn In view of various problems in onnx model conversion, it is recommended to use the latest pnnx tool to convert your model to ncnn

pip install pnnx
pnnx model.onnx inputshape=[1,3,224,224]

详细参考文档 Detailed reference documentation https://github.com/pnnx/pnnx https://github.com/Tencent/ncnn/wiki/use-ncnn-with-pytorch-or-onnx#how-to-use-pnnx