-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_onnx.py
94 lines (73 loc) · 3.38 KB
/
run_onnx.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
import time
import numpy as np
import onnxruntime as rt
# use hd630 igpu*************************************************************************************************
# --------------------------fp32 start----------------------------
ops = rt.SessionOptions()
ops.enable_mem_pattern = False
sess = rt.InferenceSession("bres256_core_sim.onnx", sess_options=ops, providers=[('DmlExecutionProvider', {
'device_id': 0,
})])
out_names = [each.name for each in sess.get_outputs()]
result = sess.run(out_names, {"img": np.random.random((1, 3, 256, 256)).astype(np.float32)})
data = [np.random.random((1, 3, 256, 256)).astype(np.float32) for _ in range(50)]
start = time.time()
for i in range(50):
result = sess.run(out_names, {"img": data[i]})
end = time.time()
avg_time = (end - start) / 50.0
print(f'resnet 50 infer with hd630 use onnx directml fps is :{1 / avg_time}')
del sess
# --------------------------fp32 end----------------------------
# --------------------------fp16 start----------------------------
ops = rt.SessionOptions()
ops.enable_mem_pattern = False
sess = rt.InferenceSession("bres256_core_sim_fp16.onnx", sess_options=ops, providers=[('DmlExecutionProvider', {
'device_id': 0,
})])
out_names = [each.name for each in sess.get_outputs()]
result = sess.run(out_names, {"img": np.random.random((1, 3, 256, 256)).astype(np.float16)})
data = [np.random.random((1, 3, 256, 256)).astype(np.float16) for _ in range(50)]
start = time.time()
for i in range(50):
result = sess.run(out_names, {"img": data[i]})
end = time.time()
avg_time = (end - start) / 50.0
print(f'resnet 50 infer with hd630 use onnx directml fp16 fps is :{1 / avg_time}')
del sess
# --------------------------fp16 end----------------------------
# use nvidia 940M gpu *************************************************************************************************
# --------------------------fp32 start----------------------------
ops = rt.SessionOptions()
ops.enable_mem_pattern = False
sess = rt.InferenceSession("bres256_core_sim.onnx", sess_options=ops, providers=[('DmlExecutionProvider', {
'device_id': 1,
})])
out_names = [each.name for each in sess.get_outputs()]
result = sess.run(out_names, {"img": np.random.random((1, 3, 256, 256)).astype(np.float32)})
data = [np.random.random((1, 3, 256, 256)).astype(np.float32) for _ in range(50)]
start = time.time()
for i in range(50):
result = sess.run(out_names, {"img": data[i]})
end = time.time()
avg_time = (end - start) / 50.0
print(f'resnet 50 infer with nvidia 940M use onnx directml fps is :{1 / avg_time}')
del sess
# --------------------------fp32 end----------------------------
# --------------------------fp16 start----------------------------
ops = rt.SessionOptions()
ops.enable_mem_pattern = False
sess = rt.InferenceSession("bres256_core_sim_fp16.onnx", sess_options=ops, providers=[('DmlExecutionProvider', {
'device_id': 1,
})])
out_names = [each.name for each in sess.get_outputs()]
result = sess.run(out_names, {"img": np.random.random((1, 3, 256, 256)).astype(np.float16)})
data = [np.random.random((1, 3, 256, 256)).astype(np.float16) for _ in range(50)]
start = time.time()
for i in range(50):
result = sess.run(out_names, {"img": data[i]})
end = time.time()
avg_time = (end - start) / 50.0
print(f'resnet 50 infer with nvidia 940M use onnx directml fp16 fps is :{1 / avg_time}')
del sess
# --------------------------fp16 end----------------------------