You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

coco_panoptic.py 22 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import itertools
  3. import os
  4. from collections import defaultdict
  5. import mmcv
  6. import numpy as np
  7. from mmcv.utils import print_log
  8. from terminaltables import AsciiTable
  9. from .api_wrappers import COCO
  10. from .builder import DATASETS
  11. from .coco import CocoDataset
  12. try:
  13. import panopticapi
  14. from panopticapi.evaluation import pq_compute_multi_core, VOID
  15. from panopticapi.utils import id2rgb
  16. except ImportError:
  17. panopticapi = None
  18. pq_compute_multi_core = None
  19. id2rgb = None
  20. VOID = None
  21. __all__ = ['CocoPanopticDataset']
  22. # A custom value to distinguish instance ID and category ID; need to
  23. # be greater than the number of categories.
  24. # For a pixel in the panoptic result map:
  25. # pan_id = ins_id * INSTANCE_OFFSET + cat_id
  26. INSTANCE_OFFSET = 1000
  27. class COCOPanoptic(COCO):
  28. """This wrapper is for loading the panoptic style annotation file.
  29. The format is shown in the CocoPanopticDataset class.
  30. Args:
  31. annotation_file (str): Path of annotation file.
  32. """
  33. def __init__(self, annotation_file=None):
  34. if panopticapi is None:
  35. raise RuntimeError(
  36. 'panopticapi is not installed, please install it by: '
  37. 'pip install git+https://github.com/cocodataset/'
  38. 'panopticapi.git.')
  39. super(COCOPanoptic, self).__init__(annotation_file)
  40. def createIndex(self):
  41. # create index
  42. print('creating index...')
  43. # anns stores 'segment_id -> annotation'
  44. anns, cats, imgs = {}, {}, {}
  45. img_to_anns, cat_to_imgs = defaultdict(list), defaultdict(list)
  46. if 'annotations' in self.dataset:
  47. for ann, img_info in zip(self.dataset['annotations'],
  48. self.dataset['images']):
  49. img_info['segm_file'] = ann['file_name']
  50. for seg_ann in ann['segments_info']:
  51. # to match with instance.json
  52. seg_ann['image_id'] = ann['image_id']
  53. seg_ann['height'] = img_info['height']
  54. seg_ann['width'] = img_info['width']
  55. img_to_anns[ann['image_id']].append(seg_ann)
  56. # segment_id is not unique in coco dataset orz...
  57. if seg_ann['id'] in anns.keys():
  58. anns[seg_ann['id']].append(seg_ann)
  59. else:
  60. anns[seg_ann['id']] = [seg_ann]
  61. if 'images' in self.dataset:
  62. for img in self.dataset['images']:
  63. imgs[img['id']] = img
  64. if 'categories' in self.dataset:
  65. for cat in self.dataset['categories']:
  66. cats[cat['id']] = cat
  67. if 'annotations' in self.dataset and 'categories' in self.dataset:
  68. for ann in self.dataset['annotations']:
  69. for seg_ann in ann['segments_info']:
  70. cat_to_imgs[seg_ann['category_id']].append(ann['image_id'])
  71. print('index created!')
  72. self.anns = anns
  73. self.imgToAnns = img_to_anns
  74. self.catToImgs = cat_to_imgs
  75. self.imgs = imgs
  76. self.cats = cats
  77. def load_anns(self, ids=[]):
  78. """Load anns with the specified ids.
  79. self.anns is a list of annotation lists instead of a
  80. list of annotations.
  81. Args:
  82. ids (int array): integer ids specifying anns
  83. Returns:
  84. anns (object array): loaded ann objects
  85. """
  86. anns = []
  87. if hasattr(ids, '__iter__') and hasattr(ids, '__len__'):
  88. # self.anns is a list of annotation lists instead of
  89. # a list of annotations
  90. for id in ids:
  91. anns += self.anns[id]
  92. return anns
  93. elif type(ids) == int:
  94. return self.anns[ids]
  95. @DATASETS.register_module()
  96. class CocoPanopticDataset(CocoDataset):
  97. """Coco dataset for Panoptic segmentation.
  98. The annotation format is shown as follows. The `ann` field is optional
  99. for testing.
  100. .. code-block:: none
  101. [
  102. {
  103. 'filename': f'{image_id:012}.png',
  104. 'image_id':9
  105. 'segments_info': {
  106. [
  107. {
  108. 'id': 8345037, (segment_id in panoptic png,
  109. convert from rgb)
  110. 'category_id': 51,
  111. 'iscrowd': 0,
  112. 'bbox': (x1, y1, w, h),
  113. 'area': 24315,
  114. 'segmentation': list,(coded mask)
  115. },
  116. ...
  117. }
  118. }
  119. },
  120. ...
  121. ]
  122. """
  123. CLASSES = [
  124. 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
  125. ' truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
  126. 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
  127. 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
  128. 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
  129. 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
  130. 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
  131. 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
  132. 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
  133. 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
  134. 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
  135. 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
  136. 'scissors', 'teddy bear', 'hair drier', 'toothbrush', 'banner',
  137. 'blanket', 'bridge', 'cardboard', 'counter', 'curtain', 'door-stuff',
  138. 'floor-wood', 'flower', 'fruit', 'gravel', 'house', 'light',
  139. 'mirror-stuff', 'net', 'pillow', 'platform', 'playingfield',
  140. 'railroad', 'river', 'road', 'roof', 'sand', 'sea', 'shelf', 'snow',
  141. 'stairs', 'tent', 'towel', 'wall-brick', 'wall-stone', 'wall-tile',
  142. 'wall-wood', 'water-other', 'window-blind', 'window-other',
  143. 'tree-merged', 'fence-merged', 'ceiling-merged', 'sky-other-merged',
  144. 'cabinet-merged', 'table-merged', 'floor-other-merged',
  145. 'pavement-merged', 'mountain-merged', 'grass-merged', 'dirt-merged',
  146. 'paper-merged', 'food-other-merged', 'building-other-merged',
  147. 'rock-merged', 'wall-other-merged', 'rug-merged'
  148. ]
  149. THING_CLASSES = [
  150. 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
  151. 'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
  152. 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
  153. 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
  154. 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
  155. 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
  156. 'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
  157. 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
  158. 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
  159. 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
  160. 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
  161. 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
  162. 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
  163. ]
  164. STUFF_CLASSES = [
  165. 'banner', 'blanket', 'bridge', 'cardboard', 'counter', 'curtain',
  166. 'door-stuff', 'floor-wood', 'flower', 'fruit', 'gravel', 'house',
  167. 'light', 'mirror-stuff', 'net', 'pillow', 'platform', 'playingfield',
  168. 'railroad', 'river', 'road', 'roof', 'sand', 'sea', 'shelf', 'snow',
  169. 'stairs', 'tent', 'towel', 'wall-brick', 'wall-stone', 'wall-tile',
  170. 'wall-wood', 'water-other', 'window-blind', 'window-other',
  171. 'tree-merged', 'fence-merged', 'ceiling-merged', 'sky-other-merged',
  172. 'cabinet-merged', 'table-merged', 'floor-other-merged',
  173. 'pavement-merged', 'mountain-merged', 'grass-merged', 'dirt-merged',
  174. 'paper-merged', 'food-other-merged', 'building-other-merged',
  175. 'rock-merged', 'wall-other-merged', 'rug-merged'
  176. ]
  177. def load_annotations(self, ann_file):
  178. """Load annotation from COCO Panoptic style annotation file.
  179. Args:
  180. ann_file (str): Path of annotation file.
  181. Returns:
  182. list[dict]: Annotation info from COCO api.
  183. """
  184. self.coco = COCOPanoptic(ann_file)
  185. self.cat_ids = self.coco.get_cat_ids()
  186. self.cat2label = {cat_id: i for i, cat_id in enumerate(self.cat_ids)}
  187. self.categories = self.coco.cats
  188. self.img_ids = self.coco.get_img_ids()
  189. data_infos = []
  190. for i in self.img_ids:
  191. info = self.coco.load_imgs([i])[0]
  192. info['filename'] = info['file_name']
  193. info['segm_file'] = info['filename'].replace('jpg', 'png')
  194. data_infos.append(info)
  195. return data_infos
  196. def get_ann_info(self, idx):
  197. """Get COCO annotation by index.
  198. Args:
  199. idx (int): Index of data.
  200. Returns:
  201. dict: Annotation info of specified index.
  202. """
  203. img_id = self.data_infos[idx]['id']
  204. ann_ids = self.coco.get_ann_ids(img_ids=[img_id])
  205. ann_info = self.coco.load_anns(ann_ids)
  206. # filter out unmatched images
  207. ann_info = [i for i in ann_info if i['image_id'] == img_id]
  208. return self._parse_ann_info(self.data_infos[idx], ann_info)
  209. def _parse_ann_info(self, img_info, ann_info):
  210. """Parse annotations and load panoptic ground truths.
  211. Args:
  212. img_info (int): Image info of an image.
  213. ann_info (list[dict]): Annotation info of an image.
  214. Returns:
  215. dict: A dict containing the following keys: bboxes, bboxes_ignore,
  216. labels, masks, seg_map.
  217. """
  218. gt_bboxes = []
  219. gt_labels = []
  220. gt_bboxes_ignore = []
  221. gt_mask_infos = []
  222. for i, ann in enumerate(ann_info):
  223. x1, y1, w, h = ann['bbox']
  224. if ann['area'] <= 0 or w < 1 or h < 1:
  225. continue
  226. bbox = [x1, y1, x1 + w, y1 + h]
  227. category_id = ann['category_id']
  228. contiguous_cat_id = self.cat2label[category_id]
  229. is_thing = self.coco.load_cats(ids=category_id)[0]['isthing']
  230. if is_thing:
  231. is_crowd = ann.get('iscrowd', False)
  232. if not is_crowd:
  233. gt_bboxes.append(bbox)
  234. gt_labels.append(contiguous_cat_id)
  235. else:
  236. gt_bboxes_ignore.append(bbox)
  237. is_thing = False
  238. mask_info = {
  239. 'id': ann['id'],
  240. 'category': contiguous_cat_id,
  241. 'is_thing': is_thing
  242. }
  243. gt_mask_infos.append(mask_info)
  244. if gt_bboxes:
  245. gt_bboxes = np.array(gt_bboxes, dtype=np.float32)
  246. gt_labels = np.array(gt_labels, dtype=np.int64)
  247. else:
  248. gt_bboxes = np.zeros((0, 4), dtype=np.float32)
  249. gt_labels = np.array([], dtype=np.int64)
  250. if gt_bboxes_ignore:
  251. gt_bboxes_ignore = np.array(gt_bboxes_ignore, dtype=np.float32)
  252. else:
  253. gt_bboxes_ignore = np.zeros((0, 4), dtype=np.float32)
  254. ann = dict(
  255. bboxes=gt_bboxes,
  256. labels=gt_labels,
  257. bboxes_ignore=gt_bboxes_ignore,
  258. masks=gt_mask_infos,
  259. seg_map=img_info['segm_file'])
  260. return ann
  261. def _filter_imgs(self, min_size=32):
  262. """Filter images too small or without ground truths."""
  263. ids_with_ann = []
  264. # check whether images have legal thing annotations.
  265. for lists in self.coco.anns.values():
  266. for item in lists:
  267. category_id = item['category_id']
  268. is_thing = self.coco.load_cats(ids=category_id)[0]['isthing']
  269. if not is_thing:
  270. continue
  271. ids_with_ann.append(item['image_id'])
  272. ids_with_ann = set(ids_with_ann)
  273. valid_inds = []
  274. valid_img_ids = []
  275. for i, img_info in enumerate(self.data_infos):
  276. img_id = self.img_ids[i]
  277. if self.filter_empty_gt and img_id not in ids_with_ann:
  278. continue
  279. if min(img_info['width'], img_info['height']) >= min_size:
  280. valid_inds.append(i)
  281. valid_img_ids.append(img_id)
  282. self.img_ids = valid_img_ids
  283. return valid_inds
  284. def _pan2json(self, results, outfile_prefix):
  285. """Convert panoptic results to COCO panoptic json style."""
  286. label2cat = dict((v, k) for (k, v) in self.cat2label.items())
  287. pred_annotations = []
  288. outdir = os.path.join(os.path.dirname(outfile_prefix), 'panoptic')
  289. for idx in range(len(self)):
  290. img_id = self.img_ids[idx]
  291. segm_file = self.data_infos[idx]['segm_file']
  292. pan = results[idx]
  293. pan_labels = np.unique(pan)
  294. segm_info = []
  295. for pan_label in pan_labels:
  296. sem_label = pan_label % INSTANCE_OFFSET
  297. # We reserve the length of self.CLASSES for VOID label
  298. if sem_label == len(self.CLASSES):
  299. continue
  300. # convert sem_label to json label
  301. cat_id = label2cat[sem_label]
  302. is_thing = self.categories[cat_id]['isthing']
  303. mask = pan == pan_label
  304. area = mask.sum()
  305. segm_info.append({
  306. 'id': int(pan_label),
  307. 'category_id': cat_id,
  308. 'isthing': is_thing,
  309. 'area': int(area)
  310. })
  311. # evaluation script uses 0 for VOID label.
  312. pan[pan % INSTANCE_OFFSET == len(self.CLASSES)] = VOID
  313. pan = id2rgb(pan).astype(np.uint8)
  314. mmcv.imwrite(pan[:, :, ::-1], os.path.join(outdir, segm_file))
  315. record = {
  316. 'image_id': img_id,
  317. 'segments_info': segm_info,
  318. 'file_name': segm_file
  319. }
  320. pred_annotations.append(record)
  321. pan_json_results = dict(annotations=pred_annotations)
  322. return pan_json_results
  323. def results2json(self, results, outfile_prefix):
  324. """Dump the panoptic results to a COCO panoptic style json file.
  325. Args:
  326. results (dict): Testing results of the dataset.
  327. outfile_prefix (str): The filename prefix of the json files. If the
  328. prefix is "somepath/xxx", the json files will be named
  329. "somepath/xxx.panoptic.json"
  330. Returns:
  331. dict[str: str]: The key is 'panoptic' and the value is
  332. corresponding filename.
  333. """
  334. result_files = dict()
  335. pan_results = [result['pan_results'] for result in results]
  336. pan_json_results = self._pan2json(pan_results, outfile_prefix)
  337. result_files['panoptic'] = f'{outfile_prefix}.panoptic.json'
  338. mmcv.dump(pan_json_results, result_files['panoptic'])
  339. return result_files
  340. def evaluate_pan_json(self,
  341. result_files,
  342. outfile_prefix,
  343. logger=None,
  344. classwise=False):
  345. """Evaluate PQ according to the panoptic results json file."""
  346. imgs = self.coco.imgs
  347. gt_json = self.coco.img_ann_map # image to annotations
  348. gt_json = [{
  349. 'image_id': k,
  350. 'segments_info': v,
  351. 'file_name': imgs[k]['segm_file']
  352. } for k, v in gt_json.items()]
  353. pred_json = mmcv.load(result_files['panoptic'])
  354. pred_json = dict(
  355. (el['image_id'], el) for el in pred_json['annotations'])
  356. # match the gt_anns and pred_anns in the same image
  357. matched_annotations_list = []
  358. for gt_ann in gt_json:
  359. img_id = gt_ann['image_id']
  360. if img_id not in pred_json.keys():
  361. raise Exception('no prediction for the image'
  362. ' with id: {}'.format(img_id))
  363. matched_annotations_list.append((gt_ann, pred_json[img_id]))
  364. gt_folder = self.seg_prefix
  365. pred_folder = os.path.join(os.path.dirname(outfile_prefix), 'panoptic')
  366. pq_stat = pq_compute_multi_core(matched_annotations_list, gt_folder,
  367. pred_folder, self.categories)
  368. metrics = [('All', None), ('Things', True), ('Stuff', False)]
  369. pq_results = {}
  370. for name, isthing in metrics:
  371. pq_results[name], classwise_results = pq_stat.pq_average(
  372. self.categories, isthing=isthing)
  373. if name == 'All':
  374. pq_results['classwise'] = classwise_results
  375. classwise_results = None
  376. if classwise:
  377. classwise_results = {
  378. k: v
  379. for k, v in zip(self.CLASSES, pq_results['classwise'].values())
  380. }
  381. print_panoptic_table(pq_results, classwise_results, logger=logger)
  382. return parse_pq_results(pq_results)
  383. def evaluate(self,
  384. results,
  385. metric='PQ',
  386. logger=None,
  387. jsonfile_prefix=None,
  388. classwise=False,
  389. **kwargs):
  390. """Evaluation in COCO Panoptic protocol.
  391. Args:
  392. results (list[dict]): Testing results of the dataset.
  393. metric (str | list[str]): Metrics to be evaluated. Only
  394. support 'PQ' at present. 'pq' will be regarded as 'PQ.
  395. logger (logging.Logger | str | None): Logger used for printing
  396. related information during evaluation. Default: None.
  397. jsonfile_prefix (str | None): The prefix of json files. It includes
  398. the file path and the prefix of filename, e.g., "a/b/prefix".
  399. If not specified, a temp file will be created. Default: None.
  400. classwise (bool): Whether to print classwise evaluation results.
  401. Default: False.
  402. Returns:
  403. dict[str, float]: COCO Panoptic style evaluation metric.
  404. """
  405. metrics = metric if isinstance(metric, list) else [metric]
  406. # Compatible with lowercase 'pq'
  407. metrics = ['PQ' if metric == 'pq' else metric for metric in metrics]
  408. allowed_metrics = ['PQ'] # todo: support other metrics like 'bbox'
  409. for metric in metrics:
  410. if metric not in allowed_metrics:
  411. raise KeyError(f'metric {metric} is not supported')
  412. result_files, tmp_dir = self.format_results(results, jsonfile_prefix)
  413. eval_results = {}
  414. outfile_prefix = os.path.join(tmp_dir.name, 'results') \
  415. if tmp_dir is not None else jsonfile_prefix
  416. if 'PQ' in metrics:
  417. eval_pan_results = self.evaluate_pan_json(result_files,
  418. outfile_prefix, logger,
  419. classwise)
  420. eval_results.update(eval_pan_results)
  421. if tmp_dir is not None:
  422. tmp_dir.cleanup()
  423. return eval_results
  424. def parse_pq_results(pq_results):
  425. """Parse the Panoptic Quality results."""
  426. result = dict()
  427. result['PQ'] = 100 * pq_results['All']['pq']
  428. result['SQ'] = 100 * pq_results['All']['sq']
  429. result['RQ'] = 100 * pq_results['All']['rq']
  430. result['PQ_th'] = 100 * pq_results['Things']['pq']
  431. result['SQ_th'] = 100 * pq_results['Things']['sq']
  432. result['RQ_th'] = 100 * pq_results['Things']['rq']
  433. result['PQ_st'] = 100 * pq_results['Stuff']['pq']
  434. result['SQ_st'] = 100 * pq_results['Stuff']['sq']
  435. result['RQ_st'] = 100 * pq_results['Stuff']['rq']
  436. return result
  437. def print_panoptic_table(pq_results, classwise_results=None, logger=None):
  438. """Print the panoptic evaluation results table.
  439. Args:
  440. pq_results(dict): The Panoptic Quality results.
  441. classwise_results(dict | None): The classwise Panoptic Quality results.
  442. The keys are class names and the values are metrics.
  443. logger (logging.Logger | str | None): Logger used for printing
  444. related information during evaluation. Default: None.
  445. """
  446. headers = ['', 'PQ', 'SQ', 'RQ', 'categories']
  447. data = [headers]
  448. for name in ['All', 'Things', 'Stuff']:
  449. numbers = [
  450. f'{(pq_results[name][k] * 100):0.3f}' for k in ['pq', 'sq', 'rq']
  451. ]
  452. row = [name] + numbers + [pq_results[name]['n']]
  453. data.append(row)
  454. table = AsciiTable(data)
  455. print_log('Panoptic Evaluation Results:\n' + table.table, logger=logger)
  456. if classwise_results is not None:
  457. class_metrics = [(name, ) + tuple(f'{(metrics[k] * 100):0.3f}'
  458. for k in ['pq', 'sq', 'rq'])
  459. for name, metrics in classwise_results.items()]
  460. num_columns = min(8, len(class_metrics) * 4)
  461. results_flatten = list(itertools.chain(*class_metrics))
  462. headers = ['category', 'PQ', 'SQ', 'RQ'] * (num_columns // 4)
  463. results_2d = itertools.zip_longest(
  464. *[results_flatten[i::num_columns] for i in range(num_columns)])
  465. data = [headers]
  466. data += [result for result in results_2d]
  467. table = AsciiTable(data)
  468. print_log(
  469. 'Classwise Panoptic Evaluation Results:\n' + table.table,
  470. logger=logger)

No Description

Contributors (1)