-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
176 lines (136 loc) · 5.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
import sys
sys.path.append('model')
import time
import os
import random
from tqdm import tqdm
import wandb
import torch
import numpy as np
from utils.file_utils import get_logger
from dataloader.dsec_full import make_data_loader
####Important####
from model.TMA import TMA
####Important####
MAX_FLOW = 400
SUM_FREQ = 100
class Loss_Tracker:
def __init__(self, wandb):
self.running_loss = {}
self.total_steps = 0
self.wandb = wandb
def push(self, metrics):
self.total_steps += 1
for key in metrics:
if key not in self.running_loss:
self.running_loss[key] = 0.0
self.running_loss[key] += metrics[key]
if self.total_steps % SUM_FREQ == 0:
if self.wandb:
wandb.log({'EPE': self.running_loss['epe']/SUM_FREQ}, step=self.total_steps)
self.running_loss = {}
class Trainer:
def __init__(self, args):
self.args = args
self.model = TMA(input_bins=15)
self.model = self.model.cuda()
#Loader
self.train_loader = make_data_loader('trainval', args.batch_size, args.num_workers)
print('train_loader done!')
#Optimizer and scheduler for training
self.optimizer = torch.optim.AdamW(
self.model.parameters(),
lr=args.lr,
weight_decay=0.0001
)
self.scheduler = torch.optim.lr_scheduler.OneCycleLR(
self.optimizer,
max_lr=args.lr,
total_steps=args.num_steps + 100,
pct_start=0.01,
cycle_momentum=False,
anneal_strategy='linear')
#Logger
self.checkpoint_dir = args.checkpoint_dir
if not os.path.exists(self.checkpoint_dir):
os.mkdir(self.checkpoint_dir)
self.writer = get_logger(os.path.join(self.checkpoint_dir, 'train.log'))
self.tracker = Loss_Tracker(args.wandb)
self.writer.info('====A NEW TRAINING PROCESS====')
def train(self):
# self.writer.info(self.model)
self.writer.info(self.args)
self.model.train()
total_steps = 0
keep_training = True
while keep_training:
bar = tqdm(enumerate(self.train_loader),total=len(self.train_loader), ncols=60)
for index, (voxel1, voxel2, flow_map, valid2D) in bar:
self.optimizer.zero_grad()
flow_preds = self.model(voxel1.cuda(), voxel2.cuda())
flow_loss, loss_metrics = sequence_loss(flow_preds, flow_map.cuda(), valid2D.cuda(), self.args.weight, MAX_FLOW)
flow_loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.args.grad_clip)
self.optimizer.step()
self.scheduler.step()
bar.set_description(f'Step: {total_steps}/{self.args.num_steps}')
self.tracker.push(loss_metrics)
total_steps += 1
if total_steps >= self.args.num_steps - 10000 and total_steps % 5000 == 0:
ckpt = os.path.join(self.args.checkpoint_dir, f'checkpoint_{total_steps}.pth')
torch.save(self.model.state_dict(), ckpt)
if total_steps > self.args.num_steps:
keep_training = False
break
time.sleep(0.03)
ckpt_path = os.path.join(self.args.checkpoint_dir, 'checkpoint.pth')
torch.save(self.model.state_dict(), ckpt_path)
return ckpt_path
def set_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
def sequence_loss(flow_preds, flow_gt, valid, gamma=0.8, max_flow=MAX_FLOW):
""" Loss function defined over sequence of flow predictions """
n_predictions = len(flow_preds)
flow_loss = 0.0
# exlude invalid pixels and extremely large diplacements
mag = torch.sum(flow_gt**2, dim=1).sqrt()#b,h,w
valid = (valid >= 0.5) & (mag < max_flow)#b,1,h,w
for i in range(n_predictions):
i_weight = gamma**(n_predictions - i - 1)
i_loss = (flow_preds[i] - flow_gt).abs()
flow_loss += i_weight * (valid[:, None] * i_loss).mean()
epe = torch.sum((flow_preds[-1] - flow_gt)**2, dim=1).sqrt()
epe = epe.view(-1)[valid.view(-1)]
metrics = {
'epe': epe.mean().item(),
'1px': (epe < 1).float().mean().item(),
'3px': (epe < 3).float().mean().item(),
'5px': (epe < 5).float().mean().item(),
}
return flow_loss, metrics
if __name__=='__main__':
import argparse
parser = argparse.ArgumentParser(description='TMA')
#training setting
parser.add_argument('--num_steps', type=int, default=200000)
parser.add_argument('--checkpoint_dir', type=str, default='')
parser.add_argument('--lr', type=float, default=2e-4)
#dataloader setting
parser.add_argument('--batch_size', type=int, default=6)
parser.add_argument('--num_workers', type=int, default=8)
#model setting
parser.add_argument('--grad_clip', type=float, default=1)
# loss setting
parser.add_argument('--weight', type=float, default=0.8)
#wandb setting
parser.add_argument('--wandb', action='store_true', default=False)
args = parser.parse_args()
set_seed(1)
if args.wandb:
wandb_name = args.checkpoint_dir.split('/')[-1]
wandb.init(name=wandb_name, project='TMA_DSEC_full')
trainer = Trainer(args)
trainer.train()