File size: 7,548 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | # Copyright (c) OpenMMLab. All rights reserved.
from unittest import TestCase
import numpy as np
from numpy.testing import assert_array_almost_equal
from mmpose.evaluation.functional import (keypoint_auc, keypoint_epe,
keypoint_mpjpe, keypoint_nme,
keypoint_pck_accuracy,
multilabel_classification_accuracy,
pose_pck_accuracy)
class TestKeypointEval(TestCase):
def test_keypoint_pck_accuracy(self):
output = np.zeros((2, 5, 2))
target = np.zeros((2, 5, 2))
mask = np.array([[True, True, False, True, True],
[True, True, False, True, True]])
# first channel
output[0, 0] = [10, 0]
target[0, 0] = [10, 0]
# second channel
output[0, 1] = [20, 20]
target[0, 1] = [10, 10]
# third channel
output[0, 2] = [0, 0]
target[0, 2] = [-1, 0]
# fourth channel
output[0, 3] = [30, 30]
target[0, 3] = [30, 30]
# fifth channel
output[0, 4] = [0, 10]
target[0, 4] = [0, 10]
thr = np.full((2, 2), 10, dtype=np.float32)
acc, avg_acc, cnt = keypoint_pck_accuracy(output, target, mask, 0.5,
thr)
assert_array_almost_equal(acc, np.array([1, 0.5, -1, 1, 1]), decimal=4)
self.assertAlmostEqual(avg_acc, 0.875, delta=1e-4)
self.assertAlmostEqual(cnt, 4, delta=1e-4)
acc, avg_acc, cnt = keypoint_pck_accuracy(output, target, mask, 0.5,
np.zeros((2, 2)))
assert_array_almost_equal(
acc, np.array([-1, -1, -1, -1, -1]), decimal=4)
self.assertAlmostEqual(avg_acc, 0, delta=1e-4)
self.assertAlmostEqual(cnt, 0, delta=1e-4)
acc, avg_acc, cnt = keypoint_pck_accuracy(output, target, mask, 0.5,
np.array([[0, 0], [10, 10]]))
assert_array_almost_equal(acc, np.array([1, 1, -1, 1, 1]), decimal=4)
self.assertAlmostEqual(avg_acc, 1, delta=1e-4)
self.assertAlmostEqual(cnt, 4, delta=1e-4)
def test_keypoint_auc(self):
output = np.zeros((1, 5, 2))
target = np.zeros((1, 5, 2))
mask = np.array([[True, True, False, True, True]])
# first channel
output[0, 0] = [10, 4]
target[0, 0] = [10, 0]
# second channel
output[0, 1] = [10, 18]
target[0, 1] = [10, 10]
# third channel
output[0, 2] = [0, 0]
target[0, 2] = [0, -1]
# fourth channel
output[0, 3] = [40, 40]
target[0, 3] = [30, 30]
# fifth channel
output[0, 4] = [20, 10]
target[0, 4] = [0, 10]
auc = keypoint_auc(output, target, mask, 20, 4)
self.assertAlmostEqual(auc, 0.375, delta=1e-4)
def test_keypoint_epe(self):
output = np.zeros((1, 5, 2))
target = np.zeros((1, 5, 2))
mask = np.array([[True, True, False, True, True]])
# first channel
output[0, 0] = [10, 4]
target[0, 0] = [10, 0]
# second channel
output[0, 1] = [10, 18]
target[0, 1] = [10, 10]
# third channel
output[0, 2] = [0, 0]
target[0, 2] = [-1, -1]
# fourth channel
output[0, 3] = [40, 40]
target[0, 3] = [30, 30]
# fifth channel
output[0, 4] = [20, 10]
target[0, 4] = [0, 10]
epe = keypoint_epe(output, target, mask)
self.assertAlmostEqual(epe, 11.5355339, delta=1e-4)
def test_keypoint_nme(self):
output = np.zeros((1, 5, 2))
target = np.zeros((1, 5, 2))
mask = np.array([[True, True, False, True, True]])
# first channel
output[0, 0] = [10, 4]
target[0, 0] = [10, 0]
# second channel
output[0, 1] = [10, 18]
target[0, 1] = [10, 10]
# third channel
output[0, 2] = [0, 0]
target[0, 2] = [-1, -1]
# fourth channel
output[0, 3] = [40, 40]
target[0, 3] = [30, 30]
# fifth channel
output[0, 4] = [20, 10]
target[0, 4] = [0, 10]
normalize_factor = np.ones((output.shape[0], output.shape[2]))
nme = keypoint_nme(output, target, mask, normalize_factor)
self.assertAlmostEqual(nme, 11.5355339, delta=1e-4)
def test_pose_pck_accuracy(self):
output = np.zeros((1, 5, 64, 64), dtype=np.float32)
target = np.zeros((1, 5, 64, 64), dtype=np.float32)
mask = np.array([[True, True, False, False, False]])
# first channel
output[0, 0, 20, 20] = 1
target[0, 0, 10, 10] = 1
# second channel
output[0, 1, 30, 30] = 1
target[0, 1, 30, 30] = 1
acc, avg_acc, cnt = pose_pck_accuracy(output, target, mask)
assert_array_almost_equal(acc, np.array([0, 1, -1, -1, -1]), decimal=4)
self.assertAlmostEqual(avg_acc, 0.5, delta=1e-4)
self.assertAlmostEqual(cnt, 2, delta=1e-4)
def test_multilabel_classification_accuracy(self):
output = np.array([[0.7, 0.8, 0.4], [0.8, 0.1, 0.1]])
target = np.array([[1, 0, 0], [1, 0, 1]])
mask = np.array([[True, True, True], [True, True, True]])
thr = 0.5
acc = multilabel_classification_accuracy(output, target, mask, thr)
self.assertEqual(acc, 0)
output = np.array([[0.7, 0.2, 0.4], [0.8, 0.1, 0.9]])
thr = 0.5
acc = multilabel_classification_accuracy(output, target, mask, thr)
self.assertEqual(acc, 1)
thr = 0.3
acc = multilabel_classification_accuracy(output, target, mask, thr)
self.assertEqual(acc, 0.5)
mask = np.array([[True, True, False], [True, True, True]])
acc = multilabel_classification_accuracy(output, target, mask, thr)
self.assertEqual(acc, 1)
def test_keypoint_mpjpe(self):
output = np.zeros((2, 5, 3))
target = np.zeros((2, 5, 3))
mask = np.array([[True, True, False, True, True],
[True, True, False, True, True]])
# first channel
output[0, 0] = [1, 0, 0]
target[0, 0] = [1, 0, 0]
output[1, 0] = [1, 0, 0]
target[1, 0] = [1, 1, 0]
# second channel
output[0, 1] = [2, 2, 0]
target[0, 1] = [1, 1, 1]
output[1, 1] = [2, 2, 1]
target[1, 1] = [1, 0, 1]
# third channel
output[0, 2] = [0, 0, -1]
target[0, 2] = [-1, 0, 0]
output[1, 2] = [-1, 0, 0]
target[1, 2] = [-1, 0, 0]
# fourth channel
output[0, 3] = [3, 3, 1]
target[0, 3] = [3, 3, 1]
output[1, 3] = [0, 0, 3]
target[1, 3] = [0, 0, 3]
# fifth channel
output[0, 4] = [0, 1, 1]
target[0, 4] = [0, 1, 0]
output[1, 4] = [0, 0, 1]
target[1, 4] = [1, 1, 0]
mpjpe = keypoint_mpjpe(output, target, mask)
self.assertAlmostEqual(mpjpe, 0.9625211990796929, delta=1e-4)
p_mpjpe = keypoint_mpjpe(output, target, mask, 'procrustes')
self.assertAlmostEqual(p_mpjpe, 1.0047897634604497, delta=1e-4)
s_mpjpe = keypoint_mpjpe(output, target, mask, 'scale')
self.assertAlmostEqual(s_mpjpe, 1.0277129678465953, delta=1e-4)
with self.assertRaises(ValueError):
_ = keypoint_mpjpe(output, target, mask, 'alignment')
|