-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
352 lines (308 loc) · 13.8 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
343
344
345
346
347
348
349
350
351
352
import torch
from torch.utils.data import DataLoader
from ego_pose.data_process import MoCapDataset, EgoMotionDataset
from ego_pose.transforms import *
from ego_pose.model import *
from ego_pose.loss import *
import shutil
from opts import parser
import torch.optim
import torch.nn.parallel
from torch.nn.utils import clip_grad_norm
import os
import time
from tqdm import tqdm
import logging
from torch.optim.lr_scheduler import LambdaLR
def main():
global args, best_loss
args = parser.parse_args()
best_loss = 1e10
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = EgoNet()
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model, device_ids=args.gpus).cuda()
else:
model.to(device)
path = os.getcwd()
save_path = os.path.join(path, 'logs', args.exp_name)
if not os.path.exists(save_path):
os.makedirs(save_path)
### save hyper parameters
save_hyperparameter(args)
### create log
logger = loadLogger(args)
### load checkpoints if exist
if args.resume:
if os.path.isfile(args.resume):
print(("=> loading checkpoint '{}'".format(args.resume)))
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
loss = checkpoint['loss']
model.load_state_dict(checkpoint['state_dict'])
print(("=> loaded checkpoint '{}' (epoch {})"
.format(args.evaluate, checkpoint['epoch'])))
else:
print(("=> no checkpoint found at '{}'".format(args.resume)))
if args.dataset == 'Yuan':
train_data = MoCapDataset(dataset_path=args.dataset_path,
config_path=args.config_path,
image_tmpl="{:05d}.png",
image_transform=torchvision.transforms.Compose([
Scale(256),
ToTorchFormatTensor(),
GroupNormalize(
mean=[.485, .456, .406],
std=[.229, .224, .225])
]), test_mode=False)
val_data = MoCapDataset(dataset_path=args.dataset_path,
config_path=args.config_path,
image_tmpl="{:05d}.png",
image_transform=torchvision.transforms.Compose([
Scale(256),
ToTorchFormatTensor(),
GroupNormalize(
mean=[.485, .456, .406],
std=[.229, .224, .225])
]), test_mode=True)
if args.dataset == 'EgoMotion':
train_data = EgoMotionDataset(dataset_path=args.dataset_path,
config_path=args.config_path,
image_tmpl="{:04d}.jpg",
image_transform=torchvision.transforms.Compose([
Scale(256),
ToTorchFormatTensor(),
GroupNormalize(
mean=[.485, .456, .406],
std=[.229, .224, .225])
]), test_mode=False)
val_data = EgoMotionDataset(dataset_path=args.dataset_path,
config_path=args.config_path,
image_tmpl="{:04d}.jpg",
image_transform=torchvision.transforms.Compose([
Scale(256),
ToTorchFormatTensor(),
GroupNormalize(
mean=[.485, .456, .406],
std=[.229, .224, .225])
]), test_mode=True)
train_loader = DataLoader(dataset=train_data, batch_size=args.batch_size,
shuffle=True,num_workers=args.workers, pin_memory=True)
val_loader = DataLoader(dataset=val_data, batch_size=args.batch_size,
shuffle=False, num_workers=args.workers, pin_memory=True)
if(args.optimizer=='SGD'):
optimizer = torch.optim.SGD(model.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
if(args.optimizer=='Adam'):
opt = [256, 100, 4000]
optimizer = torch.optim.Adam(
model.parameters(), lr=args.lr, betas=(0.9, 0.98), eps=1e-9
)
lr_scheduler = LambdaLR(
optimizer=optimizer, lr_lambda=lambda step: rate(step, *opt)
)
if args.evaluate:
validate(val_loader, model, 0)
return
for epoch in range(args.start_epoch, args.epochs):
logger.info(" Training epoch: {}".format(epoch+1))
if(args.optimizer=='SGD'):
adjust_learning_rate(optimizer, epoch, args.lr_steps)
# train for one epoch
# train(train_loader, model, optimizer, lr_scheduler, device, 200, logger)
train(train_loader, model, optimizer, scheduler=None, device=device, logger=logger)
# evaluate on validation set
if (epoch + 1) % args.eval_freq == 0 or epoch == args.epochs - 1:
logger.info(" Eval epoch: {}".format(epoch + 1))
loss1 = validate(val_loader, model, device, 30, logger)
# remember best prec@1 and save checkpoint
is_best = loss1 < best_loss
best_loss = min(loss1, best_loss)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'loss': loss1,
}, save_path, is_best)
# train(train_loader, model, optimizer, epoch, device)
def train(train_loader, model, optimizer, scheduler, device, batch_num=None, logger=None):
# dataset_path = '/data1/lty/dataset/egopose_dataset/datasets'
# config_path = '/data1/lty/dataset/egopose_dataset/datasets/meta/meta_subject_01.yml'
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
end = time.time()
if batch_num==None:
max_iter = len(train_loader)
else:
max_iter = batch_num
model.train()
for i, (image, label, motion) in tqdm(enumerate(train_loader), total=len(train_loader)):
data_time.update(time.time() - end)
# label shape: (32,1,48)->(32,48)
label = label.to(device).squeeze()
with torch.no_grad():
foreground = build_foreground(image)
foreground = foreground.to(device)
motion_input = motion.to(device)
keypoint = model(foreground, motion_input)
# loss = ComputeLoss(keypoint, head1, head2, label)
# print("keypoint shape: ", keypoint.shape)
# keypoint shape: (32,48)
# print("label shape: ", label.shape)
# label shape: (32,1,48)
loss = ComputeLoss_nohead(keypoint, label)
# print("loss: ", loss)
losses.update(loss.item(), image.shape[0])
# optimizer.zero_grad()
loss.backward()
### gradient clip: 用来限制过大的梯度
if args.clip_gradient is not None:
total_norm = clip_grad_norm(model.parameters(), args.clip_gradient)
# if total_norm > args.clip_gradient:
# print("clipping gradient: {} with coef {}".format(total_norm, args.clip_gradient / total_norm))
optimizer.step()
# scheduler.step()
optimizer.zero_grad()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
logger.info("lr: {:.5f} \tBatch({:>3}/{:>3}) done. Loss: {:.4f}".format(optimizer.param_groups[0]['lr'], i+1, max_iter, loss.data.item()))
def validate(val_loader, model, device, batch_num=None, logger=None):
batch_time = AverageMeter()
losses = AverageMeter()
end = time.time()
if batch_num==None:
max_iter = len(val_loader)
else:
max_iter = batch_num
model.eval()
for i, (image, label, motion) in tqdm(enumerate(val_loader), total=max_iter):
with torch.no_grad():
label = label.to(device).squeeze()
foreground = build_foreground(image)
# motion_input = build_motion_history(R, d)
foreground = foreground.to(device)
motion_input = motion.to(device)
keypoint = model(foreground, motion_input)
# loss = ComputeLoss(keypoint, head1, head2, label)
loss = ComputeLoss_nohead(keypoint, label)
losses.update(loss.item(), image.shape[0])
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
logger.info(" \tBatch({:>3}/{:>3}) done. Loss:{:.4f}".format(i+1, max_iter, loss.data.item()))
# if i > max_iter:
# break
# if i % args.print_freq == 0:
# print(('Test: [{0}/{1}]\t'
# 'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
# 'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
# i, len(val_loader), batch_time=batch_time, loss=losses)))
logger.info('Testing Results: Loss {loss.avg:.5f}'.format(loss=losses))
return loss
def save_checkpoint(state, save_path, is_best=True, filename='checkpoint.pth.tar'):
filename = '_'.join((args.snapshot_pref, filename))
file_path = os.path.join(save_path, filename)
torch.save(state, file_path)
if is_best:
best_name = '_'.join((args.snapshot_pref, 'model_best.pth.tar'))
best_path = os.path.join(save_path, best_name)
shutil.copyfile(file_path, best_path)
def adjust_learning_rate(optimizer, epoch, lr_steps):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
"""
lr_steps为预先给定的epoch列表
比如为[10,30,50]
那么一旦当前epoch数大于10 学习率就衰减为原来的0.1倍
大于30 再次衰减
......
"""
decay = 0.1 ** (sum(epoch >= np.array(lr_steps)))
lr = args.lr * decay
decay = args.weight_decay
# for param_group in optimizer.param_groups:
# param_group['lr'] = lr * param_group['lr_mult']
# param_group['weight_decay'] = decay * param_group['decay_mult']
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def save_hyperparameter(args):
path = os.getcwd()
basedir = os.path.join(path, 'logs', args.exp_name)
f = os.path.join(basedir, 'args.txt')
with open(f, 'w') as file:
for arg in sorted(vars(args)):
attr = getattr(args, arg)
file.write('{} = {}\n'.format(arg, attr))
def loadLogger(args):
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter(fmt="[ %(asctime)s ] %(message)s",
datefmt="%a %b %d %H:%M:%S %Y")
sHandler = logging.StreamHandler()
sHandler.setFormatter(formatter)
logger.addHandler(sHandler)
path = os.getcwd()
basedir = os.path.join(path, 'logs', args.exp_name)
work_dir = os.path.join(basedir,
time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime()))
if not os.path.exists(work_dir):
os.makedirs(work_dir)
fHandler = logging.FileHandler(work_dir + '/log.txt', mode='w')
fHandler.setLevel(logging.DEBUG)
fHandler.setFormatter(formatter)
logger.addHandler(fHandler)
return logger
def rate(step, model_size, factor, warmup):
"""
we have to default the step to 1 for LambdaLR function
to avoid zero raising to negative power.
"""
if step == 0:
step = 1
return factor * (
model_size ** (-0.5) * min(step ** (-0.5), step * warmup ** (-1.5))
)
def build_motion_history(R, d, nframes=31):
batch = R.shape[0]
R_t = R
d_t = d
#R_t = R.reshape(-1,3,3) # b,31,3,3 -> b*31,3,3
#d_t = d.reshape(-1,1,3) # b,31,1,3 -> b*31,1,3
R_hat = (R_t - torch.eye(3)).reshape(-1,nframes,1,9) # flatten: b,31,3,3->b,31,1,9
d_hat = d_t / 1.8 # 1.8 is the estimated height 这里是随便指定的
g_hat = 1
d_hat *= 15
g_hat = torch.tensor([0.3*(g_hat - 0.5)]).expand(batch, nframes, 1, 1)
motion_input = torch.cat([R_hat, d_hat, g_hat], dim=-1).permute(0,2,3,1) # (b,31,1,13).permute(0,2,3,1)
# print(motion_input.shape)
return motion_input
def build_foreground(img):
# img shape: b,3,256,256
batch = img.shape[0]
img_h = img.shape[2]
img_w = img.shape[3]
x_ = torch.linspace(0., 1., img_h)
y_ = torch.linspace(0., 1., img_w)
x_cord, y_cord = torch.meshgrid(x_, y_)
x = x_cord.reshape(1, 1, img_h, img_w).expand(batch, 1, img_h, img_w)
y = y_cord.reshape(1, 1, img_h, img_w).expand(batch, 1, img_h, img_w)
foreground = torch.cat([img, x, y], dim=1) # b,3,256,256 -> b,5,256,256
return foreground
if __name__ == '__main__':
main()