forked from XiaohangZhan/deocclusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
executable file
·260 lines (218 loc) · 10 KB
/
trainer.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
import os
import cv2
import time
import numpy as np
import torch
import torch.optim
import torch.distributed as dist
import torchvision.utils as vutils
from torch.utils.data import DataLoader
import models
import utils
import datasets
#from dataset import ImageRawDataset, PartialCompEvalDataset, PartialCompDataset
import inference as infer
import pdb
class Trainer(object):
def __init__(self, args):
# get rank
self.world_size = dist.get_world_size()
self.rank = dist.get_rank()
if self.rank == 0:
# mkdir path
if not os.path.exists('{}/events'.format(args.exp_path)):
os.makedirs('{}/events'.format(args.exp_path))
if not os.path.exists('{}/images'.format(args.exp_path)):
os.makedirs('{}/images'.format(args.exp_path))
if not os.path.exists('{}/logs'.format(args.exp_path)):
os.makedirs('{}/logs'.format(args.exp_path))
if not os.path.exists('{}/checkpoints'.format(args.exp_path)):
os.makedirs('{}/checkpoints'.format(args.exp_path))
# logger
if args.trainer['tensorboard']:
try:
from tensorboardX import SummaryWriter
except:
raise Exception("Please switch off \"tensorboard\" "
"in your config file if you do not "
"want to use it, otherwise install it.")
self.tb_logger = SummaryWriter('{}/events'.format(
args.exp_path))
else:
self.tb_logger = None
if args.validate:
self.logger = utils.create_logger(
'global_logger',
'{}/logs/log_offline_val.txt'.format(args.exp_path))
else:
self.logger = utils.create_logger(
'global_logger',
'{}/logs/log_train.txt'.format(args.exp_path))
# create model
self.model = models.__dict__[args.model['algo']](
args.model, load_pretrain=args.load_pretrain, dist_model=True)
# optionally resume from a checkpoint
assert not (args.load_iter is not None and args.load_pretrain is not None), \
"load_iter and load_pretrain are exclusive."
if args.load_iter is not None:
self.model.load_state("{}/checkpoints".format(args.exp_path),
args.load_iter, args.resume)
self.start_iter = args.load_iter
else:
self.start_iter = 0
self.curr_step = self.start_iter
# lr scheduler & datasets
trainval_class = datasets.__dict__[args.data['trainval_dataset']]
if not args.validate: # train
self.lr_scheduler = utils.StepLRScheduler(
self.model.optim,
args.model['lr_steps'],
args.model['lr_mults'],
args.model['lr'],
args.model['warmup_lr'],
args.model['warmup_steps'],
last_iter=self.start_iter - 1)
train_dataset = trainval_class(args.data, 'train')
train_sampler = utils.DistributedGivenIterationSampler(
train_dataset,
args.model['total_iter'],
args.data['batch_size'],
last_iter=self.start_iter - 1)
self.train_loader = DataLoader(train_dataset,
batch_size=args.data['batch_size'],
shuffle=False,
num_workers=args.data['workers'],
pin_memory=False,
sampler=train_sampler)
val_dataset = trainval_class(args.data, 'val')
val_sampler = utils.DistributedSequentialSampler(val_dataset)
self.val_loader = DataLoader(
val_dataset,
batch_size=args.data['batch_size_val'],
shuffle=False,
num_workers=args.data['workers'],
pin_memory=False,
sampler=val_sampler)
self.args = args
def run(self):
# offline validate
if self.args.validate:
self.validate('off_val')
return
if self.args.trainer['initial_val']:
self.validate('on_val')
# train
self.train()
def train(self):
btime_rec = utils.AverageMeter(10)
dtime_rec = utils.AverageMeter(10)
recorder = {}
for rec in self.args.trainer['loss_record']:
recorder[rec] = utils.AverageMeter(10)
self.model.switch_to('train')
end = time.time()
for i, inputs in enumerate(self.train_loader):
self.curr_step = self.start_iter + i
self.lr_scheduler.step(self.curr_step)
curr_lr = self.lr_scheduler.get_lr()[0]
# measure data loading time
dtime_rec.update(time.time() - end)
self.model.set_input(*inputs)
loss_dict = self.model.step()
for k in loss_dict.keys():
recorder[k].update(utils.reduce_tensors(loss_dict[k]).item())
btime_rec.update(time.time() - end)
end = time.time()
self.curr_step += 1
# logging
if self.rank == 0 and self.curr_step % self.args.trainer[
'print_freq'] == 0:
loss_str = ""
if self.tb_logger is not None:
self.tb_logger.add_scalar('lr', curr_lr, self.curr_step)
for k in recorder.keys():
if self.tb_logger is not None:
self.tb_logger.add_scalar('train_{}'.format(k),
recorder[k].avg,
self.curr_step)
loss_str += '{}: {loss.val:.4g} ({loss.avg:.4g})\t'.format(
k, loss=recorder[k])
self.logger.info(
'Iter: [{0}/{1}]\t'.format(self.curr_step,
len(self.train_loader)) +
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'.format(
batch_time=btime_rec) +
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'.format(
data_time=dtime_rec) + loss_str +
'lr {lr:.2g}'.format(lr=curr_lr))
# save
if (self.rank == 0 and
(self.curr_step % self.args.trainer['save_freq'] == 0 or
self.curr_step == self.args.model['total_iter'])):
self.model.save_state(
"{}/checkpoints".format(self.args.exp_path),
self.curr_step)
# validate
if (self.curr_step % self.args.trainer['val_freq'] == 0 or
self.curr_step == self.args.model['total_iter']):
self.validate('on_val')
def validate(self, phase):
btime_rec = utils.AverageMeter(0)
dtime_rec = utils.AverageMeter(0)
recorder = {}
for rec in self.args.trainer['loss_record']:
recorder[rec] = utils.AverageMeter(10)
self.model.switch_to('eval')
end = time.time()
all_together = []
for i, inputs in enumerate(self.val_loader):
if ('val_iter' in self.args.trainer and
self.args.trainer['val_iter'] != -1 and
i == self.args.trainer['val_iter']):
break
dtime_rec.update(time.time() - end)
self.model.set_input(*inputs)
tensor_dict, loss_dict = self.model.forward_only()
for k in loss_dict.keys():
recorder[k].update(utils.reduce_tensors(loss_dict[k]).item())
btime_rec.update(time.time() - end)
end = time.time()
# tb visualize
if self.rank == 0:
disp_start = max(self.args.trainer['val_disp_start_iter'], 0)
disp_end = min(self.args.trainer['val_disp_end_iter'], len(self.val_loader))
if (i >= disp_start and i < disp_end):
all_together.append(
utils.visualize_tensor(tensor_dict,
self.args.data.get('data_mean', [0,0,0]),
self.args.data.get('data_std', [1,1,1])))
if (i == disp_end - 1 and disp_end > disp_start):
all_together = torch.cat(all_together, dim=2)
grid = vutils.make_grid(all_together,
nrow=1,
normalize=True,
range=(0, 255),
scale_each=False)
if self.tb_logger is not None:
self.tb_logger.add_image('Image_' + phase, grid,
self.curr_step)
cv2.imwrite("{}/images/{}_{}.png".format(
self.args.exp_path, phase, self.curr_step),
grid.permute(1, 2, 0).numpy())
# logging
if self.rank == 0:
loss_str = ""
for k in recorder.keys():
if self.tb_logger is not None and phase == 'on_val':
self.tb_logger.add_scalar('val_{}'.format(k),
recorder[k].avg,
self.curr_step)
loss_str += '{}: {loss.val:.4g} ({loss.avg:.4g})\t'.format(
k, loss=recorder[k])
self.logger.info(
'Validation Iter: [{0}]\t'.format(self.curr_step) +
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'.format(
batch_time=btime_rec) +
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'.format(
data_time=dtime_rec) + loss_str)
self.model.switch_to('train')