-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathmain_test_avatarposer.py
181 lines (135 loc) · 6.23 KB
/
main_test_avatarposer.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
'''
# --------------------------------------------
# main testing code
# --------------------------------------------
# AvatarPoser: Articulated Full-Body Pose Tracking from Sparse Motion Sensing (ECCV 2022)
# https://github.com/eth-siplab/AvatarPoser
# Jiaxi Jiang ([email protected])
# Sensing, Interaction & Perception Lab,
# Department of Computer Science, ETH Zurich
'''
import os.path
import argparse
import numpy as np
import logging
import torch
from torch.utils.data import DataLoader
from utils import utils_logger
from utils import utils_option as option
from data.select_dataset import define_Dataset
from models.select_model import define_Model
from utils import utils_visualize as vis
save_animation = False
resolution = (800,800)
def main(json_path='options/test_avatarposer.json'):
'''
# ----------------------------------------
# Step--1 (prepare opt)
# ----------------------------------------
'''
parser = argparse.ArgumentParser()
parser.add_argument('-opt', type=str, default=json_path, help='Path to option JSON file.')
opt = option.parse(parser.parse_args().opt, is_train=True)
paths = (path for key, path in opt['path'].items() if 'pretrained' not in key)
if isinstance(paths, str):
if not os.path.exists(paths):
os.makedirs(paths)
else:
for path in paths:
if not os.path.exists(path):
os.makedirs(path)
# ----------------------------------------
# update opt
# ----------------------------------------
# -->-->-->-->-->-->-->-->-->-->-->-->-->-
init_iter, init_path_G = option.find_last_checkpoint(opt['path']['models'], net_type='G')
opt['path']['pretrained_netG'] = init_path_G
current_step = init_iter
# --<--<--<--<--<--<--<--<--<--<--<--<--<-
# ----------------------------------------
# save opt to a '../option.json' file
# ----------------------------------------
option.save(opt)
# ----------------------------------------
# return None for missing key
# ----------------------------------------
opt = option.dict_to_nonedict(opt)
# ----------------------------------------
# configure logger
# ----------------------------------------
logger_name = 'train'
utils_logger.logger_info(logger_name, os.path.join(opt['path']['log'], logger_name+'.log'))
logger = logging.getLogger(logger_name)
'''
# ----------------------------------------
# Step--2 (creat dataloader)
# ----------------------------------------
'''
# ----------------------------------------
# 1) create_dataset
# 2) creat_dataloader for train and test
# ----------------------------------------
dataset_type = opt['datasets']['test']['dataset_type']
for phase, dataset_opt in opt['datasets'].items():
if phase == 'test':
test_set = define_Dataset(dataset_opt)
test_loader = DataLoader(test_set, batch_size=dataset_opt['dataloader_batch_size'],
shuffle=False, num_workers=1,
drop_last=False, pin_memory=True)
elif phase == 'train':
continue
else:
raise NotImplementedError("Phase [%s] is not recognized." % phase)
'''
# ----------------------------------------
# Step--3 (initialize model)
# ----------------------------------------
'''
model = define_Model(opt)
if opt['merge_bn'] and current_step > opt['merge_bn_startpoint']:
logger.info('^_^ -----merging bnorm----- ^_^')
model.merge_bnorm_test()
model.init_test()
pos_error = []
vel_error = []
pos_error_hands = []
for index, test_data in enumerate(test_loader):
logger.info("testing the sample {}/{}".format(index, len(test_loader)))
model.feed_data(test_data, test=True)
model.test()
body_parms_pred = model.current_prediction()
body_parms_gt = model.current_gt()
predicted_angle = body_parms_pred['pose_body']
predicted_position = body_parms_pred['position']
predicted_body = body_parms_pred['body']
gt_angle = body_parms_gt['pose_body']
gt_position = body_parms_gt['position']
gt_body = body_parms_gt['body']
if index in [0, 10, 20] and save_animation:
video_dir = os.path.join(opt['path']['images'], str(index))
if not os.path.exists(video_dir):
os.makedirs(video_dir)
save_video_path_gt = os.path.join(video_dir, 'gt.avi')
if not os.path.exists(save_video_path_gt):
vis.save_animation(body_pose=gt_body, savepath=save_video_path_gt, bm = model.bm, fps=60, resolution = resolution)
save_video_path = os.path.join(video_dir, '{:d}.avi'.format(current_step))
vis.save_animation(body_pose=predicted_body, savepath=save_video_path, bm = model.bm, fps=60, resolution = resolution)
predicted_position = predicted_position#.cpu().numpy()
gt_position = gt_position#.cpu().numpy()
predicted_angle = predicted_angle.reshape(body_parms_pred['pose_body'].shape[0],-1,3)
gt_angle = gt_angle.reshape(body_parms_gt['pose_body'].shape[0],-1,3)
pos_error_ = torch.mean(torch.sqrt(torch.sum(torch.square(gt_position-predicted_position),axis=-1)))
pos_error_hands_ = torch.mean(torch.sqrt(torch.sum(torch.square(gt_position-predicted_position),axis=-1))[...,[20,21]])
gt_velocity = (gt_position[1:,...] - gt_position[:-1,...])*60
predicted_velocity = (predicted_position[1:,...] - predicted_position[:-1,...])*60
vel_error_ = torch.mean(torch.sqrt(torch.sum(torch.square(gt_velocity-predicted_velocity),axis=-1)))
pos_error.append(pos_error_)
vel_error.append(vel_error_)
pos_error_hands.append(pos_error_hands_)
pos_error = sum(pos_error)/len(pos_error)
vel_error = sum(vel_error)/len(vel_error)
pos_error_hands = sum(pos_error_hands)/len(pos_error_hands)
# testing log
logger.info('Average positional error [cm]: {:<.5f}, Average velocity error [cm/s]: {:<.5f}, Average positional error at hand [cm]: {:<.5f}\n'.format(pos_error*100, vel_error*100, pos_error_hands*100))
if __name__ == '__main__':
main()