-
Notifications
You must be signed in to change notification settings - Fork 15
/
circle_alpha.py
153 lines (123 loc) · 4.21 KB
/
circle_alpha.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
import cv2, random, os, sys
import numpy as np
from copy import deepcopy
from skimage.measure import compare_mse
import multiprocessing as mp
filepath = sys.argv[1]
filename, ext = os.path.splitext(os.path.basename(filepath))
img = cv2.imread(filepath)
height, width, channels = img.shape
# hyperparameters
n_initial_genes = 50
n_population = 50
prob_mutation = 0.01
prob_add = 0.3
prob_remove = 0.2
min_radius, max_radius = 5, 15
save_every_n_iter = 100
# Gene
class Gene():
def __init__(self):
self.center = np.array([random.randint(0, width), random.randint(0, height)])
self.radius = random.randint(min_radius, max_radius)
self.color = np.array([random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)])
self.alpha = random.uniform(0, 1)
def mutate(self):
mutation_size = max(1, int(round(random.gauss(15, 4)))) / 100
r = random.uniform(0, 1)
if r < 0.25: # radius
self.radius = np.clip(random.randint(
int(self.radius * (1 - mutation_size)),
int(self.radius * (1 + mutation_size))
), 1, 100)
elif r < 0.5: # center
self.center = np.array([
np.clip(random.randint(
int(self.center[0] * (1 - mutation_size)),
int(self.center[0] * (1 + mutation_size))),
0, width),
np.clip(random.randint(
int(self.center[1] * (1 - mutation_size)),
int(self.center[1] * (1 + mutation_size))),
0, height)
])
elif r < 0.75: # color
self.color = np.array([
np.clip(random.randint(
int(self.color[0] * (1 - mutation_size)),
int(self.color[0] * (1 + mutation_size))),
0, 255),
np.clip(random.randint(
int(self.color[1] * (1 - mutation_size)),
int(self.color[1] * (1 + mutation_size))),
0, 255),
np.clip(random.randint(
int(self.color[2] * (1 - mutation_size)),
int(self.color[2] * (1 + mutation_size))),
0, 255)
])
else: # alpha
self.alpha = np.clip(random.randint(
int(self.alpha * (1 - mutation_size)),
int(self.alpha * (1 + mutation_size))
), 0, 1)
# compute fitness
def compute_fitness(genome):
out = np.ones((height, width, channels), dtype=np.uint8) * 255
for gene in genome:
overlay = out.copy()
cv2.circle(overlay, center=tuple(gene.center), radius=gene.radius, color=(int(gene.color[0]), int(gene.color[1]), int(gene.color[2])), thickness=-1)
cv2.addWeighted(overlay, gene.alpha, out, 1 - gene.alpha, 0, out)
# mean squared error
fitness = 255. / compare_mse(img, out)
return fitness, out
# compute population
def compute_population(g):
genome = deepcopy(g)
# mutation
if len(genome) < 200:
for gene in genome:
if random.uniform(0, 1) < prob_mutation:
gene.mutate()
else:
for gene in random.sample(genome, k=int(len(genome) * prob_mutation)):
gene.mutate()
# add gene
if random.uniform(0, 1) < prob_add:
genome.append(Gene())
# remove gene
if len(genome) > 0 and random.uniform(0, 1) < prob_remove:
genome.remove(random.choice(genome))
# compute fitness
new_fitness, new_out = compute_fitness(genome)
return new_fitness, genome, new_out
# main
if __name__ == '__main__':
os.makedirs('result', exist_ok=True)
p = mp.Pool(mp.cpu_count() - 1)
# 1st gene
best_genome = [Gene() for _ in range(n_initial_genes)]
best_fitness, best_out = compute_fitness(best_genome)
n_gen = 0
while True:
try:
results = p.map(compute_population, [deepcopy(best_genome)] * n_population)
except KeyboardInterrupt:
p.close()
break
results.append([best_fitness, best_genome, best_out])
new_fitnesses, new_genomes, new_outs = zip(*results)
best_result = sorted(zip(new_fitnesses, new_genomes, new_outs), key=lambda x: x[0], reverse=True)
best_fitness, best_genome, best_out = best_result[0]
# end of generation
print('Generation #%s, Fitness %s' % (n_gen, best_fitness))
n_gen += 1
# visualize
if n_gen % save_every_n_iter == 0:
cv2.imwrite('result/%s_%s.jpg' % (filename, n_gen), best_out)
cv2.imshow('best out', best_out)
if cv2.waitKey(1) == ord('q'):
p.close()
break
cv2.imshow('best out', best_out)
cv2.waitKey(0)