forked from MengHao666/Minimal-Hand-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimize_shape.py
97 lines (72 loc) · 2.58 KB
/
optimize_shape.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
import argparse
import numpy as np
from tqdm import tqdm
from utils import func, bone
from utils.LM import LM_Solver
def align_bone_len(opt_, pre_):
opt = opt_.copy()
pre = pre_.copy()
opt_align = opt.copy()
for i in range(opt.shape[0]):
ratio = pre[i][6] / opt[i][6]
opt_align[i] = ratio * opt_align[i]
err = np.abs(opt_align - pre).mean(0)
return err
def main(args):
path=args.path
for dataset in args.dataset:
# load predictions (N*21*3)
print("load {}'s joint 3D".format(dataset))
pred_j3d = np.load("{}/{}_pre_joints.npy".format(path, dataset),allow_pickle=True)
opt_shapes = []
opt_bone_lens = []
pre_useful_bone_lens = []
# loop
for pred in tqdm(pred_j3d):
# 0 initialization
pose, shape = func.initiate("zero")
pre_useful_bone_len = bone.caculate_length(pred, label="useful")
pre_useful_bone_lens.append(pre_useful_bone_len)
# optimize here!
solver = LM_Solver(num_Iter=500, th_beta=shape, th_pose=pose, lb_target=pre_useful_bone_len,
weight=args.weight)
opt_shape = solver.LM()
opt_shapes.append(opt_shape)
opt_bone_len = solver.get_bones(opt_shape)
opt_bone_lens.append(opt_bone_len)
# plt.plot(solver.get_result(), 'r')
# plt.show()
# break
opt_shapes = np.array(opt_shapes).reshape(-1, 10)
opt_bone_lens = np.array(opt_bone_lens).reshape(-1, 15)
pre_useful_bone_lens = np.array(pre_useful_bone_lens).reshape(-1, 15)
np.save("{}/{}_shapes.npy".format(path, dataset, args.weight), opt_shapes)
error = align_bone_len(opt_bone_lens, pre_useful_bone_lens)
print("dataset:{} weight:{} ERR sum: {}".format(dataset, args.weight, error.sum()))
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='optimize shape params. of mano model ')
# Dataset setting
parser.add_argument(
'-ds',
"--dataset",
nargs="+",
default=['rhd', 'stb', 'do', 'eo'],
type=str,
help="sub datasets, should be listed in: [stb|rhd|do|eo]"
)
parser.add_argument(
'-wt', '--weight',
default=1e-5,
type=float,
metavar='weight',
help='weight of L2 regularizer '
)
parser.add_argument(
'-p',
'--path',
default='out_testset',
type=str,
metavar='data_root',
help='directory')
main(parser.parse_args())