@@ -59,6 +59,8 @@ class CocoMetric(BaseMetric):
5959 names to disambiguate homonymous metrics of different evaluators.
6060 If prefix is not provided in the argument, self.default_prefix
6161 will be used instead. Defaults to None.
62+ sort_categories (bool): Whether sort categories in annotations. Only
63+ used for `Objects365V1Dataset`. Defaults to False.
6264 """
6365 default_prefix : Optional [str ] = 'coco'
6466
@@ -73,7 +75,8 @@ def __init__(self,
7375 outfile_prefix : Optional [str ] = None ,
7476 file_client_args : dict = dict (backend = 'disk' ),
7577 collect_device : str = 'cpu' ,
76- prefix : Optional [str ] = None ) -> None :
78+ prefix : Optional [str ] = None ,
79+ sort_categories : bool = False ) -> None :
7780 super ().__init__ (collect_device = collect_device , prefix = prefix )
7881 # coco evaluation metrics
7982 self .metrics = metric if isinstance (metric , list ) else [metric ]
@@ -112,6 +115,17 @@ def __init__(self,
112115 if ann_file is not None :
113116 with self .file_client .get_local_path (ann_file ) as local_path :
114117 self ._coco_api = COCO (local_path )
118+ if sort_categories :
119+ # 'categories' list in objects365_train.json and
120+ # objects365_val.json is inconsistent, need sort
121+ # list(or dict) before get cat_ids.
122+ cats = self ._coco_api .cats
123+ sorted_cats = {i : cats [i ] for i in sorted (cats )}
124+ self ._coco_api .cats = sorted_cats
125+ categories = self ._coco_api .dataset ['categories' ]
126+ sorted_categories = sorted (
127+ categories , key = lambda i : i ['id' ])
128+ self ._coco_api .dataset ['categories' ] = sorted_categories
115129 else :
116130 self ._coco_api = None
117131
0 commit comments