Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export yolov4-large models to ONNX and inference with TensorRT #56

Open
linghu8812 opened this issue Nov 27, 2020 · 12 comments
Open

Export yolov4-large models to ONNX and inference with TensorRT #56

linghu8812 opened this issue Nov 27, 2020 · 12 comments

Comments

@linghu8812
Copy link

linghu8812 commented Nov 27, 2020

Hello everyone, here is a code that can convert the ScaledYOLOv4 model to onnx model.

yolov4-large branch

For the branch of yolov4-large, the code is here: https://github.com/linghu8812/tensorrt_inference/tree/master/ScaledYOLOv4, now it can support the conversion of yolov4-p5, yolov4-p6, yolov4-p7 models. After the model exported, a TensorRT inference code was supplied, the TensorRT inference results has been shown below, which was consistent with the PyTorch results.

image

yolov4-csp and yolov4-tiny branch

As for the darknet supportted models, please refer this issue: AlexeyAB/darknet#7002, it shows how to convert darknet models to ONNX models, now it has supported yolov4, yolov4x-mish, yolov4-tiny models and so on.

@DaChaoXc
Copy link

yolov4-csp convert onnx is support? Me really looking forward!

@linghu8812
Copy link
Author

yolov4-csp convert onnx is support? Me really looking forward!

@DaChaoXc Hello, yolov4-csp.weights can be exported to onnx model, using Yolov4/export_onnx.py and run:

python3 export_onnx.py --cfg_file cfg/yolov4-csp.cfg --weights_file yolov4-csp.weights --output_file yolov4-csp.onnx --sigmoid

inference can be done with:

./Yolov4_trt ../config-yolov4-csp.yaml ../samples/

@DaChaoXc
Copy link

DaChaoXc commented Dec 1, 2020

@linghu8812 Hello ,I use https://github.com/CaoWGG/TensorRT-YOLOv4, the box is wrong, it seems the diffenence is yolo layer convert need add function of sigmoid?

@DaChaoXc
Copy link

DaChaoXc commented Dec 1, 2020

@linghu8812 something wrong
Traceback (most recent call last):
File "/home/xc/xc/code/obj/YOLO/scaleyolov4-onnx/tensorrt_inference/Yolov4/export_onnx.py", line 1114, in
neck=args.neck, sigmoid=args.sigmoid)
File "/home/xc/xc/code/obj/YOLO/scaleyolov4-onnx/tensorrt_inference/Yolov4/export_onnx.py", line 1096, in main
onnx.checker.check_model(yolo_model_def)
File "/home/xc/xc/softwares/anaconda3/lib/python3.6/site-packages/onnx/checker.py", line 93, in check_model
C.check_model(model.SerializeToString())
onnx.onnx_cpp2py_export.checker.ValidationError: Node (116_upsample) has input size 2 not in range [min=3, max=4].

==> Context: Bad node spec: input: "115_convolutional_mish" input: "116_upsample_scale" output: "116_upsample" name: "116_upsample" op_type: "Resize" attribute { name: "mode" s: "nearest" type: STRING }

@DaChaoXc
Copy link

DaChaoXc commented Dec 1, 2020

@linghu8812 something wrong
Traceback (most recent call last):
File "/home/xc/xc/code/obj/YOLO/scaleyolov4-onnx/tensorrt_inference/Yolov4/export_onnx.py", line 1114, in
neck=args.neck, sigmoid=args.sigmoid)
File "/home/xc/xc/code/obj/YOLO/scaleyolov4-onnx/tensorrt_inference/Yolov4/export_onnx.py", line 1096, in main
onnx.checker.check_model(yolo_model_def)
File "/home/xc/xc/softwares/anaconda3/lib/python3.6/site-packages/onnx/checker.py", line 93, in check_model
C.check_model(model.SerializeToString())
onnx.onnx_cpp2py_export.checker.ValidationError: Node (116_upsample) has input size 2 not in range [min=3, max=4].

==> Context: Bad node spec: input: "115_convolutional_mish" input: "116_upsample_scale" output: "116_upsample" name: "116_upsample" op_type: "Resize" attribute { name: "mode" s: "nearest" type: STRING }

solved ,onnx must 1.5.0

@linghu8812
Copy link
Author

@DaChaoXc Hello,
for the new feature [yolo] new_coords=1 in yolov4-csp and yolov4x-mish, the decode boxes function has been changed, I add sigmoid at the end just for the convience of postprocess. For more information, please refer this: AlexeyAB/darknet#6987 (comment)

@DaChaoXc
Copy link

DaChaoXc commented Dec 3, 2020

i changed the compute way, it works!
// x1,y1,x2,y2,cls,conf,batch_id
//data[0] = (row + sigmoid(input[begin_id])) * down_stride;
//data[1] = (col + sigmoid(input[begin_id+stride])) * down_stride;
//data[2] = expf(input[begin_id+2stride]) * (float)shared_anchors[2 * anchor_id];
//data[3] = expf(input[begin_id+3
stride]) * (float)shared_anchors[2 * anchor_id + 1];

data[0] = (row + sigmoid(input[begin_id]) * 2 - 0.5) * down_stride;
data[1] = (col + sigmoid(input[begin_id+stride]) * 2 - 0.5) * down_stride;
data[2] = pow(sigmoid(input[begin_id+2stride]) * 2, 2) * (float)shared_anchors[2 * anchor_id];
data[3] = pow(sigmoid(input[begin_id+3
stride]) * 2, 2) * (float)shared_anchors[2 * anchor_id + 1];

@linghu8812
Copy link
Author

@DaChaoXc yes, if add sigmoid op to onnx model, the sigmoid op will run with tensorrt, the code can be simplified as:

data[0] = (row + input[begin_id] * 2 - 0.5)down_stride;
data[1] = (col + input[begin_id+stride] * 2 - 0.5)down_stride;
data[2] = pow(input[begin_id+2stride] * 2, 2) (float)shared_anchors[2anchor_id];
data[3] = pow(input[begin_id+3stride] * 2, 2) * (float)shared_anchors[2*anchor_id + 1];

@linghu8812
Copy link
Author

@linghu8812 something wrong
Traceback (most recent call last):
File "/home/xc/xc/code/obj/YOLO/scaleyolov4-onnx/tensorrt_inference/Yolov4/export_onnx.py", line 1114, in
neck=args.neck, sigmoid=args.sigmoid)
File "/home/xc/xc/code/obj/YOLO/scaleyolov4-onnx/tensorrt_inference/Yolov4/export_onnx.py", line 1096, in main
onnx.checker.check_model(yolo_model_def)
File "/home/xc/xc/softwares/anaconda3/lib/python3.6/site-packages/onnx/checker.py", line 93, in check_model
C.check_model(model.SerializeToString())
onnx.onnx_cpp2py_export.checker.ValidationError: Node (116_upsample) has input size 2 not in range [min=3, max=4].
==> Context: Bad node spec: input: "115_convolutional_mish" input: "116_upsample_scale" output: "116_upsample" name: "116_upsample" op_type: "Resize" attribute { name: "mode" s: "nearest" type: STRING }

solved ,onnx must 1.5.0

@DaChaoXc I have add export onnx model with opset 10 code, onnx version has been no longer a restriction.

@tjuskyzhang
Copy link

tjuskyzhang commented May 11, 2021

A yolov4-csp/p5/p6/p7-tensorrt project
https://github.com/tjuskyzhang/Scaled-YOLOv4-TensorRT

@huyhoangle86
Copy link

hey do you have code to export Scaled Yolov4 CSP version to Onnx ? @linghu8812

@lantudou
Copy link

lantudou commented Jun 1, 2022

#371

Hi, Could you update your code for the newest branch ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants