-
Notifications
You must be signed in to change notification settings - Fork 0
/
trt_inference.py
146 lines (127 loc) · 5.79 KB
/
trt_inference.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
"""
Script is Owned by Nvidia. Link - https://github.com/NVIDIA/TensorRT/blob/master/samples/python/efficientnet/infer.py
"""
import os
import sys
import argparse
import numpy as np
import tensorrt as trt
import pycuda.driver as cuda
import pycuda.autoinit
from image_batcher import ImageBatcher
class TensorRTInfer:
"""
Implements inference for the EfficientNet TensorRT engine.
"""
def __init__(self, engine_path):
"""
:param engine_path: The path to the serialized engine to load from disk.
"""
# Load TRT engine
self.logger = trt.Logger(trt.Logger.ERROR)
with open(engine_path, "rb") as f, trt.Runtime(self.logger) as runtime:
self.engine = runtime.deserialize_cuda_engine(f.read())
self.context = self.engine.create_execution_context()
assert self.engine
assert self.context
# Setup I/O bindings
self.inputs = []
self.outputs = []
self.allocations = []
for i in range(self.engine.num_bindings):
is_input = False
if self.engine.binding_is_input(i):
is_input = True
name = self.engine.get_binding_name(i)
dtype = self.engine.get_binding_dtype(i)
shape = self.engine.get_binding_shape(i)
if is_input:
self.batch_size = shape[0]
size = np.dtype(trt.nptype(dtype)).itemsize
for s in shape:
size *= s
allocation = cuda.mem_alloc(size)
binding = {
'index': i,
'name': name,
'dtype': np.dtype(trt.nptype(dtype)),
'shape': list(shape),
'allocation': allocation,
}
self.allocations.append(allocation)
if self.engine.binding_is_input(i):
self.inputs.append(binding)
else:
self.outputs.append(binding)
assert self.batch_size > 0
assert len(self.inputs) > 0
assert len(self.outputs) > 0
assert len(self.allocations) > 0
def input_spec(self):
"""
Get the specs for the input tensor of the network. Useful to prepare memory allocations.
:return: Two items, the shape of the input tensor and its (numpy) datatype.
"""
return self.inputs[0]['shape'], self.inputs[0]['dtype']
def output_spec(self):
"""
Get the specs for the output tensor of the network. Useful to prepare memory allocations.
:return: Two items, the shape of the output tensor and its (numpy) datatype.
"""
return self.outputs[0]['shape'], self.outputs[0]['dtype']
def infer(self, batch, top=1):
"""
Execute inference on a batch of images. The images should already be batched and preprocessed, as prepared by
the ImageBatcher class. Memory copying to and from the GPU device will be performed here.
:param batch: A numpy array holding the image batch.
:param top: The number of classes to return as top_predicitons, in descending order by their score. By default,
setting to one will return the same as the maximum score class. Useful for Top-5 accuracy metrics in validation.
:return: Three items, as numpy arrays for each batch image: The maximum score class, the corresponding maximum
score, and a list of the top N classes and scores.
"""
# Prepare the output data
output = np.zeros(*self.output_spec())
# Process I/O and execute the network
cuda.memcpy_htod(self.inputs[0]['allocation'], np.ascontiguousarray(batch))
self.context.execute_v2(self.allocations)
cuda.memcpy_dtoh(output, self.outputs[0]['allocation'])
# Process the results
classes = np.argmax(output, axis=1)
scores = np.max(output, axis=1)
top = min(top, output.shape[1])
top_classes = np.flip(np.argsort(output, axis=1), axis=1)[:, 0:top]
top_scores = np.flip(np.sort(output, axis=1), axis=1)[:, 0:top]
return classes, scores, [top_classes, top_scores]
def main(args):
trt_infer = TensorRTInfer(args.engine)
batcher = ImageBatcher(args.input, *trt_infer.input_spec(), preprocessor=args.preprocessor)
for batch, images in batcher.get_batch():
classes, scores, top = trt_infer.infer(batch)
for i in range(len(images)):
if args.top == 1:
print(images[i], classes[i], scores[i], sep=args.separator)
else:
line = [images[i]]
assert args.top <= top[0].shape[1]
for t in range(args.top):
line.append(str(top[0][i][t]))
for t in range(args.top):
line.append(str(top[1][i][t]))
print(args.separator.join(line))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-e", "--engine", help="The TensorRT engine to infer with")
parser.add_argument("-i", "--input",
help="The input to infer, either a single image path, or a directory of images")
parser.add_argument("-t", "--top", default=1, type=int,
help="The amount of top classes and scores to output per image, default: 1")
parser.add_argument("-s", "--separator", default="\t",
help="Separator to use between columns when printing the results, default: \\t")
parser.add_argument("-p", "--preprocessor", default="V2", choices=["V1", "V1MS", "V2"],
help="Select the image preprocessor to use, either 'V2', 'V1' or 'V1MS', default: V2")
args = parser.parse_args()
if not all([args.engine, args.input]):
parser.print_help()
print("\nThese arguments are required: --engine and --input")
sys.exit(1)
main(args)