-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptor_trainner.py
195 lines (134 loc) · 5.27 KB
/
adaptor_trainner.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
# -*- coding: utf-8 -*-
import os
import numpy as np
import random
import subprocess
from IPython import display
from modules.msg_alert import MsgAlert
"""# Genetic Algorithm
Algoritmo genetico para otimizar os paramentros
"""
class Individual:
chromosome = None
pontuation = None
def __init__(self, chromosome=None, bits=8):
self.bits = bits
self.max_val = 2**bits-1
if chromosome!=None:
self.chromosome = chromosome
else:
self.chromosome = random.randint(0, self.max_val)
self.pontuation = 0.0
def muted(self,chromosome_result):
# Flip value with 1
mask_mut = random.randint(1,self.max_val)
maks = 0b1
for _ in range(self.bits):
if mask_mut & 0b1:
chromosome_result = chromosome_result ^ maks
mask_mut = maks << 1
mask_mut = mask_mut >> 1
return chromosome_result
def cross(self,femele):
prob_muted_fathers = random.random()
male = self.chromosome
femele = femele.chromosome
if prob_muted_fathers < 0.2:
male = self.muted(male)
elif prob_muted_fathers < 0.4:
femele = self.muted(femele)
mask_mut = random.randint(1,self.max_val)
child = 0
maks = 0b1 << self.bits
for _ in range(self.bits):
if mask_mut & 0b1:
child = child | (male & maks)
else:
child = child | (femele & maks)
maks = maks >> 1
mask_mut = mask_mut >> 1
return Individual(chromosome=child, bits=self.bits)
def get_indice(self,mapped_bits=2):
list_indices = []
mask = 2**mapped_bits-1
chromo = self.chromosome
for _ in range(self.bits//mapped_bits):
val = chromo & mask
list_indices.append(val)
chromo = chromo >> mapped_bits
list_indices.reverse()
return list_indices
def __str__(self,):
return f"chromo: {bin(self.chromosome)}, pont: {self.pontuation}"
def __lt__(self, individual_comparator):
return self.pontuation > individual_comparator.pontuation
def __eq__(self, __o: object) -> bool:
val = self.chromosome == __o.chromosome
return val
"""## Definindo os paramentros"""
alert = MsgAlert()
loss_functions = ['mse_loss','ssim_loss','ssim_mse_loss','ssim_psnr_mse_loss','mse_loss','ssim_loss','ssim_mse_loss','acurracy_kl_loss',]
opt_names = ['Adam','AdamX', 'RMSprop', 'SGD','Adam','AdamX', 'RMSprop', 'SGD']
lrs_gen = [0.0001, 0.001 , 0.01 , 0.1, 0.00046415888336127773, 0.002154434690031882, 0.02 , 0.2 ]
bts_1_gen = [0.2,0.9,0.95,0.85, 0.2,0.9,0.95,0.85]
bts_2_gen = [0.9999,0.995,0.999,0.99,0.9999,0.995,0.999,0.99]
total_individous = 10
population = np.array([ Individual( bits=15 ) for _ in range(total_individous) ]) if not os.path.isfile('./population.npy') else np.load('./population.npy', allow_pickle=True)
generations = 2
steps = 100
for generation in range(generations+1):
alert.send_msg(msg=f'Generation:{generation}')
# calculando a pontuação
for i in range(total_individous):
individou = population[i]
indices = individou.get_indice(mapped_bits=3)
# Command to execute
command = f"python trainner_model.py 100 {loss_functions[indices[0]]} {opt_names[indices[1]]} {lrs_gen[indices[2]]} {bts_1_gen[indices[3]]} {bts_2_gen[indices[4]]} 1"
# Execute the command and wait for it to complete
process = subprocess.run(command, shell=True)
# Check if the command was executed successfully
print('.', end='', flush=True)
if process.returncode == 0:
try:
pontuation = np.load(f"./outputs/1.npy")
except:
pontuation = 100.0
individou.pontuation = pontuation
else:
print(f"Execution failed with return code {process.returncode}")
population = np.sort(population)[::-1]
individou = random.choice(population[:3])
indices = individou.get_indice(mapped_bits=3)
# Command to execute
command = f"python training.py 100000 {loss_functions[indices[0]]} {opt_names[indices[1]]} {lrs_gen[indices[2]]} {bts_1_gen[indices[3]]} {bts_2_gen[indices[4]]}"
# Execute the command and wait for it to complete
process = subprocess.run(command, shell=True)
with open('./population.npy', 'wb') as f:
np.save(f,population)
if generation >= generations:
break
new_generation = []
s = int(total_individous*0.1) if int(total_individous*0.1) != 0 else 1
new_generation.extend(population[:s])
population_for_adaptar = int(total_individous*0.9)
for _ in range(population_for_adaptar):
male = random.choice(population[:int(total_individous*0.95)])
femele = random.choice(population[:int(total_individous*0.95)])
child = male.cross(femele)
if not (child in new_generation):
new_generation.append(child)
else:
cont = 0
while child in new_generation:
male = random.choice(population[:int(total_individous*0.95)])
femele = random.choice(population[:int(total_individous*0.95)])
child = male.cross(femele)
if cont > 5000:
break
cont += 1
new_generation.append(child)
population = new_generation
display.clear_output(wait=True)
indices = population[0].get_indice()
alert.send_msg(msg=f"python training.py 100000 {loss_functions[indices[0]]} {opt_names[indices[1]]} {lrs_gen[indices[2]]} {bts_1_gen[indices[3]]} {bts_2_gen[indices[4]]}")
alert.send_msg(msg='Adaptor trainner: fim da execução')