Skip to content

Commit 356410c

Browse files
committed
change preprocess and postprocess
1 parent 3b803f1 commit 356410c

18 files changed

+217
-232
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ weights/*
33
!weights/ckpt.t7
44
!weights/DHN.pth
55
!weights/osnet_x0_25.pth
6+
visdrone/*
7+
uavdt/*
8+
mot17/*
9+
runs/*
10+
wandb/*
11+
track_result.txt
12+
.idea/
613

714
# Byte-compiled / optimized / DLL files
815
__pycache__/

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 0. 更新记录
44

5+
**2023.5.6[大更新]**: 对于v5, v7, 改变前处理和后处理方式(采用原有方式), ***解决了部分边界框近大远小的bug, 边界框更加精确***. 此外, 对于v8, 弃用了resize步骤, 直接推理.
6+
57
**2023.3.14**解决了`DeepSORT``C_BIoUTracker`后面出现的目标不跟踪的bug.
68

79
**2023.2.28**优化了`track_demo.py`, 减少了内存占用.
@@ -34,7 +36,7 @@ YOLO v8 代码的参数与之前完全相同. 安装YOLO v8以及训练步骤请
3436

3537
**2022.10.15**增加了对yolo v5的支持, 只需替换track.py, 将tracker文件夹放到v5的根目录(我测试的是官方的[repo](https://github.com/ultralytics/yolov5))下即可. 代码在[yolo v5](https://github.com/JackWoo0831/Yolov7-tracker/blob/master/tracker/track_yolov5.py).
3638

37-
**2022.09.27**修复了STrack类中update不更新外观的问题, 代码有较大更改, **您可能需要重新下载```./tracker```文件夹**.
39+
**2022.09.27[大更新]**修复了STrack类中update不更新外观的问题, 代码有较大更改, **您可能需要重新下载```./tracker```文件夹**.
3840
尝试加入StrongSORT, 但是目前还不work:(, 尽力调一调
3941

4042
## 1. 亮点
@@ -144,7 +146,7 @@ origin意味着您直接使用数据集原本的路径, **而不是通过yolo要
144146
DATA_ROOT的值应为/data/datasets/VisDrone2019/VisDrone2019-MOT-test-dev/sequences, 即DATA_ROOT目录下应该为各个视频序列的文件夹.
145147
```
146148

147-
***2. yolo***
149+
***2. yolo[推荐]***
148150

149151
yolo格式意味着您通过yolo训练时所要求的txt文件读取序列. 我们知道yolo要求txt文件记录图片的路径, 例如:
150152

data/uavdt.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
train: ./uavdt/train.txt
2+
val: ./uavdt/test.txt
3+
test: ./uavdt/test.txt
4+
5+
nc: 1
6+
7+
names: ['car']

run_yolov7.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ python train_aux.py --dataset visdrone --workers 8 --device 4 --batch-size 16 --
1717
## all category
1818
python train_aux.py --dataset visdrone --workers 8 --device 0 --batch-size 16 --data data/visdrone_all.yaml --img 1280 1280 --cfg cfg/training/yolov7-w6.yaml --weights './weights/yolov7-w6_training.pt' --name yolov7-w6-custom --hyp data/hyp.scratch.custom.yaml
1919

20+
uavdt:
21+
python train_aux.py --dataset uavdt --workers 8 --device 5 --batch-size 16 --data data/uavdt.yaml --img 1280 1280 --cfg cfg/training/yolov7-w6.yaml --weights './weights/yolov7-w6_training.pt' --name yolov7-w6-custom --hyp data/hyp.scratch.custom.yaml
2022

2123
## all category, no mosaic, rect training
2224
NOTE: edited augment = False in line 246, train_aux.py
@@ -61,4 +63,4 @@ CUDA_VISIBLE_DEVICES=0 python train_aux.py --dataset mot17 --workers 8 --device
6163
track:
6264
python tracker/track.py --dataset mot17 --data_format yolo --tracker sort --model_path runs/train/yolov7-w6-custom3/weights/best.pt --save_images
6365
python tracker/track.py --dataset mot17 --data_format yolo --tracker bytetrack --model_path weights/best.pt --save_images
64-
python tracker/track.py --dataset mot17 --data_format yolo --tracker deepsort --model_path weights/best.pt --save_images
66+
python tracker/track.py --dataset mot17 --data_format yolo --tracker deepsort --model_path weights/best.pt --save_images

tools/convert_UAVDT_to_yolo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def generate_imgs_and_labels(opts):
4747

4848
# 分别处理train与test
4949
process_train_test(train_seq_list, frame_range, CATEGOTY_ID, 'train')
50-
process_train_test(test_seq_list, frame_range, CATEGOTY_ID, 'test')
50+
process_train_test(test_seq_list, {'start': 0.0, 'end': 1.0}, CATEGOTY_ID, 'test')
5151
print('All Done!!')
5252

5353

@@ -156,4 +156,4 @@ def process_train_test(seqs: list, frame_range: dict, cat_id: int = 0, split: st
156156
opts = parser.parse_args()
157157

158158
generate_imgs_and_labels(opts)
159-
# python uav_tools/convert_UAVDT_to_yolo.py --generate_imgs --half --random
159+
# python tools/convert_UAVDT_to_yolo.py --generate_imgs --half --random

tracker/basetrack.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ def __init__(self, cls, tlwh, score, kalman_format='default',
107107
self.mean, self.cov = None, None # for kalman predict
108108

109109
# some tool funcs
110+
@staticmethod
111+
def tlbr2tlwh(tlbr):
112+
"""
113+
convert tlbr to tlwh
114+
"""
115+
result = np.asarray(tlbr).copy()
116+
result[2] -= result[0]
117+
result[3] -= result[1]
118+
119+
return result
120+
110121
@staticmethod
111122
def tlwh2xyah(tlwh):
112123
"""
@@ -375,26 +386,12 @@ def update(self, det_results, ori_img):
375386
"""step 1. filter results and init tracks"""
376387
det_results = det_results[det_results[:, 4] > self.det_thresh]
377388

378-
# convert the scale to origin size
379-
# NOTE: yolo v7 origin out format: [xc, yc, w, h, conf, cls0_conf, cls1_conf, ..., clsn_conf]
380-
# TODO: check here, if nesscessary use two ratio
381-
img_h, img_w = ori_img.shape[0], ori_img.shape[1]
382-
ratio = [img_h / self.model_img_size[0], img_w / self.model_img_size[1]] # usually > 1
383-
det_results[:, 0], det_results[:, 2] = det_results[:, 0]*ratio[1], det_results[:, 2]*ratio[1]
384-
det_results[:, 1], det_results[:, 3] = det_results[:, 1]*ratio[0], det_results[:, 3]*ratio[0]
385-
386389

387390
if det_results.shape[0] > 0:
388-
if self.NMS:
389-
# TODO: Note nms need tlbr format
390-
bbox_temp = STrack.xywh2tlbr(det_results[:, :4])
391-
nms_indices = nms(torch.from_numpy(bbox_temp), torch.from_numpy(det_results[:, 4]),
392-
self.opts.nms_thresh)
393-
det_results = det_results[nms_indices.numpy()]
394391

395392
# detections: List[Strack]
396-
detections = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
397-
for (cls, xywh, score) in zip(det_results[:, -1], det_results[:, :4], det_results[:, 4])]
393+
detections = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
394+
for (cls, tlbr, score) in zip(det_results[:, -1], det_results[:, :4], det_results[:, 4])]
398395

399396
else:
400397
detections = []

tracker/botsort.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -328,28 +328,13 @@ def update(self, det_results, ori_img):
328328
lost_stracks = [] # The tracks which are not obtained in the current frame but are not removed.(Lost for some time lesser than the threshold for removing)
329329
removed_stracks = []
330330

331-
# convert the scale to origin size
332-
# NOTE: yolo v7 origin out format: [xc, yc, w, h, conf, cls0_conf, cls1_conf, ..., clsn_conf]
333-
# TODO: check here, if nesscessary use two ratio
334-
img_h, img_w = ori_img.shape[0], ori_img.shape[1]
335-
ratio = [img_h / self.model_img_size[0], img_w / self.model_img_size[1]] # usually > 1
336-
det_results[:, 0], det_results[:, 2] = det_results[:, 0]*ratio[1], det_results[:, 2]*ratio[1]
337-
det_results[:, 1], det_results[:, 3] = det_results[:, 1]*ratio[0], det_results[:, 3]*ratio[0]
338-
339331
"""step 1. filter results and init tracks"""
340332

341333
# filter small area bboxs
342334
if self.filter_small_area:
343335
small_indicies = det_results[:, 2]*det_results[:, 3] > 50
344336
det_results = det_results[small_indicies]
345337

346-
# run NMS
347-
if self.NMS:
348-
# NOTE: Note nms need tlbr format
349-
nms_indices = nms(torch.from_numpy(STrack.xywh2tlbr(det_results[:, :4])), torch.from_numpy(det_results[:, 4]),
350-
self.opts.nms_thresh)
351-
det_results = det_results[nms_indices.numpy()]
352-
353338
# cal high and low indicies
354339
det_high_indicies = det_results[:, 4] >= self.det_thresh
355340
det_low_indicies = np.logical_and(np.logical_not(det_high_indicies), det_results[:, 4] > self.low_conf_thresh)
@@ -358,19 +343,19 @@ def update(self, det_results, ori_img):
358343
det_high, det_low = det_results[det_high_indicies], det_results[det_low_indicies]
359344
if det_high.shape[0] > 0:
360345
if self.use_apperance_model:
361-
features = self.get_feature(STrack.xywh2tlbr(det_high[:, :4]), ori_img)
346+
features = self.get_feature(det_high[:, :4], ori_img)
362347
# detections: List[Strack]
363-
D_high = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format, feature=feature)
364-
for (cls, xywh, score, feature) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4], features)]
348+
D_high = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format, feature=feature)
349+
for (cls, tlbr, score, feature) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4], features)]
365350
else:
366-
D_high = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
367-
for (cls, xywh, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
351+
D_high = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
352+
for (cls, tlbr, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
368353
else:
369354
D_high = []
370355

371356
if det_low.shape[0] > 0:
372-
D_low = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
373-
for (cls, xywh, score) in zip(det_low[:, -1], det_low[:, :4], det_low[:, 4])]
357+
D_low = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
358+
for (cls, tlbr, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
374359
else:
375360
D_low = []
376361

tracker/bytetrack.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,13 @@ def update(self, det_results, ori_img):
5757
lost_stracks = [] # The tracks which are not obtained in the current frame but are not removed.(Lost for some time lesser than the threshold for removing)
5858
removed_stracks = []
5959

60-
# convert the scale to origin size
61-
# NOTE: yolo v7 origin out format: [xc, yc, w, h, conf, cls0_conf, cls1_conf, ..., clsn_conf]
62-
# TODO: check here, if nesscessary use two ratio
63-
img_h, img_w = ori_img.shape[0], ori_img.shape[1]
64-
ratio = [img_h / self.model_img_size[0], img_w / self.model_img_size[1]] # usually > 1
65-
det_results[:, 0], det_results[:, 2] = det_results[:, 0]*ratio[1], det_results[:, 2]*ratio[1]
66-
det_results[:, 1], det_results[:, 3] = det_results[:, 1]*ratio[0], det_results[:, 3]*ratio[0]
67-
6860
"""step 1. filter results and init tracks"""
6961

7062
# filter small area bboxs
7163
if self.filter_small_area:
7264
small_indicies = det_results[:, 2]*det_results[:, 3] > 50
7365
det_results = det_results[small_indicies]
7466

75-
# run NMS
76-
if self.NMS:
77-
# NOTE: Note nms need tlbr format
78-
nms_indices = nms(torch.from_numpy(STrack.xywh2tlbr(det_results[:, :4])), torch.from_numpy(det_results[:, 4]),
79-
self.opts.nms_thresh)
80-
det_results = det_results[nms_indices.numpy()]
8167

8268
# cal high and low indicies
8369
det_high_indicies = det_results[:, 4] >= self.det_thresh
@@ -88,19 +74,19 @@ def update(self, det_results, ori_img):
8874
det_high, det_low = det_results[det_high_indicies], det_results[det_low_indicies]
8975
if det_high.shape[0] > 0:
9076
if self.use_apperance_model:
91-
features = self.get_feature(STrack.xywh2tlbr(det_high[:, :4]), ori_img)
77+
features = self.get_feature(det_high[:, :4], ori_img)
9278
# detections: List[Strack]
93-
D_high = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format, feature=feature)
94-
for (cls, xywh, score, feature) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4], features)]
79+
D_high = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format, feature=feature)
80+
for (cls, tlbr, score, feature) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4], features)]
9581
else:
96-
D_high = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
97-
for (cls, xywh, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
82+
D_high = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
83+
for (cls, tlbr, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
9884
else:
9985
D_high = []
10086

10187
if det_low.shape[0] > 0:
102-
D_low = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
103-
for (cls, xywh, score) in zip(det_low[:, -1], det_low[:, :4], det_low[:, 4])]
88+
D_low = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
89+
for (cls, tlbr, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
10490
else:
10591
D_low = []
10692

tracker/c_biou_tracker.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ def get_buffer_bbox(self, level=1, bbox=None):
6161
buffer_bbox = bbox + np.array([-b*bbox[2], -b*bbox[3], 2*b*bbox[2], 2*b*bbox[3]])
6262
return np.maximum(0.0, buffer_bbox)
6363

64-
6564
@property
6665
def tlbr(self):
6766
ret = self.origin_bbox_buffer[-1].copy()
@@ -152,6 +151,18 @@ def update(self, new_track, frame_id):
152151

153152
self.time_since_update = 0
154153

154+
155+
@staticmethod
156+
def tlbr2tlwh(tlbr):
157+
"""
158+
convert tlbr to tlwh
159+
"""
160+
result = np.asarray(tlbr).copy()
161+
result[2] -= result[0]
162+
result[3] -= result[1]
163+
164+
return result
165+
155166
@staticmethod
156167
def xywh2tlbr(xywh):
157168
"""
@@ -226,25 +237,11 @@ def update(self, det_results, ori_img):
226237
"""step 1. filter results and init tracks"""
227238
det_results = det_results[det_results[:, 4] > self.det_thresh]
228239

229-
# convert the scale to origin size
230-
# NOTE: yolo v7 origin out format: [xc, yc, w, h, conf, cls0_conf, cls1_conf, ..., clsn_conf]
231-
# TODO: check here, if nesscessary use two ratio
232-
img_h, img_w = ori_img.shape[0], ori_img.shape[1]
233-
ratio = [img_h / self.model_img_size[0], img_w / self.model_img_size[1]] # usually > 1
234-
det_results[:, 0], det_results[:, 2] = det_results[:, 0]*ratio[1], det_results[:, 2]*ratio[1]
235-
det_results[:, 1], det_results[:, 3] = det_results[:, 1]*ratio[0], det_results[:, 3]*ratio[0]
236-
237240
if det_results.shape[0] > 0:
238-
if self.NMS:
239-
# TODO: Note nms need tlbr format
240-
bbox_temp = C_BIoUSTrack.xywh2tlbr(det_results[:, :4])
241-
nms_indices = nms(torch.from_numpy(bbox_temp), torch.from_numpy(det_results[:, 4]),
242-
self.opts.nms_thresh)
243-
det_results = det_results[nms_indices.numpy()]
244241

245242
# detections: List[Strack]
246-
detections = [C_BIoUSTrack(cls, C_BIoUSTrack.xywh2tlwh(xywh), score)
247-
for (cls, xywh, score) in zip(det_results[:, -1], det_results[:, :4], det_results[:, 4])]
243+
detections = [C_BIoUSTrack(cls, C_BIoUSTrack.tlbr2tlwh(tlbr), score)
244+
for (cls, tlbr, score) in zip(det_results[:, -1], det_results[:, :4], det_results[:, 4])]
248245

249246
else:
250247
detections = []

tracker/deepmot.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,42 +176,29 @@ def update(self, det_results, ori_img):
176176
lost_stracks = [] # The tracks which are not obtained in the current frame but are not removed.(Lost for some time lesser than the threshold for removing)
177177
removed_stracks = []
178178

179-
# convert the scale to origin size
180-
# NOTE: yolo v7 origin out format: [xc, yc, w, h, conf, cls0_conf, cls1_conf, ..., clsn_conf]
181-
# TODO: check here, if nesscessary use two ratio
182-
img_h, img_w = ori_img.shape[0], ori_img.shape[1]
183-
ratio = [img_h / self.model_img_size[0], img_w / self.model_img_size[1]] # usually > 1
184-
det_results[:, 0], det_results[:, 2] = det_results[:, 0]*ratio[1], det_results[:, 2]*ratio[1]
185-
det_results[:, 1], det_results[:, 3] = det_results[:, 1]*ratio[0], det_results[:, 3]*ratio[0]
186-
187179
"""step 1. filter results and init tracks"""
188180

189181
# filter small area bboxs
190182
if self.filter_small_area:
191183
small_indicies = det_results[:, 2]*det_results[:, 3] > 50
192184
det_results = det_results[small_indicies]
193185

194-
# run NMS
195-
if self.NMS:
196-
# NOTE: Note nms need tlbr format
197-
nms_indices = nms(torch.from_numpy(STrack.xywh2tlbr(det_results[:, :4])), torch.from_numpy(det_results[:, 4]),
198-
self.opts.nms_thresh)
199-
det_results = det_results[nms_indices.numpy()]
200186

201187
# cal high and low indicies
202188
det_high_indicies = det_results[:, 4] >= self.det_thresh
203189
det_low_indicies = np.logical_and(np.logical_not(det_high_indicies), det_results[:, 4] > self.low_conf_thresh)
204190

205191
det_high, det_low = det_results[det_high_indicies], det_results[det_low_indicies]
192+
206193
if det_high.shape[0] > 0:
207-
D_high = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
208-
for (cls, xywh, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
194+
D_high = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
195+
for (cls, tlbr, score) in zip(det_high[:, -1], det_high[:, :4], det_high[:, 4])]
209196
else:
210197
D_high = []
211198

212199
if det_low.shape[0] > 0:
213-
D_low = [STrack(cls, STrack.xywh2tlwh(xywh), score, kalman_format=self.opts.kalman_format)
214-
for (cls, xywh, score) in zip(det_low[:, -1], det_low[:, :4], det_low[:, 4])]
200+
D_low = [STrack(cls, STrack.tlbr2tlwh(tlbr), score, kalman_format=self.opts.kalman_format)
201+
for (cls, tlbr, score) in zip(det_low[:, -1], det_low[:, :4], det_low[:, 4])]
215202
else:
216203
D_low = []
217204

0 commit comments

Comments
 (0)