-
Notifications
You must be signed in to change notification settings - Fork 0
/
New Text Document.txt
57 lines (43 loc) · 1.9 KB
/
New Text Document.txt
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
def get_image():
img = _load_and_resize("/home/abdullah/Desktop/Profile.png")
img = _shuffle_and_normalize(img)
return img
def _load_and_resize(input_image_path):
image_raw = Image.open(input_image_path)
# convention (width, height) in PIL:
image_resized = image_raw.resize(
(512, 512), resample=Image.BICUBIC)
image_resized = np.array(image_resized, dtype=np.float32, order='C')
return image_resized
def _shuffle_and_normalize(image):
image /= 255.0
# HWC to CHW format:
image = np.transpose(image, [2, 0, 1])
# CHW to NCHW format
image = np.expand_dims(image, axis=0)
# Convert the image to row-major order, also known as "C order":
image = np.array(image, dtype=np.float32, order='C')
return image
def _reshape_output(output):
return np.reshape(output, (512, 512, 3))
def main():
"""Create inference for generator for eye."""
onnx_file_path = 'generator.onnx'
engine_file_path = "generator.trt"
output_shapes = [(1, 3,512, 512)]
image = get_image()
print('IMage shape is ', image.shape)
# Do inference with TensorRT
with get_engine(onnx_file_path, engine_file_path) as engine, engine.create_execution_context() as context:
inputs, outputs, bindings, stream = common.allocate_buffers(engine)
print('Running inference on image {}...'.format(1))
inputs[0].host = image
print(inputs)
trt_outputs = common.do_inference(context, bindings=bindings, inputs=inputs, outputs=outputs, stream=stream)
# Before doing post-processing, we need to reshape the outputs as the common.do_inference will give us flat arrays.
trt_outputs = [output.reshape(shape) for output, shape in zip(trt_outputs, output_shapes)]
image = _reshape_output(trt_outputs)
image = np.array(image, dtype = 'float32')
plt.imshow(image)
plt.show()
print('Output shape is ', trt_outputs.shape)