-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon_tpu.py
More file actions
146 lines (115 loc) · 4.08 KB
/
Copy pathcommon_tpu.py
File metadata and controls
146 lines (115 loc) · 4.08 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import tensorflow as tf
from tensorflow import keras
import numpy as np
import os
import sys
from pathlib import Path
def output_tensor(interpreter, i):
"""Gets a model's ith output tensor.
Args:
interpreter: The ``tf.lite.Interpreter`` holding the model.
i (int): The index position of an output tensor.
Returns:
The output tensor at the specified position.
"""
return interpreter.tensor(interpreter.get_output_details()[i]['index'])()
def input_details(interpreter, key):
"""Gets a model's input details by specified key.
Args:
interpreter: The ``tf.lite.Interpreter`` holding the model.
key (int): The index position of an input tensor.
Returns:
The input details.
"""
return interpreter.get_input_details()[0][key]
def input_tensor(interpreter):
"""Gets a model's input tensor view as numpy array of shape (height, width, 3).
Args:
interpreter: The ``tf.lite.Interpreter`` holding the model.
Returns:
The input tensor view as :obj:`numpy.array` (height, width, 3).
"""
tensor_index = input_details(interpreter, 'index')
return interpreter.tensor(tensor_index)()#[0]
def set_interpreter_input_double(interpreter, a, b):
"""Copies data to a model's input tensor.
Args:
interpreter: The ``tf.lite.Interpreter`` to update.
data: The input tensor.
"""
input_details = interpreter.get_input_details()
interpreter.set_tensor(input_details[0]['index'], a)
interpreter.set_tensor(input_details[1]['index'], b)
def set_interpreter_input_single(interpreter, data):
"""Copies data to a model's input tensor.
Args:
interpreter: The ``tf.lite.Interpreter`` to update.
data: The input tensor.
"""
input_details = interpreter.get_input_details()
interpreter.set_tensor(input_details[0]['index'], data)
def create_interpreter(model_file, cpu=True, device=":0"):
"""
Pablo's code (with some simplifications).
Returns the interpreter with the loaded model from the file.
Args:
model_file: The (.tflite) file with the model
cpu: Whether to use CPU or TPU interpreter
device: Which TPU to use. If CPU flag is true, this is ignored.
Returns:
interpreter: The interpreter created for CPU or TPU.
"""
if cpu:
interpreter = tf.lite.Interpreter(model_file,experimental_op_resolver_type=tf.lite.experimental.OpResolverType.
BUILTIN_REF,)
else:
from pycoral.utils.edgetpu import make_interpreter
interpreter = make_interpreter(model_file, device=device)
return interpreter
def run_inference(interpreter, input_data, additional_data=None):
"""
Simple wrapper function to process some input and return only the output data.
Args:
interpreter: The CPU or TPU interpreter with the model loaded.
input_data: The tensor with the input data to be fed into the model.
Returns:
output_data: The tensor with the output data from model inference.
"""
if additional_data is None:
set_interpreter_input_single(interpreter, input_data)
else:
set_interpreter_input_double(interpreter, input_data, additional_data)
interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output_data = interpreter.tensor(output_details['index'])()
return output_data
def load_model(*args, **kwargs):
"""
Simple wrapper to load a model from file and allocate tensors to interpreter.
Args:
See the args for create_interpreter()
Returns:
interpreter: The interpreter created for CPU or TPU (after allocating tensors).
"""
interpreter = create_interpreter(*args, **kwargs)
interpreter.allocate_tensors()
return interpreter
def load_data(file_path):
np_data = np.load(file_path, allow_pickle=True)
data = tf.convert_to_tensor(np_data)
return data
def save_data(data, file_path):
try:
data = data.numpy()
#if already numpy array
except AttributeError:
pass
dir_path = Path(file_path).parent
dir_path.mkdir(exist_ok=True, parents=True)
np.save(file_path, data, allow_pickle=True)
# alias
load_input_data = load_data
load_golden = load_data
load_tokens = load_data
save_golden = save_data