-
Notifications
You must be signed in to change notification settings - Fork 5
/
train.py
99 lines (79 loc) · 2.48 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
import open3d as o3d # prevent loading error
import sys
import json
import logging
import torch
from easydict import EasyDict as edict
from lib.all_data_loaders import make_data_loader
from config import get_config
from lib.lfgc_trainer import LFGCTrainer
from lib.oa_trainer import OATrainer
from lib.twodim_trainer import ImageCorrespondenceTrainer
ch = logging.StreamHandler(sys.stdout)
logging.getLogger().setLevel(logging.INFO)
logging.basicConfig(
format='%(asctime)s %(message)s', datefmt='%m/%d %H:%M:%S', handlers=[ch])
torch.manual_seed(0)
torch.cuda.manual_seed(0)
logging.basicConfig(level=logging.INFO, format="")
TRAINERS = [
# Register Trainer here
LFGCTrainer,
OATrainer,
ImageCorrespondenceTrainer,
]
trainer_map = {t.__name__: t for t in TRAINERS}
def get_trainer(trainer):
if trainer in trainer_map.keys():
return trainer_map[trainer]
else:
raise ValueError(f'Trainer {trainer} not found')
def main(config, resume=False):
train_loader = make_data_loader(
config,
config.train_phase,
config.batch_size,
shuffle=True,
repeat=True,
num_workers=config.train_num_workers)
if config.test_valid:
val_loader = make_data_loader(
config,
config.val_phase,
config.val_batch_size,
shuffle=True,
repeat=True,
num_workers=config.val_num_workers)
else:
val_loader = None
Trainer = get_trainer(config.trainer)
trainer = Trainer(
config=config,
data_loader=train_loader,
val_data_loader=val_loader,
)
trainer.train()
if config.final_test:
test_loader = make_data_loader(
config, "test", config.val_batch_size, num_workers=config.val_num_workers)
trainer.val_data_loader = test_loader
test_dict = trainer._valid_epoch()
test_loss = test_dict['loss']
trainer.writer.add_scalar('test/loss', test_loss, config.max_epoch)
logging.info(f" Test loss: {test_loss}")
if __name__ == "__main__":
logger = logging.getLogger()
config = get_config()
dconfig = vars(config)
if config.resume_dir:
resume_config = json.load(open(config.resume_dir + '/config.json', 'r'))
for k in dconfig:
if k not in ['resume_dir'] and k in resume_config:
dconfig[k] = resume_config[k]
dconfig['resume'] = resume_config['out_dir'] + '/checkpoint.pth'
logging.info('===> Configurations')
for k in dconfig:
logging.info(' {}: {}'.format(k, dconfig[k]))
# Convert to dict
config = edict(dconfig)
main(config)