forked from MCG-NJU/CamLiFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.py
64 lines (54 loc) · 1.84 KB
/
factory.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
import torch
from omegaconf import DictConfig
from models import PWCFusionProSupervised
from driving import Driving
from flyingthings3d import FlyingThings3D
from kitti import KITTI
def dataset_factory(cfgs: DictConfig):
if cfgs.name == 'driving':
return Driving(cfgs)
elif cfgs.name == 'flyingthings3d':
return FlyingThings3D(cfgs)
elif cfgs.name == 'kitti':
return KITTI(cfgs)
else:
raise NotImplementedError('Unknown dataset: %s' % cfgs.name)
def model_factory(cfgs: DictConfig):
return PWCFusionProSupervised(cfgs)
def optimizer_factory(cfgs, named_params, last_epoch):
param_groups = [
{'params': [p for name, p in named_params if 'weight' in name],
'weight_decay': cfgs.weight_decay},
{'params': [p for name, p in named_params if 'bias' in name],
'weight_decay': cfgs.bias_decay}
]
if cfgs.optimizer == 'adam':
optimizer = torch.optim.Adam(
params=param_groups,
lr=cfgs.lr.init_value,
eps=1e-7
)
elif cfgs.optimizer == 'sgd':
optimizer = torch.optim.SGD(
params=param_groups,
lr=cfgs.lr.init_value,
momentum=cfgs.lr.momentum
)
else:
raise NotImplementedError('Unknown optimizer: %s' % cfgs.optimizer)
if isinstance(cfgs.lr.decay_milestones, int):
lr_scheduler = torch.optim.lr_scheduler.StepLR(
optimizer=optimizer,
step_size=cfgs.lr.decay_milestones,
gamma=cfgs.lr.decay_rate
)
else:
lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(
optimizer=optimizer,
milestones=cfgs.lr.decay_milestones,
gamma=cfgs.lr.decay_rate
)
for _ in range(last_epoch):
optimizer.step()
lr_scheduler.step()
return optimizer, lr_scheduler