-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMedGA.py
427 lines (353 loc) · 15.1 KB
/
MedGA.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
"""
* L. Rundo, A. Tangherloni et al.: MedGA: a novel evolutionary method for image enhancement in medical imaging systems,
Expert Systems with Applications, 119, 387-399, 2019. doi: 10.1016/j.eswa.2018.11.013
* L. Rundo, A. Tangherloni et al.: A novel framework for MR image segmentation and quantification by using MedGA,
Computer Methods and Programs in Biomedicine, 2019. doi: 10.1016/j.cmpb.2019.04.016
* Copyright (C) 2019 - Andrea Tangherloni & Leonardo Rundo
* Distributed under the terms of the GNU General Public License (GPL)
* This file is part of MedGA.
* MedGA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License v3.0 as published by
* the Free Software Foundation.
* MedGA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the
* GNU General Public License for more details.
"""
import getopt, sys, glob, os, time, subprocess
import numpy as np
sys.path.append(os.path.join(os.path.dirname(__file__), "src"))
from MedGA_sequential import MedGA
# MPI version of MedGA. It requires both MPI and mpi4py.
def runMPI(folderIn, folderOut, population, generations, selection, cross_rate, mut_rate, pressure, elitism, cores, verbose):
try:
# Using mpiexec
run = "mpiexec -np %d python src/MedGA_mpi.py %s %s %d %d %s %f %f %d %d %s"%(cores, folderIn, folderOut,
population, generations, selection,
cross_rate, mut_rate, pressure,
elitism, str(verbose))
# Calling the MPI version of MedGA, which distributes the computation onto multiple cores
# by means of a Master-Slave paradigm
p = subprocess.call(run, shell=True)
except:
# Using mpirun
run = "mpirun -np %d python src/MedGA_mpi.py %s %s %d %d %s %f %f %d %d %s"%(cores, folderIn, folderOut,
population, generations, selection,
cross_rate, mut_rate, pressure,
elitism, str(verbose))
# Calling the MPI version of MedGA, which is based on the sequential version
p = subprocess.call(run, shell=True)
# Sequential version of MedGA
def run(imagePath, folderIn, folderOut, population, generations, selection, cross_rate, mut_rate, pressure, elitism, verbose):
startAll = time.time()
toProcess = []
# Looking for the provided input image
if folderIn is None:
ext = imagePath.split(".")[-1].lower()
listExts = ["tiff", "tif", "png", "jpeg", "jpg"]
if ext not in listExts:
print "******************************************************************************************"
print "Unsupported format. Please provide", listExts, "images"
print "Warning", imagePath, "has not been processed"
print "******************************************************************************************"
exit(-6)
if not os.path.exists(imagePath):
print "******************************************************************************************"
print imagePath, "does not exists"
print "Warning", imagePath, "has not been processed"
print "******************************************************************************************"
exit(-7)
else:
toProcess.append(imagePath)
else:
alreadyPrint = False
# Looking for the images in the provided input folder
listImages = glob.glob(folderIn+os.sep+"*")
for imagePath in listImages:
ext = imagePath.split(".")[-1]
listExts = ["tiff", "tif", "png", "png", "jpeg", "jpg"]
# Only tiff, png and jpg images can be analyzed
if ext not in listExts:
if not alreadyPrint:
print "******************************************************************************************"
print "Unsupported format. Please provide", listExts, "images"
print "Warning", imagePath, "will be not processed\n"
alreadyPrint = True
pass
elif not os.path.exists(imagePath):
if not alreadyPrint:
print "******************************************************************************************"
print imagePath, "does not exists"
print "Warning", imagePath, "will be not processed\n"
alreadyPrint = True
pass
else:
toProcess.append(imagePath)
if not os.path.exists(folderOut):
os.makedirs(folderOut)
if len(toProcess) == 0:
print "******************************************************************************************"
exit(-11)
if verbose:
print "******************************************************************************************"
print "* Running the sequential version of MedGA\n"
print " * GA settings"
print " -> Number of chromosome: %d"%population
print " -> Number of elite chromosomes: %d"%elitism
print " -> Number of generations: %d"%generations
print " -> Crossover rate: %.2f"%cross_rate
print " -> Mutation rate: %.2f"%mut_rate
if selection == 'wheel':
print " -> Selection: wheel roulette\n\n"
elif selection == 'ranking':
print " -> Selection: ranking \n\n"
else:
print " -> Selection: tournament with %d individuals\n\n"%pressure
times = np.zeros(len(toProcess))
# Processing the images in the input folder
# The input images are characterized by an undelying bimodal histogram (intensity level distribution)
# Possibly, previously masked and cropped images (according to a bounding region containing the Region of Interest)
for i in xrange(len(toProcess)):
# Output folders
string = toProcess[i].split("/")[1:]
subfolder = string[-1].split(".")[:-1]
pathOutput = folderOut+os.sep+subfolder[0]
if not os.path.exists(pathOutput):
os.makedirs(pathOutput)
if verbose:
print " * Analyzed image %s"%toProcess[i],
start = time.time()
# MedGA execution on the image to be processed by using the provided GA settings
medga = MedGA(toProcess[i], pathOutput)
medga.startGA(population, generations, selection, cross_rate, mut_rate, elitism, pressure)
end = time.time()
elapsed = end-start
times[i] = elapsed
if verbose:
print "-> Elapsed time %5.2fs" % (elapsed)
print
endAll = time.time()
elapsedAll = endAll-startAll
if verbose:
if len(toProcess) > 1:
print "\n * Total elapsed time %5.2fs" % elapsedAll, "for computing", len(toProcess), "images"
print " * Mean elapsed time %5.2fs per image" % np.mean(times)
print "******************************************************************************************"
if __name__ == '__main__':
helpString = """MedGA.py -h <help>
-i <image>
-f <folder>
-o <output> (default: output)
-p <population> (default: 100)
-g <generations> (default: 100)
-s <selection> (default: tournament)
-c <cross_rate> (default: 0.9)
-m <mut_rate> (default: 0.01)
-k <pressure> (default: 20)
-e <elitism> (default: 1)
-d <distributed> (default: False)
-t <cores> (default: 4)
-v <verbose> (default: False)"""
try:
opts, args = getopt.getopt(sys.argv[1:], "hi:f:o:p:g:s:c:m:k:e:t:dv", ["help", "image", "folder", "output",
"population", "generations", "selection",
"cross_rate", "mut_rate", "pressure",
"elitism", "cores", "distributed", "verbose"])
except:
print helpString
exit(-1)
if len(opts)==0:
print helpString
exit(-2)
# default settings of MedGA
imagePath = None
folderIn = None
folderOut = "output"
population = 100
generations = 100
selection = "tournament"
cross_rate = 0.9
mut_rate = 0.01
pressure = 20
elitism = 1
mpi = False
cores = 5
verbose = False
warning = False
alreadyPrint = False
for opt, arg in opts:
if opt in ("-h", "--help"):
print helpString
exit(-3)
elif opt in ("-i", "--image"):
imagePath = arg
elif opt in ("-f", "--folder"):
folderIn = arg
elif opt in ("-o", "--output"):
folderOut = arg
elif opt in ("-p", "--population"):
try:
population = int(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided population is not correct. It has been set to 100"
population = 100
warning = True
alreadyPrint = True
elif opt in ("-g", "--generations"):
try:
generations = int(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided generations is not correct. It has been set to 100"
generations = 100
warning = True
alreadyPrint = True
elif opt in ("-s", "--selection"):
try:
selection = arg
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided selection is not correct. It has been set to tournament"
selection = tournament
warning = True
alreadyPrint = True
elif opt in ("-c", "--cross_rate"):
try:
cross_rate = float(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided cross_rate is not correct. It has been set to 0.9"
cross_rate = 0.9
warning = True
alreadyPrint = True
elif opt in ("-m", "--mut_rate"):
try:
mut_rate = float(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided mut_rate is not correct. It has been set to 0.01"
mut_rate = 0.01
warning = True
alreadyPrint = True
elif opt in ("-k", "--pressure"):
try:
pressure = int(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided pressure is not correct. It has been set to 20"
pressure = 20
warning = True
alreadyPrint = True
elif opt in ("-e", "--elitism"):
try:
elitism = int(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided elitism is not correct. It has been set to 1"
elitism = 1
warning = True
alreadyPrint = True
elif opt in ("-t", "--cores"):
try:
cores = int(arg)
except:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided number of cores is not correct. It has been set to 5"
cores = 5
warning = True
alreadyPrint = True
elif opt in ("-d", "--distributed"):
mpi = True
elif opt in ("-v", "--verbose"):
verbose = True
if warning:
print
# ************************************************ Checking provided settings ************************************************
warning = False
if population <= 0:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided population is %d. It has been set to 100"%population
population = 100
warning = True
alreadyPrint = True
if generations <= 0:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided generations is %d. It has been set to 100"%generations
generations = 100
warning = True
alreadyPrint = True
if selection not in ["wheel", "ranking", "tournament"]:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided selection is %s. It has been set to tournament"%selection
selection = "tournament"
warning = True
alreadyPrint = True
if (cross_rate < 0) or (cross_rate > 1):
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided cross_rate is %f. It has been set to 0.9"%cross_rate
cross_rate = 0.9
warning = True
alreadyPrint = True
if (mut_rate < 0) or (mut_rate > 1):
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided mut_rate is %f. It has been set to 0.01"%mut_rate
mut_rate = 0.01
warning = True
alreadyPrint = True
if (pressure > population) or (pressure <= 0):
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided pressure is %d. It has been set to 20"%pressure
pressure = 20
warning = True
alreadyPrint = True
if (elitism > population) or (elitism < 0):
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided elitism is %d. It has been set to 1"%elitism
elitism = 1
warning = True
alreadyPrint = True
if cores <= 1:
if not alreadyPrint:
print "******************************************************************************************"
print " * Warning, the provided number of cores is %d. It has been set to 5"%cores
cores = 5
warning = True
alreadyPrint = True
if (imagePath is None) and (folderIn is None):
if not alreadyPrint:
print "******************************************************************************************"
print " * Please, provide either an image or a folder containing at least an image"
print "******************************************************************************************"
exit(-4)
if (imagePath is not None) and (folderIn is not None):
if not alreadyPrint:
print "******************************************************************************************"
print " * Please, provide either an image or a folder containing at least an image"
print "******************************************************************************************"
exit(-5)
if warning:
print
# ************************************************ Running MedGA ************************************************
if mpi and folderIn is not None:
# Run MPI version on a folder
runMPI(folderIn, folderOut, population, generations, selection, cross_rate, mut_rate, pressure, elitism, cores, verbose)
else:
# Run sequential version on either a folder or a single image
run(imagePath, folderIn, folderOut, population, generations, selection, cross_rate, mut_rate, pressure, elitism, verbose)