-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtrain.py
309 lines (249 loc) · 11.7 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# ------------------------------------------------------------------------------
# The code is from GLPDepth (https://github.com/vinvino02/GLPDepth).
# For non-commercial purpose only (research, evaluation etc).
# -----------------------------------------------------------------------------
import os
import cv2
import numpy as np
from datetime import datetime
import torch
import torch.optim as optim
import torch.backends.cudnn as cudnn
from tensorboardX import SummaryWriter
from models.model import GLPDepth
from models.optimizer import build_optimizers
import utils.metrics as metrics
from utils.criterion import SiLogLoss
import utils.logging as logging
from dataset.base_dataset import get_dataset
from configs.train_options import TrainOptions
import glob
metric_name = ['d1', 'd2', 'd3', 'abs_rel', 'sq_rel', 'rmse', 'rmse_log',
'log10', 'silog']
def load_model(ckpt, model, optimizer=None):
ckpt_dict = torch.load(ckpt, map_location='cpu')
# keep backward compatibility
if 'model' not in ckpt_dict and 'optimizer' not in ckpt_dict:
state_dict = ckpt_dict
else:
state_dict = ckpt_dict['model']
weights = {}
for key, value in state_dict.items():
if key.startswith('module.'):
weights[key[len('module.'):]] = value
else:
weights[key] = value
model.load_state_dict(weights)
if optimizer is not None:
optimizer_state = ckpt_dict['optimizer']
optimizer.load_state_dict(optimizer_state)
def main():
opt = TrainOptions()
args = opt.initialize().parse_args()
print(args)
pretrain = args.pretrained.split('.')[0]
maxlrstr = str(args.max_lr).replace('.', '')
minlrstr = str(args.min_lr).replace('.', '')
layer_decaystr = str(args.layer_decay).replace('.', '')
weight_decaystr = str(args.weight_decay).replace('.', '')
num_filter = str(args.num_filters[0]) if args.num_deconv > 0 else ''
num_kernel = str(args.deconv_kernels[0]) if args.num_deconv > 0 else ''
name = [args.dataset, str(args.batch_size), pretrain.split('/')[-1], 'deconv'+str(args.num_deconv), \
str(num_filter), str(num_kernel), str(args.crop_h), str(args.crop_w), maxlrstr, minlrstr, \
layer_decaystr, weight_decaystr, str(args.epochs)]
if 'swin' in args.backbone:
for i in args.window_size:
name.append(str(i))
for i in args.depths:
name.append(str(i))
if args.exp_name != '':
name.append(args.exp_name)
exp_name = '_'.join(name)
print('This experiments: ', exp_name)
# Logging
exp_name = '%s_%s' % (datetime.now().strftime('%m%d'), exp_name)
log_dir = os.path.join(args.log_dir, exp_name)
logging.check_and_make_dirs(log_dir)
writer = SummaryWriter(logdir=log_dir)
log_txt = os.path.join(log_dir, 'logs.txt')
logging.log_args_to_txt(log_txt, args)
global result_dir
result_dir = os.path.join(log_dir, 'results')
if not os.path.exists(result_dir):
os.makedirs(result_dir)
model = GLPDepth(args=args)
# CPU-GPU agnostic settings
if args.gpu_or_cpu == 'gpu':
device = torch.device('cuda')
cudnn.benchmark = True
model = torch.nn.DataParallel(model)
else:
device = torch.device('cpu')
model.to(device)
# Dataset setting
dataset_kwargs = {'dataset_name': args.dataset, 'data_path': args.data_path}
dataset_kwargs['crop_size'] = (args.crop_h, args.crop_w)
train_dataset = get_dataset(**dataset_kwargs)
val_dataset = get_dataset(**dataset_kwargs, is_train=False)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=args.batch_size,
shuffle=True, num_workers=args.workers,
pin_memory=True, drop_last=True)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=1, shuffle=False,
pin_memory=True)
# Training settings
criterion_d = SiLogLoss()
optimizer = build_optimizers(model, dict(type='AdamW', lr=args.max_lr, betas=(0.9, 0.999), weight_decay=args.weight_decay,
constructor='SwinLayerDecayOptimizerConstructor',
paramwise_cfg=dict(num_layers=args.depths, layer_decay_rate=args.layer_decay, no_decay_names=['relative_position_bias_table', 'rpe_mlp', 'logit_scale'])))
start_ep = 1
if args.resume_from:
load_model(args.resume_from, model.module, optimizer)
strlength = len('_model.ckpt')
resume_ep = int(args.resume_from[-strlength-2:-strlength])
print(f'resumed from epoch {resume_ep}, ckpt {args.resume_from}')
start_ep = resume_ep + 1
if args.auto_resume:
ckpt_list = glob.glob(f'{log_dir}/epoch_*_model.ckpt')
strlength = len('_model.ckpt')
idx = [ckpt[-strlength-2:-strlength] for ckpt in ckpt_list]
if len(idx) > 0:
idx.sort(key=lambda x: -int(x))
ckpt = f'{log_dir}/epoch_{idx[0]}_model.ckpt'
load_model(ckpt, model.module, optimizer)
resume_ep = int(idx[0])
print(f'resumed from epoch {resume_ep}, ckpt {ckpt}')
start_ep = resume_ep + 1
global global_step
iterations = len(train_loader)
global_step = iterations * (start_ep - 1)
# Perform experiment
for epoch in range(start_ep, args.epochs + 1):
print('\nEpoch: %03d - %03d' % (epoch, args.epochs))
loss_train = train(train_loader, model, criterion_d, log_txt, optimizer=optimizer,
device=device, epoch=epoch, args=args)
writer.add_scalar('Training loss', loss_train, epoch)
if args.save_model:
torch.save(
{
'model': model.state_dict(),
'optimizer': optimizer.state_dict()
},
os.path.join(log_dir, 'epoch_%02d_model.ckpt' % epoch))
if epoch % args.val_freq == 0:
results_dict, loss_val = validate(val_loader, model, criterion_d,
device=device, epoch=epoch, args=args,
log_dir=log_dir)
writer.add_scalar('Val loss', loss_val, epoch)
result_lines = logging.display_result(results_dict)
if args.kitti_crop:
print("\nCrop Method: ", args.kitti_crop)
print(result_lines)
with open(log_txt, 'a') as txtfile:
txtfile.write('\nEpoch: %03d - %03d' % (epoch, args.epochs))
txtfile.write(result_lines)
for each_metric, each_results in results_dict.items():
writer.add_scalar(each_metric, each_results, epoch)
def train(train_loader, model, criterion_d, log_txt, optimizer, device, epoch, args):
global global_step
model.train()
depth_loss = logging.AverageMeter()
half_epoch = args.epochs // 2
iterations = len(train_loader)
result_lines = []
for batch_idx, batch in enumerate(train_loader):
global_step += 1
if global_step < iterations * half_epoch:
current_lr = (args.max_lr - args.min_lr) * (global_step /
iterations/half_epoch) ** 0.9 + args.min_lr
else:
current_lr = max(args.min_lr, (args.min_lr - args.max_lr) * (global_step /
iterations/half_epoch - 1) ** 0.9 + args.max_lr)
for param_group in optimizer.param_groups:
param_group['lr'] = current_lr*param_group['lr_scale'] if 'swin' in args.backbone else current_lr
input_RGB = batch['image'].to(device)
depth_gt = batch['depth'].to(device)
preds = model(input_RGB)
optimizer.zero_grad()
loss_d = criterion_d(preds['pred_d'].squeeze(), depth_gt)
depth_loss.update(loss_d.item(), input_RGB.size(0))
loss_d.backward()
if args.pro_bar:
logging.progress_bar(batch_idx, len(train_loader), args.epochs, epoch,
('Depth Loss: %.4f (%.4f)' %
(depth_loss.val, depth_loss.avg)))
if batch_idx % args.print_freq == 0:
result_line = 'Epoch: [{0}][{1}/{2}]\t' \
'Loss: {loss}, LR: {lr}\n'.format(
epoch, batch_idx, iterations,
loss=depth_loss.avg, lr=current_lr
)
result_lines.append(result_line)
print(result_line)
optimizer.step()
with open(log_txt, 'a') as txtfile:
txtfile.write('\nEpoch: %03d - %03d' % (epoch, args.epochs))
for result_line in result_lines:
txtfile.write(result_line)
return loss_d
def validate(val_loader, model, criterion_d, device, epoch, args, log_dir):
depth_loss = logging.AverageMeter()
model.eval()
result_metrics = {}
for metric in metric_name:
result_metrics[metric] = 0.0
for batch_idx, batch in enumerate(val_loader):
input_RGB = batch['image'].to(device)
depth_gt = batch['depth'].to(device)
filename = batch['filename'][0]
with torch.no_grad():
if args.shift_window_test:
bs, _, h, w = input_RGB.shape
assert w > h and bs == 1
interval_all = w - h
interval = interval_all // (args.shift_size-1)
sliding_images = []
sliding_masks = torch.zeros((bs, 1, h, w), device=input_RGB.device)
for i in range(args.shift_size):
sliding_images.append(input_RGB[..., :, i*interval:i*interval+h])
sliding_masks[..., :, i*interval:i*interval+h] += 1
input_RGB = torch.cat(sliding_images, dim=0)
if args.flip_test:
input_RGB = torch.cat((input_RGB, torch.flip(input_RGB, [3])), dim=0)
pred = model(input_RGB)
pred_d = pred['pred_d']
if args.flip_test:
batch_s = pred_d.shape[0]//2
pred_d = (pred_d[:batch_s] + torch.flip(pred_d[batch_s:], [3]))/2.0
if args.shift_window_test:
pred_s = torch.zeros((bs, 1, h, w), device=pred_d.device)
for i in range(args.shift_size):
pred_s[..., :, i*interval:i*interval+h] += pred_d[i:i+1]
pred_d = pred_s/sliding_masks
pred_d = pred_d.squeeze()
depth_gt = depth_gt.squeeze()
loss_d = criterion_d(pred_d.squeeze(), depth_gt)
depth_loss.update(loss_d.item(), input_RGB.size(0))
pred_crop, gt_crop = metrics.cropping_img(args, pred_d, depth_gt)
computed_result = metrics.eval_depth(pred_crop, gt_crop)
save_path = os.path.join(result_dir, filename)
if save_path.split('.')[-1] == 'jpg':
save_path = save_path.replace('jpg', 'png')
if args.save_result:
if args.dataset == 'kitti':
pred_d_numpy = pred_d.cpu().numpy() * 256.0
cv2.imwrite(save_path, pred_d_numpy.astype(np.uint16),
[cv2.IMWRITE_PNG_COMPRESSION, 0])
else:
pred_d_numpy = pred_d.cpu().numpy() * 1000.0
cv2.imwrite(save_path, pred_d_numpy.astype(np.uint16),
[cv2.IMWRITE_PNG_COMPRESSION, 0])
loss_d = depth_loss.avg
if args.pro_bar:
logging.progress_bar(batch_idx, len(val_loader), args.epochs, epoch)
for key in result_metrics.keys():
result_metrics[key] += computed_result[key]
for key in result_metrics.keys():
result_metrics[key] = result_metrics[key] / (batch_idx + 1)
return result_metrics, loss_d
if __name__ == '__main__':
main()