-
Notifications
You must be signed in to change notification settings - Fork 15
/
train_FlowFormer.py
171 lines (134 loc) · 5.12 KB
/
train_FlowFormer.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
from __future__ import print_function, division
import sys
# sys.path.append('core')
import argparse
import os
import cv2
import time
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from core import optimizer
import core.datasets as datasets
from core.loss import sequence_loss
from core.loss import sequence_loss_smooth
from core.optimizer import fetch_optimizer
from core.utils.misc import process_cfg
from loguru import logger as loguru_logger
# from torch.utils.tensorboard import SummaryWriter
from core.utils.logger import Logger
# from core.FlowFormer import FlowFormer
from core.FlowFormer import build_flowformer
try:
from torch.cuda.amp import GradScaler
except:
# dummy GradScaler for PyTorch < 1.6
class GradScaler:
def __init__(self):
pass
def scale(self, loss):
return loss
def unscale_(self, optimizer):
pass
def step(self, optimizer):
optimizer.step()
def update(self):
pass
# exclude extremly large displacements
# VAL_FREQ = 5000
#torch.autograd.set_detect_anomaly(True)
def on_load_checkpoint(state_dict, model_state_dict):
is_changed = False
for k in state_dict:
if k in model_state_dict:
if state_dict[k].shape != model_state_dict[k].shape:
print(f"Skip loading parameter: {k}, "
f"required shape: {model_state_dict[k].shape}, "
f"loaded shape: {state_dict[k].shape}")
state_dict[k] = model_state_dict[k]
is_changed = True
return state_dict
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def train(cfg):
loss_func = sequence_loss
if cfg.use_smoothl1:
print("[Using smooth L1 loss]")
loss_func = sequence_loss_smooth
model = nn.DataParallel(build_flowformer(cfg))
loguru_logger.info("Parameter Count: %d" % count_parameters(model))
if cfg.restore_ckpt is not None:
print("[Loading ckpt from {}]".format(cfg.restore_ckpt))
#checkpoint = torch.load(cfg.restore_ckpt)
#checkpoint = on_load_checkpoint(checkpoint, model.state_dict())
#model.load_state_dict(checkpoint, strict=False)
model.load_state_dict(torch.load(cfg.restore_ckpt), strict=True)
model.cuda()
model.train()
#if args.stage != 'chairs':
# model.module.freeze_bn()
train_loader = datasets.fetch_dataloader(cfg)
optimizer, scheduler = fetch_optimizer(model, cfg.trainer)
total_steps = 0
scaler = GradScaler(enabled=cfg.mixed_precision)
logger = Logger(model, scheduler, cfg)
#add_noise = True
should_keep_training = True
while should_keep_training:
for i_batch, data_blob in enumerate(train_loader):
optimizer.zero_grad()
image1, image2, flow, valid = [x.cuda() for x in data_blob]
if cfg.add_noise:
#print("[Adding noise]")
stdv = np.random.uniform(0.0, 5.0)
image1 = (image1 + stdv * torch.randn(*image1.shape).cuda()).clamp(0.0, 255.0)
image2 = (image2 + stdv * torch.randn(*image2.shape).cuda()).clamp(0.0, 255.0)
output = {}
flow_predictions = model(image1, image2, output)
loss, metrics = loss_func(flow_predictions, flow, valid, cfg)
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), cfg.trainer.clip)
scaler.step(optimizer)
scheduler.step()
scaler.update()
metrics.update(output)
logger.push(metrics)
total_steps += 1
if total_steps > cfg.trainer.num_steps:
should_keep_training = False
break
logger.close()
PATH = cfg.log_dir + '/final'
torch.save(model.state_dict(), PATH)
return PATH
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--name', default='flowformer', help="name your experiment")
parser.add_argument('--stage', help="determines which dataset to use for training")
parser.add_argument('--validation', type=str, nargs='+')
parser.add_argument('--mixed_precision', action='store_true', help='use mixed precision')
args = parser.parse_args()
if args.stage == 'chairs':
from configs.default import get_cfg
elif args.stage == 'things':
from configs.things import get_cfg
elif args.stage == 'sintel':
from configs.sintel import get_cfg
elif args.stage == 'kitti':
from configs.kitti import get_cfg
cfg = get_cfg()
cfg.update(vars(args))
process_cfg(cfg)
loguru_logger.add(str(Path(cfg.log_dir) / 'log.txt'), encoding="utf8")
loguru_logger.info(cfg)
torch.manual_seed(1234)
np.random.seed(1234)
if not os.path.isdir('checkpoints'):
os.mkdir('checkpoints')
train(cfg)