-
Notifications
You must be signed in to change notification settings - Fork 11
/
batch_smpl.py
145 lines (121 loc) · 5.17 KB
/
batch_smpl.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
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
"""
Tensorflow SMPL implementation as batch.
Specify joint types:
'coco': Returns COCO+ 19 joints
'lsp': Returns H3.6M-LSP 14 joints
Note: To get original smpl joints, use self.J_transformed
"""
import numpy as np
import pickle
import tensorflow as tf
from .batch_lbs import batch_rodrigues, batch_global_rigid_transformation
class SMPL(object):
def __init__(self, pkl_path, joint_type='cocoplus', dtype=tf.float32):
"""
pkl_path is the path to a SMPL model
"""
# -- Load SMPL params --
with open(pkl_path, 'rb') as f:
dd = pickle.load(f, encoding='latin1')
# Mean template vertices
self.v_template = tf.constant(
dd['v_template'],
name='v_template',
dtype=dtype)
# Size of mesh [Number of vertices, 3]
self.size = [self.v_template.shape[0].value, 3]
self.num_betas = dd['shapedirs'].shape[-1]
# Shape blend shape basis: 6980 x 3 x 10
# reshaped to 6980*3 x 10, transposed to 10x6980*3
shapedir = np.reshape(
dd['shapedirs'], [-1, self.num_betas]).T
self.shapedirs = tf.constant(
shapedir, name='shapedirs', dtype=dtype)
# Regressor for joint locations given shape - 6890 x 24
self.J_regressor = tf.constant(
dd['J_regressor'].T.todense(),
name="J_regressor",
dtype=dtype)
# Pose blend shape basis: 6890 x 3 x 207, reshaped to 6890*30 x 207
num_pose_basis = dd['posedirs'].shape[-1]
# 207 x 20670
posedirs = np.reshape(
dd['posedirs'], [-1, num_pose_basis]).T
self.posedirs = tf.constant(
posedirs, name='posedirs', dtype=dtype)
# indices of parents for each joints
self.parents = dd['kintree_table'][0].astype(np.int32)
# LBS weights
self.weights = tf.constant(
dd['weights'],
name='lbs_weights',
dtype=dtype)
# This returns 19 keypoints: 6890 x 19
self.joint_regressor = tf.constant(
dd['cocoplus_regressor'].T.todense(),
name="cocoplus_regressor",
dtype=dtype)
if joint_type == 'lsp': # 14 LSP joints!
self.joint_regressor = self.joint_regressor[:, :14]
def __call__(self, beta, theta, get_skin=False, name=None):
"""
Obtain SMPL with shape (beta) & pose (theta) inputs.
Theta includes the global rotation.
Args:
beta: N x 10
theta: N x 72 (with 3-D axis-angle rep)
Updates:
self.J_transformed: N x 24 x 3 joint location after shaping
& posing with beta and theta
Returns:
- joints: N x 19 or 14 x 3 joint locations depending on joint_type
If get_skin is True, also returns
- Verts: N x 6980 x 3
"""
with tf.variable_scope(name, "smpl_main", [beta, theta]):
num_batch = tf.shape(beta)[0]
# 1. Add shape blend shapes
# (N x 10) x (10 x 6890*3) = N x 6890 x 3
v_shaped = tf.reshape(
tf.matmul(beta, self.shapedirs, name='shape_bs'),
[-1, self.size[0], self.size[1]]) + self.v_template
# 2. Infer shape-dependent joint locations.
Jx = tf.matmul(v_shaped[:, :, 0], self.J_regressor)
Jy = tf.matmul(v_shaped[:, :, 1], self.J_regressor)
Jz = tf.matmul(v_shaped[:, :, 2], self.J_regressor)
J = tf.stack([Jx, Jy, Jz], axis=2)
# 3. Add pose blend shapes
# N x 24 x 3 x 3
Rs = tf.reshape(
batch_rodrigues(tf.reshape(theta, [-1, 3])), [-1, 24, 3, 3])
with tf.variable_scope("lrotmin"):
# Ignore global rotation.
pose_feature = tf.reshape(Rs[:, 1:, :, :] - tf.eye(3),
[-1, 207])
# (N x 207) x (207, 20670) -> N x 6890 x 3
v_posed = tf.reshape(
tf.matmul(pose_feature, self.posedirs),
[-1, self.size[0], self.size[1]]) + v_shaped
#4. Get the global joint location
self.J_transformed, A = batch_global_rigid_transformation(Rs, J, self.parents)
# 5. Do skinning:
# W is N x 6890 x 24
W = tf.reshape(
tf.tile(self.weights, [num_batch, 1]), [num_batch, -1, 24])
# (N x 6890 x 24) x (N x 24 x 16)
T = tf.reshape(
tf.matmul(W, tf.reshape(A, [num_batch, 24, 16])),
[num_batch, -1, 4, 4])
v_posed_homo = tf.concat(
[v_posed, tf.ones([num_batch, v_posed.shape[1], 1])], 2)
v_homo = tf.matmul(T, tf.expand_dims(v_posed_homo, -1))
verts = v_homo[:, :, :3, 0]
# Get cocoplus or lsp joints:
joint_x = tf.matmul(verts[:, :, 0], self.joint_regressor)
joint_y = tf.matmul(verts[:, :, 1], self.joint_regressor)
joint_z = tf.matmul(verts[:, :, 2], self.joint_regressor)
joints = tf.stack([joint_x, joint_y, joint_z], axis=2)
if get_skin:
return verts, joints, Rs
else:
return joints