-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_npu_inference.py
52 lines (39 loc) · 1.24 KB
/
run_npu_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
#
# Sine model inference accelerated with vx_delegate
#
import numpy as np
import tflite_runtime.interpreter as tflite
import math
# Set path to the VX_delegate Libraries
#
# Delegate path:
DELEGATE_PATH = "./libvx_delegate.so"
# Set path to the TFLite model
#
# Model path:
MODEL_PATH = "./sine_model.tflite"
vx_delegate = tflite.load_delegate(
library = DELEGATE_PATH,
options={"logging-severity":"debug"}
)
interpreter = tflite.Interpreter(
model_path = MODEL_PATH,
experimental_delegates = [vx_delegate]
)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# print("Input details\n", input_details)
# print("Output details\n", output_details)
input_type = input_details[0]['dtype']
output_type = output_details[0]['dtype']
input_value = math.pi/2
np_features = np.array((input_value,))
np_features = np_features.astype(input_type)
np_features = np.expand_dims(np_features, axis=0)
interpreter.set_tensor(input_details[0]['index'], np_features)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])
prediction = output.astype(output_type)[0][0]
actual = math.sin(input_value)
print('Actual {}, Predicted {}'.format(actual, prediction))