/
cnn_scripts.py
242 lines (195 loc) · 8.53 KB
/
cnn_scripts.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# ------------------------------------------------------------------------------------------------------------
# MS lesion segmentation pipeline
# ---------------------------------
# - incorporates:
# - MRI identification
# - registration
# - skull stripping
# - MS lesion segmentation training and testing using the CNN aproach
# of Valverde et al (NI2017)
#
# Sergi Valverde 2017
# svalverde@eia.udg.edu
# ------------------------------------------------------------------------------------------------------------
import os
import sys
import platform
import time
import ConfigParser
from utils.preprocess import preprocess_scan
from utils.load_options import load_options, print_options
CURRENT_PATH = CURRENT_PATH = os.path.split(os.path.realpath(__file__))[0]
sys.path.append(os.path.join(CURRENT_PATH, 'libs'))
def get_config():
"""
Get the CNN configuration from file
"""
default_config = ConfigParser.SafeConfigParser()
default_config.read(os.path.join(CURRENT_PATH, 'config', 'default.cfg'))
user_config = ConfigParser.RawConfigParser()
user_config.read(os.path.join(CURRENT_PATH, 'config', 'configuration.cfg'))
# read user's configuration file
options = load_options(default_config, user_config)
options['tmp_folder'] = CURRENT_PATH + '/tmp'
# set paths taking into account the host OS
host_os = platform.system()
if host_os == 'Linux':
options['niftyreg_path'] = CURRENT_PATH + '/libs/linux/niftyreg'
options['robex_path'] = CURRENT_PATH + '/libs/linux/ROBEX/runROBEX.sh'
options['test_slices'] = 256
elif host_os == 'Windows':
options['niftyreg_path'] = os.path.normpath(
os.path.join(CURRENT_PATH,
'libs',
'win',
'niftyreg'))
options['robex_path'] = os.path.normpath(
os.path.join(CURRENT_PATH,
'libs',
'win',
'ROBEX',
'runROBEX.bat'))
options['test_slices'] = 256
else:
print "The OS system", host_os, "is not currently supported."
exit()
# print options when debugging
if options['debug']:
print_options(options)
return options
def define_backend(options):
"""
Define the library backend and write options
"""
#
# if options['backend'] == 'theano':
# device = 'cuda' + str(options['gpu_number']) if options['gpu_mode'] else 'cpu'
# os.environ['KERAS_BACKEND'] = options['backend']
# os.environ['THEANO_FLAGS'] = 'mode=FAST_RUN,device=' + device + ',floatX=float32,optimizer=fast_compile'
# else:
# device = str(options['gpu_number']) if options['gpu_mode'] is not None else " "
# print "DEBUG: ", device
# os.environ['KERAS_BACKEND'] = 'tensorflow'
# os.environ["CUDA_VISIBLE_DEVICES"] = device
# forcing tensorflow
device = str(options['gpu_number'])
print "DEBUG: ", device
os.environ['KERAS_BACKEND'] = 'tensorflow'
os.environ["CUDA_VISIBLE_DEVICES"] = device
def train_network(options):
"""
Train the CNN network given the options passed as parameter
"""
# set GPU mode from the configuration file. Trying to update
# the backend automatically from here in order to use either theano
# or tensorflow backends
from CNN.base import train_cascaded_model
from CNN.build_model import cascade_model
# define the training backend
define_backend(options)
scan_list = os.listdir(options['train_folder'])
scan_list.sort()
options['task'] = 'training'
options['train_folder'] = os.path.normpath(options['train_folder'])
for scan in scan_list:
total_time = time.time()
# --------------------------------------------------
# move things to a tmp folder before starting
# --------------------------------------------------
options['tmp_scan'] = scan
current_folder = os.path.join(options['train_folder'], scan)
options['tmp_folder'] = os.path.normpath(os.path.join(current_folder,
'tmp'))
# preprocess scan
preprocess_scan(current_folder, options)
# --------------------------------------------------
# WM MS lesion training
# - configure net and train
# --------------------------------------------------
seg_time = time.time()
print "> CNN: Starting training session"
# select training scans
train_x_data = {f: {m: os.path.join(options['train_folder'], f, 'tmp', n)
for m, n in zip(options['modalities'],
options['x_names'])}
for f in scan_list}
train_y_data = {f: os.path.join(options['train_folder'],
f,
'tmp',
'lesion.nii.gz')
for f in scan_list}
options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets')
options['load_weights'] = False
# train the model for the current scan
print "> CNN: training net with %d subjects" % (len(train_x_data.keys()))
# --------------------------------------------------
# initialize the CNN and train the classifier
# --------------------------------------------------
model = cascade_model(options)
model = train_cascaded_model(model, train_x_data, train_y_data, options)
print "> INFO: training time:", round(time.time() - seg_time), "sec"
print "> INFO: total pipeline time: ", round(time.time() - total_time), "sec"
print "> INFO: All processes have been finished. Have a good day!"
def infer_segmentation(options):
"""
Infer segmentation given the input options passed as parameters
"""
# define the training backend
define_backend(options)
from CNN.base import test_cascaded_model
from CNN.build_model import cascade_model
# --------------------------------------------------
# net configuration
# take into account if the pretrained models have to be used
# all images share the same network model
# --------------------------------------------------
options['full_train'] = True
options['load_weights'] = True
options['weight_paths'] = os.path.join(CURRENT_PATH, 'nets')
options['net_verbose'] = 0
model = cascade_model(options)
# --------------------------------------------------
# process each of the scans
# - image identification
# - image registration
# - skull-stripping
# - WM segmentation
# --------------------------------------------------
options['task'] = 'testing'
scan_list = os.listdir(options['test_folder'])
scan_list.sort()
for scan in scan_list:
total_time = time.time()
options['tmp_scan'] = scan
# --------------------------------------------------
# move things to a tmp folder before starting
# --------------------------------------------------
current_folder = os.path.join(options['test_folder'], scan)
options['tmp_folder'] = os.path.normpath(
os.path.join(current_folder, 'tmp'))
# --------------------------------------------------
# preprocess scans
# --------------------------------------------------
preprocess_scan(current_folder, options)
# --------------------------------------------------
# WM MS lesion inference
# --------------------------------------------------
seg_time = time.time()
"> CNN:", scan, "running WM lesion segmentation"
sys.stdout.flush()
options['test_scan'] = scan
test_x_data = {scan: {m: os.path.join(options['tmp_folder'], n)
for m, n in zip(options['modalities'],
options['x_names'])}}
test_cascaded_model(model, test_x_data, options)
print "> INFO:", scan, "CNN Segmentation time: ", round(time.time() - seg_time), "sec"
print "> INFO:", scan, "total pipeline time: ", round(time.time() - total_time), "sec"
# remove tmps if not set
if options['save_tmp'] is False:
try:
os.rmdir(options['tmp_folder'])
os.rmdir(os.path.join(options['current_folder'],
options['experiment']))
except:
pass
print "> INFO: All processes have been finished. Have a good day!"