tracker.py 25.8 KB
Newer Older
G
George Ni 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import glob
F
Feng Ni 已提交
21
import re
G
George Ni 已提交
22
import paddle
23
import paddle.nn as nn
G
George Ni 已提交
24
import numpy as np
F
Feng Ni 已提交
25
from tqdm import tqdm
26
from collections import defaultdict
G
George Ni 已提交
27 28 29

from ppdet.core.workspace import create
from ppdet.utils.checkpoint import load_weight, load_pretrain_weight
G
George Ni 已提交
30
from ppdet.modeling.mot.utils import Detection, get_crops, scale_coords, clip_box
31
from ppdet.modeling.mot.utils import MOTTimer, load_det_results, write_mot_results, save_vis_results
F
Feng Ni 已提交
32
from ppdet.modeling.mot.tracker import JDETracker, DeepSORTTracker, OCSORTTracker
33
from ppdet.modeling.architectures import YOLOX
F
Feng Ni 已提交
34
from ppdet.metrics import Metric, MOTMetric, KITTIMOTMetric, MCMOTMetric
G
George Ni 已提交
35 36 37 38 39 40 41
import ppdet.utils.stats as stats

from .callbacks import Callback, ComposeCallback

from ppdet.utils.logger import setup_logger
logger = setup_logger(__name__)

42 43 44 45 46
MOT_ARCH = ['DeepSORT', 'JDE', 'FairMOT', 'ByteTrack']
MOT_ARCH_JDE = ['JDE', 'FairMOT']
MOT_ARCH_SDE = ['DeepSORT', 'ByteTrack']
MOT_DATA_TYPE = ['mot', 'mcmot', 'kitti']

G
George Ni 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
__all__ = ['Tracker']


class Tracker(object):
    def __init__(self, cfg, mode='eval'):
        self.cfg = cfg
        assert mode.lower() in ['test', 'eval'], \
                "mode should be 'test' or 'eval'"
        self.mode = mode.lower()
        self.optimizer = None

        # build MOT data loader
        self.dataset = cfg['{}MOTDataset'.format(self.mode.capitalize())]

        # build model
        self.model = create(cfg.architecture)

64 65 66 67 68 69
        if isinstance(self.model.detector, YOLOX):
            for k, m in self.model.named_sublayers():
                if isinstance(m, nn.BatchNorm2D):
                    m._epsilon = 1e-3  # for amp(fp16)
                    m._momentum = 0.97  # 0.03 in pytorch

G
George Ni 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        self.status = {}
        self.start_epoch = 0

        # initial default callbacks
        self._init_callbacks()

        # initial default metrics
        self._init_metrics()
        self._reset_metrics()

    def _init_callbacks(self):
        self._callbacks = []
        self._compose_callback = None

    def _init_metrics(self):
        if self.mode in ['test']:
            self._metrics = []
            return

        if self.cfg.metric == 'MOT':
            self._metrics = [MOTMetric(), ]
91 92
        elif self.cfg.metric == 'MCMOT':
            self._metrics = [MCMOTMetric(self.cfg.num_classes), ]
G
George Ni 已提交
93 94
        elif self.cfg.metric == 'KITTI':
            self._metrics = [KITTIMOTMetric(), ]
G
George Ni 已提交
95
        else:
96
            logger.warning("Metric not support for metric type {}".format(
G
George Ni 已提交
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
                self.cfg.metric))
            self._metrics = []

    def _reset_metrics(self):
        for metric in self._metrics:
            metric.reset()

    def register_callbacks(self, callbacks):
        callbacks = [h for h in list(callbacks) if h is not None]
        for c in callbacks:
            assert isinstance(c, Callback), \
                    "metrics shoule be instances of subclass of Metric"
        self._callbacks.extend(callbacks)
        self._compose_callback = ComposeCallback(self._callbacks)

    def register_metrics(self, metrics):
        metrics = [m for m in list(metrics) if m is not None]
        for m in metrics:
            assert isinstance(m, Metric), \
                    "metrics shoule be instances of subclass of Metric"
        self._metrics.extend(metrics)

    def load_weights_jde(self, weights):
        load_weight(self.model, weights, self.optimizer)

    def load_weights_sde(self, det_weights, reid_weights):
123 124 125 126
        with_detector = self.model.detector is not None
        with_reid = self.model.reid is not None

        if with_detector:
127
            load_weight(self.model.detector, det_weights)
128 129
            if with_reid:
                load_weight(self.model.reid, reid_weights)
130
        else:
131
            load_weight(self.model.reid, reid_weights)
G
George Ni 已提交
132 133 134 135 136

    def _eval_seq_jde(self,
                      dataloader,
                      save_dir=None,
                      show_image=False,
137 138
                      frame_rate=30,
                      draw_threshold=0):
G
George Ni 已提交
139 140 141 142 143
        if save_dir:
            if not os.path.exists(save_dir): os.makedirs(save_dir)
        tracker = self.model.tracker
        tracker.max_time_lost = int(frame_rate / 30.0 * tracker.track_buffer)

144
        timer = MOTTimer()
G
George Ni 已提交
145 146 147
        frame_id = 0
        self.status['mode'] = 'track'
        self.model.eval()
148 149
        results = defaultdict(list)  # support single class and multi classes

F
Feng Ni 已提交
150
        for step_id, data in enumerate(tqdm(dataloader)):
G
George Ni 已提交
151 152 153
            self.status['step_id'] = step_id
            # forward
            timer.tic()
154
            pred_dets, pred_embs = self.model(data)
G
George Ni 已提交
155

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
            pred_dets, pred_embs = pred_dets.numpy(), pred_embs.numpy()
            online_targets_dict = self.model.tracker.update(pred_dets,
                                                            pred_embs)
            online_tlwhs = defaultdict(list)
            online_scores = defaultdict(list)
            online_ids = defaultdict(list)
            for cls_id in range(self.cfg.num_classes):
                online_targets = online_targets_dict[cls_id]
                for t in online_targets:
                    tlwh = t.tlwh
                    tid = t.track_id
                    tscore = t.score
                    if tlwh[2] * tlwh[3] <= tracker.min_box_area: continue
                    if tracker.vertical_ratio > 0 and tlwh[2] / tlwh[
                            3] > tracker.vertical_ratio:
                        continue
                    online_tlwhs[cls_id].append(tlwh)
                    online_ids[cls_id].append(tid)
                    online_scores[cls_id].append(tscore)
                # save results
                results[cls_id].append(
                    (frame_id + 1, online_tlwhs[cls_id], online_scores[cls_id],
                     online_ids[cls_id]))
G
George Ni 已提交
179

180 181 182 183
            timer.toc()
            save_vis_results(data, frame_id, online_ids, online_tlwhs,
                             online_scores, timer.average_time, show_image,
                             save_dir, self.cfg.num_classes)
G
George Ni 已提交
184 185 186 187 188 189 190 191 192
            frame_id += 1

        return results, frame_id, timer.average_time, timer.calls

    def _eval_seq_sde(self,
                      dataloader,
                      save_dir=None,
                      show_image=False,
                      frame_rate=30,
F
Feng Ni 已提交
193
                      seq_name='',
194
                      scaled=False,
195 196
                      det_file='',
                      draw_threshold=0):
G
George Ni 已提交
197 198 199
        if save_dir:
            if not os.path.exists(save_dir): os.makedirs(save_dir)
        use_detector = False if not self.model.detector else True
200
        use_reid = False if not self.model.reid else True
G
George Ni 已提交
201

202
        timer = MOTTimer()
F
Feng Ni 已提交
203
        results = defaultdict(list)
G
George Ni 已提交
204 205 206
        frame_id = 0
        self.status['mode'] = 'track'
        self.model.eval()
207 208
        if use_reid:
            self.model.reid.eval()
G
George Ni 已提交
209 210 211 212 213
        if not use_detector:
            dets_list = load_det_results(det_file, len(dataloader))
            logger.info('Finish loading detection results file {}.'.format(
                det_file))

214
        tracker = self.model.tracker
F
Feng Ni 已提交
215
        for step_id, data in enumerate(tqdm(dataloader)):
G
George Ni 已提交
216
            self.status['step_id'] = step_id
F
Feng Ni 已提交
217 218 219 220
            ori_image = data['ori_image']  # [bs, H, W, 3]
            ori_image_shape = data['ori_image'].shape[1:3]
            # ori_image_shape: [H, W]

G
George Ni 已提交
221
            input_shape = data['image'].shape[2:]
F
Feng Ni 已提交
222 223 224 225 226 227 228 229 230
            # input_shape: [h, w], before data transforms, set in model config

            im_shape = data['im_shape'][0].numpy()
            # im_shape: [new_h, new_w], after data transforms
            scale_factor = data['scale_factor'][0].numpy()

            empty_detections = False
            # when it has no detected bboxes, will not inference reid model 
            # and if visualize, use original image instead
231 232

            # forward
G
George Ni 已提交
233 234 235
            timer.tic()
            if not use_detector:
                dets = dets_list[frame_id]
F
Feng Ni 已提交
236
                bbox_tlwh = np.array(dets['bbox'], dtype='float32')
G
George Ni 已提交
237
                if bbox_tlwh.shape[0] > 0:
238
                    # detector outputs: pred_cls_ids, pred_scores, pred_bboxes
F
Feng Ni 已提交
239 240 241
                    pred_cls_ids = np.array(dets['cls_id'], dtype='float32')
                    pred_scores = np.array(dets['score'], dtype='float32')
                    pred_bboxes = np.concatenate(
G
George Ni 已提交
242 243 244 245
                        (bbox_tlwh[:, 0:2],
                         bbox_tlwh[:, 2:4] + bbox_tlwh[:, 0:2]),
                        axis=1)
                else:
246 247 248
                    logger.warning(
                        'Frame {} has not object, try to modify score threshold.'.
                        format(frame_id))
F
Feng Ni 已提交
249
                    empty_detections = True
G
George Ni 已提交
250 251
            else:
                outs = self.model.detector(data)
F
Feng Ni 已提交
252 253 254
                outs['bbox'] = outs['bbox'].numpy()
                outs['bbox_num'] = outs['bbox_num'].numpy()

255
                if len(outs['bbox']) > 0 and empty_detections == False:
256 257 258
                    # detector outputs: pred_cls_ids, pred_scores, pred_bboxes
                    pred_cls_ids = outs['bbox'][:, 0:1]
                    pred_scores = outs['bbox'][:, 1:2]
259
                    if not scaled:
F
Feng Ni 已提交
260 261 262 263
                        # Note: scaled=False only in JDE YOLOv3 or other detectors
                        # with LetterBoxResize and JDEBBoxPostProcess.
                        #
                        # 'scaled' means whether the coords after detector outputs
264 265
                        # have been scaled back to the original image, set True 
                        # in general detector, set False in JDE YOLOv3.
266 267 268 269 270
                        pred_bboxes = scale_coords(outs['bbox'][:, 2:],
                                                   input_shape, im_shape,
                                                   scale_factor)
                    else:
                        pred_bboxes = outs['bbox'][:, 2:]
271 272
                    pred_dets_old = np.concatenate(
                        (pred_cls_ids, pred_scores, pred_bboxes), axis=1)
G
George Ni 已提交
273
                else:
274
                    logger.warning(
F
Feng Ni 已提交
275
                        'Frame {} has not detected object, try to modify score threshold.'.
276
                        format(frame_id))
F
Feng Ni 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
                    empty_detections = True

            if not empty_detections:
                pred_xyxys, keep_idx = clip_box(pred_bboxes, ori_image_shape)
                if len(keep_idx[0]) == 0:
                    logger.warning(
                        'Frame {} has not detected object left after clip_box.'.
                        format(frame_id))
                    empty_detections = True

            if empty_detections:
                timer.toc()
                # if visualize, use original image instead
                online_ids, online_tlwhs, online_scores = None, None, None
                save_vis_results(data, frame_id, online_ids, online_tlwhs,
                                 online_scores, timer.average_time, show_image,
                                 save_dir, self.cfg.num_classes)
                frame_id += 1
                # thus will not inference reid model
                continue
G
George Ni 已提交
297

F
Feng Ni 已提交
298
            pred_cls_ids = pred_cls_ids[keep_idx[0]]
299
            pred_scores = pred_scores[keep_idx[0]]
F
Feng Ni 已提交
300
            pred_dets = np.concatenate(
301 302 303 304 305 306 307 308 309 310 311
                (pred_cls_ids, pred_scores, pred_xyxys), axis=1)

            if use_reid:
                crops = get_crops(
                    pred_xyxys,
                    ori_image,
                    w=tracker.input_size[0],
                    h=tracker.input_size[1])
                crops = paddle.to_tensor(crops)

                data.update({'crops': crops})
312
                pred_embs = self.model(data)['embeddings'].numpy()
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
            else:
                pred_embs = None

            if isinstance(tracker, DeepSORTTracker):
                online_tlwhs, online_scores, online_ids = [], [], []
                tracker.predict()
                online_targets = tracker.update(pred_dets, pred_embs)
                for t in online_targets:
                    if not t.is_confirmed() or t.time_since_update > 1:
                        continue
                    tlwh = t.to_tlwh()
                    tscore = t.score
                    tid = t.track_id
                    if tscore < draw_threshold: continue
                    if tlwh[2] * tlwh[3] <= tracker.min_box_area: continue
                    if tracker.vertical_ratio > 0 and tlwh[2] / tlwh[
                            3] > tracker.vertical_ratio:
                        continue
                    online_tlwhs.append(tlwh)
                    online_scores.append(tscore)
                    online_ids.append(tid)
                timer.toc()

                # save results
                results[0].append(
                    (frame_id + 1, online_tlwhs, online_scores, online_ids))
                save_vis_results(data, frame_id, online_ids, online_tlwhs,
F
Feng Ni 已提交
340 341
                                 online_scores, timer.average_time, show_image,
                                 save_dir, self.cfg.num_classes)
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

            elif isinstance(tracker, JDETracker):
                # trick hyperparams only used for MOTChallenge (MOT17, MOT20) Test-set
                tracker.track_buffer, tracker.conf_thres = get_trick_hyperparams(
                    seq_name, tracker.track_buffer, tracker.conf_thres)

                online_targets_dict = tracker.update(pred_dets_old, pred_embs)
                online_tlwhs = defaultdict(list)
                online_scores = defaultdict(list)
                online_ids = defaultdict(list)
                for cls_id in range(self.cfg.num_classes):
                    online_targets = online_targets_dict[cls_id]
                    for t in online_targets:
                        tlwh = t.tlwh
                        tid = t.track_id
                        tscore = t.score
                        if tlwh[2] * tlwh[3] <= tracker.min_box_area: continue
                        if tracker.vertical_ratio > 0 and tlwh[2] / tlwh[
                                3] > tracker.vertical_ratio:
                            continue
                        online_tlwhs[cls_id].append(tlwh)
                        online_ids[cls_id].append(tid)
                        online_scores[cls_id].append(tscore)
                    # save results
                    results[cls_id].append(
F
Feng Ni 已提交
367 368
                        (frame_id + 1, online_tlwhs[cls_id],
                         online_scores[cls_id], online_ids[cls_id]))
369 370
                timer.toc()
                save_vis_results(data, frame_id, online_ids, online_tlwhs,
F
Feng Ni 已提交
371 372
                                 online_scores, timer.average_time, show_image,
                                 save_dir, self.cfg.num_classes)
F
Feng Ni 已提交
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
            elif isinstance(tracker, OCSORTTracker):
                # OC_SORT Tracker
                online_targets = tracker.update(pred_dets_old, pred_embs)
                online_tlwhs = []
                online_ids = []
                online_scores = []
                for t in online_targets:
                    tlwh = [t[0], t[1], t[2] - t[0], t[3] - t[1]]
                    tscore = float(t[4])
                    tid = int(t[5])
                    if tlwh[2] * tlwh[3] > 0:
                        online_tlwhs.append(tlwh)
                        online_ids.append(tid)
                        online_scores.append(tscore)
                timer.toc()
                # save results
                results[0].append(
                    (frame_id + 1, online_tlwhs, online_scores, online_ids))
                save_vis_results(data, frame_id, online_ids, online_tlwhs,
                                 online_scores, timer.average_time, show_image,
                                 save_dir, self.cfg.num_classes)
            else:
                raise ValueError(tracker)
G
George Ni 已提交
396 397 398 399 400 401 402 403 404 405 406 407 408
            frame_id += 1

        return results, frame_id, timer.average_time, timer.calls

    def mot_evaluate(self,
                     data_root,
                     seqs,
                     output_dir,
                     data_type='mot',
                     model_type='JDE',
                     save_images=False,
                     save_videos=False,
                     show_image=False,
409
                     scaled=False,
G
George Ni 已提交
410 411 412 413
                     det_results_dir=''):
        if not os.path.exists(output_dir): os.makedirs(output_dir)
        result_root = os.path.join(output_dir, 'mot_results')
        if not os.path.exists(result_root): os.makedirs(result_root)
414
        assert data_type in MOT_DATA_TYPE, \
415
            "data_type should be 'mot', 'mcmot' or 'kitti'"
416 417
        assert model_type in MOT_ARCH, \
            "model_type should be 'JDE', 'DeepSORT', 'FairMOT' or 'ByteTrack'"
G
George Ni 已提交
418 419 420 421 422

        # run tracking
        n_frame = 0
        timer_avgs, timer_calls = [], []
        for seq in seqs:
423 424 425 426
            infer_dir = os.path.join(data_root, seq)
            if not os.path.exists(infer_dir) or not os.path.isdir(infer_dir):
                logger.warning("Seq {} error, {} has no images.".format(
                    seq, infer_dir))
G
George Ni 已提交
427
                continue
428 429 430 431
            if os.path.exists(os.path.join(infer_dir, 'img1')):
                infer_dir = os.path.join(infer_dir, 'img1')

            frame_rate = 30
G
George Ni 已提交
432
            seqinfo = os.path.join(data_root, seq, 'seqinfo.ini')
433 434 435 436
            if os.path.exists(seqinfo):
                meta_info = open(seqinfo).read()
                frame_rate = int(meta_info[meta_info.find('frameRate') + 10:
                                           meta_info.find('\nseqLength')])
G
George Ni 已提交
437

G
George Ni 已提交
438 439
            save_dir = os.path.join(output_dir, 'mot_outputs',
                                    seq) if save_images or save_videos else None
F
Feng Ni 已提交
440
            logger.info('Evaluate seq: {}'.format(seq))
G
George Ni 已提交
441

442
            self.dataset.set_images(self.get_infer_images(infer_dir))
G
George Ni 已提交
443 444 445
            dataloader = create('EvalMOTReader')(self.dataset, 0)

            result_filename = os.path.join(result_root, '{}.txt'.format(seq))
446

G
George Ni 已提交
447
            with paddle.no_grad():
448
                if model_type in MOT_ARCH_JDE:
G
George Ni 已提交
449 450 451 452 453
                    results, nf, ta, tc = self._eval_seq_jde(
                        dataloader,
                        save_dir=save_dir,
                        show_image=show_image,
                        frame_rate=frame_rate)
454
                elif model_type in MOT_ARCH_SDE:
G
George Ni 已提交
455 456 457 458 459
                    results, nf, ta, tc = self._eval_seq_sde(
                        dataloader,
                        save_dir=save_dir,
                        show_image=show_image,
                        frame_rate=frame_rate,
F
Feng Ni 已提交
460
                        seq_name=seq,
461
                        scaled=scaled,
G
George Ni 已提交
462 463 464 465
                        det_file=os.path.join(det_results_dir,
                                              '{}.txt'.format(seq)))
                else:
                    raise ValueError(model_type)
G
George Ni 已提交
466

467 468
            write_mot_results(result_filename, results, data_type,
                              self.cfg.num_classes)
G
George Ni 已提交
469 470 471 472 473
            n_frame += nf
            timer_avgs.append(ta)
            timer_calls.append(tc)

            if save_videos:
G
George Ni 已提交
474 475
                output_video_path = os.path.join(save_dir, '..',
                                                 '{}_vis.mp4'.format(seq))
F
Feng Ni 已提交
476
                cmd_str = 'ffmpeg -f image2 -i {}/%05d.jpg {}'.format(
G
George Ni 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
                    save_dir, output_video_path)
                os.system(cmd_str)
                logger.info('Save video in {}.'.format(output_video_path))

            # update metrics
            for metric in self._metrics:
                metric.update(data_root, seq, data_type, result_root,
                              result_filename)

        timer_avgs = np.asarray(timer_avgs)
        timer_calls = np.asarray(timer_calls)
        all_time = np.dot(timer_avgs, timer_calls)
        avg_time = all_time / np.sum(timer_calls)
        logger.info('Time elapsed: {:.2f} seconds, FPS: {:.2f}'.format(
            all_time, 1.0 / avg_time))

        # accumulate metric to log out
        for metric in self._metrics:
            metric.accumulate()
            metric.log()
        # reset metric states for metric may performed multiple times
        self._reset_metrics()

    def get_infer_images(self, infer_dir):
        assert infer_dir is None or os.path.isdir(infer_dir), \
            "{} is not a directory".format(infer_dir)
        images = set()
        assert os.path.isdir(infer_dir), \
            "infer_dir {} is not a directory".format(infer_dir)
        exts = ['jpg', 'jpeg', 'png', 'bmp']
        exts += [ext.upper() for ext in exts]
        for ext in exts:
            images.update(glob.glob('{}/*.{}'.format(infer_dir, ext)))
        images = list(images)
        images.sort()
        assert len(images) > 0, "no image found in {}".format(infer_dir)
        logger.info("Found {} inference images in total.".format(len(images)))
        return images

F
Feng Ni 已提交
516 517 518 519 520 521 522 523 524 525 526 527 528
    def mot_predict_seq(self,
                        video_file,
                        frame_rate,
                        image_dir,
                        output_dir,
                        data_type='mot',
                        model_type='JDE',
                        save_images=False,
                        save_videos=True,
                        show_image=False,
                        scaled=False,
                        det_results_dir='',
                        draw_threshold=0.5):
G
George Ni 已提交
529 530 531 532 533 534 535
        assert video_file is not None or image_dir is not None, \
            "--video_file or --image_dir should be set."
        assert video_file is None or os.path.isfile(video_file), \
                "{} is not a file".format(video_file)
        assert image_dir is None or os.path.isdir(image_dir), \
                "{} is not a directory".format(image_dir)

G
George Ni 已提交
536 537 538
        if not os.path.exists(output_dir): os.makedirs(output_dir)
        result_root = os.path.join(output_dir, 'mot_results')
        if not os.path.exists(result_root): os.makedirs(result_root)
539
        assert data_type in MOT_DATA_TYPE, \
540
            "data_type should be 'mot', 'mcmot' or 'kitti'"
541 542
        assert model_type in MOT_ARCH, \
            "model_type should be 'JDE', 'DeepSORT', 'FairMOT' or 'ByteTrack'"
G
George Ni 已提交
543

G
George Ni 已提交
544 545 546
        # run tracking        
        if video_file:
            seq = video_file.split('/')[-1].split('.')[0]
547
            self.dataset.set_video(video_file, frame_rate)
G
George Ni 已提交
548 549 550
            logger.info('Starting tracking video {}'.format(video_file))
        elif image_dir:
            seq = image_dir.split('/')[-1].split('.')[0]
F
Feng Ni 已提交
551 552
            if os.path.exists(os.path.join(image_dir, 'img1')):
                image_dir = os.path.join(image_dir, 'img1')
G
George Ni 已提交
553 554 555 556 557 558 559 560 561 562
            images = [
                '{}/{}'.format(image_dir, x) for x in os.listdir(image_dir)
            ]
            images.sort()
            self.dataset.set_images(images)
            logger.info('Starting tracking folder {}, found {} images'.format(
                image_dir, len(images)))
        else:
            raise ValueError('--video_file or --image_dir should be set.')

G
George Ni 已提交
563 564 565 566 567
        save_dir = os.path.join(output_dir, 'mot_outputs',
                                seq) if save_images or save_videos else None

        dataloader = create('TestMOTReader')(self.dataset, 0)
        result_filename = os.path.join(result_root, '{}.txt'.format(seq))
568 569
        if frame_rate == -1:
            frame_rate = self.dataset.frame_rate
G
George Ni 已提交
570

G
George Ni 已提交
571
        with paddle.no_grad():
572
            if model_type in MOT_ARCH_JDE:
G
George Ni 已提交
573 574 575 576
                results, nf, ta, tc = self._eval_seq_jde(
                    dataloader,
                    save_dir=save_dir,
                    show_image=show_image,
577 578
                    frame_rate=frame_rate,
                    draw_threshold=draw_threshold)
579
            elif model_type in MOT_ARCH_SDE:
G
George Ni 已提交
580 581 582 583 584
                results, nf, ta, tc = self._eval_seq_sde(
                    dataloader,
                    save_dir=save_dir,
                    show_image=show_image,
                    frame_rate=frame_rate,
F
Feng Ni 已提交
585
                    seq_name=seq,
586
                    scaled=scaled,
G
George Ni 已提交
587
                    det_file=os.path.join(det_results_dir,
588 589
                                          '{}.txt'.format(seq)),
                    draw_threshold=draw_threshold)
G
George Ni 已提交
590 591
            else:
                raise ValueError(model_type)
G
George Ni 已提交
592 593

        if save_videos:
G
George Ni 已提交
594 595
            output_video_path = os.path.join(save_dir, '..',
                                             '{}_vis.mp4'.format(seq))
F
Feng Ni 已提交
596
            cmd_str = 'ffmpeg -f image2 -i {}/%05d.jpg {}'.format(
G
George Ni 已提交
597 598 599
                save_dir, output_video_path)
            os.system(cmd_str)
            logger.info('Save video in {}'.format(output_video_path))
F
Feng Ni 已提交
600 601 602

        write_mot_results(result_filename, results, data_type,
                          self.cfg.num_classes)
603

F
Feng Ni 已提交
604

605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
def get_trick_hyperparams(video_name, ori_buffer, ori_thresh):
    if video_name[:3] != 'MOT':
        # only used for MOTChallenge (MOT17, MOT20) Test-set
        return ori_buffer, ori_thresh

    video_name = video_name[:8]
    if 'MOT17-05' in video_name:
        track_buffer = 14
    elif 'MOT17-13' in video_name:
        track_buffer = 25
    else:
        track_buffer = ori_buffer

    if 'MOT17-01' in video_name:
        track_thresh = 0.65
    elif 'MOT17-06' in video_name:
        track_thresh = 0.65
    elif 'MOT17-12' in video_name:
        track_thresh = 0.7
    elif 'MOT17-14' in video_name:
        track_thresh = 0.67
    else:
        track_thresh = ori_thresh

    if 'MOT20-06' in video_name or 'MOT20-08' in video_name:
        track_thresh = 0.3
    else:
        track_thresh = ori_thresh
F
Feng Ni 已提交
633

634
    return track_buffer, ori_thresh