-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathtracking_helper.py
439 lines (367 loc) · 16.2 KB
/
tracking_helper.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
''' Details
Author: Zhipeng Zhang ([email protected])
Function: supporting functions during tracking phase
Data: 2021.6.23
'''
import os
import os.path as osp
import cv2
import math
import torch
import torch.nn.functional as F
import numpy as np
from .box_helper import *
from .log_helper import logger
import pdb
# ----------------------------- MOT -------------------------------
def parser_mot_train_data(cfg):
"""
parser training and validation data
:param cfg:
:return:
"""
mode = cfg.TRAIN.DATASET.WHICH_MODE
train_use, val_use = cfg.TRAIN.DATASET.CONFIG[mode].TRAIN_USE, cfg.TRAIN.DATASET.CONFIG[mode].VAL_USE
train_set, val_set = dict(), dict()
cur_path = osp.dirname(__file__)
for data in train_use:
train_set[data] = osp.join(cur_path, '../dataset/mot_imgs/{}'.format(data.replace('_', '.')))
for data in val_use:
val_set[data] = osp.join(cur_path, '../dataset/mot_imgs/{}'.format(data.replace('_', '.')))
return train_set, val_set
def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True):
"""
Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232
:param img:
:param new_shape:
:param color:
:param auto:
:param scaleFill:
:param scaleup:
:return:
"""
shape = img.shape[:2] # current shape [height, width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
# Scale ratio (new / old)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleup: # only scale down, do not scale up (for better test mAP)
r = min(r, 1.0)
# Compute padding
ratio = r, r # width, height ratios
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
if auto: # minimum rectangle
dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
elif scaleFill: # stretch
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
dw /= 2 # divide padding into 2 sides
dh /= 2
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
return img, ratio, (dw, dh)
def letterbox_jde(img, height=608, width=1088, color=(127.5, 127.5, 127.5)):
"""
resize and pad a image to network input size
:param img:
:param height: height for network input
:param width: width for network input
:param color:
:return:
"""
shape = img.shape[:2] # shape = [height, width]
ratio = min(float(height) / shape[0], float(width) / shape[1])
new_shape = (round(shape[1] * ratio), round(shape[0] * ratio)) # new_shape = [width, height]
dw = (width - new_shape[0]) / 2 # width padding
dh = (height - new_shape[1]) / 2 # height padding
top, bottom = round(dh - 0.1), round(dh + 0.1)
left, right = round(dw - 0.1), round(dw + 0.1)
img = cv2.resize(img, new_shape, interpolation=cv2.INTER_AREA) # resized, no border
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # padded rectangular
return img, ratio, dw, dh
def scale_img(img, ratio=1.0, same_shape=False): # img(16,3,256,416), r=ratio
# scales img(bs,3,y,x) by ratio
if ratio == 1.0:
return img
else:
h, w = img.shape[2:]
s = (int(h * ratio), int(w * ratio)) # new size
img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize
if not same_shape: # pad/crop img
gs = 32 # (pixels) grid size
h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)]
return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean
def update_cstrack_hypers(opt, args, config):
"""
update hyper-parameters of mot models
:param opt: edict, output
:param args: script arg-parser input
:param config: .yaml file configures (experiments/xx.yaml)
:return: opt
"""
opt.args = args
opt.cfg = config
# copy all keys and values of cfg to opt
for k, v in config.items():
opt[k] = v
cfg_hypers = config.TEST.COMMON_HYPER if args.benchmark is None else config.TEST[args.benchmark] # special benchmarks in .yaml file
opt.nms_thres = args.nms_thres if args.nms_thres is not None else cfg_hypers.nms_thres
opt.conf_thres = args.conf_thres if args.conf_thres is not None else cfg_hypers.conf_thres
opt.track_buffer = args.track_buffer if args.track_buffer is not None else cfg_hypers.track_buffer
opt.min_box_area = args.min_box_area if args.min_box_area is not None else cfg_hypers.min_box_area
opt.img_size = args.img_size if args.img_size is not None else tuple(cfg_hypers.img_size)
opt.mean = args.mean if args.mean is not None else cfg_hypers.mean
opt.std = args.std if args.std is not None else cfg_hypers.std
return opt
def get_mot_benchmark_path(opt):
curr_path = osp.realpath(osp.dirname(__file__))
if opt.args.val_mot15:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot15_train.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT15/train')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT15/images/train')
benchmark_name = 'MOT15'
elif opt.args.test_mot15:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot15_test.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT15/test')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT15/images/test')
benchmark_name = 'MOT15'
elif opt.args.val_mot16: # training sequences
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot16_train.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT16/train')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT16/images/train')
benchmark_name = 'MOT16'
elif opt.args.test_mot16:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot16_test.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT16/test')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT16/images/test')
benchmark_name = 'MOT16'
elif opt.args.val_mot17:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot17_train.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT17/train')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT17/images/train')
benchmark_name = 'MOT17'
elif opt.args.test_mot17:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot17_test.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT17/test')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT17/images/test')
benchmark_name = 'MOT17'
elif opt.args.val_mot20:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot20_train.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT20/train')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT20/images/train')
benchmark_name = 'MOT20'
elif opt.args.test_mot20:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', 'mot20_test.txt')).readlines()
data_root = osp.join(opt.args.data_dir, 'MOT20/test')
if not osp.isdir(data_root): data_root = os.path.join(opt.args.data_dir, 'MOT20/images/test')
benchmark_name = 'MOT20'
else:
seqs = open(osp.join(curr_path, '../dataset/mot_videos', '{}.txt').format(opt.args.benchmark)).readlines()
data_root = osp.join(opt.args.data_dir, opt.args.benchmark)
benchmark_name = opt.args.benchmark
logger.info('testing videos: '.format(seqs))
logger.info('data path: '.format(data_root))
return seqs, data_root, benchmark_name
# ----------------------------- SOT -------------------------------
def siam_crop(crop_input, mode='torch', pysot_crop=False):
"""
cropping image for tracking in Siamese framework
"""
im, pos, model_sz, original_sz, avg_chans = crop_input['image'], crop_input['pos'], crop_input['model_sz'], \
crop_input['original_sz'], crop_input['avg_chans']
if len(im.shape) == 2:
mask_format = True
else:
mask_format = False
crop_info = dict()
if isinstance(pos, float):
pos = [pos, pos]
sz = original_sz
im_sz = im.shape
c = (original_sz+1) / 2
if pysot_crop:
context_xmin = np.floor(pos[0] - c + 0.5)
context_ymin = np.floor(pos[1] - c + 0.5)
else:
context_xmin = round(pos[0] - c)
context_ymin = round(pos[1] - c)
context_xmax = context_xmin + sz - 1
context_ymax = context_ymin + sz - 1
left_pad = int(max(0., -context_xmin))
top_pad = int(max(0., -context_ymin))
right_pad = int(max(0., context_xmax - im_sz[1] + 1))
bottom_pad = int(max(0., context_ymax - im_sz[0] + 1))
context_xmin = context_xmin + left_pad
context_xmax = context_xmax + left_pad
context_ymin = context_ymin + top_pad
context_ymax = context_ymax + top_pad
if not mask_format:
r, c, k = im.shape
if any([top_pad, bottom_pad, left_pad, right_pad]):
te_im = np.zeros((r + top_pad + bottom_pad, c + left_pad + right_pad, k), np.uint8)
# for return mask
tete_im = np.zeros((r + top_pad + bottom_pad, c + left_pad + right_pad))
te_im[top_pad:top_pad + r, left_pad:left_pad + c, :] = im
if top_pad:
te_im[0:top_pad, left_pad:left_pad + c, :] = avg_chans
if bottom_pad:
te_im[r + top_pad:, left_pad:left_pad + c, :] = avg_chans
if left_pad:
te_im[:, 0:left_pad, :] = avg_chans
if right_pad:
te_im[:, c + left_pad:, :] = avg_chans
im_patch_original = te_im[int(context_ymin):int(context_ymax + 1), int(context_xmin):int(context_xmax + 1),:]
else:
tete_im = np.zeros(im.shape[0:2])
im_patch_original = im[int(context_ymin):int(context_ymax + 1), int(context_xmin):int(context_xmax + 1), :]
if not np.array_equal(model_sz, original_sz):
im_patch = cv2.resize(im_patch_original, (model_sz, model_sz))
else:
im_patch = im_patch_original
else:
r, c = im.shape
if any([top_pad, bottom_pad, left_pad, right_pad]):
te_im = np.zeros((r + top_pad + bottom_pad, c + left_pad + right_pad), np.uint8)
# for return mask
tete_im = np.zeros((r + top_pad + bottom_pad, c + left_pad + right_pad))
te_im[top_pad:top_pad + r, left_pad:left_pad + c] = im
if top_pad:
te_im[0:top_pad, left_pad:left_pad + c] = 0
if bottom_pad:
te_im[r + top_pad:, left_pad:left_pad + c] = 0
if left_pad:
te_im[:, 0:left_pad] = 0
if right_pad:
te_im[:, c + left_pad:] = 0
im_patch_original = te_im[int(context_ymin):int(context_ymax + 1), int(context_xmin):int(context_xmax + 1)]
else:
tete_im = np.zeros(im.shape[0:2])
im_patch_original = im[int(context_ymin):int(context_ymax + 1), int(context_xmin):int(context_xmax + 1)]
if not np.array_equal(model_sz, original_sz):
im_patch = cv2.resize(im_patch_original, (model_sz, model_sz))
else:
im_patch = im_patch_original
crop_info['crop_cords'] = [context_xmin, context_xmax, context_ymin, context_ymax]
crop_info['empty_mask'] = tete_im
crop_info['pad_info'] = [top_pad, left_pad, r, c]
if mode == 'torch':
crop_output = {'image_tensor': im_to_torch(im_patch.copy()), 'meta_info': crop_info}
else:
crop_output = {'image_tensor': im_patch, 'meta_info': crop_info}
return crop_output
def siamfc_pyramid_crop(inputs):
"""
crop siamfc pyramid images
"""
im, pos, in_side_scaled, out_side, avg_chans = inputs['image'], inputs['pos'], inputs['scaled_instance'], \
inputs['instance_size'], inputs['avg_chans']
in_side_scaled = [round(x) for x in in_side_scaled]
num_scale = len(in_side_scaled)
pyramid = torch.zeros(num_scale, 3, out_side, out_side)
max_target_side = in_side_scaled[-1]
min_target_side = in_side_scaled[0]
beta = out_side / min_target_side
search_side = round(beta * max_target_side)
crop_input = {'image': im, 'pos': pos, 'model_sz': int(search_side),
'original_sz': int(max_target_side),
'avg_chans': avg_chans}
out1 = siam_crop(crop_input, mode='numpy')
search_region = out1['image_tensor']
for s, temp in enumerate(in_side_scaled):
target_side = round(beta * temp)
crop_input = {'image': search_region, 'pos': (1 + search_side) / 2, 'model_sz': out_side,
'original_sz': target_side,
'avg_chans': avg_chans}
temp = siam_crop(crop_input)
pyramid[s, :] = temp['image_tensor']
crop_output = {'image_tensor': pyramid, 'meta_info': None}
return crop_output
def im_to_torch(img):
"""
numpy image to pytorch tensor
"""
img = np.transpose(img, (2, 0, 1)) # C*H*W
img = torch.from_numpy(img).float()
return img
def python2round(f):
"""
use python2 round function in python3
"""
if round(f + 1) - round(f) != 1:
return f + abs(f) / f * 0.5
return round(f)
def generate_psedou_mask(target_pos, target_sz, img_sz):
"""
generate psedou mask for OceanPlus and AutoMatch
where we set the pixel in bbox as 1, outside that as 0
"""
img_h, img_w = img_sz
cx, cy = target_pos
w, h = target_sz
x1, y1, x2, y2 = cx - w / 2, cy - h / 2, cx + w / 2, cy + h / 2
x1, y1, x2, y2 = int(x1), int(y1), math.ceil(x2 + 1), math.ceil(y2 + 1)
mask = np.zeros((img_sz[0], img_sz[1])) # h,w
mask[y1:y2, x1:x2] = 1
return mask
def get_bbox(s_z, p, tsz):
"""
map the GT bounding box in the first frame to template (127*127)
"""
exemplar_size = p.exemplar_size
scale_z = exemplar_size / s_z
w, h = tsz[0], tsz[1]
imh, imw = p.exemplar_size, p.exemplar_size
w = w*scale_z
h = h*scale_z
cx, cy = imw//2, imh//2
bbox = center2corner([cx, cy, w, h])
return bbox # [x1, y1, x2, y2]
def jitter_shift():
"""
jitter box (for zoom trick in AutoMatch)
"""
add = np.array([4, 8, 12, 16]).astype(np.float)
minus = -1 * add
add2 = add.reshape(4, 1).repeat(2, axis=-1)
minus2 = minus.reshape(4, 1).repeat(2, axis=1)
if True:
shift = np.zeros((96, 4))
# settle (x1, y1) change (x2, y2)
shift[0:4, 2] += add
shift[4:8, 2] += minus
shift[8:12, 3] += add
shift[12:16, 3] += minus
shift[16:20, 2:4] += add2
shift[20:24, 2:4] += minus2
# settle (x2, y1) change (x1, y2)
shift[24:28, 0] += add
shift[28:32, 0] += minus
shift[32:36, 3] += add
shift[36:40, 3] += minus
shift[40:44, 0] += add
shift[40:44, 3] += add
shift[44:48, 0] += minus
shift[44:48, 3] += minus
# settle (x2, y2) change (x1, y1)
shift[48:52, 0] += add
shift[52:56, 0] += minus
shift[56:60, 1] += add
shift[60:64, 1] += minus
shift[64:68, 0:2] += add2
shift[68:72, 0:2] += minus2
# settle (x1, y2) change (x2, y1)
shift[72:76, 2] += add
shift[76:80, 2] += minus
shift[80:84, 1] += add
shift[84:88, 1] += minus
shift[88:92, 1:3] += add2
shift[92:96, 1:3] += minus2
return shift
def bbox_clip(x, min_value, max_value):
new_x = max(min_value, min(x, max_value))
return new_x