-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (55 loc) · 2.08 KB
/
main.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
import numpy as np
from pymoo.algorithms.moo.nsga2 import NSGA2
from pymoo.core.problem import ElementwiseProblem
from pymoo.termination import get_termination
from pymoo.optimize import minimize
from tensorflow import keras
from inner_genetic_algorithm import inner_algorithm
from pymoo.operators.sampling.rnd import PermutationRandomSampling
from pymoo.operators.crossover.ox import OrderCrossover
from pymoo.operators.mutation.inversion import InversionMutation
class MyProblem(ElementwiseProblem):
def __init__(self, tf_model):
n_variables = 5 - 1
## since our chromosomes will be permuted, the filter count should be the same as n_variables
filter_count = n_variables
super().__init__(
n_var= n_variables,
n_obj=2,
n_ieq_constr=0,
xl= np.zeros(n_variables),
xu=np.full(n_variables, filter_count - 1)
)
self.tf_model = tf_model
def _evaluate(self, x, out, *args, **kwargs):
algorithm = inner_algorithm(self.tf_model, x)
X_res, F_res = algorithm.start()
with open('results/filters_with_param_X.txt', 'a+') as file:
np.savetxt(file, X_res)
file.write('\n')
with open('results/filters_with_param_F.txt', 'a+') as file:
np.savetxt(file, F_res)
file.write('\n')
out["F"] = F_res[0]
model = keras.models.load_model('model/cifar10_model_90%val_accuracy.h5')
problem = MyProblem(model)
algorithm = NSGA2(
pop_size=10,
n_offsprings=10,
sampling=PermutationRandomSampling(),
crossover=OrderCrossover(),
mutation=InversionMutation(),
eliminate_duplicates=True
)
generation_count = 3
termination = get_termination("n_gen", generation_count)
res = minimize(problem,
algorithm,
termination,
seed=1,
save_history=False,
verbose=True)
with open('results/final_results_X.txt', 'w') as file:
np.savetxt(file, res.X)
with open('results/final_results_F.txt', 'w') as file:
np.savetxt(file, res.F)