forked from soumickmj/DS6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_crossvalidation.py
190 lines (165 loc) · 8.32 KB
/
main_crossvalidation.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
"""
"""
import argparse
import apex
import torch.utils.data
from apex import amp
from torch.utils.tensorboard import SummaryWriter
from crossvalidation import FoldManager
from pipeline import Pipeline
from Utils.logger import Logger
from Utils.model_manager import getModel
from Utils.vessel_utils import load_model, load_model_with_amp
__author__ = "Kartik Prabhu, Mahantesh Pattadkal, and Soumick Chatterjee"
__copyright__ = "Copyright 2020, Faculty of Computer Science, Otto von Guericke University Magdeburg, Germany"
__credits__ = ["Kartik Prabhu", "Mahantesh Pattadkal", "Soumick Chatterjee"]
__license__ = "GPL"
__version__ = "1.0.0"
__maintainer__ = "Soumick Chatterjee"
__email__ = "soumick.chatterjee@ovgu.de"
__status__ = "Production"
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-model",
type=int,
default=1,
help="1{U-Net}; \n"
"2{U-Net_Deepsup}; \n"
"3{Attention-U-Net};")
parser.add_argument("-model_name",
default="Model_v1",
help="Name of the model")
parser.add_argument("-dataset_path",
help="Path to folder containing dataset."
"Further divide folders into train,validate,test, train_label,validate_label and test_label."
"Example: /home/dataset/")
parser.add_argument("-output_path",
help="Folder path to store output "
"Example: /home/output/")
parser.add_argument('-train',
default=True,
help="To train the model")
parser.add_argument('-test',
default=True,
help="To test the model")
parser.add_argument('-predict',
default=False,
help="To predict a segmentation output of the model and to get a diff between label and output")
parser.add_argument('-predictor_path',
default="",
help="Path to the input image to predict an output, ex:/home/test/ww25.nii ")
parser.add_argument('-predictor_label_path',
default="",
help="Path to the label image to find the diff between label an output, ex:/home/test/ww25_label.nii ")
parser.add_argument('-load_path',
default="",
help="Path to checkpoint of existing model to load, ex:/home/model/checkpoint/ ")
parser.add_argument('-load_best',
default=False,
help="Specifiy whether to load the best checkpoiont or the last")
parser.add_argument('-deform',
default=False,
help="To use deformation for training")
parser.add_argument('-apex',
default=True,
help="To use half precision on model weights.")
parser.add_argument("-batch_size",
type=int,
default=20,
help="Batch size for training")
parser.add_argument("-num_epochs",
type=int,
default=50,
help="Number of epochs for training")
parser.add_argument("-learning_rate",
type=float,
default=0.01,
help="Learning rate")
parser.add_argument("-patch_size",
type=int,
default=64,
help="Patch size of the input volume")
parser.add_argument("-stride_depth",
type=int,
default=16,
help="Strides for dividing the input volume into patches in depth dimension")
parser.add_argument("-stride_width",
type=int,
default=32,
help="Strides for dividing the input volume into patches in width dimension")
parser.add_argument("-stride_length",
type=int,
default=32,
help="Strides for dividing the input volume into patches in length dimension")
parser.add_argument("-samples_per_epoch",
type=int,
default=8000,
help="Number of samples per epoch")
parser.add_argument("-num_worker",
type=int,
default=8,
help="Number of worker threads")
parser.add_argument("-set_number",
default=6,
help="Set number to select the folds")
args = parser.parse_args()
MODEL_NAME = args.model_name
DATASET_FOLDER = args.dataset_path
OUTPUT_PATH = args.output_path
LOAD_PATH = args.load_path
SET_NUMBER = args.set_number
if args.deform:
old_model_names = ["model1", "model2", "model3"] #TODO: add previously pretrained model names
else:
old_model_names = ["set" + str(SET_NUMBER) + "_fold1", "set" + str(SET_NUMBER) + "_fold2",
"set" + str(SET_NUMBER) + "_fold3"]
for training_set, validation_set, test_set, old_model_name in zip(FoldManager.getTrainingFolds(SET_NUMBER),
FoldManager.getValidationFolds(SET_NUMBER),
FoldManager.getTestingFolds(SET_NUMBER),
old_model_names):
if args.deform:
NEW_MODEL_NAME = old_model_name + '_deformation' # TODO: change this depending on best deformation model acheived
else:
NEW_MODEL_NAME = MODEL_NAME + old_model_name
CHECKPOINT_PATH = OUTPUT_PATH + "/" + MODEL_NAME + '/checkpoint/'
TENSORBOARD_PATH_TRAINING = OUTPUT_PATH + "/" + MODEL_NAME + '/tensorboard/tensorboard_training/'
TENSORBOARD_PATH_VALIDATION = OUTPUT_PATH + "/" + MODEL_NAME + '/tensorboard/tensorboard_validation/'
TENSORBOARD_PATH_TESTING = OUTPUT_PATH + "/" + MODEL_NAME + '/tensorboard/tensorboard_testing/'
LOGGER_PATH = OUTPUT_PATH + "/" + MODEL_NAME + '.log'
logger = Logger(MODEL_NAME, LOGGER_PATH).get_logger()
test_logger = Logger(MODEL_NAME + '_test', LOGGER_PATH).get_logger()
# Model
model = getModel(args.model)
model.cuda()
# No loading
if not bool(LOAD_PATH):
optimizer = torch.optim.Adam(model.parameters(), lr=args.learning_rate)
if args.apex:
model, optimizer = amp.initialize(model, optimizer, opt_level="O1")
else:
if args.apex:
model, optimizer, amp = load_model_with_amp(model, LOAD_PATH)
else:
model, optimizer = load_model(model, LOAD_PATH)
writer_training = SummaryWriter(TENSORBOARD_PATH_TRAINING)
writer_validating = SummaryWriter(TENSORBOARD_PATH_VALIDATION)
pipeline = Pipeline(model=model, optimizer=optimizer, logger=logger, with_apex=args.apex, num_epochs=args.num_epochs,
dir_path=DATASET_FOLDER, checkpoint_path=CHECKPOINT_PATH, deform=args.deform,
writer_training=writer_training, writer_validating=writer_validating,
stride_depth=args.stride_depth, stride_length=args.stride_length, stride_width=args.stride_width,
training_set=training_set, validation_set=validation_set, test_set=test_set,
predict_only=(not args.train) and (not args.test))
try:
if args.train:
pipeline.train()
torch.cuda.empty_cache() # to avoid memory errors
if args.test:
pipeline.test(test_logger=test_logger)
torch.cuda.empty_cache() # to avoid memory errors
if args.predict:
pipeline.predict(MODEL_NAME, args.predictor_path, args.predictor_label_path, OUTPUT_PATH)
except Exception as error:
logger.exception(error)
writer_training.close()
writer_validating.close()