forked from DL-Practise/YoloAll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_utils.py
408 lines (373 loc) · 12.4 KB
/
common_utils.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import sys
import os
import torch
import importlib
import numpy as np
import cv2
from collections import OrderedDict
from ruamel import yaml
import time
COCO_CLASSES = (
"person",
"bicycle",
"car",
"motorcycle",
"airplane",
"bus",
"train",
"truck",
"boat",
"traffic light",
"fire hydrant",
"stop sign",
"parking meter",
"bench",
"bird",
"cat",
"dog",
"horse",
"sheep",
"cow",
"elephant",
"bear",
"zebra",
"giraffe",
"backpack",
"umbrella",
"handbag",
"tie",
"suitcase",
"frisbee",
"skis",
"snowboard",
"sports ball",
"kite",
"baseball bat",
"baseball glove",
"skateboard",
"surfboard",
"tennis racket",
"bottle",
"wine glass",
"cup",
"fork",
"knife",
"spoon",
"bowl",
"banana",
"apple",
"sandwich",
"orange",
"broccoli",
"carrot",
"hot dog",
"pizza",
"donut",
"cake",
"chair",
"couch",
"potted plant",
"bed",
"dining table",
"toilet",
"tv",
"laptop",
"mouse",
"remote",
"keyboard",
"cell phone",
"microwave",
"oven",
"toaster",
"sink",
"refrigerator",
"book",
"clock",
"vase",
"scissors",
"teddy bear",
"hair drier",
"toothbrush",
)
COCO_COLORS = np.array(
[
0.000, 0.447, 0.741,
0.850, 0.325, 0.098,
0.929, 0.694, 0.125,
0.494, 0.184, 0.556,
0.466, 0.674, 0.188,
0.301, 0.745, 0.933,
0.635, 0.078, 0.184,
0.300, 0.300, 0.300,
0.600, 0.600, 0.600,
1.000, 0.000, 0.000,
1.000, 0.500, 0.000,
0.749, 0.749, 0.000,
0.000, 1.000, 0.000,
0.000, 0.000, 1.000,
0.667, 0.000, 1.000,
0.333, 0.333, 0.000,
0.333, 0.667, 0.000,
0.333, 1.000, 0.000,
0.667, 0.333, 0.000,
0.667, 0.667, 0.000,
0.667, 1.000, 0.000,
1.000, 0.333, 0.000,
1.000, 0.667, 0.000,
1.000, 1.000, 0.000,
0.000, 0.333, 0.500,
0.000, 0.667, 0.500,
0.000, 1.000, 0.500,
0.333, 0.000, 0.500,
0.333, 0.333, 0.500,
0.333, 0.667, 0.500,
0.333, 1.000, 0.500,
0.667, 0.000, 0.500,
0.667, 0.333, 0.500,
0.667, 0.667, 0.500,
0.667, 1.000, 0.500,
1.000, 0.000, 0.500,
1.000, 0.333, 0.500,
1.000, 0.667, 0.500,
1.000, 1.000, 0.500,
0.000, 0.333, 1.000,
0.000, 0.667, 1.000,
0.000, 1.000, 1.000,
0.333, 0.000, 1.000,
0.333, 0.333, 1.000,
0.333, 0.667, 1.000,
0.333, 1.000, 1.000,
0.667, 0.000, 1.000,
0.667, 0.333, 1.000,
0.667, 0.667, 1.000,
0.667, 1.000, 1.000,
1.000, 0.000, 1.000,
1.000, 0.333, 1.000,
1.000, 0.667, 1.000,
0.333, 0.000, 0.000,
0.500, 0.000, 0.000,
0.667, 0.000, 0.000,
0.833, 0.000, 0.000,
1.000, 0.000, 0.000,
0.000, 0.167, 0.000,
0.000, 0.333, 0.000,
0.000, 0.500, 0.000,
0.000, 0.667, 0.000,
0.000, 0.833, 0.000,
0.000, 1.000, 0.000,
0.000, 0.000, 0.167,
0.000, 0.000, 0.333,
0.000, 0.000, 0.500,
0.000, 0.000, 0.667,
0.000, 0.000, 0.833,
0.000, 0.000, 1.000,
0.000, 0.000, 0.000,
0.143, 0.143, 0.143,
0.286, 0.286, 0.286,
0.429, 0.429, 0.429,
0.571, 0.571, 0.571,
0.714, 0.714, 0.714,
0.857, 0.857, 0.857,
0.000, 0.447, 0.741,
0.314, 0.717, 0.741,
0.50, 0.5, 0
]
).astype(np.float32).reshape(-1, 3)
def del_all_model_zoo_modules():
alg_names = []
for dir in os.listdir('./model_zoo'):
if os.path.isdir(os.path.join('./model_zoo',dir)):
alg_names.append(str(dir))
# del path in sys.path
del_p = []
for p in sys.path:
for alg_name in alg_names:
if alg_name in p:
del_p.append(p)
break
for p in del_p:
sys.path.remove(p)
# del modeules
old_alg_names = []
all_keys = sys.modules.keys()
time.sleep(0.2)
for alg_name in all_keys:
if 'from' in str(sys.modules[alg_name]) and \
'model_zoo' in str(sys.modules[alg_name]) and \
'YoloAll' in str(sys.modules[alg_name]):
old_alg_names.append(alg_name)
if 'namespace' in str(sys.modules[alg_name]) and hasattr(sys.modules[alg_name], '__path__'):
module_path = str(sys.modules[alg_name].__path__)
if 'model_zoo' in module_path and \
'YoloAll' in module_path:
old_alg_names.append(alg_name)
for alg_name in old_alg_names:
del sys.modules[alg_name]
def add_one_model_path(alg_name):
sub_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'model_zoo/'+ alg_name)
#sys.path.append(sub_dir)
sys.path.insert(0, sub_dir)
def get_api_from_model(alg_name):
api = None
del_all_model_zoo_modules()
add_one_model_path(alg_name)
try:
api = importlib.import_module('model_zoo.%s.alg'%alg_name)
print('create api from', alg_name, 'success')
except ImportError as e:
print('create api from', alg_name, 'failed')
print('error:', str(e))
api = None
return api
def load_pre_train_ignore_name(net, pre_train):
if pre_train == '':
print('the pre_train is null, skip')
return
else:
print('the pre_train is %s' % pre_train)
new_dict = {}
pretrained_model = torch.load(pre_train, map_location=torch.device('cpu'))
pre_keys = pretrained_model.keys()
net_keys = net.state_dict().keys()
print('net keys len:%d, pretrain keys len:%d' % (len(net_keys), len(pre_keys)))
if len(net_keys) != len(pre_keys):
print('key lens not same, maybe the pytorch version for pretrain and net are difficent; use name load')
for key_net in net_keys:
strip_key_net = key_net.replace('module.', '')
if strip_key_net not in pre_keys:
print('op: %s not exist in pretrain, ignore' % (key_net))
new_dict[key_net] = net.state_dict()[key_net]
continue
else:
net_shape = str(net.state_dict()[key_net].shape).replace('torch.Size', '')
pre_shape = str(pretrained_model[strip_key_net].shape).replace('torch.Size', '')
if net.state_dict()[key_net].shape != pretrained_model[strip_key_net].shape:
print('op: %s exist in pretrain but shape difficenet(%s:%s), ignore' % (
key_net, net_shape, pre_shape))
new_dict[key_net] = net.state_dict()[key_net]
else:
print(
'op: %s exist in pretrain and shape same(%s:%s), load' % (key_net, net_shape, pre_shape))
new_dict[key_net] = pretrained_model[strip_key_net]
else:
for key_pre, key_net in zip(pretrained_model.keys(), net.state_dict().keys()):
if net.state_dict()[key_net].shape == pretrained_model[key_pre].shape:
new_dict[key_net] = pretrained_model[key_pre]
print('op: %s shape same, load weights' % (key_net))
else:
new_dict[key_net] = net.state_dict()[key_net]
print('op: %s:%s shape diffient(%s:%s), ignore weights' %
(key_net, key_pre,
str(net.state_dict()[key_net].shape).replace('torch.Size', ''),
str(pretrained_model[key_pre].shape).replace('torch.Size', '')))
net.load_state_dict(new_dict, strict=False)
def vis(img, boxes, scores, cls_ids, conf=0.5, class_names=COCO_CLASSES):
for i in range(len(boxes)):
box = boxes[i]
cls_id = int(cls_ids[i])
score = scores[i]
if score < conf:
continue
x0 = int(box[0])
y0 = int(box[1])
x1 = int(box[2])
y1 = int(box[3])
color = (COCO_COLORS[cls_id] * 255).astype(np.uint8).tolist()
text = '{}:{:.1f}%'.format(class_names[cls_id], score * 100)
txt_color = (0, 0, 0) if np.mean(COCO_COLORS[cls_id]) > 0.5 else (255, 255, 255)
font = cv2.FONT_HERSHEY_SIMPLEX
txt_size = cv2.getTextSize(text, font, 0.4, 1)[0]
cv2.rectangle(img, (x0, y0), (x1, y1), color, 2)
txt_bk_color = (COCO_COLORS[cls_id] * 255 * 0.7).astype(np.uint8).tolist()
cv2.rectangle(
img,
(x0, y0 + 1),
(x0 + txt_size[0] + 1, y0 + int(1.5*txt_size[1])),
txt_bk_color,
-1
)
cv2.putText(img, text, (x0, y0 + txt_size[1]), font, 0.4, txt_color, thickness=1)
return img
class AlgBase:
def __init__(self):
self.cfg_file = None
self.ignore_keys = []
def load_cfg(self):
print(self.cfg_file)
with open(self.cfg_file, "r", encoding='utf-8') as f:
self.cfg_info = yaml.round_trip_load(f)
def save_cfg(self):
with open(self.cfg_file, "w", encoding='utf-8') as f:
yaml.round_trip_dump(self.cfg_info, f, default_flow_style=False)
def get_model_cfg(self, model_name):
cfg_map = {}
for key in self.cfg_info[model_name].keys():
cfg_map[key] = {}
for sub_key in self.cfg_info[model_name][key].keys():
if sub_key not in self.ignore_keys:
cfg_map[key][sub_key] = self.cfg_info[model_name][key][sub_key]
return cfg_map
def put_model_cfg(self, model_name, cfg_map):
# the cfg_map is from qt widget , so they are all strings
# i need to write them to cfg.yaml by my self
#for key in cfg_map.keys():
# self.cfg_info[model_name][key] = cfg_map[key]
old_lines = None
with open(self.cfg_file, "r", encoding='utf-8') as f:
old_lines = f.readlines()
new_lines = []
into_model_flag = False
out_model_flag = False
key_back = None
for line in old_lines:
top_key = False if line.startswith(' ') else True
key_name = line.split(':')[0].strip('\r\n').replace(' ', '')
if key_name == model_name and into_model_flag is False:
into_model_flag = True
new_lines.append(line)
continue
elif into_model_flag and top_key:
out_model_flag = True
new_lines.append(line)
continue
'''
if into_model_flag and not out_model_flag and key_name in cfg_map.keys():
new_line = line.split(':')[0] + ': ' + cfg_map[key_name]
if '\r' in line:
new_line = new_line + '\r'
if '\n' in line:
new_line = new_line + '\n'
new_lines.append(new_line)
else:
new_lines.append(line)
'''
if into_model_flag and not out_model_flag:
if line.startswith(' ') and not line.startswith(' '):
key_back = key_name
new_lines.append(line)
elif line.startswith(' '):
if key_back in cfg_map.keys() and key_name in cfg_map[key_back].keys():
new_line = line.split(':')[0] + ': ' + cfg_map[key_back][key_name]
if '\r' in line:
new_line = new_line + '\r'
if '\n' in line:
new_line = new_line + '\n'
new_lines.append(new_line)
else:
new_lines.append(line)
else:
assert(False)
else:
new_lines.append(line)
with open(self.cfg_file, "w", encoding='utf-8') as f:
f.writelines(new_lines)
self.load_cfg()
def get_support_models(self):
model_list=[]
for key in self.cfg_info.keys():
if key != 'alg_info':
model_list.append(key)
return model_list
if __name__ == "__main__":
api = get_api_from_model('YoloFastest')
api = get_api_from_model('YoloV5')