forked from wei-mao-2019/HisRepItself
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_amass_3d.py
188 lines (163 loc) · 7.52 KB
/
main_amass_3d.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
from utils import amass3d as datasets
from model import AttModel
from utils.opt import Options
from utils import util
from utils import log
from torch.utils.data import DataLoader
import torch
import torch.nn as nn
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
from progress.bar import Bar
import time
import h5py
import torch.optim as optim
def main(opt):
lr_now = opt.lr_now
start_epoch = 1
# opt.is_eval = True
print('>>> create models')
in_features = opt.in_features # 54
d_model = opt.d_model
kernel_size = opt.kernel_size
net_pred = AttModel.AttModel(in_features=in_features, kernel_size=kernel_size, d_model=d_model,
num_stage=opt.num_stage, dct_n=opt.dct_n)
net_pred.cuda()
optimizer = optim.Adam(filter(lambda x: x.requires_grad, net_pred.parameters()), lr=opt.lr_now)
print(">>> total params: {:.2f}M".format(sum(p.numel() for p in net_pred.parameters()) / 1000000.0))
if opt.is_load or opt.is_eval:
model_path_len = './{}/ckpt_best.pth.tar'.format(opt.ckpt)
print(">>> loading ckpt len from '{}'".format(model_path_len))
ckpt = torch.load(model_path_len)
start_epoch = ckpt['epoch'] + 1
err_best = ckpt['err']
lr_now = ckpt['lr']
net_pred.load_state_dict(ckpt['state_dict'])
# net.load_state_dict(ckpt)
# optimizer.load_state_dict(ckpt['optimizer'])
# lr_now = util.lr_decay_mine(optimizer, lr_now, 0.2)
print(">>> ckpt len loaded (epoch: {} | err: {})".format(ckpt['epoch'], ckpt['err']))
print('>>> loading datasets')
if not opt.is_eval:
dataset = datasets.Datasets(opt, split=0)
print('>>> Training dataset length: {:d}'.format(dataset.__len__()))
data_loader = DataLoader(dataset, batch_size=opt.batch_size, shuffle=True, num_workers=0, pin_memory=False)
valid_dataset = datasets.Datasets(opt, split=1)
print('>>> Validation dataset length: {:d}'.format(valid_dataset.__len__()))
valid_loader = DataLoader(valid_dataset, batch_size=opt.test_batch_size, shuffle=True, num_workers=0,
pin_memory=False)
test_dataset = datasets.Datasets(opt, split=2)
print('>>> Testing dataset length: {:d}'.format(test_dataset.__len__()))
test_loader = DataLoader(test_dataset, batch_size=opt.test_batch_size, shuffle=False, num_workers=0,
pin_memory=False)
# evaluation
if opt.is_eval:
ret_test = run_model(net_pred, is_train=3, data_loader=test_loader, opt=opt, epo=0)
ret_log = np.array([])
head = np.array([])
for k in ret_test.keys():
ret_log = np.append(ret_log, [ret_test[k]])
head = np.append(head, [k])
log.save_csv_log(opt, head, ret_log, is_create=True, file_name='test_walking')
# print('testing error: {:.3f}'.format(ret_test['m_p3d_h36']))
# training
if not opt.is_eval:
err_best = 1000
for epo in range(start_epoch, opt.epoch + 1):
is_best = False
# if epo % opt.lr_decay == 0:
lr_now = util.lr_decay_mine(optimizer, lr_now, 0.1 ** (1 / opt.epoch))
print('>>> training epoch: {:d}'.format(epo))
ret_train = run_model(net_pred, optimizer, is_train=0, data_loader=data_loader, epo=epo, opt=opt)
print('train error: {:.3f}'.format(ret_train['m_p3d_h36']))
ret_valid = run_model(net_pred, is_train=1, data_loader=valid_loader, opt=opt, epo=epo)
print('validation error: {:.3f}'.format(ret_valid['m_p3d_h36']))
ret_test = run_model(net_pred, is_train=2, data_loader=test_loader, opt=opt, epo=epo)
print('testing error: {:.3f}'.format(ret_test['#1']))
ret_log = np.array([epo, lr_now])
head = np.array(['epoch', 'lr'])
for k in ret_train.keys():
ret_log = np.append(ret_log, [ret_train[k]])
head = np.append(head, [k])
for k in ret_valid.keys():
ret_log = np.append(ret_log, [ret_valid[k]])
head = np.append(head, ['valid_' + k])
for k in ret_test.keys():
ret_log = np.append(ret_log, [ret_test[k]])
head = np.append(head, ['test_' + k])
log.save_csv_log(opt, head, ret_log, is_create=(epo == 1))
if ret_valid['m_p3d_h36'] < err_best:
err_best = ret_valid['m_p3d_h36']
is_best = True
log.save_ckpt({'epoch': epo,
'lr': lr_now,
'err': ret_valid['m_p3d_h36'],
'state_dict': net_pred.state_dict(),
'optimizer': optimizer.state_dict()},
is_best=is_best, opt=opt)
def run_model(net_pred, optimizer=None, is_train=0, data_loader=None, epo=1, opt=None):
if is_train == 0:
net_pred.train()
else:
net_pred.eval()
l_p3d = 0
# l_beta = 0
# j17to14 = [6, 5, 4, 1, 2, 3, 16, 15, 14, 11, 12, 13, 8, 10]
if is_train <= 1:
m_p3d_h36 = 0
else:
titles = np.array(range(opt.output_n)) + 1
m_p3d_h36 = np.zeros([opt.output_n])
n = 0
itera = 1
in_n = opt.input_n
out_n = opt.output_n
joint_used = np.arange(4, 22)
seq_in = opt.kernel_size
idx = np.expand_dims(np.arange(seq_in + 1), axis=1) + np.expand_dims(np.arange(out_n), axis=0)
st = time.time()
for i, (p3d_h36) in enumerate(data_loader):
batch_size, seq_n, _, _ = p3d_h36.shape
n += batch_size
bt = time.time()
p3d_h36 = p3d_h36.float().cuda()[:, :, joint_used] * 1000
p3d_sup = p3d_h36.clone()[:, -out_n - seq_in:]
p3d_src = p3d_h36.clone().reshape([batch_size, in_n + out_n, len(joint_used) * 3])
p3d_out_all = net_pred(p3d_src, output_n=out_n, input_n=in_n, itera=itera)
p3d_out = p3d_out_all[:, seq_in:].reshape([batch_size, out_n, len(joint_used), 3])
p3d_out_all = p3d_out_all[:, :, 0].reshape([batch_size, seq_in + out_n, len(joint_used), 3])
# 2d joint loss:
grad_norm = 0
if is_train == 0:
# loss_p3d = torch.mean(torch.sum(torch.abs(p3d_out_all - p3d_sup), dim=4))
loss_p3d = torch.mean(torch.norm(p3d_out_all - p3d_sup, dim=3))
loss_all = loss_p3d
optimizer.zero_grad()
loss_all.backward()
nn.utils.clip_grad_norm_(list(net_pred.parameters()), max_norm=opt.max_norm)
optimizer.step()
# update log values
l_p3d += loss_p3d.cpu().data.numpy() * batch_size
if is_train <= 1: # if is validation or train simply output the overall mean error
mpjpe_p3d_h36 = torch.mean(torch.norm(p3d_h36[:, in_n:in_n + out_n] - p3d_out, dim=3))
m_p3d_h36 += mpjpe_p3d_h36.cpu().data.numpy() * batch_size
else:
mpjpe_p3d_h36 = torch.sum(torch.mean(torch.norm(p3d_h36[:, in_n:] - p3d_out, dim=3), dim=2), dim=0)
m_p3d_h36 += mpjpe_p3d_h36.cpu().data.numpy()
if i % 1000 == 0:
print('{}/{}|bt {:.3f}s|tt{:.0f}s'.format(i + 1, len(data_loader), time.time() - bt, time.time() - st))
ret = {}
if is_train == 0:
ret["l_p3d"] = l_p3d / n
if is_train <= 1:
ret["m_p3d_h36"] = m_p3d_h36 / n
else:
m_p3d_h36 = m_p3d_h36 / n
for j in range(out_n):
ret["#{:d}".format(titles[j])] = m_p3d_h36[j]
return ret
if __name__ == '__main__':
option = Options().parse()
main(option)