forked from deepmedic/deepmedic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deepMedicRun
executable file
·142 lines (120 loc) · 11.4 KB
/
deepMedicRun
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
#!/usr/bin/env python
# Copyright (c) 2016, Konstantinos Kamnitsas
# All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the BSD license. See the accompanying LICENSE file
# or read the terms at https://opensource.org/licenses/BSD-3-Clause.
import sys
import os
import argparse
sys.setrecursionlimit(20000)
from deepmedic.frontEndModules.frontEndHelpers.parsingFilesHelpers import getAbsPathEvenIfRelativeIsGiven
def strIsInt(s):
try:
int(s)
return True
except ValueError:
return False
def setupArgParser() :
parser = argparse.ArgumentParser( prog='DeepMedic', formatter_class=argparse.RawTextHelpFormatter,
description="\nThis software allows creation and supervised training of 3D, multi-scale CNN models for segmentation of structures in biomedical NIFTI volumes.\n"+\
"The project is hosted at: https://github.com/Kamnitsask/deepmedic \n"+\
"See the documentation for details on its use.\n"+\
"This software accompanies the research presented in:\n"+\
"Kamnitsas et al, \"Efficient Multi-Scale 3D CNN with Fully Connected CRF for Accurate Brain Lesion Segmentation\", Biomedical Image Analysis, 2016.\n"+\
"We hope our work aids you in your endeavours.\n"+\
"For questions and feedback contact: [email protected]")
parser.add_argument("-newModel", dest='model_conf', type=str, help="Create a new CNN model with model parameters at given config file [MODEL_CONF].")
parser.add_argument("-train", dest='training_conf', type=str, help="Train a CNN model with training parameters at given config file [TRAINING_CONF].\n"+\
"This option can follow a [-newModel MODEL_CONF] option, to create a new model and start training it immediately.\n"+\
"Otherwise, existing model can be specified in the training-config file or by the additional option [-model].")
parser.add_argument("-test", dest='testing_conf', type=str, help="Test with an existing CNN model. The testing session's parameters should be given in config file [TESTING_CONF].\n"+\
"Existing model can be specified in the given training-config file or by the additional option [-model].\n"+\
"This option cannot be used in combination with [-newModel] or [-train].")
parser.add_argument("-model", dest='saved_model', type=str, help="The path to a saved existing cnn model, to train or test with.\n"+\
"This option can follow a [-train] or [-test] option. Not in combination with a [-newModel].\n"+\
"If given, this option will overide any \"model\" parameters given in the train or testing configuration file.")
parser.add_argument("-dev", default = "cpu", dest='device', type=str, help="Specify the device to run the process on. Values: [cpu] or [gpu] (default = cpu).\n"+\
"In the case of multiple GPUs, specify a particular GPU device with a number, in the format: gpu2.\n"+\
"NOTE: For GPU processing, CUDA libraries must be first added in your environment's PATH and LD_LIBRARY_PATH. See accompanying documentation.")
parser.add_argument("-pretrained", dest='pretrained_model', type=str, help="Use to transfer the weights from a previously trained model to a new model.\n"+\
"This option must follow a [-newModel] option.\n"+\
"Usage: ./deepMedicRun -newModel /path/to/model/config -pretrained /path/to/pretrained/model \n"+\
"NOTE: By default, parameters are transfered to all layers except the classification layer. Use option [-layers] to manually specify layers to pretrain.")
parser.add_argument("-layers", dest='layers_to_transfer', nargs='+', type=int, help="Use only after a [-pretrained] option.\n"+\
"Specify to which layers of the new model parameters should be transferred to.\n"+\
"First layer is 1. Classification layer of original DeepMedic is 11. Same layers from each parallel-pathway will be transfered.\n"+\
"Usage: ./deepMedicRun -newModel /path/to/model/config -pretrained /path/to/pretrained/model -layers 1 2 3 ...")
parser.add_argument("-resetOptimizer", dest='reset_optimizer', action='store_true', help="Use optionally with a [-train] command. Does not take an argument.\n"+\
"Usage: ./deepMedicRun -train /path/to/train/config -resetOptimizer ...etc...\n"+\
"Resets the model\'s optimization state before starting the training session (eg number of epochs already trained, current learning rate etc).\n"+\
"IMPORTANT: Trainable parameters are NOT reinitialized! \n"+\
"Useful to begin a secondary training session with new learning-rate schedule, in order to fine-tune a previously trained model (Doc., Sec. 3.2)")
return parser
def checkDevicePassedCorrectly(devArg) :
if devArg <> "cpu" and devArg <> "gpu" and not (devArg.startswith("gpu") and strIsInt(devArg[3:]) ) :
print("ERROR: Value for the [-dev] option was not specified correctly. Specify the device to run the process on. Values: [cpu] or [gpu] (Default = cpu).\nIn the case of multiple GPUs, specify a particular GPU device with a number, in the format: gpu2. Exiting."); exit(1)
def setEnvironment(deviceString) :
#This adds the necessary libraries to the path/ld_library_path, if given in the accompanying file: ./environment.txt
#Finally, sets the necessary theano flags.
pathToEnvironmentFile = os.getcwd() + "/environment.txt"
if os.path.isfile(pathToEnvironmentFile) :
envGivenInFile = {}
execfile(pathToEnvironmentFile, envGivenInFile)
if "path_to_theano" in envGivenInFile :
sys.path.insert(1, envGivenInFile["path_to_theano"])
if "path_to_nose" in envGivenInFile :
sys.path.insert(1, envGivenInFile["path_to_nose"])
if "path_to_nibabel" in envGivenInFile :
sys.path.insert(1, envGivenInFile["path_to_nibabel"])
if "path_to_parallelPython" in envGivenInFile :
sys.path.insert(1, envGivenInFile["path_to_parallelPython"])
os.environ['THEANO_FLAGS']='on_unused_input=warn, mode=FAST_RUN,device=' + deviceString + ',floatX=float32,force_device=True'
#################################################
# MAIN #
#################################################
if __name__ == '__main__':
cwd = os.getcwd()
parser = setupArgParser()
args = parser.parse_args()
if len(sys.argv) == 1:
print("For help on the usage of this program, please use the option -h."); exit(1)
if not (args.model_conf or args.training_conf or args.testing_conf) :
print("ERROR: One of the options must be specified:\n\t[-newModel] to create a new model.\n\t[-train] to start a training session on a model.\n\t[-test] to test with an existing model.\nPlease try [-h] for more information. Exiting."); exit(1)
#Preliminary checks:
if args.model_conf and args.saved_model :
print("ERROR:\t[-newModel] and [-model] options cannot be used together.\n\tUse [-newModel] to create a new cnn model.\n\t[-model] can follow a [-train] or [-test] option, to specify which saved model to use.\n\tPlease try -h for more information. Exiting."); exit(1)
if args.testing_conf and (args.model_conf or args.training_conf) :
print("ERROR: [-test] cannot be used in conjuction with [-newModel] or [-train]. To test with an existing network, please just specify a configuration file for the testing process, which will include a path to a trained model to use for the inference. Exiting."); exit(1)
if args.pretrained_model and not args.model_conf :
print("ERROR: The option [-pretrained] can only be used together with the [-newModel] option.\n\tPlease try -h for more information. Exiting."); exit(1)
if args.layers_to_transfer and not args.pretrained_model :
print("ERROR: The option [-layers] can only be used together with the [-pretrained] option.\n\tPlease try -h for more information. Exiting."); exit(1)
if args.reset_optimizer and not args.training_conf :
print("ERROR: The option [-resetOptimizer] can only be used together with the [-train] option.\n\tPlease try -h for more information. Exiting."); exit(1)
checkDevicePassedCorrectly(args.device)
#Set the environment for Theano.
setEnvironment(args.device)
cnnInstanceLoaded = None
filenameAndPathWhereNewlyCreatedModelWasSaved = None
#Find out what procedure we are being asked to perform:
if args.model_conf :#and not args.training_conf and not args.testing_conf:
#Just create and save a new CNN model.
from deepmedic.frontEndModules import deepMedicNewModel
absPathToModelConf = getAbsPathEvenIfRelativeIsGiven(args.model_conf, cwd)
absPathToPreTrainedModelGivenInCmdLine = getAbsPathEvenIfRelativeIsGiven(args.pretrained_model, cwd) if args.pretrained_model else None # pretraining.
listOfLayersToTransfer = args.layers_to_transfer if args.layers_to_transfer else None # layers to transfer from pretrained model
(cnnInstanceLoaded, filenameAndPathWhereNewlyCreatedModelWasSaved) = deepMedicNewModel.deepMedicNewModelMain(absPathToModelConf, absPathToPreTrainedModelGivenInCmdLine, listOfLayersToTransfer)
if args.training_conf : #and not args.model_conf and not args.testing_conf: #Second part was checked already.
#Run training
from deepmedic.frontEndModules import deepMedicTrain
absPathToTrainConf = getAbsPathEvenIfRelativeIsGiven(args.training_conf, cwd)
absPathToCnnModelGivenInCmdLine = getAbsPathEvenIfRelativeIsGiven(args.saved_model, cwd) if args.saved_model else None
deepMedicTrain.deepMedicTrainMain(absPathToTrainConf, absPathToCnnModelGivenInCmdLine, cnnInstanceLoaded, filenameAndPathWhereNewlyCreatedModelWasSaved, args.reset_optimizer)
if args.testing_conf : #and not args.model_conf and not args.training_conf : #Second part was checked already.
#Test with an existing model.
from deepmedic.frontEndModules import deepMedicTest
absPathToTestConf = getAbsPathEvenIfRelativeIsGiven(args.testing_conf, cwd)
absPathToCnnModelGivenInCmdLine = getAbsPathEvenIfRelativeIsGiven(args.saved_model, cwd) if args.saved_model else None
deepMedicTest.deepMedicTestMain(absPathToTestConf, absPathToCnnModelGivenInCmdLine)