mcmot_metrics.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import os
  18. import copy
  19. import sys
  20. import math
  21. from collections import defaultdict
  22. from motmetrics.math_util import quiet_divide
  23. import numpy as np
  24. import pandas as pd
  25. from .metrics import Metric
  26. import motmetrics as mm
  27. import openpyxl
  28. metrics = mm.metrics.motchallenge_metrics
  29. mh = mm.metrics.create()
  30. from ppdet.utils.logger import setup_logger
  31. logger = setup_logger(__name__)
  32. __all__ = ['MCMOTEvaluator', 'MCMOTMetric']
  33. METRICS_LIST = [
  34. 'num_frames', 'num_matches', 'num_switches', 'num_transfer', 'num_ascend',
  35. 'num_migrate', 'num_false_positives', 'num_misses', 'num_detections',
  36. 'num_objects', 'num_predictions', 'num_unique_objects', 'mostly_tracked',
  37. 'partially_tracked', 'mostly_lost', 'num_fragmentations', 'motp', 'mota',
  38. 'precision', 'recall', 'idfp', 'idfn', 'idtp', 'idp', 'idr', 'idf1'
  39. ]
  40. NAME_MAP = {
  41. 'num_frames': 'num_frames',
  42. 'num_matches': 'num_matches',
  43. 'num_switches': 'IDs',
  44. 'num_transfer': 'IDt',
  45. 'num_ascend': 'IDa',
  46. 'num_migrate': 'IDm',
  47. 'num_false_positives': 'FP',
  48. 'num_misses': 'FN',
  49. 'num_detections': 'num_detections',
  50. 'num_objects': 'num_objects',
  51. 'num_predictions': 'num_predictions',
  52. 'num_unique_objects': 'GT',
  53. 'mostly_tracked': 'MT',
  54. 'partially_tracked': 'partially_tracked',
  55. 'mostly_lost': 'ML',
  56. 'num_fragmentations': 'FM',
  57. 'motp': 'MOTP',
  58. 'mota': 'MOTA',
  59. 'precision': 'Prcn',
  60. 'recall': 'Rcll',
  61. 'idfp': 'idfp',
  62. 'idfn': 'idfn',
  63. 'idtp': 'idtp',
  64. 'idp': 'IDP',
  65. 'idr': 'IDR',
  66. 'idf1': 'IDF1'
  67. }
  68. def parse_accs_metrics(seq_acc, index_name, verbose=False):
  69. """
  70. Parse the evaluation indicators of multiple MOTAccumulator
  71. """
  72. mh = mm.metrics.create()
  73. summary = MCMOTEvaluator.get_summary(seq_acc, index_name, METRICS_LIST)
  74. summary.loc['OVERALL', 'motp'] = (summary['motp'] * summary['num_detections']).sum() / \
  75. summary.loc['OVERALL', 'num_detections']
  76. if verbose:
  77. strsummary = mm.io.render_summary(
  78. summary, formatters=mh.formatters, namemap=NAME_MAP)
  79. print(strsummary)
  80. return summary
  81. def seqs_overall_metrics(summary_df, verbose=False):
  82. """
  83. Calculate overall metrics for multiple sequences
  84. """
  85. add_col = [
  86. 'num_frames', 'num_matches', 'num_switches', 'num_transfer',
  87. 'num_ascend', 'num_migrate', 'num_false_positives', 'num_misses',
  88. 'num_detections', 'num_objects', 'num_predictions',
  89. 'num_unique_objects', 'mostly_tracked', 'partially_tracked',
  90. 'mostly_lost', 'num_fragmentations', 'idfp', 'idfn', 'idtp'
  91. ]
  92. calc_col = ['motp', 'mota', 'precision', 'recall', 'idp', 'idr', 'idf1']
  93. calc_df = summary_df.copy()
  94. overall_dic = {}
  95. for col in add_col:
  96. overall_dic[col] = calc_df[col].sum()
  97. for col in calc_col:
  98. overall_dic[col] = getattr(MCMOTMetricOverall, col + '_overall')(
  99. calc_df, overall_dic)
  100. overall_df = pd.DataFrame(overall_dic, index=['overall_calc'])
  101. calc_df = pd.concat([calc_df, overall_df])
  102. if verbose:
  103. mh = mm.metrics.create()
  104. str_calc_df = mm.io.render_summary(
  105. calc_df, formatters=mh.formatters, namemap=NAME_MAP)
  106. print(str_calc_df)
  107. return calc_df
  108. class MCMOTMetricOverall(object):
  109. def motp_overall(summary_df, overall_dic):
  110. motp = quiet_divide((summary_df['motp'] *
  111. summary_df['num_detections']).sum(),
  112. overall_dic['num_detections'])
  113. return motp
  114. def mota_overall(summary_df, overall_dic):
  115. del summary_df
  116. mota = 1. - quiet_divide(
  117. (overall_dic['num_misses'] + overall_dic['num_switches'] +
  118. overall_dic['num_false_positives']), overall_dic['num_objects'])
  119. return mota
  120. def precision_overall(summary_df, overall_dic):
  121. del summary_df
  122. precision = quiet_divide(overall_dic['num_detections'], (
  123. overall_dic['num_false_positives'] + overall_dic['num_detections']))
  124. return precision
  125. def recall_overall(summary_df, overall_dic):
  126. del summary_df
  127. recall = quiet_divide(overall_dic['num_detections'],
  128. overall_dic['num_objects'])
  129. return recall
  130. def idp_overall(summary_df, overall_dic):
  131. del summary_df
  132. idp = quiet_divide(overall_dic['idtp'],
  133. (overall_dic['idtp'] + overall_dic['idfp']))
  134. return idp
  135. def idr_overall(summary_df, overall_dic):
  136. del summary_df
  137. idr = quiet_divide(overall_dic['idtp'],
  138. (overall_dic['idtp'] + overall_dic['idfn']))
  139. return idr
  140. def idf1_overall(summary_df, overall_dic):
  141. del summary_df
  142. idf1 = quiet_divide(2. * overall_dic['idtp'], (
  143. overall_dic['num_objects'] + overall_dic['num_predictions']))
  144. return idf1
  145. def read_mcmot_results_union(filename, is_gt, is_ignore):
  146. results_dict = dict()
  147. if os.path.isfile(filename):
  148. all_result = np.loadtxt(filename, delimiter=',')
  149. if all_result.shape[0] == 0 or all_result.shape[1] < 7:
  150. return results_dict
  151. if is_ignore:
  152. return results_dict
  153. if is_gt:
  154. # only for test use
  155. all_result = all_result[all_result[:, 7] != 0]
  156. all_result[:, 7] = all_result[:, 7] - 1
  157. if all_result.shape[0] == 0:
  158. return results_dict
  159. class_unique = np.unique(all_result[:, 7])
  160. last_max_id = 0
  161. result_cls_list = []
  162. for cls in class_unique:
  163. result_cls_split = all_result[all_result[:, 7] == cls]
  164. result_cls_split[:, 1] = result_cls_split[:, 1] + last_max_id
  165. # make sure track id different between every category
  166. last_max_id = max(np.unique(result_cls_split[:, 1])) + 1
  167. result_cls_list.append(result_cls_split)
  168. results_con = np.concatenate(result_cls_list)
  169. for line in range(len(results_con)):
  170. linelist = results_con[line]
  171. fid = int(linelist[0])
  172. if fid < 1:
  173. continue
  174. results_dict.setdefault(fid, list())
  175. if is_gt:
  176. score = 1
  177. else:
  178. score = float(linelist[6])
  179. tlwh = tuple(map(float, linelist[2:6]))
  180. target_id = int(linelist[1])
  181. cls = int(linelist[7])
  182. results_dict[fid].append((tlwh, target_id, cls, score))
  183. return results_dict
  184. def read_mcmot_results(filename, is_gt, is_ignore):
  185. results_dict = dict()
  186. if os.path.isfile(filename):
  187. with open(filename, 'r') as f:
  188. for line in f.readlines():
  189. linelist = line.strip().split(',')
  190. if len(linelist) < 7:
  191. continue
  192. fid = int(linelist[0])
  193. if fid < 1:
  194. continue
  195. cid = int(linelist[7])
  196. if is_gt:
  197. score = 1
  198. # only for test use
  199. cid -= 1
  200. else:
  201. score = float(linelist[6])
  202. cls_result_dict = results_dict.setdefault(cid, dict())
  203. cls_result_dict.setdefault(fid, list())
  204. tlwh = tuple(map(float, linelist[2:6]))
  205. target_id = int(linelist[1])
  206. cls_result_dict[fid].append((tlwh, target_id, score))
  207. return results_dict
  208. def read_results(filename,
  209. data_type,
  210. is_gt=False,
  211. is_ignore=False,
  212. multi_class=False,
  213. union=False):
  214. if data_type in ['mcmot', 'lab']:
  215. if multi_class:
  216. if union:
  217. # The results are evaluated by union all the categories.
  218. # Track IDs between different categories cannot be duplicate.
  219. read_fun = read_mcmot_results_union
  220. else:
  221. # The results are evaluated separately by category.
  222. read_fun = read_mcmot_results
  223. else:
  224. raise ValueError('multi_class: {}, MCMOT should have cls_id.'.
  225. format(multi_class))
  226. else:
  227. raise ValueError('Unknown data type: {}'.format(data_type))
  228. return read_fun(filename, is_gt, is_ignore)
  229. def unzip_objs(objs):
  230. if len(objs) > 0:
  231. tlwhs, ids, scores = zip(*objs)
  232. else:
  233. tlwhs, ids, scores = [], [], []
  234. tlwhs = np.asarray(tlwhs, dtype=float).reshape(-1, 4)
  235. return tlwhs, ids, scores
  236. def unzip_objs_cls(objs):
  237. if len(objs) > 0:
  238. tlwhs, ids, cls, scores = zip(*objs)
  239. else:
  240. tlwhs, ids, cls, scores = [], [], [], []
  241. tlwhs = np.asarray(tlwhs, dtype=float).reshape(-1, 4)
  242. ids = np.array(ids)
  243. cls = np.array(cls)
  244. scores = np.array(scores)
  245. return tlwhs, ids, cls, scores
  246. class MCMOTEvaluator(object):
  247. def __init__(self, data_root, seq_name, data_type, num_classes):
  248. self.data_root = data_root
  249. self.seq_name = seq_name
  250. self.data_type = data_type
  251. self.num_classes = num_classes
  252. self.load_annotations()
  253. self.reset_accumulator()
  254. self.class_accs = []
  255. def load_annotations(self):
  256. assert self.data_type == 'mcmot'
  257. self.gt_filename = os.path.join(self.data_root, '../', 'sequences',
  258. '{}.txt'.format(self.seq_name))
  259. if not os.path.exists(self.gt_filename):
  260. logger.warning(
  261. "gt_filename '{}' of MCMOTEvaluator is not exist, so the MOTA will be -INF."
  262. )
  263. def reset_accumulator(self):
  264. import motmetrics as mm
  265. mm.lap.default_solver = 'lap'
  266. self.acc = mm.MOTAccumulator(auto_id=True)
  267. def eval_frame_dict(self, trk_objs, gt_objs, rtn_events=False, union=False):
  268. import motmetrics as mm
  269. mm.lap.default_solver = 'lap'
  270. if union:
  271. trk_tlwhs, trk_ids, trk_cls = unzip_objs_cls(trk_objs)[:3]
  272. gt_tlwhs, gt_ids, gt_cls = unzip_objs_cls(gt_objs)[:3]
  273. # get distance matrix
  274. iou_distance = mm.distances.iou_matrix(
  275. gt_tlwhs, trk_tlwhs, max_iou=0.5)
  276. # Set the distance between objects of different categories to nan
  277. gt_cls_len = len(gt_cls)
  278. trk_cls_len = len(trk_cls)
  279. # When the number of GT or Trk is 0, iou_distance dimension is (0,0)
  280. if gt_cls_len != 0 and trk_cls_len != 0:
  281. gt_cls = gt_cls.reshape(gt_cls_len, 1)
  282. gt_cls = np.repeat(gt_cls, trk_cls_len, axis=1)
  283. trk_cls = trk_cls.reshape(1, trk_cls_len)
  284. trk_cls = np.repeat(trk_cls, gt_cls_len, axis=0)
  285. iou_distance = np.where(gt_cls == trk_cls, iou_distance, np.nan)
  286. else:
  287. trk_tlwhs, trk_ids = unzip_objs(trk_objs)[:2]
  288. gt_tlwhs, gt_ids = unzip_objs(gt_objs)[:2]
  289. # get distance matrix
  290. iou_distance = mm.distances.iou_matrix(
  291. gt_tlwhs, trk_tlwhs, max_iou=0.5)
  292. self.acc.update(gt_ids, trk_ids, iou_distance)
  293. if rtn_events and iou_distance.size > 0 and hasattr(self.acc,
  294. 'mot_events'):
  295. events = self.acc.mot_events # only supported by https://github.com/longcw/py-motmetrics
  296. else:
  297. events = None
  298. return events
  299. def eval_file(self, result_filename):
  300. # evaluation of each category
  301. gt_frame_dict = read_results(
  302. self.gt_filename,
  303. self.data_type,
  304. is_gt=True,
  305. multi_class=True,
  306. union=False)
  307. result_frame_dict = read_results(
  308. result_filename,
  309. self.data_type,
  310. is_gt=False,
  311. multi_class=True,
  312. union=False)
  313. for cid in range(self.num_classes):
  314. self.reset_accumulator()
  315. cls_result_frame_dict = result_frame_dict.setdefault(cid, dict())
  316. cls_gt_frame_dict = gt_frame_dict.setdefault(cid, dict())
  317. # only labeled frames will be evaluated
  318. frames = sorted(list(set(cls_gt_frame_dict.keys())))
  319. for frame_id in frames:
  320. trk_objs = cls_result_frame_dict.get(frame_id, [])
  321. gt_objs = cls_gt_frame_dict.get(frame_id, [])
  322. self.eval_frame_dict(trk_objs, gt_objs, rtn_events=False)
  323. self.class_accs.append(self.acc)
  324. return self.class_accs
  325. @staticmethod
  326. def get_summary(accs,
  327. names,
  328. metrics=('mota', 'num_switches', 'idp', 'idr', 'idf1',
  329. 'precision', 'recall')):
  330. import motmetrics as mm
  331. mm.lap.default_solver = 'lap'
  332. names = copy.deepcopy(names)
  333. if metrics is None:
  334. metrics = mm.metrics.motchallenge_metrics
  335. metrics = copy.deepcopy(metrics)
  336. mh = mm.metrics.create()
  337. summary = mh.compute_many(
  338. accs, metrics=metrics, names=names, generate_overall=True)
  339. return summary
  340. @staticmethod
  341. def save_summary(summary, filename):
  342. import pandas as pd
  343. writer = pd.ExcelWriter(filename)
  344. summary.to_excel(writer)
  345. writer.save()
  346. class MCMOTMetric(Metric):
  347. def __init__(self, num_classes, save_summary=False):
  348. self.num_classes = num_classes
  349. self.save_summary = save_summary
  350. self.MCMOTEvaluator = MCMOTEvaluator
  351. self.result_root = None
  352. self.reset()
  353. self.seqs_overall = defaultdict(list)
  354. def reset(self):
  355. self.accs = []
  356. self.seqs = []
  357. def update(self, data_root, seq, data_type, result_root, result_filename):
  358. evaluator = self.MCMOTEvaluator(data_root, seq, data_type,
  359. self.num_classes)
  360. seq_acc = evaluator.eval_file(result_filename)
  361. self.accs.append(seq_acc)
  362. self.seqs.append(seq)
  363. self.result_root = result_root
  364. cls_index_name = [
  365. '{}_{}'.format(seq, i) for i in range(self.num_classes)
  366. ]
  367. summary = parse_accs_metrics(seq_acc, cls_index_name)
  368. summary.rename(
  369. index={'OVERALL': '{}_OVERALL'.format(seq)}, inplace=True)
  370. for row in range(len(summary)):
  371. self.seqs_overall[row].append(summary.iloc[row:row + 1])
  372. def accumulate(self):
  373. self.cls_summary_list = []
  374. for row in range(self.num_classes):
  375. seqs_cls_df = pd.concat(self.seqs_overall[row])
  376. seqs_cls_summary = seqs_overall_metrics(seqs_cls_df)
  377. cls_summary_overall = seqs_cls_summary.iloc[-1:].copy()
  378. cls_summary_overall.rename(
  379. index={'overall_calc': 'overall_calc_{}'.format(row)},
  380. inplace=True)
  381. self.cls_summary_list.append(cls_summary_overall)
  382. def log(self):
  383. seqs_summary = seqs_overall_metrics(
  384. pd.concat(self.seqs_overall[self.num_classes]), verbose=True)
  385. class_summary = seqs_overall_metrics(
  386. pd.concat(self.cls_summary_list), verbose=True)
  387. def get_results(self):
  388. return 1