Skip to content

Commit f865b7c

Browse files
authored
Add files via upload
1 parent 86c0a68 commit f865b7c

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

convert_onnx/detector.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import torch
2+
import torch.nn as nn
3+
4+
from model.fpn import *
5+
from model.backbone.shufflenetv2 import *
6+
7+
class Detector(nn.Module):
8+
def __init__(self, classes, anchor_num, load_param):
9+
super(Detector, self).__init__()
10+
out_depth = 72
11+
stage_out_channels = [-1, 24, 48, 96, 192]
12+
13+
self.backbone = ShuffleNetV2(stage_out_channels, load_param)
14+
self.fpn = LightFPN(stage_out_channels[-2] + stage_out_channels[-1], stage_out_channels[-1], out_depth)
15+
16+
self.output_reg_layers = nn.Conv2d(out_depth, 4 * anchor_num, 1, 1, 0, bias=True)
17+
self.output_obj_layers = nn.Conv2d(out_depth, anchor_num, 1, 1, 0, bias=True)
18+
self.output_cls_layers = nn.Conv2d(out_depth, classes, 1, 1, 0, bias=True)
19+
20+
def forward(self, x):
21+
C2, C3 = self.backbone(x)
22+
cls_2, obj_2, reg_2, cls_3, obj_3, reg_3 = self.fpn(C2, C3)
23+
24+
out_reg_2 = self.output_reg_layers(reg_2)
25+
out_obj_2 = self.output_obj_layers(obj_2)
26+
out_cls_2 = self.output_cls_layers(cls_2)
27+
28+
out_reg_3 = self.output_reg_layers(reg_3)
29+
out_obj_3 = self.output_obj_layers(obj_3)
30+
out_cls_3 = self.output_cls_layers(cls_3)
31+
if not torch.onnx.is_in_onnx_export():
32+
return out_reg_2, out_obj_2, out_cls_2, out_reg_3, out_obj_3, out_cls_3
33+
else:
34+
# for out in (out_reg_2, out_obj_2, out_cls_2, out_reg_3, out_obj_3, out_cls_3):
35+
# print(out.shape)
36+
37+
c = out_reg_2.shape[1]
38+
out_reg_2 = out_reg_2.permute(0, 2, 3, 1).view(-1, c)
39+
c = out_obj_2.shape[1]
40+
out_obj_2 = out_obj_2.permute(0, 2, 3, 1).view(-1, c)
41+
c = out_cls_2.shape[1]
42+
out_cls_2 = out_cls_2.permute(0, 2, 3, 1).view(-1, c)
43+
out_reg_2 = torch.sigmoid(out_reg_2)
44+
out_obj_2 = torch.sigmoid(out_obj_2)
45+
out_cls_2 = F.softmax(out_cls_2, dim=1)
46+
out2 = torch.cat((out_reg_2, out_obj_2, out_cls_2), dim=1)
47+
48+
c = out_reg_3.shape[1]
49+
out_reg_3 = out_reg_3.permute(0, 2, 3, 1).view(-1, c)
50+
c = out_obj_3.shape[1]
51+
out_obj_3 = out_obj_3.permute(0, 2, 3, 1).view(-1, c)
52+
c = out_cls_3.shape[1]
53+
out_cls_3 = out_cls_3.permute(0, 2, 3, 1).view(-1, c)
54+
out_reg_3 = torch.sigmoid(out_reg_3)
55+
out_obj_3 = torch.sigmoid(out_obj_3)
56+
out_cls_3 = F.softmax(out_cls_3, dim=1)
57+
out3 = torch.cat((out_reg_3, out_obj_3, out_cls_3), dim=1)
58+
return torch.cat((out2, out3), dim=0)
59+
60+
if __name__ == "__main__":
61+
model = Detector(80, 3, False)
62+
test_data = torch.rand(1, 3, 352, 352)
63+
torch.onnx.export(model, #model being run
64+
test_data, # model input (or a tuple for multiple inputs)
65+
"test.onnx", # where to save the model (can be a file or file-like object)
66+
export_params=True, # store the trained parameter weights inside the model file
67+
opset_version=11, # the ONNX version to export the model to
68+
do_constant_folding=True) # whether to execute constant folding for optimization
69+
70+
71+

convert_onnx/pytorch2onnx.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import argparse
2+
import torch
3+
import model.detector
4+
import utils.utils
5+
6+
if __name__ == '__main__':
7+
#指定训练配置文件
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('--data', type=str, default='',
10+
help='Specify training profile *.data')
11+
parser.add_argument('--weights', type=str, default='',
12+
help='The path of the .pth model to be transformed')
13+
14+
parser.add_argument('--output', type=str, default='./model.onnx',
15+
help='The path where the onnx model is saved')
16+
17+
opt = parser.parse_args()
18+
cfg = utils.utils.load_datafile(opt.data)
19+
20+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
21+
model = model.detector.Detector(cfg["classes"], cfg["anchor_num"], True)
22+
model.load_state_dict(torch.load(opt.weights, map_location=device))
23+
#sets the module in eval node
24+
model.eval()
25+
26+
test_data = torch.rand(1, 3, cfg["height"], cfg["width"])
27+
torch.onnx.export(model, #model being run
28+
test_data, # model input (or a tuple for multiple inputs)
29+
opt.output, # where to save the model (can be a file or file-like object)
30+
export_params=True, # store the trained parameter weights inside the model file
31+
opset_version=11, # the ONNX version to export the model to
32+
do_constant_folding=True) # whether to execute constant folding for optimization
33+
# torch.onnx.export(model, # model being run
34+
# test_data, # model input (or a tuple for multiple inputs)
35+
# opt.output, # where to save the model (can be a file or file-like object)
36+
# opset_version=11, # the ONNX version to export the model to
37+
# input_names = ['images'], output_names = ['out'])
38+
39+
40+

0 commit comments

Comments
 (0)