-
Notifications
You must be signed in to change notification settings - Fork 514
/
speed_cpu.py
39 lines (28 loc) · 890 Bytes
/
speed_cpu.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
# coding: utf-8
__author__ = 'cleardusk'
import timeit
import numpy as np
SETUP_CODE = '''
import os
os.environ["OMP_NUM_THREADS"] = "4"
import numpy as np
import onnxruntime
onnx_fp = "weights/mb1_120x120.onnx" # if not existed, convert it, see "convert_to_onnx function in utils/onnx.py"
session = onnxruntime.InferenceSession(onnx_fp, None)
img = np.random.randn(1, 3, 120, 120).astype(np.float32)
'''
TEST_CODE = '''
session.run(None, {"input": img})
'''
def main():
repeat, number = 5, 100
res = timeit.repeat(setup=SETUP_CODE,
stmt=TEST_CODE,
repeat=repeat,
number=number)
res = np.array(res, dtype=np.float32)
res /= number
mean, var = np.mean(res), np.std(res)
print('Inference speed: {:.2f}±{:.2f} ms'.format(mean * 1000, var * 1000))
if __name__ == '__main__':
main()