-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrain_BOFNet.py
145 lines (113 loc) · 4.24 KB
/
train_BOFNet.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
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_3frames as datasets
from core.loss import sequence_loss
from core.optimizer import fetch_optimizer
from core.utils.misc import process_cfg
from loguru import logger as loguru_logger
from core.utils.logger import Logger
from core.Networks import build_network
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
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_network(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))
model.load_state_dict(torch.load(cfg.restore_ckpt), strict=True)
model.cuda()
model.train()
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)
should_keep_training = True
while should_keep_training:
for i_batch, data_blob in enumerate(train_loader):
optimizer.zero_grad()
images, flows, valids = [x.cuda() for x in data_blob]
if cfg.add_noise:
stdv = np.random.uniform(0.0, 5.0)
images = (images + stdv * torch.randn(*images.shape).cuda()).clamp(0.0, 255.0)
output = {}
flow_predictions = model(images, output)
loss, metrics, _ = loss_func(flow_predictions, flows, valids, 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)
if total_steps % cfg.val_freq == cfg.val_freq - 1:
PATH = '%s/%d_%s.pth' % (cfg.log_dir, total_steps+1, cfg.name)
# torch.save(model.state_dict(), PATH)
results = {}
for val_dataset in cfg.validation:
if val_dataset == 'sintel_train':
results.update(evaluate_tile.validate_sintel(model.module))
logger.write_dict(results)
model.train()
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='+')
args = parser.parse_args()
if 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)
train(cfg)