-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
executable file
·342 lines (294 loc) · 16.5 KB
/
train.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os
import shutil
import torch
import numpy as np
from tqdm import tqdm
import torch.optim as optim
from torch.utils.data import DataLoader
from config import cfg
from utils.logger import setup_logger
from dataset.interhand import InterHandDataset
from models.dir import DIR
from utils.visualize import draw_2d_skeleton
import cv2
def vis(inputs, targets, outs, id, all_sample=False, stage_num=0):
img = inputs['img_rgb'].numpy()
joints_left_gt = targets['joint_2d_left'].numpy() * 128 + 128
joints_right_gt = targets['joint_2d_right'].numpy() * 128 + 128
if len(joints_left_gt.shape) == 4:
img = img.reshape([-1, 256, 256, 3])
joints_left_gt = joints_left_gt.reshape([-1, 21, 3])
joints_right_gt = joints_right_gt.reshape([-1, 21, 3])
sample_list = [0, 1, 2, 3]
for index in sample_list:
if 'pd_joint_uv_left' in outs:
img_left = draw_2d_skeleton(img[index], joints_left_gt[index])
cv2.imwrite(cfg.output_root + '/vis/%d_left_gt.png' % (id * cfg.batch_size + index), img_left)
img_right = draw_2d_skeleton(img[index], joints_right_gt[index])
cv2.imwrite(cfg.output_root + '/vis/%d_right_gt.png' % (id * cfg.batch_size + index), img_right)
joints_left_pd = outs['pd_joint_uv_left'].detach().cpu().numpy() * 128 + 128
joints_right_pd = outs['pd_joint_uv_right'].detach().cpu().numpy() * 128 + 128
img_left = draw_2d_skeleton(img[index], joints_left_pd[index])
cv2.imwrite(cfg.output_root + '/vis/%d_left_pd_%d.png' % (id * cfg.batch_size + index, stage_num), img_left)
img_right = draw_2d_skeleton(img[index], joints_right_pd[index])
cv2.imwrite(cfg.output_root + '/vis/%d_right_pd_%d.png' % (id * cfg.batch_size + index, stage_num), img_right)
else:
sample_list = [0]
for index in sample_list:
if 'pd_joint_uv_left' in outs:
img_left = draw_2d_skeleton(img[index], joints_left_gt[index])
cv2.imwrite(cfg.output_root + '/vis/%d_left_gt.png' % (id * cfg.batch_size + index), img_left)
img_right = draw_2d_skeleton(img[index], joints_right_gt[index])
cv2.imwrite(cfg.output_root + '/vis/%d_right_gt.png' % (id * cfg.batch_size + index), img_right)
joints_left_pd = outs['pd_joint_uv_left'].detach().cpu().numpy() * 128 + 128
joints_right_pd = outs['pd_joint_uv_right'].detach().cpu().numpy() * 128 + 128
img_left = draw_2d_skeleton(img[index], joints_left_pd[index])
cv2.imwrite(cfg.output_root + '/vis/%d_left_pd_%d.png' % (id * cfg.batch_size + index, stage_num), img_left)
img_right = draw_2d_skeleton(img[index], joints_right_pd[index])
cv2.imwrite(cfg.output_root + '/vis/%d_right_pd_%d.png' % (id * cfg.batch_size + index, stage_num), img_right)
def train():
torch.backends.cudnn.benchmark = True
trainer = Trainer()
trainer._make_model()
trainer._make_batch_loader()
min_error = 100
for epoch in range(trainer.start_epoch, cfg.total_epoch):
for iteration, (inputs, targets, meta_infos) in tqdm(enumerate(trainer.trian_loader)):
trainer.optimizer.zero_grad()
outs_list, loss = trainer.model(inputs, targets, meta_infos)
sum(loss[k] for k in loss).backward()
trainer.optimizer.step()
if iteration % cfg.print_iter == 0:
screen = ['[Epoch %d/%d]' % (epoch, cfg.total_epoch),
'[Batch %d/%d]' % (iteration, len(trainer.trian_loader)),
'[lr %f]' % (trainer.get_lr())]
screen += ['[%s: %.4f]' % ('loss_' + k, v.detach()) for k, v in loss.items()]
trainer.logger.info(''.join(screen))
if iteration % cfg.draw_iter == 0:
if len(outs_list) > 1:
for stage_index, outs in enumerate(outs_list):
vis(inputs, targets, outs, iteration, stage_num=stage_index)
else:
vis(inputs, targets, outs_list[-1], iteration, stage_num=0)
trainer.schedule.step()
trainer.save_model(trainer.model, trainer.optimizer, trainer.schedule, epoch, 'latest')
if not epoch % cfg.eval_interval:
error = trainer.test_model()
if error < min_error:
trainer.save_model(trainer.model, trainer.optimizer, trainer.schedule, epoch, 'best')
min_error = error
def test():
torch.backends.cudnn.benchmark = True
tester = Tester()
tester._make_model()
tester._make_batch_loader()
tester.model.eval()
tester.test_model()
class Trainer:
def __init__(self):
log_folder = os.path.join(cfg.output_root, 'log')
if not os.path.exists(log_folder):
os.makedirs(log_folder)
logfile = os.path.join(log_folder, 'train_' + cfg.experiment_name + '.log')
vis_folder = os.path.join(cfg.output_root, 'vis')
if not os.path.exists(vis_folder):
os.makedirs(vis_folder)
file_folder = os.path.join(cfg.output_root, 'files')
if not os.path.exists(file_folder):
os.makedirs(file_folder)
shutil.copytree('./SemGCN', file_folder + '/SemGCN/')
shutil.copytree('./models', file_folder + '/models/')
shutil.copytree('./dataset', file_folder + '/dataset/')
shutil.copytree('./utils', file_folder + '/utils/')
shutil.copy('./train.py', file_folder + '/train.py')
shutil.copy('./config.py', file_folder + '/config.py')
self.logger = setup_logger(output=logfile, name="Training")
self.logger.info('Start training: %s' % ('train_' + cfg.experiment_name))
def load_model(self, checkpoint_dir, model, optimizer, schedule):
checkpoint = torch.load(checkpoint_dir)
self.logger.info("Loading the model of epoch-{} from {}...".format(checkpoint['last_epoch'], checkpoint_dir))
model.load_state_dict(checkpoint['net'])
optimizer.load_state_dict(checkpoint['optimizer'])
schedule.load_state_dict(checkpoint['schedule'])
start_epoch = checkpoint['last_epoch'] + 1
self.logger.info('The model is loaded successfully.')
return start_epoch, model
def save_model(self, model, optimizer, schedule, epoch, name):
save = {
'net': model.state_dict(),
'optimizer': optimizer.state_dict(),
'schedule': schedule.state_dict(),
'last_epoch': epoch
}
path_checkpoint = os.path.join(cfg.output_root, 'checkpoint')
if not os.path.exists(path_checkpoint):
os.makedirs(path_checkpoint)
save_path = os.path.join(path_checkpoint, "%s.pth" % (name))
torch.save(save, save_path)
self.logger.info('Save checkpoint to {}'.format(save_path))
def get_lr(self):
for g in self.optimizer.param_groups:
cur_lr = g['lr']
return cur_lr
@torch.no_grad()
def test_model(self):
self.model.eval()
iter_num = 0
joint_error_left_sum, joint_error_right_sum = np.zeros([cfg.stage_num]), np.zeros([cfg.stage_num])
mesh_error_left_sum, mesh_error_right_sum = np.zeros([cfg.stage_num]), np.zeros([cfg.stage_num])
for iteration, (inputs, targets, meta_infos) in tqdm(enumerate(self.test_loader)):
outs_list, loss = self.model(inputs, targets, meta_infos)
for stage_index in range(cfg.stage_num):
joint_error_left, joint_error_right, mesh_error_left, mesh_error_right = \
self.test_dataset.evaluate(outs_list[stage_index], targets, meta_infos)
joint_error_left_sum[stage_index] += joint_error_left
joint_error_right_sum[stage_index] += joint_error_right
mesh_error_left_sum[stage_index] += mesh_error_left
mesh_error_right_sum[stage_index] += mesh_error_right
if iteration % cfg.draw_iter == 0:
vis(inputs, targets, outs_list[-1], iteration, False, stage_index)
iter_num += 1
for stage_index in range(cfg.stage_num):
joints_mean_loss_left = joint_error_left_sum[stage_index] / iter_num
joints_mean_loss_right = joint_error_right_sum[stage_index] / iter_num
verts_mean_loss_left = mesh_error_left_sum[stage_index] / iter_num
verts_mean_loss_right = mesh_error_right_sum[stage_index] / iter_num
print('MPJPE_%d:' % (stage_index))
print(' left: {} mm, right: {} mm'.format(joints_mean_loss_left, joints_mean_loss_right))
print(' all: {} mm'.format((joints_mean_loss_left + joints_mean_loss_right) / 2))
print('MPVPE_%d:' % (stage_index))
print(' left: {} mm, right: {} mm'.format(verts_mean_loss_left, verts_mean_loss_right))
print(' all: {} mm'.format((verts_mean_loss_left + verts_mean_loss_right) / 2))
self.logger.info(
'MPJPE_{}: left {} mm, right {} mm, AVG {} mm'.format(
stage_index,
joints_mean_loss_left, joints_mean_loss_right,
(joints_mean_loss_left + joints_mean_loss_right) / 2))
self.logger.info(
'MPVPE_{}: left {} mm, right {} mm, AVG {} mm'.format(
stage_index,
verts_mean_loss_left, verts_mean_loss_right,
(verts_mean_loss_left + verts_mean_loss_right) / 2))
self.model.train()
return (joints_mean_loss_left + joints_mean_loss_right) / 2
def _make_batch_loader(self):
self.logger.info("Creating dataset...")
self.train_dataset = InterHandDataset(cfg.data_dir, 'train', cfg.root_joint)
self.trian_loader = DataLoader(self.train_dataset,
batch_size=cfg.batch_size,
num_workers=cfg.num_worker,
shuffle=True,
pin_memory=True,
drop_last=True)
self.test_dataset = InterHandDataset(cfg.data_dir, 'test', cfg.root_joint)
self.test_loader = DataLoader(self.test_dataset,
batch_size=cfg.batch_size,
num_workers=cfg.num_worker,
shuffle=False,
pin_memory=True,
drop_last=True)
self.logger.info("The dataset is created successfully.")
def _make_model(self):
self.logger.info("Making the model...")
model = DIR(cfg.joint_num, cfg.mano_path, cfg.root_joint).cuda()
optimizer = optim.AdamW([{'params': model.parameters(), 'initial_lr': cfg.lr}], cfg.lr)
if cfg.lr_scheduler == 'cosine':
schedule = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=cfg.total_epoch, eta_min=0)
elif cfg.lr_scheduler == 'step':
schedule = optim.lr_scheduler.MultiStepLR(optimizer, [30], gamma=0.1, last_epoch=-1)
if cfg.continue_train:
start_epoch, model = self.load_model(cfg.checkpoint, model, optimizer, schedule)
else:
start_epoch = 0
model.train()
self.start_epoch = start_epoch
self.model = model
self.optimizer = optimizer
self.schedule = schedule
self.logger.info("The model is made successfully.")
class Tester:
def __init__(self):
log_folder = os.path.join(cfg.output_root, 'log')
if not os.path.exists(log_folder):
os.makedirs(log_folder)
logfile = os.path.join(log_folder, 'eval_' + cfg.experiment_name + '.log')
self.logger = setup_logger(output=logfile, name="Evaluation")
self.logger.info('Start evaluation: %s' % ('eval_' + cfg.experiment_name))
vis_folder = os.path.join(cfg.output_root, 'vis')
if not os.path.exists(vis_folder):
os.makedirs(vis_folder)
def _make_batch_loader(self):
self.logger.info("Creating dataset...")
self.dataset = InterHandDataset(cfg.data_dir, 'test', cfg.root_joint)
self.loader = DataLoader(self.dataset,
batch_size=cfg.batch_size,
num_workers=cfg.num_worker,
shuffle=False,
pin_memory=True,
drop_last=True)
self.logger.info("The dataset is created successfully.")
def load_model(self, model):
if cfg.checkpoint != '':
self.logger.info('Loading the model from {}...'.format(cfg.checkpoint))
checkpoint = torch.load(cfg.checkpoint)
model.load_state_dict(checkpoint['net'])
self.logger.info('The model is loaded successfully.')
elif cfg.output_root != '':
self.logger.info('Loading the model from {}...'.format(cfg.output_root))
checkpoint = torch.load(cfg.output_root+'/checkpoint/latest.pth')
model.load_state_dict(checkpoint['net'])
self.logger.info('The model is loaded successfully.')
else:
self.logger.info('No model is loaded.')
return model
def _make_model(self):
self.logger.info("Making the model...")
model = DIR(cfg.joint_num, cfg.mano_path, cfg.root_joint).cuda()
model = self.load_model(model)
model.eval()
self.model = model
self.logger.info("The model is made successfully.")
@torch.no_grad()
def test_model(self):
self.model.eval()
iter_num = 0
joint_error_left_sum, joint_error_right_sum = np.zeros([cfg.stage_num]), np.zeros([cfg.stage_num])
mesh_error_left_sum, mesh_error_right_sum = np.zeros([cfg.stage_num]), np.zeros([cfg.stage_num])
for iteration, (inputs, targets, meta_infos) in tqdm(enumerate(self.loader)):
outs_list, loss = self.model(inputs, targets, meta_infos)
for stage_index in range(cfg.stage_num):
joint_error_left, joint_error_right, mesh_error_left, mesh_error_right = \
self.dataset.evaluate(outs_list[stage_index], targets, meta_infos)
joint_error_left_sum[stage_index] += joint_error_left
joint_error_right_sum[stage_index] += joint_error_right
mesh_error_left_sum[stage_index] += mesh_error_left
mesh_error_right_sum[stage_index] += mesh_error_right
if iteration % cfg.draw_iter == 0:
vis(inputs, targets, outs_list[-2], iteration, False, stage_index)
iter_num += 1
for stage_index in range(cfg.stage_num):
joints_mean_loss_left = joint_error_left_sum[stage_index] / iter_num
joints_mean_loss_right = joint_error_right_sum[stage_index] / iter_num
verts_mean_loss_left = mesh_error_left_sum[stage_index] / iter_num
verts_mean_loss_right = mesh_error_right_sum[stage_index] / iter_num
print('MPJPE_%d:' % (stage_index))
print(' left: {} mm, right: {} mm'.format(joints_mean_loss_left, joints_mean_loss_right))
print(' all: {} mm'.format((joints_mean_loss_left + joints_mean_loss_right) / 2))
print('MPVPE_%d:' % (stage_index))
print(' left: {} mm, right: {} mm'.format(verts_mean_loss_left, verts_mean_loss_right))
print(' all: {} mm'.format((verts_mean_loss_left + verts_mean_loss_right) / 2))
self.logger.info(
'MPJPE_{}: left {} mm, right {} mm, AVG {} mm'.format(
stage_index,
joints_mean_loss_left, joints_mean_loss_right,
(joints_mean_loss_left + joints_mean_loss_right) / 2))
self.logger.info(
'MPVPE_{}: left {} mm, right {} mm, AVG {} mm'.format(
stage_index,
verts_mean_loss_left, verts_mean_loss_right,
(verts_mean_loss_left + verts_mean_loss_right) / 2))
if __name__ == '__main__':
if cfg.phase == 'train':
train()
else:
test()