-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Separate evaluation mappings from KeypointConverter (#2738)
- Loading branch information
Showing
6 changed files
with
189 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from typing import List, Tuple, Union | ||
|
||
import numpy as np | ||
|
||
|
||
def transform_sigmas(sigmas: Union[List, np.ndarray], num_keypoints: int, | ||
mapping: Union[List[Tuple[int, int]], List[Tuple[Tuple, | ||
int]]]): | ||
"""Transforms the sigmas based on the mapping.""" | ||
if len(mapping): | ||
source_index, target_index = map(list, zip(*mapping)) | ||
else: | ||
source_index, target_index = [], [] | ||
|
||
list_input = False | ||
if isinstance(sigmas, list): | ||
sigmas = np.array(sigmas) | ||
list_input = True | ||
|
||
new_sigmas = np.ones(num_keypoints, dtype=sigmas.dtype) | ||
new_sigmas[target_index] = sigmas[source_index] | ||
|
||
if list_input: | ||
new_sigmas = new_sigmas.tolist() | ||
|
||
return new_sigmas | ||
|
||
|
||
def transform_ann(ann_info: Union[dict, list], num_keypoints: int, | ||
mapping: Union[List[Tuple[int, int]], List[Tuple[Tuple, | ||
int]]]): | ||
"""Transforms COCO-format annotations based on the mapping.""" | ||
if len(mapping): | ||
source_index, target_index = map(list, zip(*mapping)) | ||
else: | ||
source_index, target_index = [], [] | ||
|
||
list_input = True | ||
if not isinstance(ann_info, list): | ||
ann_info = [ann_info] | ||
list_input = False | ||
|
||
for each in ann_info: | ||
if 'keypoints' in each: | ||
keypoints = np.array(each['keypoints']) | ||
|
||
C = 3 # COCO-format: x, y, score | ||
keypoints = keypoints.reshape(-1, C) | ||
new_keypoints = np.zeros((num_keypoints, C), dtype=keypoints.dtype) | ||
new_keypoints[target_index] = keypoints[source_index] | ||
each['keypoints'] = new_keypoints.reshape(-1).tolist() | ||
|
||
if 'num_keypoints' in each: | ||
each['num_keypoints'] = num_keypoints | ||
|
||
if not list_input: | ||
ann_info = ann_info[0] | ||
|
||
return ann_info | ||
|
||
|
||
def transform_pred(pred_info: Union[dict, list], num_keypoints: int, | ||
mapping: Union[List[Tuple[int, int]], List[Tuple[Tuple, | ||
int]]]): | ||
"""Transforms predictions based on the mapping.""" | ||
if len(mapping): | ||
source_index, target_index = map(list, zip(*mapping)) | ||
else: | ||
source_index, target_index = [], [] | ||
|
||
list_input = True | ||
if not isinstance(pred_info, list): | ||
pred_info = [pred_info] | ||
list_input = False | ||
|
||
for each in pred_info: | ||
if 'keypoints' in each: | ||
keypoints = np.array(each['keypoints']) | ||
|
||
N, _, C = keypoints.shape | ||
new_keypoints = np.zeros((N, num_keypoints, C), | ||
dtype=keypoints.dtype) | ||
new_keypoints[:, target_index] = keypoints[:, source_index] | ||
each['keypoints'] = new_keypoints | ||
|
||
keypoint_scores = np.array(each['keypoint_scores']) | ||
new_scores = np.zeros((N, num_keypoints), | ||
dtype=keypoint_scores.dtype) | ||
new_scores[:, target_index] = keypoint_scores[:, source_index] | ||
each['keypoint_scores'] = new_scores | ||
|
||
if 'num_keypoints' in each: | ||
each['num_keypoints'] = num_keypoints | ||
|
||
if not list_input: | ||
pred_info = pred_info[0] | ||
|
||
return pred_info |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
from copy import deepcopy | ||
from unittest import TestCase | ||
|
||
import numpy as np | ||
|
||
from mmpose.evaluation.functional import (transform_ann, transform_pred, | ||
transform_sigmas) | ||
|
||
|
||
class TestKeypointEval(TestCase): | ||
|
||
def test_transform_sigmas(self): | ||
|
||
mapping = [(3, 0), (6, 1), (16, 2), (5, 3)] | ||
num_keypoints = 5 | ||
sigmas = np.random.rand(17) | ||
new_sigmas = transform_sigmas(sigmas, num_keypoints, mapping) | ||
self.assertEqual(len(new_sigmas), 5) | ||
for i, j in mapping: | ||
self.assertEqual(sigmas[i], new_sigmas[j]) | ||
|
||
def test_transform_ann(self): | ||
mapping = [(3, 0), (6, 1), (16, 2), (5, 3)] | ||
num_keypoints = 5 | ||
|
||
kpt_info = dict( | ||
num_keypoints=17, | ||
keypoints=np.random.randint(3, size=(17 * 3, )).tolist()) | ||
kpt_info_copy = deepcopy(kpt_info) | ||
|
||
_ = transform_ann(kpt_info, num_keypoints, mapping) | ||
|
||
self.assertEqual(kpt_info['num_keypoints'], 5) | ||
self.assertEqual(len(kpt_info['keypoints']), 15) | ||
for i, j in mapping: | ||
self.assertListEqual(kpt_info_copy['keypoints'][i * 3:i * 3 + 3], | ||
kpt_info['keypoints'][j * 3:j * 3 + 3]) | ||
|
||
def test_transform_pred(self): | ||
mapping = [(3, 0), (6, 1), (16, 2), (5, 3)] | ||
num_keypoints = 5 | ||
|
||
kpt_info = dict( | ||
num_keypoints=17, | ||
keypoints=np.random.randint(3, size=( | ||
1, | ||
17, | ||
3, | ||
)), | ||
keypoint_scores=np.ones((1, 17))) | ||
|
||
_ = transform_pred(kpt_info, num_keypoints, mapping) | ||
|
||
self.assertEqual(kpt_info['num_keypoints'], 5) | ||
self.assertEqual(len(kpt_info['keypoints']), 1) |