-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_inference.py
64 lines (52 loc) · 1.7 KB
/
run_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
52
53
54
55
56
57
58
59
60
61
62
63
64
#
# Mobilenet_v1 inference accelerated with ArmNN
#
import numpy as np
import tflite_runtime.interpreter as tflite
from PIL import Image
# ArmNN supports running TFLite models on both Arm Cortex-A CPUs and Arm Mali GPUs
#
# GpuAcc: GPU accelerated backend, Uses the GPU compute units for model inferencing
# -> note: there is a generally longer initialization delay on the GPU but execution times will be shorter
#
# CpuAcc: Cpu Accelerated backend, Uses CPU optimized model inferencing
# -> note: there is a shorter initialization delay on the CPU but execution times can vary
#
# CpuRef: Reference backend for running on the CPU, extremely slow
#
# Set Backend variable based on preference priority
#
# Preferred Backends: "GpuAcc,CpuAcc,CpuRef"
BACKENDS = "GpuAcc"
# Set path to the TFLite experimental delegate Libraries
#
# Delegate path:
DELEGATE_PATH = "./libarmnnDelegate.so.29"
# Set path to the TFLite model
#
# Model path:
MODEL_PATH = "./mobilenet_v1_1.0_224_quant.tflite"
# Set path to the input image (for this example)
#
# Image path:
IMAGE_PATH = "./sample.png"
img = Image.open(IMAGE_PATH).resize((224, 224))
img = np.expand_dims(img, 0)
armnn_delegate = tflite.load_delegate(
library = DELEGATE_PATH,
options = {
"backends":BACKENDS,
"logging-severity": "info",
}
)
interpreter = tflite.Interpreter(
model_path = MODEL_PATH,
experimental_delegates = [armnn_delegate]
)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.set_tensor(input_details[0]["index"], img)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]["index"])
print(np.argmax(output_data))