-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
87 lines (70 loc) · 3.39 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
import argparse
import collections
import torch
import datahandler.loaders as module_loader
import datahandler.transforms as module_transform
import trainer as module_trainer
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
import model.lr_scheduler as module_scheduler
import model.optimizer as module_optimizer
from parse_config import ConfigParser
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = False
def main(config):
logger = config.get_logger('train')
try:
tfms = config.init_obj('transformations', [module_transform])
except KeyError:
tfms = None
# setup data_loader instances
data_loader = config.init_obj('data_loader', [module_loader], tfms=tfms)
valid_data_loader = data_loader.split_validation()
# build model architecture, then print to console
model = config.init_obj('arch', [module_arch])
freeze_to = config['arch'].get('freeze_to', False)
if freeze_to:
print(freeze_to)
model.freeze(freeze_to)
logger.info(model)
# get function handles of loss and metrics
# criterion = config.init_ftn('loss', [torch.nn.functional, module_loss])
criterion = config.init_obj('loss', [module_loss, torch.nn])
epoch_metrics = [config.init_metric_ftn(met_dict, module_metric) for met_dict in config['metrics']['epoch']]
running_metrics = [config.init_metric_ftn(met_dict, module_metric) for met_dict in config['metrics']['running']]
# build optimizer, learning rate scheduler. delete every line containing lr_scheduler for disabling scheduler
trainable_params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = config.init_obj('optimizer', [torch.optim, module_optimizer], trainable_params)
try:
lr_scheduler = config.init_obj('lr_scheduler', [torch.optim.lr_scheduler, module_scheduler], optimizer)
except KeyError:
lr_scheduler = None
train_kwargs = {'model':model,
'criterion':criterion,
'metric_ftns':[epoch_metrics, running_metrics],
'optimizer':optimizer,
'config':config,
'data_loader':data_loader,
'valid_data_loader':valid_data_loader,
'lr_scheduler':lr_scheduler
}
trainer = config.init_obj('trainer', [module_trainer], **train_kwargs)
trainer.train()
if __name__ == '__main__':
args = argparse.ArgumentParser(description='PyTorch Template')
args.add_argument('-c', '--config', default=None, type=str,
help='config file path (default: None)')
args.add_argument('-r', '--resume', default=None, type=str,
help='path to latest checkpoint (default: None)')
args.add_argument('-d', '--device', default=None, type=str,
help='indices of GPUs to enable (default: all)')
# custom cli options to modify configuration from default values given in json file.
CustomArgs = collections.namedtuple('CustomArgs', 'flags type target')
options = [
CustomArgs(['--lr', '--learning_rate'], type=float, target='optimizer;args;lr'),
CustomArgs(['--bs', '--batch_size'], type=int, target='data_loader;args;batch_size'),
CustomArgs(['--k'], type=int, target='data_loader;args;k')
]
config = ConfigParser.from_args(args, options)
main(config)