/
basic_benchmark.py
48 lines (31 loc) · 1.24 KB
/
basic_benchmark.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
import argparse
import numpy as np
from time import monotonic
import tflite_runtime.interpreter as tflite
parser = argparse.ArgumentParser('benchmark_main.py',
description='Profiles movenet inference on Google Coral USB accelerator.')
parser.add_argument('model_path', help='provide complete path to a ...edgetpu.tflite model file')
args = parser.parse_args()
print('loading delegate')
delegate = tflite.load_delegate('libedgetpu.so.1', {})
modelPath = args.model_path
print(f'loading model {modelPath}')
interpreter = tflite.Interpreter(model_path=modelPath,
experimental_delegates=[delegate])
interpreter.allocate_tensors()
inputInfo = interpreter.get_input_details()
shape = inputInfo[0]['shape']
print(f'creating random input data of shape: {shape}')
input = (np.random.rand(*shape) * 255).astype(np.uint8)
interpreter.set_tensor(inputInfo[0]['index'], input)
nWarmup = 10
nMeasure = 100
print(f'running {nWarmup} warmup cycles')
for i in range(nWarmup):
interpreter.invoke()
print(f'running {nMeasure} measurement cycles')
start = monotonic()
for i in range(nMeasure):
interpreter.invoke()
print(f'average cycle time: {(monotonic() - start) / nMeasure}')
del interpreter