-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_jta.py
290 lines (208 loc) · 9.99 KB
/
train_jta.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
import argparse
from datetime import datetime
import numpy as np
import os
import random
import time
import torch
from progress.bar import Bar
from torch.utils.data import DataLoader, ConcatDataset
from torch.utils.tensorboard import SummaryWriter
from dataset_jta import collate_batch, batch_process_coords, get_datasets, create_dataset
from model_jta import create_model
from utils.utils import create_logger, load_default_config, load_config, AverageMeter
from utils.metrics import MSE_LOSS
def evaluate_loss(model, dataloader, config):
bar = Bar(f"EVAL", fill="#", max=len(dataloader))
loss_avg = AverageMeter()
dataiter = iter(dataloader)
model.eval()
with torch.no_grad():
for i in range(len(dataloader)):
try:
joints, masks, padding_mask = next(dataiter)
except StopIteration:
break
in_joints, in_masks, out_joints, out_masks, padding_mask = batch_process_coords(joints, masks, padding_mask, config)
padding_mask = padding_mask.to(config["DEVICE"])
loss, _ = compute_loss(model, config, in_joints, out_joints, in_masks, out_masks, padding_mask)
loss_avg.update(loss.item(), len(in_joints))
summary = [
f"({i + 1}/{len(dataloader)})",
f"LOSS: {loss_avg.avg:.4f}",
f"T-TOT: {bar.elapsed_td}",
f"T-ETA: {bar.eta_td:}"
]
bar.suffix = " | ".join(summary)
bar.next()
bar.finish()
return loss_avg.avg
def compute_loss(model, config, in_joints, out_joints, in_masks, out_masks, padding_mask, epoch=None, mode='val', loss_last=True, optimizer=None):
_, in_F, _, _ = in_joints.shape
metamask = (mode == 'train')
pred_joints = model(in_joints, padding_mask, metamask=metamask)
loss = MSE_LOSS(pred_joints[:,in_F:], out_joints, out_masks)
return loss, pred_joints
def adjust_learning_rate(optimizer, epoch, config):
"""
From: https://github.com/microsoft/MeshTransformer/
Sets the learning rate to the initial LR decayed by x every y epochs
x = 0.1, y = args.num_train_epochs*2/3 = 100
"""
# dct_multi_overfit_3dpw_allsize_multieval_noseg_rot_permute_id
lr = config['TRAIN']['lr'] * (config['TRAIN']['lr_decay'] ** epoch) # (0.1 ** (epoch // (config['TRAIN']['epochs']*4./5.) ))
if 'lr_drop' in config['TRAIN'] and config['TRAIN']['lr_drop']:
lr = lr * (0.1 ** (epoch // (config['TRAIN']['epochs']*4./5.) ))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
print('lr: ',lr)
def save_checkpoint(model, optimizer, epoch, config, filename, logger):
logger.info(f'Saving checkpoint to {filename}.')
ckpt = {
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
'config': config
}
torch.save(ckpt, os.path.join(config['OUTPUT']['ckpt_dir'], filename))
def dataloader_for(dataset, config, **kwargs):
return DataLoader(dataset,
batch_size=config['TRAIN']['batch_size'],
num_workers=config['TRAIN']['num_workers'],
collate_fn=collate_batch,
**kwargs)
def dataloader_for_val(dataset, config, **kwargs):
return DataLoader(dataset,
batch_size=1,
num_workers=0,
collate_fn=collate_batch,
**kwargs)
def train(config, logger, experiment_name="", dataset_name=""):
################################
# Load data
################################
in_F, out_F = config['TRAIN']['input_track_size'], config['TRAIN']['output_track_size']
dataset_train = ConcatDataset(get_datasets(config['DATA']['train_datasets'], config, logger))
dataloader_train = dataloader_for(dataset_train, config, shuffle=True, pin_memory=True)
logger.info(f"Training on a total of {len(dataset_train)} annotations.")
dataset_val = create_dataset(config['DATA']['train_datasets'][0], logger, split="val", track_size=(in_F+out_F), track_cutoff=in_F)
dataloader_val = dataloader_for(dataset_val, config, shuffle=True, pin_memory=True)
writer_name = experiment_name + "_" + str(datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
writer_train = SummaryWriter(os.path.join(config["OUTPUT"]["runs_dir"], f"{writer_name}_TRAIN"))
writer_valid = SummaryWriter(os.path.join(config["OUTPUT"]["runs_dir"], f"{writer_name}_VALID"))
################################
# Create model, loss, optimizer
################################
model = create_model(config, logger)
if config["MODEL"]["checkpoint"] != "":
logger.info(f"Loading checkpoint from {config['MODEL']['checkpoint']}")
checkpoint = torch.load(os.path.join(config['OUTPUT']['ckpt_dir'], config["MODEL"]["checkpoint"]))
model.load_state_dict(checkpoint["model"])
optimizer = torch.optim.Adam(model.parameters(), lr=config['TRAIN']['lr'])
num_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
logger.info(f"Model has {num_parameters} parameters.")
################################
# Begin Training
################################
global_step = 0
min_val_loss = 1e4
for epoch in range(config["TRAIN"]["epochs"]):
start_time = time.time()
dataiter = iter(dataloader_train)
timer = {"DATA": 0, "FORWARD": 0, "BACKWARD": 0}
loss_avg = AverageMeter()
disc_loss_avg = AverageMeter()
disc_acc_avg = AverageMeter()
if config["TRAIN"]["optimizer"] == "adam":
adjust_learning_rate(optimizer, epoch, config)
train_steps = len(dataloader_train)
bar = Bar(f"TRAIN {epoch}/{config['TRAIN']['epochs'] - 1}", fill="#", max=train_steps)
for i in range(train_steps):
model.train()
optimizer.zero_grad()
################################
# Load a batch of data
################################
start = time.time()
try:
joints, masks, padding_mask = next(dataiter)
except StopIteration:
dataiter = iter(dataloader_train)
joints, masks, padding_mask = next(dataiter)
in_joints, in_masks, out_joints, out_masks, padding_mask = batch_process_coords(joints, masks, padding_mask, config, training=True)
padding_mask = padding_mask.to(config["DEVICE"])
timer["DATA"] = time.time() - start
################################
# Forward Pass
################################
start = time.time()
loss, pred_joints = compute_loss(model, config, in_joints, out_joints, in_masks, out_masks, padding_mask, epoch=epoch, mode='train', optimizer=None)
timer["FORWARD"] = time.time() - start
################################
# Backward Pass + Optimization
################################
start = time.time()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), config["TRAIN"]["max_grad_norm"])
optimizer.step()
timer["BACKWARD"] = time.time() - start
################################
# Logging
################################
loss_avg.update(loss.item(), len(joints))
summary = [
f"{str(epoch).zfill(3)} ({i + 1}/{train_steps})",
f"LOSS: {loss_avg.avg:.4f}",
f"T-TOT: {bar.elapsed_td}",
f"T-ETA: {bar.eta_td:}"
]
for key, val in timer.items():
summary.append(f"{key}: {val:.2f}")
bar.suffix = " | ".join(summary)
bar.next()
if cfg['dry_run']:
break
bar.finish()
################################
# Tensorboard logs
################################
global_step += train_steps
writer_train.add_scalar("loss", loss_avg.avg, epoch)
val_loss = evaluate_loss(model, dataloader_val, config)
writer_valid.add_scalar("loss", val_loss, epoch)
val_ade = val_loss/100
if val_ade < min_val_loss:
min_val_loss = val_ade
print('------------------------------BEST MODEL UPDATED------------------------------')
print('Best ADE: ', val_ade)
save_checkpoint(model, optimizer, epoch, config, 'best_val'+'_checkpoint.pth.tar', logger)
if cfg['dry_run']:
break
print('time for training: ', time.time()-start_time)
print('epoch ', epoch, ' finished!')
if not cfg['dry_run']:
save_checkpoint(model, optimizer, epoch, config, 'checkpoint.pth.tar', logger)
logger.info("All done.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--exp_name", type=str, default="", help="Experiment name. Otherwise will use timestamp")
parser.add_argument("--cfg", type=str, default="", help="Config name. Otherwise will use default config")
parser.add_argument('--dry-run', action='store_true', help="Run just one iteration")
args = parser.parse_args()
if args.cfg != "":
cfg = load_config(args.cfg, exp_name=args.exp_name)
else:
cfg = load_default_config()
cfg['dry_run'] = args.dry_run
random.seed(cfg['SEED'])
torch.manual_seed(cfg['SEED'])
np.random.seed(cfg['SEED'])
if torch.cuda.is_available():
cfg["DEVICE"] = f"cuda:{torch.cuda.current_device()}"
else:
cfg["DEVICE"] = "cpu"
dataset = cfg["DATA"]["train_datasets"]
logger = create_logger(cfg["OUTPUT"]["log_dir"])
logger.info("Initializing with config:")
logger.info(cfg)
train(cfg, logger, experiment_name=args.exp_name, dataset_name=dataset)