[c7c3a1]: / src / LiviaNet / startTesting.py

Download this file

288 lines (226 with data), 12.0 kB

  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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"""
Copyright (c) 2016, Jose Dolz .All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Jose Dolz. Dec, 2016.
email: jose.dolz.upv@gmail.com
LIVIA Department, ETS, Montreal.
"""
import numpy as np
import time
import os
import pdb
from Modules.General.Evaluation import computeDice
from Modules.General.Utils import getImagesSet
from Modules.General.Utils import load_model_from_gzip_file
from Modules.IO.ImgOperations.imgOp import applyUnpadding
from Modules.IO.loadData import load_imagesSinglePatient
from Modules.IO.saveData import saveImageAsNifti
from Modules.IO.saveData import saveImageAsMatlab
from Modules.IO.sampling import *
from Modules.Parsers.parsersUtils import parserConfigIni
def segmentVolume(myNetworkModel,
i_d,
imageNames_Test,
names_Test,
groundTruthNames_Test,
roiNames_Test,
imageType,
padInputImagesBool,
receptiveField,
sampleSize_Test,
strideVal,
batch_Size,
task # Validation (0) or testing (1)
):
# Get info from the network model
networkName = myNetworkModel.networkName
folderName = myNetworkModel.folderName
n_classes = myNetworkModel.n_classes
sampleSize_Test = myNetworkModel.sampleSize_Test
receptiveField = myNetworkModel.receptiveField
outputShape = myNetworkModel.lastLayer.outputShapeTest[2:]
batch_Size = myNetworkModel.batch_Size
padInputImagesBool = True
# Get half sample size
sampleHalf = []
for h_i in range(3):
sampleHalf.append((receptiveField[h_i]-1)/2)
# Load the images to segment
[imgSubject,
gtLabelsImage,
roi,
paddingValues] = load_imagesSinglePatient(i_d,
imageNames_Test,
groundTruthNames_Test,
roiNames_Test,
padInputImagesBool,
receptiveField,
sampleSize_Test,
imageType,
)
# Get image dimensions
imgDims = list(imgSubject.shape)
[ sampleCoords ] = sampleWholeImage(imgSubject,
roi,
sampleSize_Test,
strideVal,
batch_Size
)
numberOfSamples = len(sampleCoords)
sampleID = 0
numberOfBatches = numberOfSamples/batch_Size
#The probability-map that will be constructed by the predictions.
probMaps = np.zeros([n_classes]+imgDims, dtype = "float32")
# Run over all the batches
for b_i in xrange(numberOfBatches) :
# Get samples for batch b_i
sampleCoords_b = sampleCoords[ b_i*batch_Size : (b_i+1)*batch_Size ]
[imgSamples] = extractSamples(imgSubject,
sampleCoords_b,
sampleSize_Test,
receptiveField)
# Load the data of the batch on the GPU
myNetworkModel.testingData_x.set_value(imgSamples, borrow=True)
# Call the testing Theano function
predictions = myNetworkModel.networkModel_Test(0)
predOutput = predictions[-1]
# --- Now we can generate the probability maps from the predictions ----
# Run over all the regions
for r_i in xrange(batch_Size) :
sampleCoords_i = sampleCoords[sampleID]
coords = [ sampleCoords_i[0][0], sampleCoords_i[1][0], sampleCoords_i[2][0] ]
# Get the min and max coords
xMin = coords[0] + sampleHalf[0]
xMax = coords[0] + sampleHalf[0] + strideVal[0]
yMin = coords[1] + sampleHalf[1]
yMax = coords[1] + sampleHalf[1] + strideVal[1]
zMin = coords[2] + sampleHalf[2]
zMax = coords[2] + sampleHalf[2] + strideVal[2]
probMaps[:,xMin:xMax, yMin:yMax, zMin:zMax] = predOutput[r_i]
sampleID += 1
# Release data
myNetworkModel.testingData_x.set_value(np.zeros([1,1,1,1,1], dtype="float32"))
# Segmentation has been done in this point.
# Now: Save the data
# Get the segmentation from the probability maps ---
segmentationImage = np.argmax(probMaps, axis=0)
#Save Result:
npDtypeForPredictedImage = np.dtype(np.int16)
suffixToAdd = "_Segm"
# Apply unpadding if specified
if padInputImagesBool == True:
segmentationRes = applyUnpadding(segmentationImage, paddingValues)
else:
segmentationRes = segmentationImage
# Generate folders to store the model
BASE_DIR = os.getcwd()
path_Temp = os.path.join(BASE_DIR,'outputFiles')
# For the predictions
predlFolderName = os.path.join(path_Temp,myNetworkModel.folderName)
predlFolderName = os.path.join(predlFolderName,'Pred')
if task == 0:
predTestFolderName = os.path.join(predlFolderName,'Validation')
else:
predTestFolderName = os.path.join(predlFolderName,'Testing')
nameToSave = predTestFolderName + '/Segmentation_'+ names_Test[i_d]
# Save Segmentation image
print(" ... Saving segmentation result..."),
if imageType == 0: # nifti
imageTypeToSave = np.dtype(np.int16)
saveImageAsNifti(segmentationRes,
nameToSave,
imageNames_Test[i_d],
imageTypeToSave)
else: # Matlab
# Cast to int8 for saving purposes
saveImageAsMatlab(segmentationRes.astype('int8'),
nameToSave)
# Save the prob maps for each class (except background)
for c_i in xrange(1, n_classes) :
nameToSave = predTestFolderName + '/ProbMap_class_'+ str(c_i) + '_' + names_Test[i_d]
probMapClass = probMaps[c_i,:,:,:]
# Apply unpadding if specified
if padInputImagesBool == True:
probMapClassRes = applyUnpadding(probMapClass, paddingValues)
else:
probMapClassRes = probMapClass
print(" ... Saving prob map for class {}...".format(str(c_i))),
if imageType == 0: # nifti
imageTypeToSave = np.dtype(np.float32)
saveImageAsNifti(probMapClassRes,
nameToSave,
imageNames_Test[i_d],
imageTypeToSave)
else:
# Cast to float32 for saving purposes
saveImageAsMatlab(probMapClassRes.astype('float32'),
nameToSave)
# If segmentation done during evaluation, get dice
if task == 0:
print(" ... Computing Dice scores: ")
DiceArray = computeDice(segmentationImage,gtLabelsImage)
for d_i in xrange(len(DiceArray)):
print(" -------------- DSC (Class {}) : {}".format(str(d_i+1),DiceArray[d_i]))
""" Main segmentation function """
def startTesting(networkModelName,
configIniName
) :
padInputImagesBool = True # from config ini
print " ****************************************** STARTING SEGMENTATION ******************************************"
print " ********************** Starting segmentation **********************"
myParserConfigIni = parserConfigIni()
myParserConfigIni.readConfigIniFile(configIniName,2)
print " -------- Images to segment -------------"
print " -------- Reading Images names for segmentation -------------"
# -- Get list of images used for testing -- #
(imageNames_Test, names_Test) = getImagesSet(myParserConfigIni.imagesFolder,myParserConfigIni.indexesToSegment) # Images
(groundTruthNames_Test, gt_names_Test) = getImagesSet(myParserConfigIni.GroundTruthFolder,myParserConfigIni.indexesToSegment) # Ground truth
(roiNames_Test, roi_names_Test) = getImagesSet(myParserConfigIni.ROIFolder,myParserConfigIni.indexesToSegment) # ROI
# --------------- Load my LiviaNet3D object ---------------
print (" ... Loading model from {}".format(networkModelName))
myLiviaNet3D = load_model_from_gzip_file(networkModelName)
print " ... Network architecture successfully loaded...."
# Get info from the network model
networkName = myLiviaNet3D.networkName
folderName = myLiviaNet3D.folderName
n_classes = myLiviaNet3D.n_classes
sampleSize_Test = myLiviaNet3D.sampleSize_Test
receptiveField = myLiviaNet3D.receptiveField
outputShape = myLiviaNet3D.lastLayer.outputShapeTest[2:]
batch_Size = myLiviaNet3D.batch_Size
padInputImagesBool = myParserConfigIni.applyPadding
imageType = myParserConfigIni.imageTypes
numberImagesToSegment = len(imageNames_Test)
strideValues = myLiviaNet3D.lastLayer.outputShapeTest[2:]
# Run over the images to segment
for i_d in xrange(numberImagesToSegment) :
print("********************** Segmenting subject: {} ....total: {}/{}...**********************".format(names_Test[i_d],str(i_d+1),str(numberImagesToSegment)))
segmentVolume(myLiviaNet3D,
i_d,
imageNames_Test, # Full path
names_Test, # Only image name
groundTruthNames_Test,
roiNames_Test,
imageType,
padInputImagesBool,
receptiveField,
sampleSize_Test,
strideValues,
batch_Size,
1 # Validation (0) or testing (1)
)
print(" **************************************************************************************************** ")