-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_train.py
executable file
·241 lines (183 loc) · 9.11 KB
/
run_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
import os
import argparse
from collections import defaultdict
import time
import numpy as np
import torch
from torch import optim
from torch.utils.tensorboard import SummaryWriter
from torch.nn.utils import clip_grad_norm_
from torch.utils.data import DataLoader
from torchvision.transforms import Normalize
from tqdm import tqdm
from arguments import train_parser
from model import GraphSuperResolutionNet
from data import MiddleburyDataset, NYUv2Dataset, DIMLDataset
from utils import new_log, to_cuda, seed_all
class Trainer:
def __init__(self, args: argparse.Namespace):
self.args = args
self.use_wandb = self.args.wandb
self.dataloaders = self.get_dataloaders(args)
seed_all(args.seed)
self.model = GraphSuperResolutionNet(
args.scaling,
args.crop_size,
args.feature_extractor,
lambda_init=args.lambda_init,
mu_init=args.mu_init
)
self.model.cuda()
self.experiment_folder = new_log(os.path.join(args.save_dir, args.dataset), args)
if self.use_wandb:
wandb.init(project=args.wandb_project, dir=self.experiment_folder)
wandb.config.update(self.args)
self.writer = None
else:
self.writer = SummaryWriter(log_dir=self.experiment_folder)
if args.optimizer == 'adam':
self.optimizer = optim.Adam(self.model.parameters(), lr=args.lr, weight_decay=args.w_decay)
elif args.optimizer == 'sgd':
self.optimizer = optim.SGD(self.model.parameters(), lr=args.lr, momentum=self.args.momentum,
weight_decay=args.w_decay)
if args.lr_scheduler == 'step':
self.scheduler = optim.lr_scheduler.StepLR(self.optimizer, step_size=args.lr_step, gamma=args.lr_gamma)
elif args.lr_scheduler == 'plateau':
self.scheduler = optim.lr_scheduler.ReduceLROnPlateau(self.optimizer, patience=args.lr_step,
factor=args.lr_gamma)
else:
self.scheduler = None
self.epoch = 0
self.iter = 0
self.train_stats = defaultdict(lambda: np.nan)
self.val_stats = defaultdict(lambda: np.nan)
self.best_optimization_loss = np.inf
if args.resume is not None:
self.resume(path=args.resume)
def __del__(self):
if not self.use_wandb:
self.writer.close()
def train(self):
with tqdm(range(self.epoch, self.args.num_epochs), leave=True) as tnr:
tnr.set_postfix(training_loss=np.nan, validation_loss=np.nan, best_validation_loss=np.nan)
for _ in tnr:
self.train_epoch(tnr)
if (self.epoch + 1) % self.args.val_every_n_epochs == 0:
self.validate()
if self.args.save_model in ['last', 'both']:
self.save_model('last')
if self.args.lr_scheduler == 'step':
self.scheduler.step()
if self.use_wandb:
wandb.log({'log_lr': np.log10(self.scheduler.get_last_lr())}, self.iter)
else:
self.writer.add_scalar('log_lr', np.log10(self.scheduler.get_last_lr()), self.epoch)
self.epoch += 1
def train_epoch(self, tnr=None):
self.train_stats = defaultdict(float)
self.model.train()
with tqdm(self.dataloaders['train'], leave=False) as inner_tnr:
inner_tnr.set_postfix(training_loss=np.nan)
for i, sample in enumerate(inner_tnr):
sample = to_cuda(sample)
self.optimizer.zero_grad()
output = self.model(sample)
loss, loss_dict = self.model.get_loss(output, sample, kind=self.args.loss)
for key in loss_dict:
self.train_stats[key] += loss_dict[key]
if self.epoch > 0 or not self.args.skip_first:
loss.backward()
if self.args.gradient_clip > 0.:
clip_grad_norm_(self.model.parameters(), self.args.gradient_clip)
self.optimizer.step()
self.iter += 1
if (i + 1) % min(self.args.logstep_train, len(self.dataloaders['train'])) == 0:
self.train_stats = {k: v / self.args.logstep_train for k, v in self.train_stats.items()}
inner_tnr.set_postfix(training_loss=self.train_stats['optimization_loss'])
if tnr is not None:
tnr.set_postfix(training_loss=self.train_stats['optimization_loss'],
validation_loss=self.val_stats['optimization_loss'],
best_validation_loss=self.best_optimization_loss)
if self.use_wandb:
wandb.log({k + '/train': v for k, v in self.train_stats.items()}, self.iter)
else:
for key in self.train_stats:
self.writer.add_scalar('train/' + key, self.train_stats[key], self.iter)
# reset metrics
self.train_stats = defaultdict(float)
def validate(self):
self.val_stats = defaultdict(float)
self.model.eval()
with torch.no_grad():
for sample in tqdm(self.dataloaders['val'], leave=False):
sample = to_cuda(sample)
output = self.model(sample)
loss, loss_dict = self.model.get_loss(output, sample, kind=self.args.loss)
for key in loss_dict:
self.val_stats[key] += loss_dict[key]
self.val_stats = {k: v / len(self.dataloaders['val']) for k, v in self.val_stats.items()}
if self.use_wandb:
wandb.log({k + '/val': v for k, v in self.val_stats.items()}, self.iter)
else:
for key in self.val_stats:
self.writer.add_scalar('val/' + key, self.val_stats[key], self.epoch)
if self.val_stats['optimization_loss'] < self.best_optimization_loss:
self.best_optimization_loss = self.val_stats['optimization_loss']
if self.args.save_model in ['best', 'both']:
self.save_model('best')
@staticmethod
def get_dataloaders(args):
data_args = {
'crop_size': (args.crop_size, args.crop_size),
'in_memory': args.in_memory,
'max_rotation_angle': args.max_rotation,
'do_horizontal_flip': not args.no_flip,
'crop_valid': True,
'image_transform': Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
'scaling': args.scaling
}
phases = ('train', 'val')
if args.dataset == 'Middlebury':
depth_transform = Normalize([2296.78], [1122.7])
datasets = {phase: MiddleburyDataset(os.path.join(args.data_dir, 'Middlebury'), **data_args, split=phase,
depth_transform=depth_transform, crop_deterministic=phase == 'val') for phase in phases}
elif args.dataset == 'DIML':
depth_transform = Normalize([2749.64], [1154.29])
datasets = {phase: DIMLDataset(os.path.join(args.data_dir, 'DIML'), **data_args, split=phase,
depth_transform=depth_transform) for phase in phases}
elif args.dataset == 'NYUv2':
depth_transform = Normalize([2796.32], [1386.05])
datasets = {phase: NYUv2Dataset(os.path.join(args.data_dir, 'NYU Depth v2'), **data_args, split=phase,
depth_transform=depth_transform) for phase in phases}
else:
raise NotImplementedError(f'Dataset {args.dataset}')
return {phase: DataLoader(datasets[phase], batch_size=args.batch_size, num_workers=args.num_workers,
shuffle=True, drop_last=False) for phase in phases}
def save_model(self, prefix=''):
torch.save({
'model': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
'scheduler': self.scheduler.state_dict(),
'epoch': self.epoch + 1,
'iter': self.iter
}, os.path.join(self.experiment_folder, f'{prefix}_model.pth'))
def resume(self, path):
if not os.path.isfile(path):
raise RuntimeError(f'No checkpoint found at \'{path}\'')
checkpoint = torch.load(path)
self.model.load_state_dict(checkpoint['model'])
self.optimizer.load_state_dict(checkpoint['optimizer'])
self.scheduler.load_state_dict(checkpoint['scheduler'])
self.epoch = checkpoint['epoch']
self.iter = checkpoint['iter']
print(f'Checkpoint \'{path}\' loaded.')
if __name__ == '__main__':
args = train_parser.parse_args()
print(train_parser.format_values())
if args.wandb:
import wandb
trainer = Trainer(args)
since = time.time()
trainer.train()
time_elapsed = time.time() - since
print('Training completed in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))