-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
381 lines (324 loc) · 11.8 KB
/
app.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
"""
Copyright 2019 Lummetry.AI (Knowledge Investment Group SRL). All Rights Reserved.
* NOTICE: All information contained herein is, and remains
* the property of Knowledge Investment Group SRL.
* The intellectual and technical concepts contained
* herein are proprietary to Knowledge Investment Group SRL
* and may be covered by Romanian and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Knowledge Investment Group SRL.
@copyright: Lummetry.AI
@author: Lummetry.AI
@project:
@description:
"""
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import traceback
import itertools
import pandas as pd
import constants as ct
from app_monitor import ApplicationMonitor
from libraries_pub import LummetryObject
LBL_PERS = [ct.LBL_PERSON, ct.LBL_PERSOANA]
CLF_OBJECTS = ['apple', 'chair', 'mountain_bike', 'soccer_ball', 'tabby']
NR_PERS = {
'1.png' : 3,
'207.png' : 4,
'532.png' : 3,
'646.png' : 4,
'748.png' : 4
}
NR_RUN_INF = 3
__VER__ = '1.0.2'
class VaporBoxCheck(LummetryObject):
def __init__(self, log, **kwargs):
self.__version__ = __VER__
super().__init__(log=log, **kwargs)
return
def startup(self):
self._cfg_inf = self.log.load_json('inference.txt', verbose=False)
self._opencv_ok = False
self._pytorch_ok = False
self._tensorflow_ok = False
self._results = {
'SCRIPT_VER': [self.__version__],
'SYS_MEMORY': [],
'SYS_PLATFORM': [],
'GPU_NAME': [],
'GPU_MEMORY': [],
'OPENCV_VER': [],
'TH_VER': [],
'TF_VER': [],
'TH_GPU_TIME': [],
'TH_CPU_TIME': [],
'TF_GPU_TIME': [],
'TF_CPU_TIME': []
}
system, release, version = self.log.platform()
lst_gpus = self.log.gpu_info()
sys_memory = self.log.get_machine_memory()
self.app_monitor = ApplicationMonitor(log=self.log)
self._results['SYS_MEMORY'].append(sys_memory)
self._results['SYS_PLATFORM'].append(system)
if lst_gpus:
self._results['GPU_NAME'] = lst_gpus[0]['NAME']
self._results['GPU_MEMORY'] = lst_gpus[0]['TOTAL_MEM']
else:
self._results['GPU_NAME'].append('N/A')
self._results['GPU_MEMORY'].append('N/A')
return
def _load_images(self):
def _load(path_images):
lst_names = list(sorted(os.listdir(path_images)))
lst_paths = [os.path.join(path_images, x) for x in lst_names]
lst_imgs = [cv2.imread(x) for x in lst_paths]
return lst_names, lst_imgs
#enddef
try:
import cv2
if self.DEBUG:
self.log.p('Loading images classification')
path_images = os.path.join(
self.log.get_data_folder(),
self.config_data[ct.PATH_IMAGES_CLASSIFICATION]
)
lst_names, lst_imgs = _load(path_images)
self.dct_imgs_classification = dict(zip(lst_names, lst_imgs))
path_images = os.path.join(
self.log.get_data_folder(),
self.config_data[ct.PATH_IMAGES_DETECTION]
)
lst_names, lst_imgs = _load(path_images)
self.dct_imgs_detection = dict(zip(lst_names, lst_imgs))
if self.DEBUG:
self.log.p('{} classification images loaded'.format(len(self.dct_imgs_classification)))
self.log.p('{} detection images loaded'.format(len(self.dct_imgs_detection)))
except:
self.log.p('Images could not be loaded', 'r')
return
def _run_pytorch(self):
def _predict(th_graph):
timer_name = th_graph._timer_name(ct.TIMER_SESSION_RUN)
self.log.reset_timer(timer_name)
device = next(th_graph.model.parameters()).device
self.log.p('Pytorch model running on {}'.format(device.type.upper()))
#infer
lst_imgs = list(self.dct_imgs_classification.values())
self.log.p('Running inference ...')
for i in range(NR_RUN_INF):
dct_inf = th_graph.predict(lst_imgs)
#check results
lst_inf = dct_inf[ct.INFERENCES]
lst_classes = [x['TYPE'] for x in lst_inf]
lst = list(zip(lst_classes, CLF_OBJECTS))
s = ''
for i,x in enumerate(lst):
s+= '{}/{}'.format(x[0], x[1])
if i < len(lst) - 1:
s+= ', '
# self.log.p('Classification results: {}'.format(s))
nr_missed = len(set(CLF_OBJECTS) - set(lst_classes))
nr_identified = len(CLF_OBJECTS) - nr_missed
self.log.p(
str_msg='{}/{} images correctly classified'.format(nr_identified, len(self.dct_imgs_classification)),
color='g'
)
total_time = self.log.get_timing(timer_name)
timer = self.log.get_timing(timer_name)
total_time = timer['END'] - timer['START']
time_per_frame = total_time / len(self.dct_imgs_classification)
self.log.p(
'{:.4f}s per frame, {:.4f}s per batch ({})'.format(
time_per_frame,
total_time,
len(self.dct_imgs_classification)
),
color='g'
)
return total_time
try:
try:
import torch as th
self._results['TH_VER'].append(th.__version__)
except Exception as e:
self.log.p('Pytorch not loaded. Please check if Pytorch is configured \
in the current environment')
self.log.p('Pytorch tests cannot run on the current environment.')
self.log.p('Exception: {}'.format(str(e)), color='r')
return
#end try-except
from inference import PytorchGraph
self.log.p('Pytorch v{} working'.format(th.__version__))
#loading graph
th_graph = PytorchGraph(
log=self.log,
config_graph=self._cfg_inf[ct.PYTORCH]
)
device = next(th_graph.model.parameters()).device
device_type = device.type.upper()
device_name = 'GPU' if device_type == 'CUDA' else 'CPU'
self.log.p('Memory status before Pytorch run on {}'.format(device_name))
self.app_monitor.log_gpu_info()
total_time = _predict(th_graph)
self.log.p('Memory status after Pytorch run on {}'.format(device_name))
self.app_monitor.log_gpu_info()
if device.type.upper() == 'CUDA':
self._results['TH_GPU_TIME'].append(total_time)
else:
self._results['TH_CPU_TIME'].append(total_time)
self._results['TH_GPU_TIME'].append('N/A')
#endif
if device.type.upper() == 'CUDA':
th_graph.DEVICE = th.device('cpu')
th_graph.model.to(th_graph.DEVICE)
self.log.p('Memory status before Pytorch run on CPU')
self.app_monitor.log_gpu_info()
total_time = _predict(th_graph)
self.log.p('Memory status after Pytorch run on CPU')
self.app_monitor.log_gpu_info()
self._results['TH_CPU_TIME'].append(total_time)
#endif
del th_graph.model
del th_graph
import gc
gc.collect()
th.cuda.empty_cache()
self._pytorch_ok = True
except:
str_e = traceback.format_exc()
self.log.p(
'Exception encountered in Pytorch step: {}'.format(str_e),
color='r'
)
return
def _run_tensorflow(self):
def _predict(tf_graph):
timer_name = tf_graph._timer_name(ct.TIMER_SESSION_RUN)
self.log.reset_timer(timer_name)
#infer
lst_imgs = list(self.dct_imgs_detection.values())
self.log.p('Running inference ...')
for i in range(NR_RUN_INF):
dct_inf = tf_graph.predict(lst_imgs)
#check results
lst_inf = dct_inf[ct.INFERENCES]
lst_inf = list(itertools.chain.from_iterable(lst_inf))
lst_pers = list(filter(lambda x: x[ct.TYPE] in LBL_PERS, lst_inf))
self.log.p(
str_msg='{} / {} persons detected'.format(
len(lst_pers), sum(NR_PERS.values())
),
color='g'
)
timer = self.log.get_timing(timer_name)
total_time = timer['END'] - timer['START']
time_per_frame = total_time / len(self.dct_imgs_detection)
self.log.p(
str_msg='{:.4f}s per frame, {:.4f}s per batch ({})'.format(
time_per_frame,
total_time,
len(self.dct_imgs_detection)
),
color='g'
)
return total_time
try:
try:
import tensorflow as tf
# tf.debugging.experimental.enable_dump_debug_info('dump.txt')
major = int(tf.__version__[0])
minor = int(tf.__version__[2])
assert major == 2 and int(minor) >= 0, 'Environment needs tensorflow >= 2.1.0, found: {}'.format(tf.__version__)
self._results['TF_VER'].append(tf.__version__)
except Exception as e:
self.log.p('Tensorflow not loaded. Please check if Tensorflow is \
configured in the current environment')
self.log.p('Tensorflow tests cannot run on the current environment. Exiting.')
self.log.p('Exception: {}'.format(str(e)), color='r')
return
#end try-except
from inference import TensorflowGraph
self.log.p('Tensorflow v{} working'.format(tf.__version__))
#loading graph
lst_gpus = self.log.get_gpu()
has_gpu = len(lst_gpus) > 0
if has_gpu:
tf_graph = TensorflowGraph(
log=self.log,
config_graph=self._cfg_inf[ct.TENSORFLOW],
on_gpu=True
)
self.log.p('Tensorflow model running on GPU')
self.log.p('Memory status before Tensorflow run on GPU')
self.app_monitor.log_gpu_info()
total_time = _predict(tf_graph)
self.log.p('Memory status after Tensorflow run on GPU')
self.app_monitor.log_gpu_info()
self._results['TF_GPU_TIME'].append(total_time)
else:
self._results['TF_GPU_TIME'].append('N/A')
#endif
tf_graph = TensorflowGraph(
log=self.log,
config_graph=self._cfg_inf[ct.TENSORFLOW],
on_gpu=False
)
self.log.p('Tensorflow model running on CPU')
self.log.p('Memory status before Tensorflow run on CPU')
self.app_monitor.log_gpu_info()
total_time = _predict(tf_graph)
self.log.p('Memory status after Tensorflow run on CPU')
self.app_monitor.log_gpu_info()
self._results['TF_CPU_TIME'].append(total_time)
self._tensorflow_ok = True
except:
str_e = traceback.format_exc()
self.log.p(
str_msg='Exception encountered in Tensorflow step: {}'.format(str_e),
color='r'
)
return
def _check_opencv(self):
try:
import cv2
self._results['OPENCV_VER'].append(cv2.__version__)
self._opencv_ok = True
self.log.p('OpenCV v{} working'.format(cv2.__version__))
except:
self.log.p('OpenCV not found.', color='r')
return
def run(self):
self._check_opencv()
if self._opencv_ok:
self._load_images()
self._run_pytorch()
self._run_tensorflow()
if all([self._opencv_ok, self._pytorch_ok, self._tensorflow_ok]):
self.log.p(
str_msg='Environment is properly functioning, please send ' +
'the following log file to Lummetry Team: {}'.format(self.log.log_file),
color='g'
)
df = pd.DataFrame(self._results)
fn = os.path.basename(self.log.log_file) + '.csv'
full_path = os.path.join(self.log.get_output_folder(), fn)
self.log.save_dataframe(
df=df,
fn=fn,
folder='output'
)
self.log.p(
str_msg='Results obtained for the current run can be found in: {}'.format(full_path),
color='g'
)
else:
self.log.p(
str_msg='Environment not properly functioning, please send ' +
'the following log file to Lummetry Team: {}'.format(self.log.log_file),
color='r'
)
#endif
return