-
Notifications
You must be signed in to change notification settings - Fork 0
/
genetics_GP.py
167 lines (131 loc) · 5.56 KB
/
genetics_GP.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
import copy
import image_process
from parameters import *
import morphology_transformation
import structuring_elements
from deap import algorithms
from deap import base
from deap import creator
from deap import tools
from deap import gp
import util
import numpy as np
class ImageProcessor():
"""
Klasa koja služi za procesuiranje slike transformacijama nastalim iz stablaste strukture.
"""
def _reset(self, original, mask):
self.image = copy.copy(original)
self.dominantColor1 = image_process.kmeans(original, 1)
self.dominantColor2 = image_process.kmeans(original, 2)
self.mask = mask
def dominantColor2Quadraple(self, out1, out2, out3, out4, image=None):
"""
Odluka izlaza na temelju dvije dominantne boje unutar slike
"""
if out1 and out2 and out3 and out4:
if np.allclose(self.dominantColor2, np.array([[64], [64]]), atol=64):
out1()
elif np.allclose(self.dominantColor2, np.array([[64], [192]]), atol=64):
out2()
elif np.allclose(self.dominantColor2, np.array([[192], [64]]), atol=64):
out3()
elif np.allclose(self.dominantColor2, np.array([[192], [192]]), atol=64):
out4()
def dominantColor1Bipolar(self, out1, out2, image=None):
"""
Odluka izlaza na temelju dominantne boje unutar slike, odluka je tamna ili svijetla pozadina
"""
if out1 and out2:
if np.allclose(self.dominantColor1, np.array([0]), atol=128):
out1()
else:
out2()
def dominantColor1Quadraple(self, out1, out2, out3, out4, image=None):
"""
Odluka izlaza na temelju dominantne boje unutar slike, odluka je unutar jedne od četiri kategorije
"""
if out1 and out2 and out3 and out4:
if np.allclose(self.dominantColor1, np.array([32]), atol=32):
out1()
elif np.allclose(self.dominantColor1, np.array([96]), atol=32):
out2()
elif np.allclose(self.dominantColor1, np.array([160]), atol=32):
out3()
elif np.allclose(self.dominantColor1, np.array([224]), atol=32):
out4()
def transform(self, func, kernel):
self.image = func(self.image, kernel)
def process(self, original, mask, no_cells, individual):
"""
Vraća razliku originalne slike i maske nakon trasnfomacija zadanih unutar stabla
:param original: Originalna slika
:param mask: Korespondentna maska
:param individual:Stablasta struktura
:return:Vrijednost detektirane slike
"""
self._reset(original, mask)
eval(str(individual), pset.context, {})
self.image = image_process.otsu_treshold(self.image)
return morphology_transformation.compare(self.image, self.mask, no_cells)
iprocessor = ImageProcessor()
pset = gp.PrimitiveSet("MAIN", 0)
pset.addPrimitive(iprocessor.dominantColor2Quadraple, 4)
pset.addPrimitive(iprocessor.dominantColor1Bipolar, 2)
pset.addPrimitive(iprocessor.dominantColor1Quadraple, 4)
class Transform:
def __init__(self, func, kernel):
self.func = func
self.kernel = kernel
def callTrans(self):
iprocessor.transform(self.func, self.kernel)
l = []
for i in range(4):
for j in range(1, 28):
t = Transform(morphology_transformation.defined_transforms[i],structuring_elements.elements.get(j))
pset.addTerminal(t.callTrans, "MT{}{}".format(i,j))
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox()
toolbox.register("expr_init", gp.genHalfAndHalf, pset=pset, min_=1, max_=5)
toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr_init)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def evaluateFunction(individual):
sum = 0
for i, (name, info) in enumerate(image_process.IMAGES_WITH_INFO.items()):
if i == TRAIN_NO:
break
image, gray_image, mask, no_cells = info
sum += iprocessor.process(image, mask, no_cells, individual)
return (sum / TRAIN_NO),
toolbox.register("evaluate", evaluateFunction)
toolbox.register("select", tools.selTournament, tournsize=TOURNAMENT_SIZE)
toolbox.register("mate", gp.cxOnePoint)
toolbox.register("expr_mut", gp.genGrow, min_=0, max_=2)
toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
# Otkomentirati liniju za paralelizam (parametri: -m scoop)
# toolbox.register("map", futures.map)
def statistacs(pop):
return pop
def main():
pop = toolbox.population(n=POPULATION_SIZE)
hof = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: (ind, ind.fitness.values))
stats.register("INDIVIDUAL", util.printBest)
stats.register("MED", util.calculate_median)
stats.register("AVG", util.calculate_mean)
stats.register("STD", util.calculate_std)
stats.register("MIN", util.calculate_min)
stats.register("MAX", util.calculate_max)
stats.register("GRAPH", util.graphInfo)
algorithms.eaSimple(pop, toolbox,
cxpb=CROSS_PROBABILITY,
mutpb=MUTATION_PROBABILITY,
ngen=NUMB_OF_GENERATIONS,
stats=stats, halloffame=hof)
util.makePlots()
return pop, hof, stats
if __name__ == "__main__":
image_process.load('images_serialized/images_with_info.dict')
pop, hof, stats = main()
print(str(gp.PrimitiveTree(hof[0])))