Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

metrics.py 2.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. import numpy as np
  2. from smplx import SMPL
  3. import config
  4. def compute_pve_neutral_pose_scale_corrected(predicted_smpl_shape, target_smpl_shape, gender):
  5. """
  6. Given predicted and target SMPL shape parameters, computes neutral-pose per-vertex error
  7. after scale-correction (to account for scale vs camera depth ambiguity).
  8. :param predicted_smpl_parameters: predicted SMPL shape parameters tensor with shape (1, 10)
  9. :param target_smpl_parameters: target SMPL shape parameters tensor with shape (1, 10)
  10. :param gender: gender of target
  11. """
  12. smpl_male = SMPL(config.SMPL_MODEL_DIR, batch_size=1, gender='male')
  13. smpl_female = SMPL(config.SMPL_MODEL_DIR, batch_size=1, gender='female')
  14. # Get neutral pose vertices
  15. if gender == 'm':
  16. pred_smpl_neutral_pose_output = smpl_male(betas=predicted_smpl_shape)
  17. target_smpl_neutral_pose_output = smpl_male(betas=target_smpl_shape)
  18. elif gender == 'f':
  19. pred_smpl_neutral_pose_output = smpl_female(betas=predicted_smpl_shape)
  20. target_smpl_neutral_pose_output = smpl_female(betas=target_smpl_shape)
  21. pred_smpl_neutral_pose_vertices = pred_smpl_neutral_pose_output.vertices
  22. target_smpl_neutral_pose_vertices = target_smpl_neutral_pose_output.vertices
  23. # Rescale such that RMSD of predicted vertex mesh is the same as RMSD of target mesh.
  24. # This is done to combat scale vs camera depth ambiguity.
  25. pred_smpl_neutral_pose_vertices_rescale = scale_and_translation_transform_batch(pred_smpl_neutral_pose_vertices,
  26. target_smpl_neutral_pose_vertices)
  27. # Compute PVE-T-SC
  28. pve_neutral_pose_scale_corrected = np.linalg.norm(pred_smpl_neutral_pose_vertices_rescale
  29. - target_smpl_neutral_pose_vertices,
  30. axis=-1) # (1, 6890)
  31. return pve_neutral_pose_scale_corrected
  32. def scale_and_translation_transform_batch(P, T):
  33. """
  34. First normalises batch of input 3D meshes P such that each mesh has mean (0, 0, 0) and
  35. RMS distance from mean = 1.
  36. Then transforms P such that it has the same mean and RMSD as T.
  37. :param P: (batch_size, N, 3) batch of N 3D meshes to transform.
  38. :param T: (batch_size, N, 3) batch of N reference 3D meshes.
  39. :return: P transformed
  40. """
  41. P_mean = np.mean(P, axis=1, keepdims=True)
  42. P_trans = P - P_mean
  43. P_scale = np.sqrt(np.sum(P_trans ** 2, axis=(1, 2), keepdims=True) / P.shape[1])
  44. P_normalised = P_trans / P_scale
  45. T_mean = np.mean(T, axis=1, keepdims=True)
  46. T_scale = np.sqrt(np.sum((T - T_mean) ** 2, axis=(1, 2), keepdims=True) / T.shape[1])
  47. P_transformed = P_normalised * T_scale + T_mean
  48. return P_transformed
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...