forked from soumickmj/DS6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_executor.py
184 lines (157 loc) · 7.67 KB
/
main_executor.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
#!/usr/bin/env python
"""
"""
import argparse
import random
import os
import numpy as np
import torch.utils.data
from torch.utils.tensorboard import SummaryWriter
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"
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.manual_seed(2020)
np.random.seed(2020)
random.seed(2020)
# torch.autograd.set_detect_anomaly(True)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-model",
type=int,
default=2,
help="1{U-Net}; \n"
"2{U-Net_Deepsup}; \n"
"3{Attention-U-Net}; \n"
"4{Probabilistic-U-Net};")
parser.add_argument("-model_name",
default="Model_v1",
help="Name of the model")
parser.add_argument("-dataset_path",
default="/vol3/schatter/DS6/Dataset/OriginalVols/300",
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",
default="/home/schatter/Soumick/Output/DS6/OriginalVols_FDPv0",
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="/vol3/schatter/DS6/Dataset/BiasFieldCorrected/300/test/vk04.nii",
help="Path to the input image to predict an output, ex:/home/test/ww25.nii ")
parser.add_argument('-predictor_label_path',
default="/vol3/schatter/DS6/Dataset/BiasFieldCorrected/300/test_label/vk04.nii.gz",
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="/home/schatter/Soumick/Output/DS6/OrigVol_MaskedFDIPv0_UNetV2/checkpoint",
default="/home/schatter/Soumick/Output/DS6/OriginalVols_FDPv0/UNetMSS_X2_Deform/checkpoint/",
help="Path to checkpoint of existing model to load, ex:/home/model/checkpoint")
parser.add_argument('-load_best',
default=True,
help="Specifiy whether to load the best checkpoiont or the last. Also to be used if Train and Test both are true.")
parser.add_argument('-deform',
default=True,
action="store_true",
help="To use deformation for training")
parser.add_argument('-clip_grads',
default=True,
action="store_true",
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=15,
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 (To be used during validation and inference)")
parser.add_argument("-stride_width",
type=int,
default=32,
help="Strides for dividing the input volume into patches in width dimension (To be used during validation and inference)")
parser.add_argument("-stride_length",
type=int,
default=32,
help="Strides for dividing the input volume into patches in length dimension (To be used during validation and inference)")
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")
args = parser.parse_args()
if args.deform:
args.model_name += "_Deform"
MODEL_NAME = args.model_name
DATASET_FOLDER = args.dataset_path
OUTPUT_PATH = args.output_path
LOAD_PATH = args.load_path
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()
writer_training = SummaryWriter(TENSORBOARD_PATH_TRAINING)
writer_validating = SummaryWriter(TENSORBOARD_PATH_VALIDATION)
pipeline = Pipeline(cmd_args=args, model=model, logger=logger,
dir_path=DATASET_FOLDER, checkpoint_path=CHECKPOINT_PATH,
writer_training=writer_training, writer_validating=writer_validating)
# loading existing checkpoint if supplied
if bool(LOAD_PATH):
pipeline.load(checkpoint_path=LOAD_PATH, load_best=args.load_best)
# try:
if args.train:
pipeline.train()
torch.cuda.empty_cache() # to avoid memory errors
if args.test:
if args.load_best:
pipeline.load(load_best=True)
pipeline.test(test_logger=test_logger)
torch.cuda.empty_cache() # to avoid memory errors
if args.predict:
pipeline.predict(args.predictor_path, args.predictor_label_path, predict_logger=test_logger)
# except Exception as error:
# logger.exception(error)
writer_training.close()
writer_validating.close()