def test_topdown_evaluate(self):
        """test topdown-style COCO metric evaluation."""
        # case 1: score_mode='bbox', nms_mode='none'
        metric_coco = CocoMetric(
            ann_file=self.ann_file_coco,
            outfile_prefix=f'{self.tmp_dir.name}/test1',
            score_mode='bbox',
            nms_mode='none')
        metric_coco.dataset_meta = self.dataset_meta_coco

        # process samples
        for data_batch, data_samples in self.topdown_data_coco:
            metric_coco.process(data_batch, data_samples)

        eval_results = metric_coco.evaluate(size=len(self.topdown_data_coco))

        self.assertDictEqual(eval_results, self.target_coco)
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test1.keypoints.json')))

        # case 2: score_mode='bbox_keypoint', nms_mode='oks_nms'
        metric_coco = CocoMetric(
            ann_file=self.ann_file_coco,
            outfile_prefix=f'{self.tmp_dir.name}/test2',
            score_mode='bbox_keypoint',
            nms_mode='oks_nms')
        metric_coco.dataset_meta = self.dataset_meta_coco

        # process samples
        for data_batch, data_samples in self.topdown_data_coco:
            metric_coco.process(data_batch, data_samples)

        eval_results = metric_coco.evaluate(size=len(self.topdown_data_coco))

        self.assertDictEqual(eval_results, self.target_coco)
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test2.keypoints.json')))

        # case 3: score_mode='bbox_rle', nms_mode='soft_oks_nms'
        metric_coco = CocoMetric(
            ann_file=self.ann_file_coco,
            outfile_prefix=f'{self.tmp_dir.name}/test3',
            score_mode='bbox_rle',
            nms_mode='soft_oks_nms')
        metric_coco.dataset_meta = self.dataset_meta_coco

        # process samples
        for data_batch, data_samples in self.topdown_data_coco:
            metric_coco.process(data_batch, data_samples)

        eval_results = metric_coco.evaluate(size=len(self.topdown_data_coco))

        self.assertDictEqual(eval_results, self.target_coco)
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test3.keypoints.json')))

        # case 4: test without providing ann_file
        metric_coco = CocoMetric(outfile_prefix=f'{self.tmp_dir.name}/test4')
        metric_coco.dataset_meta = self.dataset_meta_coco
        # process samples
        for data_batch, data_samples in self.topdown_data_coco:
            metric_coco.process(data_batch, data_samples)
        eval_results = metric_coco.evaluate(size=len(self.topdown_data_coco))
        self.assertDictEqual(eval_results, self.target_coco)
        # test whether convert the annotation to COCO format
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test4.gt.json')))
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test4.keypoints.json')))

        # case 5: test Crowdpose dataset
        metric_crowdpose = CocoMetric(
            ann_file=self.ann_file_crowdpose,
            outfile_prefix=f'{self.tmp_dir.name}/test5',
            use_area=False,
            iou_type='keypoints_crowd',
            prefix='crowdpose')
        metric_crowdpose.dataset_meta = self.dataset_meta_crowdpose
        # process samples
        for data_batch, data_samples in self.topdown_data_crowdpose:
            metric_crowdpose.process(data_batch, data_samples)
        eval_results = metric_crowdpose.evaluate(
            size=len(self.topdown_data_crowdpose))
        self.assertDictEqual(eval_results, self.target_crowdpose)
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test5.keypoints.json')))

        # case 6: test Crowdpose dataset + without ann_file
        metric_crowdpose = CocoMetric(
            outfile_prefix=f'{self.tmp_dir.name}/test6',
            use_area=False,
            iou_type='keypoints_crowd',
            prefix='crowdpose')
        metric_crowdpose.dataset_meta = self.dataset_meta_crowdpose
        # process samples
        for data_batch, data_samples in self.topdown_data_crowdpose:
            metric_crowdpose.process(data_batch, data_samples)
        eval_results = metric_crowdpose.evaluate(
            size=len(self.topdown_data_crowdpose))
        self.assertDictEqual(eval_results, self.target_crowdpose)
        # test whether convert the annotation to COCO format
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test6.gt.json')))
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test6.keypoints.json')))

        # case 7: test AP10k dataset
        metric_ap10k = CocoMetric(
            ann_file=self.ann_file_ap10k,
            outfile_prefix=f'{self.tmp_dir.name}/test7')
        metric_ap10k.dataset_meta = self.dataset_meta_ap10k
        # process samples
        for data_batch, data_samples in self.topdown_data_ap10k:
            metric_ap10k.process(data_batch, data_samples)
        eval_results = metric_ap10k.evaluate(size=len(self.topdown_data_ap10k))
        for key in self.target_ap10k:
            self.assertAlmostEqual(eval_results[key], self.target_ap10k[key])
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test7.keypoints.json')))

        # case 8: test Crowdpose dataset + without ann_file
        metric_ap10k = CocoMetric(outfile_prefix=f'{self.tmp_dir.name}/test8')
        metric_ap10k.dataset_meta = self.dataset_meta_ap10k
        # process samples
        for data_batch, data_samples in self.topdown_data_ap10k:
            metric_ap10k.process(data_batch, data_samples)
        eval_results = metric_ap10k.evaluate(size=len(self.topdown_data_ap10k))
        for key in self.target_ap10k:
            self.assertAlmostEqual(eval_results[key], self.target_ap10k[key])
        # test whether convert the annotation to COCO format
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test8.gt.json')))
        self.assertTrue(
            osp.isfile(osp.join(self.tmp_dir.name, 'test8.keypoints.json')))
test topdown-style COCO metric evaluation.
mmpose/tests/test_evaluation/test_metrics/test_coco_metric.py