pose3d_metrics.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import paddle
  15. import os
  16. import json
  17. from collections import defaultdict, OrderedDict
  18. import numpy as np
  19. from ppdet.utils.logger import setup_logger
  20. logger = setup_logger(__name__)
  21. __all__ = ['Pose3DEval']
  22. class AverageMeter(object):
  23. def __init__(self):
  24. self.reset()
  25. def reset(self):
  26. self.val = 0
  27. self.avg = 0
  28. self.sum = 0
  29. self.count = 0
  30. def update(self, val, n=1):
  31. self.val = val
  32. self.sum += val * n
  33. self.count += n
  34. self.avg = self.sum / self.count
  35. def mean_per_joint_position_error(pred, gt, has_3d_joints):
  36. """
  37. Compute mPJPE
  38. """
  39. gt = gt[has_3d_joints == 1]
  40. gt = gt[:, :, :3]
  41. pred = pred[has_3d_joints == 1]
  42. with paddle.no_grad():
  43. gt_pelvis = (gt[:, 2, :] + gt[:, 3, :]) / 2
  44. gt = gt - gt_pelvis[:, None, :]
  45. pred_pelvis = (pred[:, 2, :] + pred[:, 3, :]) / 2
  46. pred = pred - pred_pelvis[:, None, :]
  47. error = paddle.sqrt(((pred - gt)**2).sum(axis=-1)).mean(axis=-1).numpy()
  48. return error
  49. def compute_similarity_transform(S1, S2):
  50. """Computes a similarity transform (sR, t) that takes
  51. a set of 3D points S1 (3 x N) closest to a set of 3D points S2,
  52. where R is an 3x3 rotation matrix, t 3x1 translation, s scale.
  53. i.e. solves the orthogonal Procrutes problem.
  54. """
  55. transposed = False
  56. if S1.shape[0] != 3 and S1.shape[0] != 2:
  57. S1 = S1.T
  58. S2 = S2.T
  59. transposed = True
  60. assert (S2.shape[1] == S1.shape[1])
  61. # 1. Remove mean.
  62. mu1 = S1.mean(axis=1, keepdims=True)
  63. mu2 = S2.mean(axis=1, keepdims=True)
  64. X1 = S1 - mu1
  65. X2 = S2 - mu2
  66. # 2. Compute variance of X1 used for scale.
  67. var1 = np.sum(X1**2)
  68. # 3. The outer product of X1 and X2.
  69. K = X1.dot(X2.T)
  70. # 4. Solution that Maximizes trace(R'K) is R=U*V', where U, V are
  71. # singular vectors of K.
  72. U, s, Vh = np.linalg.svd(K)
  73. V = Vh.T
  74. # Construct Z that fixes the orientation of R to get det(R)=1.
  75. Z = np.eye(U.shape[0])
  76. Z[-1, -1] *= np.sign(np.linalg.det(U.dot(V.T)))
  77. # Construct R.
  78. R = V.dot(Z.dot(U.T))
  79. # 5. Recover scale.
  80. scale = np.trace(R.dot(K)) / var1
  81. # 6. Recover translation.
  82. t = mu2 - scale * (R.dot(mu1))
  83. # 7. Error:
  84. S1_hat = scale * R.dot(S1) + t
  85. if transposed:
  86. S1_hat = S1_hat.T
  87. return S1_hat
  88. def compute_similarity_transform_batch(S1, S2):
  89. """Batched version of compute_similarity_transform."""
  90. S1_hat = np.zeros_like(S1)
  91. for i in range(S1.shape[0]):
  92. S1_hat[i] = compute_similarity_transform(S1[i], S2[i])
  93. return S1_hat
  94. def reconstruction_error(S1, S2, reduction='mean'):
  95. """Do Procrustes alignment and compute reconstruction error."""
  96. S1_hat = compute_similarity_transform_batch(S1, S2)
  97. re = np.sqrt(((S1_hat - S2)**2).sum(axis=-1)).mean(axis=-1)
  98. if reduction == 'mean':
  99. re = re.mean()
  100. elif reduction == 'sum':
  101. re = re.sum()
  102. return re
  103. def all_gather(data):
  104. if paddle.distributed.get_world_size() == 1:
  105. return data
  106. vlist = []
  107. paddle.distributed.all_gather(vlist, data)
  108. data = paddle.concat(vlist, 0)
  109. return data
  110. class Pose3DEval(object):
  111. """refer to
  112. https://github.com/leoxiaobin/deep-high-resolution-net.pytorch
  113. Copyright (c) Microsoft, under the MIT License.
  114. """
  115. def __init__(self, output_eval, save_prediction_only=False):
  116. super(Pose3DEval, self).__init__()
  117. self.output_eval = output_eval
  118. self.res_file = os.path.join(output_eval, "pose3d_results.json")
  119. self.save_prediction_only = save_prediction_only
  120. self.reset()
  121. def reset(self):
  122. self.PAmPJPE = AverageMeter()
  123. self.mPJPE = AverageMeter()
  124. self.eval_results = {}
  125. def get_human36m_joints(self, input):
  126. J24_TO_J14 = paddle.to_tensor(
  127. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 18])
  128. J24_TO_J17 = paddle.to_tensor(
  129. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 18, 19])
  130. return paddle.index_select(input, J24_TO_J14, axis=1)
  131. def update(self, inputs, outputs):
  132. gt_3d_joints = all_gather(inputs['joints_3d'])
  133. has_3d_joints = all_gather(inputs['has_3d_joints'])
  134. pred_3d_joints = all_gather(outputs['pose3d'])
  135. if gt_3d_joints.shape[1] == 24:
  136. gt_3d_joints = self.get_human36m_joints(gt_3d_joints)
  137. if pred_3d_joints.shape[1] == 24:
  138. pred_3d_joints = self.get_human36m_joints(pred_3d_joints)
  139. mPJPE_val = mean_per_joint_position_error(pred_3d_joints, gt_3d_joints,
  140. has_3d_joints).mean()
  141. PAmPJPE_val = reconstruction_error(
  142. pred_3d_joints.numpy(),
  143. gt_3d_joints[:, :, :3].numpy(),
  144. reduction=None).mean()
  145. count = int(np.sum(has_3d_joints.numpy()))
  146. self.PAmPJPE.update(PAmPJPE_val * 1000., count)
  147. self.mPJPE.update(mPJPE_val * 1000., count)
  148. def accumulate(self):
  149. if self.save_prediction_only:
  150. logger.info(f'The pose3d result is saved to {self.res_file} '
  151. 'and do not evaluate the model.')
  152. return
  153. self.eval_results['pose3d'] = [-self.mPJPE.avg, -self.PAmPJPE.avg]
  154. def log(self):
  155. if self.save_prediction_only:
  156. return
  157. stats_names = ['mPJPE', 'PAmPJPE']
  158. num_values = len(stats_names)
  159. print(' '.join(['| {}'.format(name) for name in stats_names]) + ' |')
  160. print('|---' * (num_values + 1) + '|')
  161. print(' '.join([
  162. '| {:.3f}'.format(abs(value))
  163. for value in self.eval_results['pose3d']
  164. ]) + ' |')
  165. def get_results(self):
  166. return self.eval_results