k20hcmus/FishEye8K
3
1import os2import numpy as np3import copy4import motmetrics as mm5mm.lap.default_solver = 'lap'6from utils.io import read_results, unzip_objs7 8 9class Evaluator(object):10 11 def __init__(self, data_root, seq_name, data_type):12 self.data_root = data_root13 self.seq_name = seq_name14 self.data_type = data_type15 16 self.load_annotations()17 self.reset_accumulator()18 19 def load_annotations(self):20 assert self.data_type == 'mot'21 22 gt_filename = os.path.join(self.data_root, self.seq_name, 'gt', 'gt.txt')23 self.gt_frame_dict = read_results(gt_filename, self.data_type, is_gt=True)24 self.gt_ignore_frame_dict = read_results(gt_filename, self.data_type, is_ignore=True)25 26 def reset_accumulator(self):27 self.acc = mm.MOTAccumulator(auto_id=True)28 29 def eval_frame(self, frame_id, trk_tlwhs, trk_ids, rtn_events=False):30 # results31 trk_tlwhs = np.copy(trk_tlwhs)32 trk_ids = np.copy(trk_ids)33 34 # gts35 gt_objs = self.gt_frame_dict.get(frame_id, [])36 gt_tlwhs, gt_ids = unzip_objs(gt_objs)[:2]37 38 # ignore boxes39 ignore_objs = self.gt_ignore_frame_dict.get(frame_id, [])40 ignore_tlwhs = unzip_objs(ignore_objs)[0]41 42 43 # remove ignored results44 keep = np.ones(len(trk_tlwhs), dtype=bool)45 iou_distance = mm.distances.iou_matrix(ignore_tlwhs, trk_tlwhs, max_iou=0.5)46 if len(iou_distance) > 0:47 match_is, match_js = mm.lap.linear_sum_assignment(iou_distance)48 match_is, match_js = map(lambda a: np.asarray(a, dtype=int), [match_is, match_js])49 match_ious = iou_distance[match_is, match_js]50 51 match_js = np.asarray(match_js, dtype=int)52 match_js = match_js[np.logical_not(np.isnan(match_ious))]53 keep[match_js] = False54 trk_tlwhs = trk_tlwhs[keep]55 trk_ids = trk_ids[keep]56 57 # get distance matrix58 iou_distance = mm.distances.iou_matrix(gt_tlwhs, trk_tlwhs, max_iou=0.5)59 60 # acc61 self.acc.update(gt_ids, trk_ids, iou_distance)62 63 if rtn_events and iou_distance.size > 0 and hasattr(self.acc, 'last_mot_events'):64 events = self.acc.last_mot_events # only supported by https://github.com/longcw/py-motmetrics65 else:66 events = None67 return events68 69 def eval_file(self, filename):70 self.reset_accumulator()71 72 result_frame_dict = read_results(filename, self.data_type, is_gt=False)73 frames = sorted(list(set(self.gt_frame_dict.keys()) | set(result_frame_dict.keys())))74 for frame_id in frames:75 trk_objs = result_frame_dict.get(frame_id, [])76 trk_tlwhs, trk_ids = unzip_objs(trk_objs)[:2]77 self.eval_frame(frame_id, trk_tlwhs, trk_ids, rtn_events=False)78 79 return self.acc80 81 @staticmethod82 def get_summary(accs, names, metrics=('mota', 'num_switches', 'idp', 'idr', 'idf1', 'precision', 'recall')):83 names = copy.deepcopy(names)84 if metrics is None:85 metrics = mm.metrics.motchallenge_metrics86 metrics = copy.deepcopy(metrics)87 88 mh = mm.metrics.create()89 summary = mh.compute_many(90 accs,91 metrics=metrics,92 names=names,93 generate_overall=True94 )95 96 return summary97 98 @staticmethod99 def save_summary(summary, filename):100 import pandas as pd101 writer = pd.ExcelWriter(filename)102 summary.to_excel(writer)103 writer.save()104 