forked from akanazawa/hmr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathops.py
60 lines (50 loc) · 1.79 KB
/
ops.py
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
"""
TF util operations.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
def keypoint_l1_loss(kp_gt, kp_pred, scale=1., name=None):
"""
computes: \Sum_i [0.5 * vis[i] * |kp_gt[i] - kp_pred[i]|] / (|vis|)
Inputs:
kp_gt : N x K x 3
kp_pred: N x K x 2
"""
with tf.name_scope(name, "keypoint_l1_loss", [kp_gt, kp_pred]):
kp_gt = tf.reshape(kp_gt, (-1, 3))
kp_pred = tf.reshape(kp_pred, (-1, 2))
vis = tf.expand_dims(tf.cast(kp_gt[:, 2], tf.float32), 1)
res = tf.losses.absolute_difference(kp_gt[:, :2], kp_pred, weights=vis)
return res
def compute_3d_loss(params_pred, params_gt, has_gt3d):
"""
Computes the l2 loss between 3D params pred and gt for those data that has_gt3d is True.
Parameters to compute loss over:
3Djoints: 14*3 = 42
rotations:(24*9)= 216
shape: 10
total input: 226 (gt SMPL params) or 42 (just joints)
Inputs:
params_pred: N x {226, 42}
params_gt: N x {226, 42}
# has_gt3d: (N,) bool
has_gt3d: N x 1 tf.float32 of {0., 1.}
"""
with tf.name_scope("3d_loss", [params_pred, params_gt, has_gt3d]):
weights = tf.expand_dims(tf.cast(has_gt3d, tf.float32), 1)
res = tf.losses.mean_squared_error(
params_gt, params_pred, weights=weights) * 0.5
return res
def align_by_pelvis(joints):
"""
Assumes joints is N x 14 x 3 in LSP order.
Then hips are: [3, 2]
Takes mid point of these points, then subtracts it.
"""
with tf.name_scope("align_by_pelvis", [joints]):
left_id = 3
right_id = 2
pelvis = (joints[:, left_id, :] + joints[:, right_id, :]) / 2.
return joints - tf.expand_dims(pelvis, axis=1)