-
Notifications
You must be signed in to change notification settings - Fork 24
/
train.py
185 lines (153 loc) · 6.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
import os
import argparse
import importlib
from tqdm import tqdm, trange
from collections import Counter
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from lib.config import config, update_config, infer_exp_id
from lib import dataset
def train_loop(net, loader, optimizer):
net.train()
if config.training.fix_encoder_bn:
apply_fn_based_on_key(net.encoder, ['bn'], lambda m: m.eval())
epoch_losses = Counter()
for iit, batch in tqdm(enumerate(loader, 1), position=1, total=len(loader)):
# Move data to the given computation device
for k, v in batch.items():
if torch.is_tensor(v):
batch[k] = v.to(device)
# feed forward & compute losses
losses = net.compute_losses(batch)
if len(losses) == 0:
continue
# backprop
optimizer.zero_grad()
losses['total'].backward()
optimizer.step()
# Log
BS = len(batch['x'])
epoch_losses['N'] += BS
for k, v in losses.items():
if torch.is_tensor(v):
epoch_losses[k] += BS * v.item()
else:
epoch_losses[k] += BS * v
# Statistic over the epoch
N = epoch_losses.pop('N')
for k, v in epoch_losses.items():
epoch_losses[k] = v / N
return epoch_losses
def valid_loop(net, loader):
net.eval()
epoch_losses = Counter()
with torch.no_grad():
for iit, batch in tqdm(enumerate(loader, 1), position=1, total=len(loader)):
for k, v in batch.items():
if torch.is_tensor(v):
batch[k] = v.to(device)
# feed forward & compute losses
losses = net.compute_losses(batch)
# Log
for k, v in losses.items():
if torch.is_tensor(v):
epoch_losses[k] += float(v.item()) / len(loader)
else:
epoch_losses[k] += v / len(loader)
return epoch_losses
def apply_fn_based_on_key(net, key_lst, fn):
for name, m in net.named_modules():
if any(k in name for k in key_lst):
fn(m)
def group_parameters(net, wd_group_mode):
wd = []
nowd = []
for name, p in net.named_parameters():
if not p.requires_grad:
continue
if wd_group_mode == 'bn and bias':
if 'bn' in name or 'bias' in name:
nowd.append(p)
else:
wd.append(p)
elif wd_group_mode == 'encoder decoder':
if 'feature_extractor' in name:
nowd.append(p)
else:
wd.append(p)
return [{'params': wd}, {'params': nowd, 'weight_decay': 0}]
if __name__ == '__main__':
# Parse args & config
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--cfg', required=True)
parser.add_argument('opts',
help='Modify config options using the command-line',
default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
update_config(config, args)
# Init global variable
exp_id = infer_exp_id(args.cfg)
exp_ckpt_root = os.path.join(config.ckpt_root, exp_id)
os.makedirs(exp_ckpt_root, exist_ok=True)
device = 'cuda' if config.cuda else 'cpu'
if config.cuda and config.cuda_benchmark:
torch.backends.cudnn.benchmark = True
# Init dataset
DatasetClass = getattr(dataset, config.dataset.name)
config.dataset.train_kwargs.update(config.dataset.common_kwargs)
config.dataset.valid_kwargs.update(config.dataset.common_kwargs)
train_dataset = DatasetClass(**config.dataset.train_kwargs)
valid_dataset = DatasetClass(**config.dataset.valid_kwargs)
train_loader = DataLoader(train_dataset, config.training.batch_size,
shuffle=True, drop_last=True,
num_workers=config.num_workers,
pin_memory=config.cuda,
worker_init_fn=lambda x: np.random.seed())
valid_loader = DataLoader(valid_dataset, 1,
num_workers=config.num_workers,
pin_memory=config.cuda)
# Init network
model_file = importlib.import_module(config.model.file)
model_class = getattr(model_file, config.model.modelclass)
net = model_class(**config.model.kwargs).to(device)
if config.training.fix_encoder_bn:
apply_fn_based_on_key(net.encoder, ['bn'], lambda m: m.requires_grad_(False))
# Init optimizer
if config.training.optim == 'Adam':
optimizer = torch.optim.Adam(
group_parameters(net, config.training.wd_group_mode),
lr=config.training.optim_lr, weight_decay=config.training.weight_decay)
elif config.training.optim == 'AdamW':
optimizer = torch.optim.AdamW(
group_parameters(net, config.training.wd_group_mode),
lr=config.training.optim_lr, weight_decay=config.training.weight_decay)
elif config.training.optim == 'SGD':
optimizer = torch.optim.SGD(
group_parameters(net, config.training.wd_group_mode), momentum=0.9,
lr=config.training.optim_lr, weight_decay=config.training.weight_decay)
if config.training.optim_poly_gamma > 0:
def lr_poly_rate(epoch):
return (1 - epoch / config.training.epoch) ** config.training.optim_poly_gamma
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lr_lambda=lr_poly_rate)
else:
scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer, milestones=[int(p * config.training.epoch) for p in config.training.optim_milestons],
gamma=config.training.optim_gamma)
# Start training
for iep in trange(1, config.training.epoch + 1, position=0):
# Train phase
epoch_losses = train_loop(net, train_loader, optimizer)
scheduler.step()
print(f'EP[{iep}/{config.training.epoch}] train: ' +
' \ '.join([f'{k} {v:.3f}' for k, v in epoch_losses.items()]))
# Periodically save model
if iep % config.training.save_every == 0:
torch.save(net.state_dict(), os.path.join(exp_ckpt_root, f'ep{iep}.pth'))
print('Model saved')
# Valid phase
epoch_losses = valid_loop(net, valid_loader)
print(f'EP[{iep}/{config.training.epoch}] valid: ' +
' \ '.join([f'{k} {v:.3f}' for k, v in epoch_losses.items()]))