-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathai.py
201 lines (162 loc) · 7.37 KB
/
ai.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright (c) 2020 PHYTEC Messtechnik GmbH
# SPDX-License-Identifier: Apache-2.0
import os
import time
import cv2
import tflite_runtime.interpreter as tflite
import numpy as np
import json
import concurrent.futures
class Ai:
def __init__(self, model_path, embeddings_path, modeltype='quant'):
self.model_path = model_path
self.embeddings_path = embeddings_path
self.modeltype = modeltype
self.width = 224
self.height = 224
def initialize(self):
start = time.time()
self.init_tflite()
print('Create Embeddigns')
with open(self.embeddings_path, 'r') as f:
embeddings_data = json.load(f)
data = embeddings_data['Embedding']
self.embeddings = [np.array(data[str(i)]) for i in range(len(data))]
data = embeddings_data['Name']
self.names = [np.array(data[str(i)]) for i in range(len(data))]
data = embeddings_data['File']
self.files = [np.array(data[str(i)]) for i in range(len(data))]
self.celeb_embeddings = self.split_data_frame(
self.embeddings,
int(np.ceil(len(self.embeddings)/4)))
print('Initialization done (duration: {})'.format(time.time() - start))
def run_inference(self, face, npu=True):
#Resize face
print('Resize face')
if face.shape > (self.width, self.height):
face = cv2.resize(face, (self.width, self.height),
interpolation=cv2.INTER_AREA)
elif face.shape < (self.width, self.height):
face = cv2.resize(face, (self.width, self.height),
interpolation=cv2.INTER_CUBIC)
print('Preprocess')
if self.modeltype is 'quant':
face = face.astype('float32')
samples = np.expand_dims(face, axis=0)
samples = self.preprocess_input(samples,
data_format='channels_last',
version=3).astype('int8')
else:
face = face.astype('float32')
samples = np.expand_dims(face, axis=0)
samples = self.preprocess_input(samples,
data_format='channels_last',
version=2)
output_data = self.run_tflite(samples, npu=npu)
print('Create EUdist')
start = time.time()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
result_1 = executor.submit(self.faceembedding, output_data,
np.array(self.celeb_embeddings[0]))
result_2 = executor.submit(self.faceembedding, output_data,
np.array(self.celeb_embeddings[1]))
result_3 = executor.submit(self.faceembedding, output_data,
np.array(self.celeb_embeddings[2]))
result_4 = executor.submit(self.faceembedding, output_data,
np.array(self.celeb_embeddings[3]))
EUdist = []
if result_1.done() & result_2.done() & result_3.done() & result_4.done():
EUdist.extend(result_1.result())
EUdist.extend(result_2.result())
EUdist.extend(result_3.result())
EUdist.extend(result_4.result())
idx = np.argpartition(EUdist, 5)
idx = idx[:5]
top5 = dict()
for id in idx:
top5[id] = [EUdist[id], self.names[id], self.files[id]]
top5 = {key: value for key, value in sorted(top5.items(), key=lambda item: item[1][0])}
print('EUdist duration: {}'.format(time.time() - start))
return top5
def init_tflite(self):
os.environ['VIV_VX_CACHE_BINARY_GRAPH_DIR'] = os.getcwd()
os.environ['VIV_VX_ENABLE_CACHE_GRAPH_BINARY'] = '1'
ext_delegate= '/usr/lib/libvx_delegate.so'
ext_delegate= [ tflite.load_delegate(ext_delegate)]
try:
self.cpu_interpreter = tflite.Interpreter(self.model_path)
self.npu_interpreter = tflite.Interpreter(self.model_path, experimental_delegates=ext_delegate)
except ValueError as e:
print('Failed to find model file: ' + str(e))
return
print('Allocate Tensors')
self.cpu_interpreter.allocate_tensors()
self.input_details = self.cpu_interpreter.get_input_details()
self.output_details = self.cpu_interpreter.get_output_details()
self.npu_interpreter.allocate_tensors()
self.input_details = self.npu_interpreter.get_input_details()
self.output_details = self.npu_interpreter.get_output_details()
def run_tflite(self, samples, npu):
print('Invoke TFlite')
start = time.time()
if npu:
interpreter = self.npu_interpreter
else:
interpreter = self.cpu_interpreter
interpreter.set_tensor(self.input_details[0]['index'], samples)
interpreter.invoke()
output_data = interpreter.get_tensor(
self.output_details[0]['index'])
print('Interpreter done ({})'.format(time.time() - start))
return output_data
def split_data_frame(self, df, chunk_size):
list_of_df = list()
number_chunks = len(df) // chunk_size + 1
for i in range(number_chunks):
list_of_df.append(df[i*chunk_size:(i+1)*chunk_size])
return list_of_df
def preprocess_input(self, x, data_format, version):
x_temp = np.copy(x)
assert data_format in {'channels_last', 'channels_first'}
if version == 1:
if data_format == 'channels_first':
x_temp = x_temp[:, ::-1, ...]
x_temp[:, 0, :, :] -= 93.5940
x_temp[:, 1, :, :] -= 104.7624
x_temp[:, 2, :, :] -= 129.1863
else:
x_temp = x_temp[..., ::-1]
x_temp[..., 0] -= 93.5940
x_temp[..., 1] -= 104.7624
x_temp[..., 2] -= 129.1863
elif version == 2:
if data_format == 'channels_first':
x_temp = x_temp[:, ::-1, ...]
x_temp[:, 0, :, :] -= 91.4953
x_temp[:, 1, :, :] -= 103.8827
x_temp[:, 2, :, :] -= 131.0912
else:
x_temp = x_temp[..., ::-1]
x_temp[..., 0] -= 91.4953
x_temp[..., 1] -= 103.8827
x_temp[..., 2] -= 131.0912
elif version == 3:
if data_format == 'channels_first':
x_temp = x_temp[:, ::-1, ...]
x_temp[:, 0, :, :] -= np.round(91.4953).astype('uint8')
x_temp[:, 1, :, :] -= np.round(103.8827).astype('uint8')
x_temp[:, 2, :, :] -= np.round(131.0912).astype('uint8')
else:
x_temp = x_temp[..., ::-1]
x_temp[..., 0] -= np.round(91.4953).astype('uint8')
x_temp[..., 1] -= np.round(103.8827).astype('uint8')
x_temp[..., 2] -= np.round(131.0912).astype('uint8')
else:
raise NotImplementedError
return x_temp
def faceembedding(self, face, celebdata):
dist = []
for i in range(len(celebdata)):
celebs = np.array(celebdata[i])
dist.append(np.linalg.norm(face - celebs))
return dist