tao.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. # fmt: off
  2. # flake8: noqa
  3. # pyre-unsafe
  4. """TAO Dataset."""
  5. import copy
  6. import itertools
  7. import json
  8. import os
  9. from collections import defaultdict
  10. import numpy as np
  11. from .. import _timing
  12. from ..config import get_default_dataset_config, init_config
  13. from ..utils import TrackEvalException
  14. from ._base_dataset import _BaseDataset
  15. class TAO(_BaseDataset):
  16. """Dataset class for TAO tracking"""
  17. def __init__(self, config=None):
  18. """Initialize dataset, checking that all required files are present."""
  19. super().__init__()
  20. # Fill non-given config values with defaults
  21. self.config = init_config(config, get_default_dataset_config(), self.get_name())
  22. self.gt_fol = self.config["GT_FOLDER"]
  23. self.tracker_fol = self.config["TRACKERS_FOLDER"]
  24. self.should_classes_combine = True
  25. self.use_super_categories = False
  26. self.use_mask = self.config["USE_MASK"]
  27. self.tracker_sub_fol = self.config["TRACKER_SUB_FOLDER"]
  28. self.output_fol = self.config["OUTPUT_FOLDER"]
  29. if self.output_fol is None:
  30. self.output_fol = self.tracker_fol
  31. self.output_sub_fol = self.config["OUTPUT_SUB_FOLDER"]
  32. if self.gt_fol.endswith(".json"):
  33. self.gt_data = json.load(open(self.gt_fol, "r"))
  34. else:
  35. gt_dir_files = [
  36. file for file in os.listdir(self.gt_fol) if file.endswith(".json")
  37. ]
  38. if len(gt_dir_files) != 1:
  39. raise TrackEvalException(
  40. f"{self.gt_fol} does not contain exactly one json file."
  41. )
  42. with open(os.path.join(self.gt_fol, gt_dir_files[0])) as f:
  43. self.gt_data = json.load(f)
  44. # merge categories marked with a merged tag in TAO dataset
  45. self._merge_categories(self.gt_data["annotations"] + self.gt_data["tracks"])
  46. # get sequences to eval and sequence information
  47. self.seq_list = [
  48. vid["name"].replace("/", "-") for vid in self.gt_data["videos"]
  49. ]
  50. self.seq_name2seqid = {
  51. vid["name"].replace("/", "-"): vid["id"] for vid in self.gt_data["videos"]
  52. }
  53. # compute mappings from videos to annotation data
  54. self.video2gt_track, self.video2gt_image = self._compute_vid_mappings(
  55. self.gt_data["annotations"]
  56. )
  57. # compute sequence lengths
  58. self.seq_lengths = {vid["id"]: 0 for vid in self.gt_data["videos"]}
  59. for img in self.gt_data["images"]:
  60. self.seq_lengths[img["video_id"]] += 1
  61. self.seq2images2timestep = self._compute_image_to_timestep_mappings()
  62. self.seq2cls = {
  63. vid["id"]: {
  64. "pos_cat_ids": list(
  65. {track["category_id"] for track in self.video2gt_track[vid["id"]]}
  66. ),
  67. "neg_cat_ids": vid["neg_category_ids"],
  68. "not_exh_labeled_cat_ids": vid["not_exhaustive_category_ids"],
  69. }
  70. for vid in self.gt_data["videos"]
  71. }
  72. # Get classes to eval
  73. considered_vid_ids = [self.seq_name2seqid[vid] for vid in self.seq_list]
  74. seen_cats = set(
  75. [
  76. cat_id
  77. for vid_id in considered_vid_ids
  78. for cat_id in self.seq2cls[vid_id]["pos_cat_ids"]
  79. ]
  80. )
  81. # only classes with ground truth are evaluated in TAO
  82. self.valid_classes = [
  83. cls["name"] for cls in self.gt_data["categories"] if cls["id"] in seen_cats
  84. ]
  85. cls_name2clsid_map = {
  86. cls["name"]: cls["id"] for cls in self.gt_data["categories"]
  87. }
  88. if self.config["CLASSES_TO_EVAL"]:
  89. self.class_list = [
  90. cls.lower() if cls.lower() in self.valid_classes else None
  91. for cls in self.config["CLASSES_TO_EVAL"]
  92. ]
  93. if not all(self.class_list):
  94. valid_cls = ", ".join(self.valid_classes)
  95. raise TrackEvalException(
  96. "Attempted to evaluate an invalid class. Only classes "
  97. f"{valid_cls} are valid (classes present in ground truth"
  98. " data)."
  99. )
  100. else:
  101. self.class_list = [cls for cls in self.valid_classes]
  102. self.cls_name2clsid = {
  103. k: v for k, v in cls_name2clsid_map.items() if k in self.class_list
  104. }
  105. self.clsid2cls_name = {
  106. v: k for k, v in cls_name2clsid_map.items() if k in self.class_list
  107. }
  108. # get trackers to eval
  109. print(self.config["TRACKERS_TO_EVAL"] )
  110. if self.config["TRACKERS_TO_EVAL"] is None:
  111. self.tracker_list = os.listdir(self.tracker_fol)
  112. else:
  113. self.tracker_list = self.config["TRACKERS_TO_EVAL"]
  114. if self.config["TRACKER_DISPLAY_NAMES"] is None:
  115. self.tracker_to_disp = dict(zip(self.tracker_list, self.tracker_list))
  116. elif (self.config["TRACKERS_TO_EVAL"] is not None) and (
  117. len(self.config["TK_DISPLAY_NAMES"]) == len(self.tracker_list)
  118. ):
  119. self.tracker_to_disp = dict(
  120. zip(self.tracker_list, self.config["TK_DISPLAY_NAMES"])
  121. )
  122. else:
  123. raise TrackEvalException(
  124. "List of tracker files and tracker display names do not match."
  125. )
  126. self.tracker_data = {tracker: dict() for tracker in self.tracker_list}
  127. for tracker in self.tracker_list:
  128. if self.tracker_sub_fol.endswith(".json"):
  129. with open(os.path.join(self.tracker_sub_fol)) as f:
  130. curr_data = json.load(f)
  131. else:
  132. tr_dir = os.path.join(self.tracker_fol, tracker, self.tracker_sub_fol)
  133. tr_dir_files = [
  134. file for file in os.listdir(tr_dir) if file.endswith(".json")
  135. ]
  136. if len(tr_dir_files) != 1:
  137. raise TrackEvalException(
  138. f"{tr_dir} does not contain exactly one json file."
  139. )
  140. with open(os.path.join(tr_dir, tr_dir_files[0])) as f:
  141. curr_data = json.load(f)
  142. # limit detections if MAX_DETECTIONS > 0
  143. if self.config["MAX_DETECTIONS"]:
  144. curr_data = self._limit_dets_per_image(curr_data)
  145. # fill missing video ids
  146. self._fill_video_ids_inplace(curr_data)
  147. # make track ids unique over whole evaluation set
  148. self._make_tk_ids_unique(curr_data)
  149. # merge categories marked with a merged tag in TAO dataset
  150. self._merge_categories(curr_data)
  151. # get tracker sequence information
  152. curr_vids2tracks, curr_vids2images = self._compute_vid_mappings(curr_data)
  153. self.tracker_data[tracker]["vids_to_tracks"] = curr_vids2tracks
  154. self.tracker_data[tracker]["vids_to_images"] = curr_vids2images
  155. def get_display_name(self, tracker):
  156. return self.tracker_to_disp[tracker]
  157. def _load_raw_file(self, tracker, seq, is_gt):
  158. """Load a file (gt or tracker) in the TAO format
  159. If is_gt, this returns a dict which contains the fields:
  160. [gt_ids, gt_classes]:
  161. list (for each timestep) of 1D NDArrays (for each det).
  162. [gt_dets]: list (for each timestep) of lists of detections.
  163. if not is_gt, this returns a dict which contains the fields:
  164. [tk_ids, tk_classes, tk_confidences]:
  165. list (for each timestep) of 1D NDArrays (for each det).
  166. [tk_dets]: list (for each timestep) of lists of detections.
  167. """
  168. seq_id = self.seq_name2seqid[seq]
  169. # file location
  170. if is_gt:
  171. imgs = self.video2gt_image[seq_id]
  172. else:
  173. imgs = self.tracker_data[tracker]["vids_to_images"][seq_id]
  174. # convert data to required format
  175. num_timesteps = self.seq_lengths[seq_id]
  176. img_to_timestep = self.seq2images2timestep[seq_id]
  177. data_keys = ["ids", "classes", "dets"]
  178. if not is_gt:
  179. data_keys += ["tk_confidences"]
  180. raw_data = {key: [None] * num_timesteps for key in data_keys}
  181. for img in imgs:
  182. # some tracker data contains images without any ground truth info,
  183. # these are ignored
  184. if img["id"] not in img_to_timestep:
  185. continue
  186. t = img_to_timestep[img["id"]]
  187. anns = img["annotations"]
  188. if self.use_mask:
  189. # When using mask, extract segmentation data
  190. raw_data["dets"][t] = [ann.get("segmentation") for ann in anns]
  191. else:
  192. # When using bbox, extract bbox data
  193. raw_data["dets"][t] = np.atleast_2d([ann["bbox"] for ann in anns]).astype(
  194. float
  195. )
  196. raw_data["ids"][t] = np.atleast_1d(
  197. [ann["track_id"] for ann in anns]
  198. ).astype(int)
  199. raw_data["classes"][t] = np.atleast_1d(
  200. [ann["category_id"] for ann in anns]
  201. ).astype(int)
  202. if not is_gt:
  203. raw_data["tk_confidences"][t] = np.atleast_1d(
  204. [ann["score"] for ann in anns]
  205. ).astype(float)
  206. for t, d in enumerate(raw_data["dets"]):
  207. if d is None:
  208. raw_data["dets"][t] = np.empty((0, 4)).astype(float)
  209. raw_data["ids"][t] = np.empty(0).astype(int)
  210. raw_data["classes"][t] = np.empty(0).astype(int)
  211. if not is_gt:
  212. raw_data["tk_confidences"][t] = np.empty(0)
  213. if is_gt:
  214. key_map = {"ids": "gt_ids", "classes": "gt_classes", "dets": "gt_dets"}
  215. else:
  216. key_map = {"ids": "tk_ids", "classes": "tk_classes", "dets": "tk_dets"}
  217. for k, v in key_map.items():
  218. raw_data[v] = raw_data.pop(k)
  219. raw_data["num_timesteps"] = num_timesteps
  220. raw_data["neg_cat_ids"] = self.seq2cls[seq_id]["neg_cat_ids"]
  221. raw_data["not_exh_labeled_cls"] = self.seq2cls[seq_id][
  222. "not_exh_labeled_cat_ids"
  223. ]
  224. raw_data["seq"] = seq
  225. return raw_data
  226. def get_preprocessed_seq_data_thr(self, raw_data, cls, assignment=None):
  227. """Preprocess data for a single sequence for a single class.
  228. Inputs:
  229. raw_data: dict containing the data for the sequence already
  230. read in by get_raw_seq_data().
  231. cls: class to be evaluated.
  232. Outputs:
  233. gt_ids:
  234. list (for each timestep) of ids of GT tracks
  235. tk_ids:
  236. list (for each timestep) of ids of predicted tracks (all for TP
  237. matching (Det + AssocA))
  238. tk_overlap_ids:
  239. list (for each timestep) of ids of predicted tracks that overlap
  240. with GTs
  241. tk_neg_ids:
  242. list (for each timestep) of ids of predicted tracks that with
  243. the class id on the negative list for the current sequence.
  244. tk_exh_ids:
  245. list (for each timestep) of ids of predicted tracks that do not
  246. overlap with existing GTs but have the class id on the
  247. exhaustive annotated class list for the current sequence.
  248. tk_dets:
  249. list (for each timestep) of lists of detections that
  250. corresponding to the tk_ids
  251. tk_classes:
  252. list (for each timestep) of lists of classes that corresponding
  253. to the tk_ids
  254. tk_confidences:
  255. list (for each timestep) of lists of classes that corresponding
  256. to the tk_ids
  257. sim_scores:
  258. similarity score between gt_ids and tk_ids.
  259. """
  260. if cls != "all":
  261. cls_id = self.cls_name2clsid[cls]
  262. data_keys = [
  263. "gt_ids",
  264. "tk_ids",
  265. "gt_id_map",
  266. "tk_id_map",
  267. "gt_dets",
  268. "gt_classes",
  269. "gt_class_name",
  270. "tk_overlap_classes",
  271. "tk_overlap_ids",
  272. "tk_neg_ids",
  273. "tk_exh_ids",
  274. "tk_class_eval_tk_ids",
  275. "tk_dets",
  276. "tk_classes",
  277. "tk_confidences",
  278. "sim_scores",
  279. ]
  280. data = {key: [None] * raw_data["num_timesteps"] for key in data_keys}
  281. unique_gt_ids = []
  282. unique_tk_ids = []
  283. num_gt_dets = 0
  284. num_tk_cls_dets = 0
  285. num_tk_overlap_dets = 0
  286. overlap_ious_thr = 0.5
  287. loc_and_asso_tk_ids = []
  288. for t in range(raw_data["num_timesteps"]):
  289. # only extract relevant dets for this class for preproc and eval
  290. if cls == "all":
  291. gt_class_mask = np.ones_like(raw_data["gt_classes"][t]).astype(bool)
  292. else:
  293. gt_class_mask = np.atleast_1d(
  294. raw_data["gt_classes"][t] == cls_id
  295. ).astype(bool)
  296. # select GT that is not in the evaluating classes
  297. if assignment is not None and assignment:
  298. all_gt_ids = list(assignment[t].keys())
  299. gt_ids_in = raw_data["gt_ids"][t][gt_class_mask]
  300. gt_ids_out = set(all_gt_ids) - set(gt_ids_in)
  301. tk_ids_out = set([assignment[t][key] for key in list(gt_ids_out)])
  302. # compute overlapped tracks and add their ids to overlap_tk_ids
  303. sim_scores = raw_data["similarity_scores"]
  304. overlap_ids_masks = (sim_scores[t][gt_class_mask] >= overlap_ious_thr).any(
  305. axis=0
  306. )
  307. overlap_tk_ids_t = raw_data["tk_ids"][t][overlap_ids_masks]
  308. if assignment is not None and assignment:
  309. data["tk_overlap_ids"][t] = list(set(overlap_tk_ids_t) - tk_ids_out)
  310. else:
  311. data["tk_overlap_ids"][t] = list(set(overlap_tk_ids_t))
  312. loc_and_asso_tk_ids += data["tk_overlap_ids"][t]
  313. data["tk_exh_ids"][t] = []
  314. data["tk_neg_ids"][t] = []
  315. if cls == "all":
  316. continue
  317. # remove tk_ids that has been assigned to GT belongs to other classes.
  318. loc_and_asso_tk_ids = list(set(loc_and_asso_tk_ids))
  319. # remove all unwanted unmatched tracker detections
  320. for t in range(raw_data["num_timesteps"]):
  321. # add gt to the data
  322. if cls == "all":
  323. gt_class_mask = np.ones_like(raw_data["gt_classes"][t]).astype(bool)
  324. else:
  325. gt_class_mask = np.atleast_1d(
  326. raw_data["gt_classes"][t] == cls_id
  327. ).astype(bool)
  328. data["gt_classes"][t] = cls_id
  329. data["gt_class_name"][t] = cls
  330. gt_ids = raw_data["gt_ids"][t][gt_class_mask]
  331. if self.use_mask:
  332. gt_dets = [raw_data['gt_dets'][t][ind] for ind in range(len(gt_class_mask)) if gt_class_mask[ind]]
  333. else:
  334. gt_dets = raw_data["gt_dets"][t][gt_class_mask]
  335. data["gt_ids"][t] = gt_ids
  336. data["gt_dets"][t] = gt_dets
  337. # filter pred and only keep those that highly overlap with GTs
  338. tk_mask = np.isin(
  339. raw_data["tk_ids"][t], np.array(loc_and_asso_tk_ids), assume_unique=True
  340. )
  341. tk_overlap_mask = np.isin(
  342. raw_data["tk_ids"][t],
  343. np.array(data["tk_overlap_ids"][t]),
  344. assume_unique=True,
  345. )
  346. tk_ids = raw_data["tk_ids"][t][tk_mask]
  347. if self.use_mask:
  348. tk_dets = [raw_data['tk_dets'][t][ind] for ind in range(len(tk_mask)) if
  349. tk_mask[ind]]
  350. else:
  351. tk_dets = raw_data["tk_dets"][t][tk_mask]
  352. tracker_classes = raw_data["tk_classes"][t][tk_mask]
  353. # add overlap classes for computing the FP for Cls term
  354. tracker_overlap_classes = raw_data["tk_classes"][t][tk_overlap_mask]
  355. tracker_confidences = raw_data["tk_confidences"][t][tk_mask]
  356. sim_scores_masked = sim_scores[t][gt_class_mask, :][:, tk_mask]
  357. # add filtered prediction to the data
  358. data["tk_classes"][t] = tracker_classes
  359. data["tk_overlap_classes"][t] = tracker_overlap_classes
  360. data["tk_ids"][t] = tk_ids
  361. data["tk_dets"][t] = tk_dets
  362. data["tk_confidences"][t] = tracker_confidences
  363. data["sim_scores"][t] = sim_scores_masked
  364. data["tk_class_eval_tk_ids"][t] = set(
  365. list(data["tk_overlap_ids"][t])
  366. + list(data["tk_neg_ids"][t])
  367. + list(data["tk_exh_ids"][t])
  368. )
  369. # count total number of detections
  370. unique_gt_ids += list(np.unique(data["gt_ids"][t]))
  371. # the unique track ids are for association.
  372. unique_tk_ids += list(np.unique(data["tk_ids"][t]))
  373. num_tk_overlap_dets += len(data["tk_overlap_ids"][t])
  374. num_tk_cls_dets += len(data["tk_class_eval_tk_ids"][t])
  375. num_gt_dets += len(data["gt_ids"][t])
  376. # re-label IDs such that there are no empty IDs
  377. if len(unique_gt_ids) > 0:
  378. unique_gt_ids = np.unique(unique_gt_ids)
  379. gt_id_map = np.nan * np.ones((np.max(unique_gt_ids) + 1))
  380. gt_id_map[unique_gt_ids] = np.arange(len(unique_gt_ids))
  381. data["gt_id_map"] = {}
  382. for gt_id in unique_gt_ids:
  383. new_gt_id = gt_id_map[gt_id].astype(int)
  384. data["gt_id_map"][new_gt_id] = gt_id
  385. for t in range(raw_data["num_timesteps"]):
  386. if len(data["gt_ids"][t]) > 0:
  387. data["gt_ids"][t] = gt_id_map[data["gt_ids"][t]].astype(int)
  388. if len(unique_tk_ids) > 0:
  389. unique_tk_ids = np.unique(unique_tk_ids)
  390. tk_id_map = np.nan * np.ones((np.max(unique_tk_ids) + 1))
  391. tk_id_map[unique_tk_ids] = np.arange(len(unique_tk_ids))
  392. data["tk_id_map"] = {}
  393. for track_id in unique_tk_ids:
  394. new_track_id = tk_id_map[track_id].astype(int)
  395. data["tk_id_map"][new_track_id] = track_id
  396. for t in range(raw_data["num_timesteps"]):
  397. if len(data["tk_ids"][t]) > 0:
  398. data["tk_ids"][t] = tk_id_map[data["tk_ids"][t]].astype(int)
  399. if len(data["tk_overlap_ids"][t]) > 0:
  400. data["tk_overlap_ids"][t] = tk_id_map[
  401. data["tk_overlap_ids"][t]
  402. ].astype(int)
  403. # record overview statistics.
  404. data["num_tk_cls_dets"] = num_tk_cls_dets
  405. data["num_tk_overlap_dets"] = num_tk_overlap_dets
  406. data["num_gt_dets"] = num_gt_dets
  407. data["num_tk_ids"] = len(unique_tk_ids)
  408. data["num_gt_ids"] = len(unique_gt_ids)
  409. data["num_timesteps"] = raw_data["num_timesteps"]
  410. data["seq"] = raw_data["seq"]
  411. self._check_unique_ids(data)
  412. return data
  413. @_timing.time
  414. def get_preprocessed_seq_data(
  415. self, raw_data, cls, assignment=None, thresholds=[50, 75]
  416. ):
  417. """Preprocess data for a single sequence for a single class."""
  418. data = {}
  419. if thresholds is None:
  420. thresholds = [50]
  421. elif isinstance(thresholds, int):
  422. thresholds = [thresholds]
  423. for thr in thresholds:
  424. assignment_thr = None
  425. if assignment is not None:
  426. assignment_thr = assignment[thr]
  427. data[thr] = self.get_preprocessed_seq_data_thr(
  428. raw_data, cls, assignment_thr
  429. )
  430. return data
  431. def _calculate_similarities(self, gt_dets_t, tk_dets_t):
  432. """Compute similarity scores."""
  433. if self.use_mask:
  434. similarity_scores = self._calculate_mask_ious(gt_dets_t, tk_dets_t, is_encoded=True, do_ioa=False)
  435. else:
  436. similarity_scores = self._calculate_box_ious(gt_dets_t, tk_dets_t)
  437. return similarity_scores
  438. def _merge_categories(self, annotations):
  439. """Merges categories with a merged tag.
  440. Adapted from https://github.com/TAO-Dataset.
  441. """
  442. merge_map = {}
  443. for category in self.gt_data["categories"]:
  444. if "merged" in category:
  445. for to_merge in category["merged"]:
  446. merge_map[to_merge["id"]] = category["id"]
  447. for ann in annotations:
  448. ann["category_id"] = merge_map.get(ann["category_id"], ann["category_id"])
  449. def _compute_vid_mappings(self, annotations):
  450. """Computes mappings from videos to corresponding tracks and images."""
  451. vids_to_tracks = {}
  452. vids_to_imgs = {}
  453. vid_ids = [vid["id"] for vid in self.gt_data["videos"]]
  454. # compute an mapping from image IDs to images
  455. images = {}
  456. for image in self.gt_data["images"]:
  457. images[image["id"]] = image
  458. for ann in annotations:
  459. ann["area"] = ann["bbox"][2] * ann["bbox"][3]
  460. vid = ann["video_id"]
  461. if ann["video_id"] not in vids_to_tracks.keys():
  462. vids_to_tracks[ann["video_id"]] = list()
  463. if ann["video_id"] not in vids_to_imgs.keys():
  464. vids_to_imgs[ann["video_id"]] = list()
  465. # fill in vids_to_tracks
  466. tid = ann["track_id"]
  467. exist_tids = [track["id"] for track in vids_to_tracks[vid]]
  468. try:
  469. index1 = exist_tids.index(tid)
  470. except ValueError:
  471. index1 = -1
  472. if tid not in exist_tids:
  473. curr_track = {
  474. "id": tid,
  475. "category_id": ann["category_id"],
  476. "video_id": vid,
  477. "annotations": [ann],
  478. }
  479. vids_to_tracks[vid].append(curr_track)
  480. else:
  481. vids_to_tracks[vid][index1]["annotations"].append(ann)
  482. # fill in vids_to_imgs
  483. img_id = ann["image_id"]
  484. exist_img_ids = [img["id"] for img in vids_to_imgs[vid]]
  485. try:
  486. index2 = exist_img_ids.index(img_id)
  487. except ValueError:
  488. index2 = -1
  489. if index2 == -1:
  490. curr_img = {"id": img_id, "annotations": [ann]}
  491. vids_to_imgs[vid].append(curr_img)
  492. else:
  493. vids_to_imgs[vid][index2]["annotations"].append(ann)
  494. # sort annotations by frame index and compute track area
  495. for vid, tracks in vids_to_tracks.items():
  496. for track in tracks:
  497. track["annotations"] = sorted(
  498. track["annotations"],
  499. key=lambda x: images[x["image_id"]]["frame_index"],
  500. )
  501. # compute average area
  502. track["area"] = sum(x["area"] for x in track["annotations"]) / len(
  503. track["annotations"]
  504. )
  505. # ensure all videos are present
  506. for vid_id in vid_ids:
  507. if vid_id not in vids_to_tracks.keys():
  508. vids_to_tracks[vid_id] = []
  509. if vid_id not in vids_to_imgs.keys():
  510. vids_to_imgs[vid_id] = []
  511. return vids_to_tracks, vids_to_imgs
  512. def _compute_image_to_timestep_mappings(self):
  513. """Computes a mapping from images to timestep in sequence."""
  514. images = {}
  515. for image in self.gt_data["images"]:
  516. images[image["id"]] = image
  517. seq_to_imgs_to_timestep = {vid["id"]: dict() for vid in self.gt_data["videos"]}
  518. for vid in seq_to_imgs_to_timestep:
  519. curr_imgs = [img["id"] for img in self.video2gt_image[vid]]
  520. curr_imgs = sorted(curr_imgs, key=lambda x: images[x]["frame_index"])
  521. seq_to_imgs_to_timestep[vid] = {
  522. curr_imgs[i]: i for i in range(len(curr_imgs))
  523. }
  524. return seq_to_imgs_to_timestep
  525. def _limit_dets_per_image(self, annotations):
  526. """Limits the number of detections for each image.
  527. Adapted from https://github.com/TAO-Dataset/.
  528. """
  529. max_dets = self.config["MAX_DETECTIONS"]
  530. img_ann = defaultdict(list)
  531. for ann in annotations:
  532. img_ann[ann["image_id"]].append(ann)
  533. for img_id, _anns in img_ann.items():
  534. if len(_anns) <= max_dets:
  535. continue
  536. _anns = sorted(_anns, key=lambda x: x["score"], reverse=True)
  537. img_ann[img_id] = _anns[:max_dets]
  538. return [ann for anns in img_ann.values() for ann in anns]
  539. def _fill_video_ids_inplace(self, annotations):
  540. """Fills in missing video IDs inplace.
  541. Adapted from https://github.com/TAO-Dataset/.
  542. """
  543. missing_video_id = [x for x in annotations if "video_id" not in x]
  544. if missing_video_id:
  545. image_id_to_video_id = {
  546. x["id"]: x["video_id"] for x in self.gt_data["images"]
  547. }
  548. for x in missing_video_id:
  549. x["video_id"] = image_id_to_video_id[x["image_id"]]
  550. @staticmethod
  551. def _make_tk_ids_unique(annotations):
  552. """Makes track IDs unqiue over the whole annotation set.
  553. Adapted from https://github.com/TAO-Dataset/.
  554. """
  555. track_id_videos = {}
  556. track_ids_to_update = set()
  557. max_track_id = 0
  558. for ann in annotations:
  559. t = ann["track_id"]
  560. if t not in track_id_videos:
  561. track_id_videos[t] = ann["video_id"]
  562. if ann["video_id"] != track_id_videos[t]:
  563. # track id is assigned to multiple videos
  564. track_ids_to_update.add(t)
  565. max_track_id = max(max_track_id, t)
  566. if track_ids_to_update:
  567. print("true")
  568. next_id = itertools.count(max_track_id + 1)
  569. new_tk_ids = defaultdict(lambda: next(next_id))
  570. for ann in annotations:
  571. t = ann["track_id"]
  572. v = ann["video_id"]
  573. if t in track_ids_to_update:
  574. ann["track_id"] = new_tk_ids[t, v]
  575. return len(track_ids_to_update)