forked from rcpsilva/EANNCompress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compressTest.py
66 lines (55 loc) · 1.84 KB
/
compressTest.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
import compress_problem
import numpy as np
import pandas as pd
from pymoo.util.misc import stack
from pymoo.model.problem import Problem
import tensorflow as tf
from keras.models import load_model
import gc
from keras import backend as K
from sklearn.model_selection import train_test_split
from pymoo.model.problem import Problem
from pymoo.operators.mixed_variable_operator import MixedVariableSampling, MixedVariableMutation, MixedVariableCrossover
from pymoo.algorithms.nsga2 import NSGA2
from pymoo.factory import get_crossover, get_mutation, get_sampling
from pymoo.optimize import minimize
from pymoo.visualization.scatter import Scatter
from Neural_network_compression import neural_network_utils as nnUtils
from Neural_network_compression import nn_compression_utils as compress
import test_model
base_model = load_model('final_model.h5')
trainX, trainY, testX, testY = test_model.load_dataset()
# prepare pixel data
trainX, testX = test_model.prep_pixels(trainX, testX)
trainX, valX , trainY, valY = train_test_split(trainX, trainY, test_size=0.2)
data= [trainX, trainY], [valX, valY], [testX, testY]
problem = compress_problem.NNCompressProblem(base_model, data)
mask = problem.get_compression_mask()
sampling = MixedVariableSampling(mask, {
"real": get_sampling("real_random"),
"int": get_sampling("int_random")
})
crossover = MixedVariableCrossover(mask, {
"real": get_crossover("real_sbx", prob=1.0, eta=3.0),
"int": get_crossover("int_sbx", prob=1.0, eta=3.0)
})
mutation = MixedVariableMutation(mask, {
"real": get_mutation("real_pm", eta=3.0),
"int": get_mutation("int_pm", eta=3.0)
})
algorithm = NSGA2(
pop_size=10,
sampling=sampling,
crossover=crossover,
mutation=mutation,
eliminate_duplicates=True,
)
res = minimize(
problem,
algorithm,
('n_eval', 20),
seed=69,
pf=None,
verbose=True,
save_history=True
)