Skip to content

Commit 3123399

Browse files
authored
Merge pull request #193 from KelvinCPChiu/master
Update the Torch to ONNX for dynamic graph.
2 parents a132f27 + 5720776 commit 3123399

File tree

4 files changed

+104
-27
lines changed

4 files changed

+104
-27
lines changed

DeepStream/Readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,32 @@ you can use darknet2pytorch to convert it yourself, or download my converted mod
212212
213213
Note:Errors will occur when using "pip install onnx-tf", at least for me,it is recommended to use source code installation
214214
215+
# 7. ONNX2TensorRT and DeepStream Inference
216+
217+
1. Compile the DeepStream Nvinfer Plugin
218+
219+
```
220+
cd DeepStream
221+
make
222+
```
223+
2. Build a TRT Engine.
224+
225+
For single batch,
226+
```
227+
trtexec --onnx=<onnx_file> --explicitBatch --saveEngine=<tensorRT_engine_file> --workspace=<size_in_megabytes> --fp16
228+
```
229+
230+
For multi-batch,
231+
```
232+
trtexec --onnx=<onnx_file> --explicitBatch --shapes=input:Xx3xHxW --optShapes=input:Xx3xHxW --maxShapes=input:Xx3xHxW --minShape=input:1x3xHxW --saveEngine=<tensorRT_engine_file> --fp16
233+
```
234+
235+
Note :The maxShapes could not be larger than model original shape.
236+
237+
3. Write the deepstream config file for the TRT Engine.
238+
239+
240+
215241
Reference:
216242
- https://github.com/eriklindernoren/PyTorch-YOLOv3
217243
- https://github.com/marvis/pytorch-caffe-darknet-convert

tool/darknet2onnx.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from tool.darknet2pytorch import Darknet
44

55

6-
def transform_to_onnx(cfgfile, weightfile, batch_size=1):
6+
def transform_to_onnx(cfgfile, weightfile, batch_size=1, dynamic=False):
77
model = Darknet(cfgfile)
88

99
model.print_network()
@@ -14,21 +14,40 @@ def transform_to_onnx(cfgfile, weightfile, batch_size=1):
1414

1515
x = torch.randn((batch_size, 3, model.height, model.width), requires_grad=True) # .cuda()
1616

17-
onnx_file_name = "yolov4_{}_3_{}_{}.onnx".format(batch_size, model.height, model.width)
17+
if dynamic:
1818

19-
# Export the model
20-
print('Export the onnx model ...')
21-
torch.onnx.export(model,
22-
x,
23-
onnx_file_name,
24-
export_params=True,
25-
opset_version=11,
26-
do_constant_folding=True,
27-
input_names=['input'], output_names=['boxes', 'confs'],
28-
dynamic_axes=None)
19+
onnx_file_name = "yolov4_{}_3_{}_{}_dyna.onnx".format(batch_size, model.height, model.width)
20+
input_names = ["input"]
21+
output_names = ['boxes', 'confs']
2922

30-
print('Onnx model exporting done')
31-
return onnx_file_name
23+
dynamic_axes = {"input": {0: "batch_size"}, "boxes": {0: "batch_size"}, "confs": {0: "batch_size"}}
24+
# Export the model
25+
print('Export the onnx model ...')
26+
torch.onnx.export(model,
27+
x,
28+
onnx_file_name,
29+
export_params=True,
30+
opset_version=11,
31+
do_constant_folding=True,
32+
input_names=input_names, output_names=output_names,
33+
dynamic_axes=dynamic_axes)
34+
35+
print('Onnx model exporting done')
36+
return onnx_file_name
37+
38+
else:
39+
onnx_file_name = "yolov4_{}_3_{}_{}_static.onnx".format(batch_size, model.height, model.width)
40+
torch.onnx.export(model,
41+
x,
42+
onnx_file_name,
43+
export_params=True,
44+
opset_version=11,
45+
do_constant_folding=True,
46+
input_names=['input'], output_names=['boxes', 'confs'],
47+
dynamic_axes=None)
48+
49+
print('Onnx model exporting done')
50+
return onnx_file_name
3251

3352

3453
if __name__ == '__main__':
@@ -41,6 +60,12 @@ def transform_to_onnx(cfgfile, weightfile, batch_size=1):
4160
weightfile = sys.argv[2]
4261
batch_size = int(sys.argv[3])
4362
transform_to_onnx(cfgfile, weightfile, batch_size)
63+
elif len(sys.argv) == 5:
64+
cfgfile = sys.argv[1]
65+
weightfile = sys.argv[2]
66+
batch_size = int(sys.argv[3])
67+
dynamic = True if sys.argv[4] == 'True' else False
68+
transform_to_onnx(cfgfile, weightfile, batch_size, dynamic)
4469
else:
4570
print('Please execute this script this way:\n')
4671
print(' python darknet2onnx.py <cfgFile> <weightFile>')

tool/pytorch2onnx.py

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import torch
33

44

5-
def transform_to_onnx(cfgfile, weightfile, batch_size=1):
5+
def transform_to_onnx(cfgfile, weightfile, batch_size=1, dynamic=False):
66
model = Darknet(cfgfile)
77

88
model.print_network()
@@ -13,20 +13,39 @@ def transform_to_onnx(cfgfile, weightfile, batch_size=1):
1313

1414
x = torch.randn((batch_size, 3, model.height, model.width), requires_grad=True) # .cuda()
1515

16-
onnx_file_name = "yolov4_{}_3_{}_{}.onnx".format(batch_size, model.height, model.width)
16+
if dynamics:
17+
onnx_file_name = "yolov4_{}_3_{}_{}_dyna.onnx".format(batch_size, model.height, model.width)
18+
input_names = ["input"]
19+
output_names = ['boxes', 'confs']
1720

18-
# Export the model
19-
print('Export the onnx model ...')
20-
torch.onnx.export(model,
21-
x,
22-
onnx_file_name,
23-
export_params=True,
24-
opset_version=11,
25-
do_constant_folding=True,
26-
dynamic_axes=None)
21+
dynamic_axes = {"input": {0: "batch_size"}, "boxes": {0: "batch_size"}, "confs": {0: "batch_size"}}
22+
# Export the model
23+
24+
print('Export the onnx model ...')
25+
torch.onnx.export(model,
26+
x,
27+
onnx_file_name,
28+
export_params=True,
29+
opset_version=11,
30+
do_constant_folding=True,
31+
input_names=input_names, output_names=output_names,
32+
dynamic_axes=dynamic_axes)
2733

28-
print('Onnx model exporting done')
29-
return onnx_file_name
34+
print('Onnx model exporting done')
35+
return onnx_file_name
36+
37+
else:
38+
onnx_file_name = "yolov4_{}_3_{}_{}_static.onnx".format(batch_size, model.height, model.width)
39+
torch.onnx.export(model,
40+
x,
41+
onnx_file_name,
42+
export_params=True,
43+
opset_version=11,
44+
do_constant_folding=True,
45+
dynamic_axes=None)
46+
47+
print('Onnx model exporting done')
48+
return onnx_file_name
3049

3150

3251
if __name__ == '__main__':
@@ -39,6 +58,12 @@ def transform_to_onnx(cfgfile, weightfile, batch_size=1):
3958
weightfile = sys.argv[2]
4059
batch_size = int(sys.argv[3])
4160
transform_to_onnx(cfgfile, weightfile, batch_size)
61+
elif len(sys.argv) == 5:
62+
cfgfile = sys.argv[1]
63+
weightfile = sys.argv[2]
64+
batch_size = int(sys.argv[3])
65+
dynamic = True if sys.argv[4] == 'True' else False
66+
transform_to_onnx(cfgfile, weightfile, batch_size, dynamic)
4267
else:
4368
print('Please execute this script this way:\n')
4469
print(' python darknet2onnx.py <cfgFile> <weightFile>')

0 commit comments

Comments
 (0)