Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Add COCOPoseMetric #35

Closed
wants to merge 1 commit into from
Closed

[Feat] Add COCOPoseMetric #35

wants to merge 1 commit into from

Conversation

liqikai9
Copy link
Collaborator

@liqikai9 liqikai9 commented Oct 28, 2022

Motivation

Add COCOPoseMetric for pose estimation task.

PR in mmpose: open-mmlab/mmpose#1777, about the results verification.

Modification

  1. Add COCOPoseMetric under mmeval/metrics/coco_pose.py
  2. Add some test data: tests/test_metrics/data/coco_pose_sample.json, tests/test_metrics/data/crowdpose_sample.json, tests/test_metrics/data/ap10k_sample.json and unittests.
  3. Add nms function under mmeval/metrics/utils/nms.py

BC-breaking (Optional)

Use cases (Optional)

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@liqikai9 liqikai9 requested review from ly015 and ice-tong October 28, 2022 07:25
@CLAassistant
Copy link

CLAassistant commented Dec 13, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@C1rN09 C1rN09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update api docs && suport_matrix documentation.

BTW I was told this metric should be renamed as COCOKeyPointDetection

Comment on lines +90 to +146
>>> try:
... from xtcocotools.coco import COCO
... from xtcocotools.cocoeval import COCOeval
... HAS_XTCOCOTOOLS = True
... except ImportError:
... HAS_XTCOCOTOOLS = False
...
>>> ann_file = 'tests/test_metrics/data/coco_pose_sample.json'
>>> coco = COCO(ann_file)
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
>>> classes = coco.loadCats(coco.getCatIds())
>>> sigmas = np.array([
... 0.026, 0.025, 0.025, 0.035, 0.035, 0.079, 0.079, 0.072, 0.072,
... 0.062, 0.062, 0.107, 0.107, 0.087, 0.087, 0.089, 0.089
... ]).astype(np.float32)
>>> coco_dataset_meta = {
... 'CLASSES': classes,
... 'num_keypoints': 17,
... 'sigmas': sigmas,
... }
>>> def _convert_ann_to_pred_and_gt(ann_file):
... predictions = []
... groundtruths = []
... db = load(ann_file)
... imgid2info = dict()
... for img in db['images']:
... imgid2info[img['id']] = img
... for ann in db['annotations']:
... bboxes = np.array(ann['bbox'], dtype=np.float32).reshape(-1, 4)
... keypoints = np.array(ann['keypoints']).reshape((1, -1, 3))
... prediction = {
... 'id': ann['id'],
... 'img_id': ann['image_id'],
... 'bboxes': bboxes,
... 'keypoints': keypoints[..., :2],
... 'keypoint_scores': keypoints[..., -1],
... 'bbox_scores': np.ones((1, ), dtype=np.float32),
... }
... groundtruth = {
... 'img_id': ann['image_id'],
... 'width': 640,
... 'height': 480,
... 'num_keypoints': ann['num_keypoints'],
... 'raw_ann_info': [copy.deepcopy(ann)],
... }
... if 'area' in ann:
... groundtruth['area'] = ann['area']
... if 'crowdIndex' in imgid2info[ann['image_id']]:
... groundtruth['crowd_index'] = imgid2info[
... ann['image_id']]['crowdIndex']
... predictions.append(prediction)
... groundtruths.append(groundtruth)
... return predictions, groundtruths
>>> predictions, groundtruths = _convert_ann_to_pred_and_gt(ann_file)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is too long and it doesn't give a clear input format. Should write a plain dict as input.

Comment on lines +154 to +163
>>> coco_pose_metric(predictions, groundtruths)
loading annotations into memory...
Done (t=0.00s)
creating index...
index created!
>>> coco_pose_metric(predictions, groundtruths)
Loading and preparing results...
DONE (t=0.00s)
creating index...
index created!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there 2 function calls?

Comment on lines +147 to +149
>>> coco_pose_metric = CocoPoseMetric(
... ann_file=ann_file,
... dataset_meta=coco_dataset_meta)
Copy link
Collaborator

@C1rN09 C1rN09 Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be 2 example codes to cover the use cases:

  1. ann_file=None, a simple __call__ method
  2. ann_file is not None, call add and compute. Should illustrate what groundtruths should be in this case.

self.format_only = format_only
self.outfile_prefix = outfile_prefix

def add(self, predictions: Sequence[Dict], groundtruths: Sequence[Dict]) -> None: # type: ignore # yapf: disable # noqa: E501
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about making groundtruths defaults to None so that it's easier to use this metric with ann_file? Creating an 'empty' groundtruths is ambiguous and inconvenient, since we require it has the same length as predictions

predictions (Sequence[dict]): A sequence of dict. Each dict
representing a pose estimation result for an instance, with
the following keys:
- 'id'(int): The id of the instance
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 'id'(int): The id of the instance
- 'id' (int): The id of the instance


info = dict(
date_created=str(datetime.datetime.now()),
description='Coco json file converted by mmpose CocoMetric.')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description='Coco json file converted by mmpose CocoMetric.')
description='Coco json file converted by mmeval CocoMetric.')

Comment on lines +464 to +467
instance['keypoints'] = np.concatenate([
instance['keypoints'], instance['keypoint_scores'][:, None]
],
axis=-1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indent is strange

Comment on lines +147 to +149
>>> coco_pose_metric = CocoPoseMetric(
... ann_file=ann_file,
... dataset_meta=coco_dataset_meta)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If dataset_meta is required in this metric, I think it should be explained in docstring


Args:
kpts (Dict[int, list]): keypoint prediction results. The keys are
'`img_id`' and the values are list that may contain
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'`img_id`' and the values are list that may contain
``'img_id'`` and the values are list that may contain

Comment on lines +1 to +5
# ------------------------------------------------------------------------------
# Adapted from https://github.com/leoxiaobin/deep-high-resolution-net.pytorch
# Original licence: Copyright (c) Microsoft, under the MIT License.
# ------------------------------------------------------------------------------

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this file should be moved to metrics/_vendor? What do you think @zhouzaida

@C1rN09
Copy link
Collaborator

C1rN09 commented Feb 24, 2023

Close this because duplicated with #97

@C1rN09 C1rN09 closed this Feb 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants