-
Notifications
You must be signed in to change notification settings - Fork 0
/
convertToONNX.py
270 lines (228 loc) · 11.1 KB
/
convertToONNX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import argparse
import os
import numpy as np
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from math import ceil
from glob import glob
from PIL import Image
import dataloaders
import models
from utils.helpers import colorize_mask
import time
import torch.onnx
import onnx
import onnx.optimizer
import onnx.helper
import onnxruntime
import time
import pycuda.autoinit
import pycuda.driver as cuda
import tensorrt as trt
def to_numpy(tensor):
return tensor.detach().cpu().numpy().astype(np.float32) if tensor.requires_grad else tensor.cpu().numpy().astype(np.float32)
def add_initializers_into_inputs(model: onnx.ModelProto) -> onnx.ModelProto:
for x in model.graph.initializer:
input_names = [x.name for x in model.graph.input]
if x.name not in input_names:
shape = onnx.TensorShapeProto()
for dim in x.dims:
shape.dim.extend([onnx.TensorShapeProto.Dimension(dim_value=dim)])
model.graph.input.extend(
[onnx.ValueInfoProto(name=x.name,
type=onnx.TypeProto(tensor_type=onnx.TypeProto.Tensor(elem_type=x.data_type,
shape=shape)))])
return model
def optimize(model: onnx.ModelProto) -> onnx.ModelProto:
"""
:param model: The onnx model.
:return: The optimized onnx model.
Before simplifying, use this method to generate value_info, which is used in `forward_all`
After simplifying, use this method to fold constants generated in previous step into initializer,
and eliminate unused constants.
"""
# Due to a onnx bug, https://github.com/onnx/onnx/issues/2417, we need to add missing initializers into inputs
input_num = len(model.graph.input)
model = add_initializers_into_inputs(model)
onnx.helper.strip_doc_string(model)
model = onnx.optimizer.optimize(model, ['eliminate_deadend', 'eliminate_identity', 'eliminate_nop_dropout',
'eliminate_nop_monotone_argmax', 'eliminate_nop_pad',
'extract_constant_to_initializer', 'eliminate_unused_initializer',
'eliminate_nop_transpose'],
fixed_point=True)
del model.graph.input[input_num:]
onnx.checker.check_model(model)
return model
##################################################
class HostDeviceMem(object):
def __init__(self, host_mem, device_mem):
"""Within this context, host_mom means the cpu memory and device means the GPU memory
"""
self.host = host_mem
self.device = device_mem
def __str__(self):
return "Host:\n" + str(self.host) + "\nDevice:\n" + str(self.device)
def __repr__(self):
return self.__str__()
def do_inference(context, bindings, inputs, outputs, stream, batch_size=1):
# Transfer data from CPU to the GPU.
[cuda.memcpy_htod_async(inp.device, inp.host, stream) for inp in inputs]
# Run inference.
context.execute_async(batch_size=batch_size, bindings=bindings, stream_handle=stream.handle)
# Transfer predictions back from the GPU.
[cuda.memcpy_dtoh_async(out.host, out.device, stream) for out in outputs]
# Synchronize the stream
stream.synchronize()
# Return only the host outputs.
return [out.host for out in outputs]
def postprocess_the_outputs(h_outputs, shape_of_output):
h_outputs = h_outputs.reshape(*shape_of_output)
return h_outputs
def allocate_buffers(engine):
inputs = []
outputs = []
bindings = []
stream = cuda.Stream()
for binding in engine:
size = trt.volume(engine.get_binding_shape(binding)) * engine.max_batch_size
dtype = trt.nptype(engine.get_binding_dtype(binding))
print(binding)
print(size)
print(dtype)
# Allocate host and device buffers
host_mem = cuda.pagelocked_empty(size, dtype)
device_mem = cuda.mem_alloc(host_mem.nbytes)
# Append the device buffer to device bindings.
bindings.append(int(device_mem))
# Append to the appropriate list.
if engine.binding_is_input(binding):
inputs.append(HostDeviceMem(host_mem, device_mem))
else:
outputs.append(HostDeviceMem(host_mem, device_mem))
return inputs, outputs, bindings, stream
######################################################
def main():
args = parse_arguments()
config = json.load(open(args.config))
# Dataset used for training the model
dataset_type = config['train_loader']['type']
loader = getattr(dataloaders, config['train_loader']['type'])(**config['train_loader']['args'])
to_tensor = transforms.ToTensor()
#normalize = transforms.Normalize(loader.MEAN, loader.STD)
num_classes = loader.dataset.num_classes
palette = loader.dataset.palette
base_size = loader.dataset.base_size
# Model
model = getattr(models, config['arch']['type'])(num_classes, **config['arch']['args'])
availble_gpus = list(range(torch.cuda.device_count()))
device = torch.device('cuda:0' if len(availble_gpus) > 0 else 'cpu')
checkpoint = torch.load(args.model)
if isinstance(checkpoint, dict) and 'state_dict' in checkpoint.keys():
print("getting checkpoint")
checkpoint = checkpoint['state_dict']
if 'module' in list(checkpoint.keys())[0] and not isinstance(model, torch.nn.DataParallel):
print('convert model to DataParallel')
model = torch.nn.DataParallel(model)
model.load_state_dict(checkpoint)
model.to(device)
'''
print("saving model")
torch.save(model.module.state_dict(), 'model.pkl')
print(model.module.state_dict())
'''
model.eval()
###########################################################################################################
batch_size=1
image = Image.open(args.images).convert('RGB')
original_size=image.size
image_name = os.path.basename(args.images)
target=Image.open("/home/ubuntu/TM2/mask/"+image_name)
if base_size:
image = image.resize(size=(base_size, base_size), resample=Image.BILINEAR)
target = target.resize(size=(base_size, base_size), resample=Image.NEAREST)
#dummy_input = torch.randn(batch_size, 3, base_size, base_size, device="cuda")
dummy_input = to_tensor(image).unsqueeze(0).to(device)
print("exporting model")
# Export the model
torch.onnx.export(model.module, # model being run
dummy_input, # model input (or a tuple for multiple inputs)
"model.onnx", # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
do_constant_folding=True, # whether to execute constant folding for optimization
input_names = ['input'], # the model's input names
output_names = ['output'], # the model's output names
#dynamic_axes={'input' : {0 : 'batch_size'}, 'output' : {0 : 'batch_size'}}, # variable lenght axes
verbose=True,
opset_version=11,
keep_initializers_as_inputs=True)
# #####################################################################
print("checking onnx model")
onnx_model = onnx.load("model.onnx")
onnx.checker.check_model(onnx_model)
onnx.checker.check_graph(onnx_model.graph)
print("onnx model is checked")
# #####################################################################
print("optimizing onnx model")
optimized_onnx_model = optimize(onnx_model)
onnx.checker.check_model(optimized_onnx_model)
onnx.checker.check_graph(optimized_onnx_model.graph)
print("optimization done")
onnx.save(optimized_onnx_model, 'optimized_model.onnx')
#############################################
image = Image.open(args.images).convert('RGB')
original_size=image.size
image_name = os.path.basename(args.images)
target=Image.open("/home/ubuntu/TM2/mask/"+image_name)
if base_size:
image = image.resize(size=(base_size, base_size), resample=Image.BILINEAR)
target = target.resize(size=(base_size, base_size), resample=Image.NEAREST)
####################################################################
pytorch_input = to_tensor(image).unsqueeze(0)
pytorch_time=time.time()
with torch.no_grad():
pytorch_prediction = model(pytorch_input.to(device))
pytorch_prediction = to_numpy(pytorch_prediction)
print("pytorch time used:{}".format(time.time()-pytorch_time))
#######################################################################
ort_session = onnxruntime.InferenceSession("optimized_model.onnx")
ort_input = to_numpy(pytorch_input)
ort_inputs = {ort_session.get_inputs()[0].name: ort_input}
ort_time=time.time()
ort_outs = ort_session.run(None, ort_inputs)
ort_prediction = ort_outs[0]
print("ort time used:{}".format(time.time()-ort_time))
# compare ONNX Runtime and PyTorch results
np.testing.assert_allclose(pytorch_prediction, ort_prediction, rtol=1e-03, atol=1e-05)
print("Exported model has been tested with ONNXRuntime, and the result looks good!")
###################################################################
with open("model.engine", 'rb') as f, trt.Runtime(trt.Logger(trt.Logger.WARNING)) as runtime:
print("Trying Tensorrt")
shape_of_output = (batch_size, num_classes, 128, 128)
engine = runtime.deserialize_cuda_engine(f.read())
inputs, outputs, bindings, stream = allocate_buffers(engine) # input, output: host # bindings
with engine.create_execution_context() as context:
inputs[0].host = ort_input.reshape(-1)
t1 = time.time()
trt_outputs = do_inference(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream) # numpy data
t2 = time.time()
feat = postprocess_the_outputs(trt_outputs[0], shape_of_output)
print('TensorRT ok')
print("Inference time with the TensorRT engine: {}".format(t2-t1))
np.testing.assert_allclose(pytorch_prediction, feat, rtol=1e-03, atol=1e-05)
print('All completed!')
print("DLLMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM")
def parse_arguments():
parser = argparse.ArgumentParser(description='convertToONNX')
parser.add_argument('-c', '--config', default='./saved/TM2-HRNetV2_OCR/03-01_07-05/config.json',type=str,
help='The config used to train the model')
parser.add_argument('-m', '--model', default='./saved/TM2-HRNetV2_OCR/03-01_07-05/checkpoint-epoch200.pth', type=str,
help='Path to the .pth model checkpoint to be used in the prediction')
parser.add_argument('-i', '--images', default='/home/ubuntu/TM2/photo/104.png', type=str,
help='Path to the images to be segmented')
args = parser.parse_args()
return args
if __name__ == '__main__':
main()