forked from TimoStoff/event_cnn_minimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
107 lines (88 loc) · 3.95 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
100
101
102
103
104
105
106
107
import argparse
import collections
import torch
import numpy as np
import data_loader.data_loaders as module_data
import model.loss as module_loss
import model.model as module_arch
from parse_config import ConfigParser
from trainer import Trainer
# fix random seeds for reproducibility
SEED = 123
torch.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(SEED)
def load_model(args, checkpoint=None, config=None):
"""
negative voxel indicates a model trained on negative voxels -.-
"""
resume = checkpoint is not None
if resume:
config = checkpoint['config']
state_dict = checkpoint['state_dict']
try:
model_info['num_bins'] = config['arch']['args']['unet_kwargs']['num_bins']
except KeyError:
model_info['num_bins'] = config['arch']['args']['num_bins']
logger = config.get_logger('test')
if args.legacy:
config['arch']['type'] += '_legacy'
# build model architecture
model = config.init_obj('arch', module_arch)
logger.info(model)
if config['n_gpu'] > 1:
model = torch.nn.DataParallel(model)
if resume:
model.load_state_dict(state_dict)
# prepare model for testing
model = model.to(device)
model.eval()
if args.color:
model = ColorNet(model)
print('Loaded ColorNet')
return model
def main(config):
logger = config.get_logger('train')
# setup data_loader instances
data_loader = config.init_obj('data_loader', module_data)
valid_data_loader = config.init_obj('valid_data_loader', module_data)
# build model architecture, then print to console
model = config.init_obj('arch', module_arch)
logger.info(model)
# init loss classes
loss_ftns = [getattr(module_loss, loss)(**kwargs) for loss, kwargs in config['loss_ftns'].items()]
# build optimizer, learning rate scheduler. delete every lines containing lr_scheduler for disabling scheduler
trainable_params = filter(lambda p: p.requires_grad, model.parameters())
optimizer = config.init_obj('optimizer', torch.optim, trainable_params)
lr_scheduler = config.init_obj('lr_scheduler', torch.optim.lr_scheduler, optimizer)
trainer = Trainer(model, loss_ftns, optimizer,
config=config,
data_loader=data_loader,
valid_data_loader=valid_data_loader,
lr_scheduler=lr_scheduler)
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)')
args.add_argument('--limited_memory', default=False, action='store_true',
help='prevent "too many open files" error by setting pytorch multiprocessing to "file_system".')
# 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(['--rmb', '--reset_monitor_best'], type=bool, target='trainer;reset_monitor_best'),
CustomArgs(['--vo', '--valid_only'], type=bool, target='trainer;valid_only')
]
config = ConfigParser.from_args(args, options)
if args.parse_args().limited_memory:
# https://github.com/pytorch/pytorch/issues/11201#issuecomment-421146936
import torch.multiprocessing
torch.multiprocessing.set_sharing_strategy('file_system')
main(config)