File size: 5,463 Bytes
152f0f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Copyright (c) OpenMMLab. All rights reserved.
from unittest import TestCase

import numpy as np

from mmpose.codecs import SimCCLabel  # noqa: F401
from mmpose.registry import KEYPOINT_CODECS


class TestSimCCLabel(TestCase):

    # name and configs of all test cases
    def setUp(self) -> None:
        self.configs = [
            (
                'simcc gaussian',
                dict(
                    type='SimCCLabel',
                    input_size=(192, 256),
                    smoothing_type='gaussian',
                    sigma=6.0,
                    simcc_split_ratio=2.0),
            ),
            (
                'simcc smoothing',
                dict(
                    type='SimCCLabel',
                    input_size=(192, 256),
                    smoothing_type='standard',
                    sigma=5.0,
                    simcc_split_ratio=3.0,
                    label_smooth_weight=0.1),
            ),
            (
                'simcc one-hot',
                dict(
                    type='SimCCLabel',
                    input_size=(192, 256),
                    smoothing_type='standard',
                    sigma=5.0,
                    simcc_split_ratio=3.0),
            ),
            (
                'simcc dark',
                dict(
                    type='SimCCLabel',
                    input_size=(192, 256),
                    smoothing_type='gaussian',
                    sigma=6.0,
                    simcc_split_ratio=2.0,
                    use_dark=True),
            ),
            (
                'simcc separated sigmas',
                dict(
                    type='SimCCLabel',
                    input_size=(192, 256),
                    smoothing_type='gaussian',
                    sigma=(4.9, 5.66),
                    simcc_split_ratio=2.0),
            ),
        ]

        # The bbox is usually padded so the keypoint will not be near the
        # boundary
        keypoints = (0.1 + 0.8 * np.random.rand(1, 17, 2)) * [192, 256]
        keypoints = np.round(keypoints).astype(np.float32)
        keypoints_visible = np.ones((1, 17), dtype=np.float32)
        self.data = dict(
            keypoints=keypoints, keypoints_visible=keypoints_visible)

    def test_encode(self):
        keypoints = self.data['keypoints']
        keypoints_visible = self.data['keypoints_visible']

        for name, cfg in self.configs:
            codec = KEYPOINT_CODECS.build(cfg)

            encoded = codec.encode(keypoints, keypoints_visible)

            self.assertEqual(encoded['keypoint_x_labels'].shape,
                             (1, 17, int(192 * codec.simcc_split_ratio)),
                             f'Failed case: "{name}"')
            self.assertEqual(encoded['keypoint_y_labels'].shape,
                             (1, 17, int(256 * codec.simcc_split_ratio)),
                             f'Failed case: "{name}"')
            self.assertEqual(encoded['keypoint_weights'].shape, (1, 17),
                             f'Failed case: "{name}"')

    def test_decode(self):
        for name, cfg in self.configs:
            codec = KEYPOINT_CODECS.build(cfg)

            simcc_x = np.random.rand(1, 17, int(192 * codec.simcc_split_ratio))
            simcc_y = np.random.rand(1, 17, int(256 * codec.simcc_split_ratio))

            keypoints, scores = codec.decode(simcc_x, simcc_y)

            self.assertEqual(keypoints.shape, (1, 17, 2),
                             f'Failed case: "{name}"')
            self.assertEqual(scores.shape, (1, 17), f'Failed case: "{name}"')

    def test_cicular_verification(self):
        keypoints = self.data['keypoints']
        keypoints_visible = self.data['keypoints_visible']

        for name, cfg in self.configs:
            codec = KEYPOINT_CODECS.build(cfg)

            encoded = codec.encode(keypoints, keypoints_visible)
            keypoint_x_labels = encoded['keypoint_x_labels']
            keypoint_y_labels = encoded['keypoint_y_labels']

            _keypoints, _ = codec.decode(keypoint_x_labels, keypoint_y_labels)

            self.assertTrue(
                np.allclose(keypoints, _keypoints, atol=5.),
                f'Failed case: "{name}"')

    def test_errors(self):
        cfg = dict(
            type='SimCCLabel',
            input_size=(192, 256),
            smoothing_type='uniform',
            sigma=1.0,
            simcc_split_ratio=2.0)

        with self.assertRaisesRegex(ValueError,
                                    'got invalid `smoothing_type`'):
            _ = KEYPOINT_CODECS.build(cfg)

        # invalid label_smooth_weight in smoothing
        cfg = dict(
            type='SimCCLabel',
            input_size=(192, 256),
            smoothing_type='standard',
            sigma=1.0,
            simcc_split_ratio=2.0,
            label_smooth_weight=1.1)

        with self.assertRaisesRegex(ValueError,
                                    '`label_smooth_weight` should be'):
            _ = KEYPOINT_CODECS.build(cfg)

        # invalid label_smooth_weight for gaussian
        cfg = dict(
            type='SimCCLabel',
            input_size=(192, 256),
            smoothing_type='gaussian',
            sigma=1.0,
            simcc_split_ratio=2.0,
            label_smooth_weight=0.1)

        with self.assertRaisesRegex(ValueError,
                                    'is only used for `standard` mode.'):
            _ = KEYPOINT_CODECS.build(cfg)