-
Notifications
You must be signed in to change notification settings - Fork 112
/
train.py
84 lines (66 loc) · 2.15 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
# -*- coding: future_fstrings -*-
import open3d as o3d # prevent loading error
import sys
import json
import logging
import torch
from easydict import EasyDict as edict
from lib.data_loaders import make_data_loader
from config import get_config
from lib.trainer import ContrastiveLossTrainer, HardestContrastiveLossTrainer, \
TripletLossTrainer, HardestTripletLossTrainer
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="")
def get_trainer(trainer):
if trainer == 'ContrastiveLossTrainer':
return ContrastiveLossTrainer
elif trainer == 'HardestContrastiveLossTrainer':
return HardestContrastiveLossTrainer
elif trainer == 'TripletLossTrainer':
return TripletLossTrainer
elif trainer == 'HardestTripletLossTrainer':
return HardestTripletLossTrainer
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,
num_threads=config.train_num_thread)
if config.test_valid:
val_loader = make_data_loader(
config,
config.val_phase,
config.val_batch_size,
num_threads=config.val_num_thread)
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 __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)