nanmi / yolov7-pose

pose detection base on yolov7
118 stars 18 forks source link

how to get yolov7-w6-pose-sim-yolo.onnx? #10

Open tuteming opened 1 year ago

tuteming commented 1 year ago

how to get yolov7-w6-pose-sim-yolo.onnx? we only have yolov7-w6-pose.onnx from yolov7-w6-pose.pt.

NTS-SW commented 1 year ago

Do you try to use yolov7-w6-pose.onnx?

I did get wrong results.... hmmmm

JJLimmm commented 1 year ago

Which code did you use to export the yolov7-w6-pose.pt to the onnx model?

JJLimmm commented 1 year ago

Hello all @tuteming @NTS-SW , to those that are confused. I have managed to convert the yolov7-w6-pose.pt model (taken from YOLOv7's main branch README.md) to onnx.

  1. Firstly, you clone this repo either in the directory where the original yolov7 repo is in your local folder or within the original yolov7 repo itself.

  2. Next, you can follow the instructions to create the build/ dir in this repo and make the build file for YOLO tensorrt layer plugin.

  3. Change a portion in the code within the original yolov7 main branch repo /models/yolo.py under class IKeypoint(nn.Module)'s forward() module. Copy and replace the section of the code that corresponds to the changes stated in the README.md.

  4. create a new script in the original yolov7 folder _export_pose_onnx.py_ with the code below. Change the paths to the weights downloaded and where to save the exported onnx model accordingly.

    
    import sys
    sys.path.append('./')  # to run '$ python *.py' files in subdirectories
    import torch
    import torch.nn as nn
    import models
    from models.experimental import attempt_load
    from utils.activations import Hardswish, SiLU

weights = 'yolov7-w6-pose.pt' device = torch.device('cuda:0') model = attempt_load(weights, map_location=device) # load FP32 model

for k, m in model.named_modules(): m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility if isinstance(m, models.common.Conv): # assign export-friendly activations if isinstance(m.act, nn.Hardswish): m.act = Hardswish() elif isinstance(m.act, nn.SiLU): m.act = SiLU() model.model[-1].export = True # set Detect() layer grid export model.eval()

img = torch.randn(1, 3, 960, 960).to(device) # image size(1,3,320,192) iDetection torch.onnx.export(model, img, 'yolov7-w6-pose.onnx', verbose=False, opset_version=12, input_names=['images'])



5. run this `_export_pose_onnx.py_` script within the original yolov7 folder, to avoid python's relative import issues.

6. Once done, you can use netron to check the exported onnx model and you can see that the outputs matches the [first image](https://github.com/nanmi/yolov7-pose#:~:text=%27images%27%5D)-,you%20will%20get%3A,-use%20YoloLayer_TRT_v7.0/script) 

7. Next, change the path (**_line 7_** and **_line 43_**) to the onnx model and run the code at  _**./YoloLayer_TRT_v7.0/script/add_custom_yolo_op.py**_

8. And you are done, use netron to visualize the model weights and it should match [image 2](https://github.com/nanmi/yolov7-pose#:~:text=a%20new%20op%20lookes%20like%20this%3A)
tuteming commented 1 year ago

Dear JJLimmm: I have try very times, but I get a wrong 1.jpg. image and the final result is image

yolov7-w6-pose.pt is changed?

Thanks.

daohaofunk commented 7 months ago

Dear JJLimmm: I have try very times, but I get a wrong 1.jpg. image and the final result is image

yolov7-w6-pose.pt is changed?

Thanks.

Have you solved the problem when you applied the trt model on inference? I tried many times and got a similar output like you. And I was confused that the output-number of author's onnx file was 859, but I got 875.

jia0511 commented 3 months ago

链接: https://pan.baidu.com/s/1XO7zDjCskQglegcXQ3lIoQ 提取码: j3ny

FunnyWii commented 3 months ago

链接: https://pan.baidu.com/s/1XO7zDjCskQglegcXQ3lIoQ 提取码: j3ny

请问你推理结果正确吗