-
Notifications
You must be signed in to change notification settings - Fork 1
/
models_res_nimble.py
236 lines (205 loc) · 10.3 KB
/
models_res_nimble.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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import pickle
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import os
import math
import time
import numpy as np
import pytorch3d
from pytorch3d.renderer import (
RasterizationSettings,
MeshRenderer,
MeshRasterizer,
HardPhongShader,
Materials
)
from pytorch3d.renderer.lighting import DirectionalLights
import pytorch3d.renderer as p3d_renderer
from network.res_encoder import ResEncoder, HandEncoder, LightEstimator
from network.effnet_encoder import EffiEncoder
from utils.NIMBLE_model.myNIMBLELayer import MyNIMBLELayer
from utils.traineval_util import Mano2Frei, trans_proj_j2d
from utils.my_mano import MyMANOLayer
from utils.Freihand_GNN_mano.mano_network_PCA import YTBHand
from utils.Freihand_GNN_mano.Freihand_trainer_mano_fullsup import dense_pose_Trainer
ytbHand_trainer = dense_pose_Trainer(None, None)
class Model(nn.Module):
def __init__(self, ifRender, device, if_4c, hand_model, use_mean_shape, pretrain, root_id=9, root_id_nimble=11, ifLight=True):
super(Model, self).__init__()
self.hand_model = hand_model
self.root_id = root_id
self.root_id_nimble = root_id_nimble
if hand_model == 'mano_new':
self.ytbHand = YTBHand(None, None, use_pca=True, pca_comps=48)
return
if pretrain == 'hr18sv2':
self.features_dim = 1024 # for HRnet
self.low_feat_dim = 512 # not sure
self.base_encoder = ResEncoder(pretrain=pretrain, if_4c=if_4c)
elif pretrain in ['res18', 'res50', 'res101']:
self.features_dim = 2048
self.low_feat_dim = 512
self.base_encoder = ResEncoder(pretrain=pretrain, if_4c=if_4c)
elif pretrain == 'effb3':
self.features_dim = 1536
self.low_feat_dim = 32
self.base_encoder = EffiEncoder(pretrain=pretrain)
if hand_model == 'nimble':
self.ncomps = [20, 30, 10] # shape, pose, tex respectively.
self.hand_layer = MyNIMBLELayer(ifRender, device, shape_ncomp=self.ncomps[0], pose_ncomp=self.ncomps[1], tex_ncomp=self.ncomps[2])
elif hand_model == 'mano':
self.ncomps = [10, 48, None] # shape, pose, no texture.
self.hand_layer = MyMANOLayer(ifRender, device, shape_ncomp=self.ncomps[0], pose_ncomp=self.ncomps[1], tex_ncomp=self.ncomps[2])
self.hand_encoder = HandEncoder(hand_model=hand_model, ncomps=self.ncomps, in_dim=self.features_dim, ifRender=ifRender, use_mean_shape=use_mean_shape)
MANO_file = os.path.join(os.path.dirname(__file__),'data/MANO_RIGHT.pkl')
dd = pickle.load(open(MANO_file, 'rb'),encoding='latin1')
self.mano_face = Variable(torch.from_numpy(np.expand_dims(dd['f'],0).astype(np.int16)).to(device=device))
self.ifRender = ifRender
self.ifLight = ifLight
self.aa_factor = 3
# Renderer
if self.ifRender:
# Define a RGB renderer with HardPhongShader in pytorch3d
raster_settings_soft = RasterizationSettings(
image_size=224 * self.aa_factor,
blur_radius=0.0,
faces_per_pixel=1,
)
materials = Materials(
# ambient_color=((0.9, 0.9, 0.9),),
diffuse_color=((0.8, 0.8, 0.8),),
specular_color=((0.2, 0.2, 0.2),),
shininess=30,
device=device,
)
# Differentiable soft renderer with SoftPhongShader
self.renderer_p3d = MeshRenderer(
rasterizer=MeshRasterizer(
raster_settings=raster_settings_soft
),
shader=HardPhongShader(
materials=materials,
device=device,
),
)
if self.ifLight:
self.light_estimator = LightEstimator(self.low_feat_dim)
def forward(self, dat_name, mode_train, images, Ks=None, root_xyz=None):
if self.hand_model == 'mano_new':
pred = self.ytbHand(images)
outputs = {
'pose_params': pred['theta'],
'shape_params': pred['beta'],
'verts': pred['mesh']
}
return outputs
device = images.device
batch_size = images.shape[0]
# Use base_encoder to extract features
# low_features, features = self.base_encoder(images) # [b, 512, 14, 14], [b,1024]
low_features, features = self.base_encoder(images) # [b, 512, 14, 14], [b,1024]
# Use light_estimator to get light parameters
if self.ifLight:
light_params = self.light_estimator(low_features)
# Use hand_encoder to get hand parameters
hand_params = self.hand_encoder(features)
# hand_params = {
# 'pose_params': pose_params,
# 'shape_params': shape_params,
# 'texture_params': texture_params,
# 'scale': scale,
# 'trans': trans,
# 'rot': rot # only for mano hand model
# }
# Use nimble_layer to get 3D hand models
outputs = self.hand_layer(hand_params, handle_collision=False)
# outputs = {
# 'nimble_joints': bone_joints, # 25 joints
# 'verts': skin_v, # 5990 verts
# 'faces': None #faces,
# 'skin_meshes': skin_v_smooth, # smoothed verts and faces
# 'mano_verts': skin_mano_v, # 5990 -> 778 verts according to mano
# 'textures': tex_img,
# 'rot':rot
# }
outputs.update(hand_params)
# map nimble 25 joints to freihand 21 joints
if self.hand_model == 'mano_new':
# regress joints from verts
vertice_pred_list = outputs['verts']
outputs['joints'] = ytbHand_trainer.xyz_from_vertice(vertice_pred_list[-1]).permute(1,0,2)
elif self.hand_model == 'mano':
# regress joints from verts
vertice_pred_list = outputs['mano_verts']
outputs['joints'] = ytbHand_trainer.xyz_from_vertice(vertice_pred_list).permute(1,0,2)
else: # nimble
# Mano joints map to Frei joints
outputs['joints'] = Mano2Frei(outputs['joints'])
# ** offset positions relative to root.
if dat_name == 'HO3D' and not mode_train:
# they only provide wrist joint (0) in test set
pred_root_xyz = outputs['joints'][:, 0, :].unsqueeze(1)
else:
pred_root_xyz = outputs['joints'][:, self.root_id, :].unsqueeze(1)
outputs['joints'] = outputs['joints'] - pred_root_xyz
outputs['mano_verts'] = outputs['mano_verts'] - pred_root_xyz
if self.hand_model == 'nimble':
if dat_name == 'HO3D' and not mode_train:
pred_root_xyz = outputs['nimble_joints'][:, 0, :].unsqueeze(1)
else:
pred_root_xyz = outputs['nimble_joints'][:, self.root_id_nimble, :].unsqueeze(1)
outputs['nimble_joints'] = outputs['nimble_joints'] - pred_root_xyz
# Render image
if self.ifRender:
# set up renderer parameters
# k_44 = torch.eye(4).unsqueeze(0).repeat(batch_size, 1, 1)
# k_44[:, :3, :4] = Ks
# cameras = p3d_renderer.cameras.PerspectiveCameras(K=k_44, device=device, in_ndc=False, image_size=((224,224),)) # R and t are identity and zeros by default
# get ndc fx, fy, cx, cy from Ks
fcl, prp = self.get_ndc_fx_fy_cx_cy(Ks)
cameras = p3d_renderer.cameras.PerspectiveCameras(focal_length=-fcl,
principal_point=prp,
device=device) # R and t are identity and zeros by default
if self.ifLight:
lighting = DirectionalLights(diffuse_color=light_params['colors'], # N, 3
direction=light_params['directions'], # N, 3
device=device)
else:
lighting = p3d_renderer.lighting.PointLights(
# ambient_color=((1.0, 1.0, 1.0),),
# diffuse_color=((0.0, 0.0, 0.0),),
# specular_color=((0.0, 0.0, 0.0),),
# location=((0.0, 0.0, 0.0),),
device=device,
)
# move to the root relative coord.
# verts = verts - pred_root_xyz + root_xyz
verts_num = outputs['skin_meshes']._num_verts_per_mesh[0]
outputs['skin_meshes'].offset_verts_(-pred_root_xyz.repeat(1, verts_num, 1).view(verts_num*batch_size, 3))
outputs['skin_meshes'].offset_verts_(root_xyz.repeat(1, verts_num, 1).view(verts_num*batch_size, 3))
# render the image
rendered_images = self.renderer_p3d(outputs['skin_meshes'], cameras=cameras, lights=lighting)
# average pooling to downsample the rendered image (anti-aliasing)
rendered_images = rendered_images.permute(0, 3, 1, 2) # NHWC -> NCHW
rendered_images = F.avg_pool2d(rendered_images, kernel_size=self.aa_factor, stride=self.aa_factor)
# rendered_images = rendered_images.permute(0, 2, 3, 1) # NCHW -> NHWC
# import torchvision
# torchvision.utils.save_image(rendered_images[...,:3][1].permute(2,0,1),"test.png")
outputs['re_img'] = rendered_images[:, :3, :, :] # the last dim is alpha
outputs['re_sil'] = rendered_images[:, 3:4, :, :] # [B, 1, w, h]. the last dim is alpha
outputs['re_sil'][outputs['re_sil'] > 0] = 255 # Binarize segmentation mask
outputs['maskRGBs'] = images.mul((outputs['re_sil']>0).float().repeat(1,3,1,1))
# add mano faces to outputs (used in losses)
outputs['mano_faces'] = self.mano_face.repeat(batch_size, 1, 1)
return outputs
# get ndc fx, fy, cx, cy from Ks
def get_ndc_fx_fy_cx_cy(self, Ks):
ndc_fx = Ks[:, 0, 0] * 2 / 224.0
ndc_fy = Ks[:, 1, 1] * 2 / 224.0
ndc_px = - (Ks[:, 0, 2] - 112.0) * 2 / 224.0
ndc_py = - (Ks[:, 1, 2] - 112.0) * 2 / 224.0
focal_length = torch.stack([ndc_fx, ndc_fy], dim=-1)
principal_point = torch.stack([ndc_px, ndc_py], dim=-1)
return focal_length, principal_point