evaluation.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import os
  2. import numpy as np
  3. import copy
  4. import motmetrics as mm
  5. mm.lap.default_solver = 'lap'
  6. from utils.io import read_results, unzip_objs
  7. class Evaluator(object):
  8. def __init__(self, data_root, seq_name, data_type):
  9. self.data_root = data_root
  10. self.seq_name = seq_name
  11. self.data_type = data_type
  12. self.load_annotations()
  13. self.reset_accumulator()
  14. def load_annotations(self):
  15. assert self.data_type == 'mot'
  16. gt_filename = os.path.join(self.data_root, self.seq_name, 'gt', 'gt.txt')
  17. self.gt_frame_dict = read_results(gt_filename, self.data_type, is_gt=True)
  18. self.gt_ignore_frame_dict = read_results(gt_filename, self.data_type, is_ignore=True)
  19. def reset_accumulator(self):
  20. self.acc = mm.MOTAccumulator(auto_id=True)
  21. def eval_frame(self, frame_id, trk_tlwhs, trk_ids, rtn_events=False):
  22. # results
  23. trk_tlwhs = np.copy(trk_tlwhs)
  24. trk_ids = np.copy(trk_ids)
  25. # gts
  26. gt_objs = self.gt_frame_dict.get(frame_id, [])
  27. gt_tlwhs, gt_ids = unzip_objs(gt_objs)[:2]
  28. # ignore boxes
  29. ignore_objs = self.gt_ignore_frame_dict.get(frame_id, [])
  30. ignore_tlwhs = unzip_objs(ignore_objs)[0]
  31. # remove ignored results
  32. keep = np.ones(len(trk_tlwhs), dtype=bool)
  33. iou_distance = mm.distances.iou_matrix(ignore_tlwhs, trk_tlwhs, max_iou=0.5)
  34. if len(iou_distance) > 0:
  35. match_is, match_js = mm.lap.linear_sum_assignment(iou_distance)
  36. match_is, match_js = map(lambda a: np.asarray(a, dtype=int), [match_is, match_js])
  37. match_ious = iou_distance[match_is, match_js]
  38. match_js = np.asarray(match_js, dtype=int)
  39. match_js = match_js[np.logical_not(np.isnan(match_ious))]
  40. keep[match_js] = False
  41. trk_tlwhs = trk_tlwhs[keep]
  42. trk_ids = trk_ids[keep]
  43. # get distance matrix
  44. iou_distance = mm.distances.iou_matrix(gt_tlwhs, trk_tlwhs, max_iou=0.5)
  45. # acc
  46. self.acc.update(gt_ids, trk_ids, iou_distance)
  47. if rtn_events and iou_distance.size > 0 and hasattr(self.acc, 'last_mot_events'):
  48. events = self.acc.last_mot_events # only supported by https://github.com/longcw/py-motmetrics
  49. else:
  50. events = None
  51. return events
  52. def eval_file(self, filename):
  53. self.reset_accumulator()
  54. result_frame_dict = read_results(filename, self.data_type, is_gt=False)
  55. frames = sorted(list(set(self.gt_frame_dict.keys()) | set(result_frame_dict.keys())))
  56. for frame_id in frames:
  57. trk_objs = result_frame_dict.get(frame_id, [])
  58. trk_tlwhs, trk_ids = unzip_objs(trk_objs)[:2]
  59. self.eval_frame(frame_id, trk_tlwhs, trk_ids, rtn_events=False)
  60. return self.acc
  61. @staticmethod
  62. def get_summary(accs, names, metrics=('mota', 'num_switches', 'idp', 'idr', 'idf1', 'precision', 'recall')):
  63. names = copy.deepcopy(names)
  64. if metrics is None:
  65. metrics = mm.metrics.motchallenge_metrics
  66. metrics = copy.deepcopy(metrics)
  67. mh = mm.metrics.create()
  68. summary = mh.compute_many(
  69. accs,
  70. metrics=metrics,
  71. names=names,
  72. generate_overall=True
  73. )
  74. return summary
  75. @staticmethod
  76. def save_summary(summary, filename):
  77. import pandas as pd
  78. writer = pd.ExcelWriter(filename)
  79. summary.to_excel(writer)
  80. writer.save()