13
13
from .tracklet import Tracklet , Tracklet_w_reid
14
14
from .matching import *
15
15
16
- from .reid_models .OSNet import *
17
- from .reid_models .load_model_tools import load_pretrained_weights
18
- from .reid_models .deepsort_reid import Extractor
19
-
20
- from .camera_motion_compensation import GMC
21
-
22
- REID_MODEL_DICT = {
23
- 'osnet_x1_0' : osnet_x1_0 ,
24
- 'osnet_x0_75' : osnet_x0_75 ,
25
- 'osnet_x0_5' : osnet_x0_5 ,
26
- 'osnet_x0_25' : osnet_x0_25 ,
27
- 'deepsort' : Extractor
28
- }
29
-
30
-
31
- def load_reid_model (reid_model , reid_model_path ):
32
-
33
- if 'osnet' in reid_model :
34
- func = REID_MODEL_DICT [reid_model ]
35
- model = func (num_classes = 1 , pretrained = False , )
36
- load_pretrained_weights (model , reid_model_path )
37
- model .cuda ().eval ()
38
-
39
- elif 'deepsort' in reid_model :
40
- model = REID_MODEL_DICT [reid_model ](reid_model_path , use_cuda = True )
16
+ # for reid
17
+ from .reid_models .engine import load_reid_model , crop_and_resize , select_device
41
18
42
- else :
43
- raise NotImplementedError
44
-
45
- return model
19
+ from .camera_motion_compensation .cmc import GMC
46
20
47
21
class BotTracker (object ):
48
22
def __init__ (self , args , frame_rate = 30 ):
@@ -59,60 +33,34 @@ def __init__(self, args, frame_rate=30):
59
33
60
34
self .motion = args .kalman_format
61
35
62
- self .with_reid = not args .discard_reid
36
+ self .with_reid = args .reid
63
37
64
- self .reid_model , self . crop_transforms = None , None
38
+ self .reid_model = None
65
39
if self .with_reid :
66
- self .reid_model = load_reid_model (args .reid_model , args .reid_model_path )
67
- self .crop_transforms = T .Compose ([
68
- # T.ToPILImage(),
69
- # T.Resize(size=(256, 128)),
70
- T .ToTensor (), # (c, 128, 256)
71
- T .Normalize (mean = [0.485 , 0.456 , 0.406 ], std = [0.229 , 0.224 , 0.225 ])
72
- ])
73
-
40
+ self .reid_model = load_reid_model (args .reid_model , args .reid_model_path , device = args .device )
41
+ self .reid_model .eval ()
74
42
75
43
# camera motion compensation module
76
44
self .gmc = GMC (method = 'orb' , downscale = 2 , verbose = None )
77
45
78
- def reid_preprocess (self , obj_bbox ):
79
- """
80
- preprocess cropped object bboxes
81
-
82
- obj_bbox: np.ndarray, shape=(h_obj, w_obj, c)
83
-
84
- return:
85
- torch.Tensor of shape (c, 128, 256)
86
- """
87
- obj_bbox = cv2 .resize (obj_bbox .astype (np .float32 ) / 255.0 , dsize = (128 , 128 )) # shape: (128, 256, c)
88
-
89
- return self .crop_transforms (obj_bbox )
46
+ # once init, clear all trackid count to avoid large id
47
+ BaseTrack .clear_count ()
90
48
49
+ @torch .no_grad ()
91
50
def get_feature (self , tlwhs , ori_img ):
92
51
"""
93
52
get apperance feature of an object
94
53
tlwhs: shape (num_of_objects, 4)
95
54
ori_img: original image, np.ndarray, shape(H, W, C)
96
55
"""
97
- obj_bbox = []
98
-
99
- for tlwh in tlwhs :
100
- tlwh = list (map (int , tlwh ))
101
- # if any(tlbr_ == -1 for tlbr_ in tlwh):
102
- # print(tlwh)
103
-
104
- tlbr_tensor = self .reid_preprocess (ori_img [tlwh [1 ]: tlwh [1 ] + tlwh [3 ], tlwh [0 ]: tlwh [0 ] + tlwh [2 ]])
105
- obj_bbox .append (tlbr_tensor )
106
-
107
- if not obj_bbox :
108
- return np .array ([])
109
-
110
- obj_bbox = torch .stack (obj_bbox , dim = 0 )
111
- obj_bbox = obj_bbox .cuda ()
112
-
113
- features = self .reid_model (obj_bbox ) # shape: (num_of_objects, feature_dim)
114
- return features .cpu ().detach ().numpy ()
115
56
57
+ if tlwhs .size == 0 :
58
+ return np .empty ((0 , 512 ))
59
+
60
+ crop_bboxes = crop_and_resize (tlwhs , ori_img , input_format = 'tlwh' , sz = (64 , 128 ))
61
+ features = self .reid_model (crop_bboxes ).cpu ().numpy ()
62
+
63
+ return features
116
64
117
65
def update (self , output_results , img , ori_img ):
118
66
"""
@@ -181,10 +129,13 @@ def update(self, output_results, img, ori_img):
181
129
ious_dists = iou_distance (tracklet_pool , detections )
182
130
ious_dists_mask = (ious_dists > 0.5 ) # high conf iou
183
131
132
+ # fuse detection conf into iou dist
133
+ if self .args .fuse_detection_score :
134
+ ious_dists = fuse_det_score (ious_dists , detections )
135
+
184
136
if self .with_reid :
185
137
# mixed cost matrix
186
138
emb_dists = embedding_distance (tracklet_pool , detections ) / 2.0
187
- raw_emb_dists = emb_dists .copy ()
188
139
emb_dists [emb_dists > 0.25 ] = 1.0
189
140
emb_dists [ious_dists_mask ] = 1.0
190
141
dists = np .minimum (ious_dists , emb_dists )
@@ -238,9 +189,12 @@ def update(self, output_results, img, ori_img):
238
189
ious_dists = iou_distance (unconfirmed , detections )
239
190
ious_dists_mask = (ious_dists > 0.5 )
240
191
192
+ # fuse detection conf into iou dist
193
+ if self .args .fuse_detection_score :
194
+ ious_dists = fuse_det_score (ious_dists , detections )
195
+
241
196
if self .with_reid :
242
197
emb_dists = embedding_distance (unconfirmed , detections ) / 2.0
243
- raw_emb_dists = emb_dists .copy ()
244
198
emb_dists [emb_dists > 0.25 ] = 1.0
245
199
emb_dists [ious_dists_mask ] = 1.0
246
200
dists = np .minimum (ious_dists , emb_dists )
0 commit comments