forked from patienceFromZhou/simpleHand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhand_net.py
214 lines (156 loc) · 6.47 KB
/
hand_net.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
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch import Tensor
import timm
from timm.models.layers import DropPath, Mlp
import hiera
from models.modules import MeshHead, AttentionBlock, IdentityBlock, SepConvBlock
from models.losses import mesh_to_joints
from models.losses import l1_loss
class HandNet(nn.Module):
def __init__(self, cfg, pretrained=None):
super().__init__()
self.cfg = cfg
model_cfg = cfg["MODEL"]
backbone_cfg = model_cfg["BACKBONE"]
self.loss_cfg = model_cfg["LOSSES"]
if pretrained is None:
pretrained=backbone_cfg['pretrain']
if "hiera" in backbone_cfg['model_name']:
self.backbone = hiera.__dict__[backbone_cfg['model_name']](pretrained=True, checkpoint="mae_in1k", drop_path_rate=backbone_cfg['drop_path_rate'])
self.is_hiera = True
else:
self.backbone = timm.create_model(backbone_cfg['model_name'], pretrained=pretrained, drop_path_rate=backbone_cfg['drop_path_rate'])
self.is_hiera = False
self.avg_pool = nn.AvgPool2d((7, 7), 1)
uv_cfg = model_cfg['UV_HEAD']
depth_cfg = model_cfg['DEPTH_HEAD']
self.keypoints_2d_head = nn.Linear(uv_cfg['in_features'], uv_cfg['out_features'])
# self.depth_head = nn.Linear(depth_cfg['in_features'], depth_cfg['out_features'])
mesh_head_cfg = model_cfg["MESH_HEAD"].copy()
block_types_name = mesh_head_cfg['block_types']
block_types = []
block_map = {
"attention": AttentionBlock,
"identity": IdentityBlock,
"conv": SepConvBlock,
}
for name in block_types_name:
block_types.append(block_map[name])
mesh_head_cfg['block_types'] = block_types
self.mesh_head = MeshHead(**mesh_head_cfg)
def infer(self, image):
if self.is_hiera:
x, intermediates = self.backbone(image, return_intermediates=True)
features = intermediates[-1]
features = features.permute(0, 3, 1, 2).contiguous()
else:
features = self.backbone.forward_features(image)
global_feature = self.avg_pool(features).squeeze(-1).squeeze(-1)
uv = self.keypoints_2d_head(global_feature)
# depth = self.depth_head(global_feature)
vertices = self.mesh_head(features, uv)
joints = mesh_to_joints(vertices)
return {
"uv": uv,
# "root_depth": depth,
"joints": joints,
"vertices": vertices,
}
def forward(self, image, target=None):
"""get training loss
Args:
inputs (dict): {
'img': (B, 1, H, W),
"uv": [B, 21, 2],
"xyz": [B, 21, 3],
"hand_uv_valid": [B, 21],
"gamma": [B, 1],
"vertices": [B, 778, 3],
"xyz_valid": [B, 21],
"verts_valid": [B, 1],
"hand_valid": [B, 1],
}
"""
image = image / 255 - 0.5
output_dict = self.infer(image)
if self.training:
assert target is not None
loss_dict = self._cal_single_hand_losses(output_dict, target)
return loss_dict
return output_dict
def _cal_single_hand_losses(self, pred_hand_dict, gt_hand_dict):
"""get training loss
Args:
pred_hand_dict (dict): {
'uv': [B, 21, 2],
'root_depth': [B, 1],
'joints': [B, 21, 3],
# 'vertices': [B, 778, 2],
},
gt_hand_dict (dict): {
'uv': [B, 21, 2],
'xyz': [B, 21, 3],
'gamma': [B, 1],
'uv_valid': [B, 21],
# 'vertices': [B, 778, 3],
# 'xyz_valid': [B, 21],
# 'verts_valid': [B, 1],
},
"""
uv_pred = pred_hand_dict['uv']
# root_depth_pred = pred_hand_dict['root_depth']
joints_pred = pred_hand_dict["joints"]
vertices_pred = pred_hand_dict['vertices']
uv_pred = uv_pred.reshape(-1, 21, 2).contiguous()
joints_pred = joints_pred.reshape(-1, 21, 3).contiguous()
# root_depth_pred = root_depth_pred.reshape(-1, 1).contiguous()
uv_gt = gt_hand_dict['uv']
joints_gt = gt_hand_dict['xyz']
# root_depth_gt = gt_hand_dict['gamma'].reshape(-1, 1).contiguous()
hand_uv_valid = gt_hand_dict['uv_valid']
hand_xyz_valid = gt_hand_dict['xyz_valid'] # N, 1
vertices_gt = gt_hand_dict['vertices']
uv_loss = l1_loss(uv_pred, uv_gt, hand_uv_valid)
joints_loss = l1_loss(joints_pred, joints_gt, valid=hand_xyz_valid)
vertices_loss = l1_loss(vertices_pred, vertices_gt, valid=hand_xyz_valid)
# root_depth_loss = (torch.abs(root_depth_pred- root_depth_gt)).mean()
# root_depth_loss = root_depth_loss.mean()
loss_dict = {
"uv_loss": uv_loss * self.loss_cfg["UV_LOSS_WEIGHT"],
"joints_loss": joints_loss * self.loss_cfg["JOINTS_LOSS_WEIGHT"],
# "root_depth_loss": root_depth_loss * self.loss_cfg["DEPTH_LOSS_WEIGHT"],
"vertices_loss": vertices_loss * self.loss_cfg["VERTICES_LOSS_WEIGHT"],
}
total_loss = 0
for k in loss_dict:
total_loss += loss_dict[k]
loss_dict['total_loss'] = total_loss
return loss_dict
if __name__ == "__main__":
import pickle
import numpy as np
from cfg import _CONFIG
print('test forward')
x = np.random.uniform(0, 255, (1, 3, 224, 224)).astype(np.float32)
x = Tensor(x)
print(x.shape)
# model = timm.create_model("convnext_tiny", pretrained=True)
# print(model)
# out = model.forward_features(x)
# print(out.shape)
net = HandNet(_CONFIG)
print(net)
print("get losses")
path = 'batch_data.pkl'
with open(path, 'rb') as f:
batch_data = pickle.load(f)
for k in batch_data:
batch_data[k] = Tensor(batch_data[k]).float()
print(k, batch_data[k].shape, batch_data[k].max(), batch_data[k].min())
losses_dict = net(batch_data['img'],batch_data)
for key in losses_dict:
print(key, losses_dict[key].item())
# loss = losses_dict['total_loss']
# loss.backward()