-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
150 lines (132 loc) · 5.34 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from train.train import RunTrain
from train.eval import RunTest
from networks.prepare_networks import get_nets
from data.dataloaders import get_dataloaders
import os
import argparse
import yaml
import torch
def run_experiment(config):
""" Runs training from config file
Args:
config: dictionary (main config file) with experiment parameter info (dict)
"""
# Make checkpoint and results dir
if not os.path.isdir(config['ckpt_dir']):
os.makedirs(config['ckpt_dir'])
if not os.path.isdir(config['res_dir']):
os.makedirs(config['res_dir'])
torch.multiprocessing.set_sharing_strategy('file_system')
# Define networks, optimizers and load any existing checkpoints, prepare lists to store losses
(segmenter, optimizer_seg, lr_scheduler_seg,
classifier, optimizer_class, lr_scheduler_class,
iteration, epoch, max_epoch,
losses_train_init_seg, losses_valid_init_seg, best_metric_seg, binary_seg_weight,
losses_train_init_class, losses_valid_init_class, best_metric_class) = get_nets(config)
# Get dataloaders
train_loader, val_loader, test_ds, test_files, infer_ds, infer_files = get_dataloaders(config)
# Train experiment
if config['training'] is True:
# Set up Trainer class
runtrain = RunTrain(
train_loader=train_loader,
val_loader=val_loader,
max_iterations=int(config['max_iterations']),
ckpt_dir=config['ckpt_dir'],
res_dir=config['res_dir'],
experiment_type=config['experiment_type'],
optimizer_seg=optimizer_seg,
optimizer_class=optimizer_class,
lr_scheduler_seg=lr_scheduler_seg,
lr_scheduler_class=lr_scheduler_class,
input_type_class=config['input_type_class'],
eval_num=int(config['eval_num']),
gpu_device=config['gpu_ids'],
N_seg_labels=int(config['N_seg_labels'])
)
runtrain.train_experiment(
iteration,
max_epoch,
epoch,
segmenter=segmenter,
losses_train_seg=losses_train_init_seg,
losses_valid_seg=losses_valid_init_seg,
best_metrics_valid_seg=best_metric_seg,
binary_seg_weight=binary_seg_weight,
multi_seg_weight=float(config['multi_seg_weight']),
classifier=classifier,
losses_train_class=losses_train_init_class,
losses_valid_class=losses_valid_init_class,
best_metrics_valid_class=best_metric_class,
multi_task_weight=float(config['multi_task_weight'])
)
# Run testing
runtest = RunTest(
train_loader=train_loader,
val_loader=val_loader,
max_iterations=int(config['max_iterations']),
ckpt_dir=config['ckpt_dir'],
res_dir=config['res_dir'],
experiment_type=config['experiment_type'],
optimizer_seg=optimizer_seg,
optimizer_class=optimizer_class,
lr_scheduler_seg=lr_scheduler_seg,
lr_scheduler_class=lr_scheduler_class,
input_type_class=config['input_type_class'],
eval_num=int(config['eval_num']),
gpu_device=config['gpu_ids'],
N_seg_labels=int(config['N_seg_labels'])
)
runtest.test_experiment(test_files=test_files, test_ds=test_ds, segmenter=segmenter, classifier=classifier)
# Run inference
if config['infer']:
runtest.infer(model=segmenter, test_files=infer_files, test_ds=infer_ds, classifier=classifier)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--config", help="config file (.yaml) containing experiment parameters and directories")
args = parser.parse_args()
if args.config:
print('Reading config file')
with open(args.config) as cf_file:
CONFIG = yaml.safe_load(cf_file.read())
else:
print('Using default config parameters')
CONFIG = {
# Directories & filenames
'ckpt_dir': './Checkpoints/',
'res_dir': './Results/',
'data_dir': '/Data/',
'data_JSON_file': 'data.json',
'ckpt_name_seg': 'latest_segmenter',
'ckpt_name_class': 'latest_classifier',
# Experiment types
'experiment_type': 'joint',
'input_type_class': 'multi',
'training': True,
'infer': True,
# Experiment parameters
'eval_num': 3,
'max_iterations': 10000,
'batch_size': 12,
'gpu_ids': 0,
# Classifier parameters
'dropout_class': 0.2,
'lr_class': 1e-4,
'weight_decay_class': 1e-5,
# Segmenter parameters
'dropout_seg': 0.2,
'lr_seg': 1e-3,
'weight_decay_seg': 1e-5,
'chann_segnet': (32, 64, 128, 256, 512),
'strides_segnet': (2, 2, 2, 2),
'ksize_segnet': 3,
'up_ksize_segnet': 3,
'binary_seg_weight': 1,
'multi_seg_weight': 1,
'multi_task_weight': 14,
# Data parameters
'spatial_dims': 3,
'N_diagnosis': 3,
'N_seg_labels': 12
}
run_experiment(CONFIG)