-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyModel.py
More file actions
152 lines (125 loc) · 4.78 KB
/
Copy pathmyModel.py
File metadata and controls
152 lines (125 loc) · 4.78 KB
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
"""
myModel
MyModel is the brain and heartpiece of creating the model
It uses all the available files to piece together a final model that can be transformed into c code
First it gets all the testing and training data
Then it creates a basis Model using trainBasisModel.py
Then it uses prune.py to prune and retrain
In the end quantization is necessary
After each step the testbench can be uses to check on the changes
Author: Anton Giese
Date: 26.10.2020
"""
import os
import keras
import trainBasisModel
import prune
import Testbench
import numpy as np
import gatherGestures
from quantize import quantize
from quantize import quantizePerLayer
"""
basis
Get the basis model with the configuration 180-8-5
However by changing the variables this can be customized
"""
def basis(run):
numLayers = 2 #3
numNeurons = np.array([8,5])
activations = np.array(['relu','softmax'])
epoch = 2000
model = trainBasisModel.createTrainedModel(numLayers,numNeurons,activations,epoch,False)
t1 = Testbench.Testbench(model,"basis")
t1.getTestData()
#t1.introduceTestData(testX,testY)
t1.checkAll()
model.save("myModelOutput/basisModelFinal"+str(run)+".h5")
return model
"""
pruneModel
Apply pruning to a model
With the specified sparsity
@param sparsity: parameter that will be given to prune function
"""
def pruneModel(model,sparsity):
epochPrune = 2000
modelPruned = prune.prune(model,trainX,trainY,testX,testY,epochPrune,sparsity)
modelPruned.save("myModelOutput/prunedModelFinal.h5")
return modelPruned
"""
quantizeModelPerLayer
This is the quantize function.
This is the recommended approach to quantization
@param epoch: How many retraining epochs there should be
@param k: K is an array where each element says how many clusters there should be in the respective layer
"""
def quantizeModelPerLayer(model,k,epoch):
q = quantizePerLayer(m)
q.compile()
m2 = q.quantizeModel(k)
m2 = q.retrain(trainX,trainY,epoch)
m2.save("myModelOutput/quantizedModelFinal.h5")
return m2
"""
quantizeModelAllInOne
This can quantize ALL weights at once. Dont use this
"""
def quantizeModelAllInOne(model,k,epoch):
q = quantize(m)
q.compile()
m2 = q.getQuantizedModel(k)
m2 = q.retrain(trainX,trainY,epoch)
#t3.showWeights()
m2.save("myModelOutput/quantizedModelFinal.h5")
#t3.checkAll()
"""
main
"""
if __name__ == "__main__":
# get data
g = gatherGestures.gatherGestures()
trainX,trainY,testX,testY = g.collectAllGestures()
a=basis(1)
pruneModel(m,0.5)
m = keras.models.load_model("finalModels/prunedModelFinal9896.h5")
# perform initial check on testbench
t3 = Testbench.Testbench(m,"pruned")
t3.introduceTestData(trainX,trainY,testX,testY)
t3.checkAll()
# get 12 quantized models for different combinations of number of clusters in first and second layer
for first in range(3,25):
# first create the folder where all models are savec
try:
os.mkdir(os.getcwd()+"/finalModelsEmperically/basisModelsPrunedQuant/"+str(first)+"infirst")
except:
print("already exists")
for second in range(2,9):
# again create folder in folder
try:
os.mkdir(os.getcwd()+"/finalModelsEmperically/basisModelsPrunedQuant/"+str(first)+"infirst/"+str(second)+"insecond")
except:
print("already exists")
# save all results of this configuration in two arrays
testsetRes = np.array([])
trainsetRes = np.array([])
for cycle in range(12):
# load old model
m = keras.models.load_model("finalModels/prunedModelFinal9896.h5")
print("first ",first)
print("second ",second)
# quantize it
new = quantizeModelPerLayer(m,[first,second],60)
# check it
t3 = Testbench.Testbench(new,"quantized")
t3.introduceTestData(trainX,trainY,testX,testY)
t3.getTestData()
res = t3.checkAll()
testsetRes = np.append(testsetRes,res[0][1])
trainsetRes = np.append(trainsetRes,res[1][1])
# save it
new.save("finalModelsEmperically/basisModelsPrunedQuant/"+str(first)+"infirst/"+str(second)+"insecond/"+str(cycle)+".h5")
print("index: {} for first: {} and second: {}".format(cycle,first,second))
# save arrays
np.save("finalModelsEmperically/basisModelsPrunedQuant/"+str(first)+"infirst/"+str(second)+"insecond/testsetResults.npy",testsetRes)
np.save("finalModelsEmperically/basisModelsPrunedQuant/"+str(first)+"infirst/"+str(second)+"insecond/trainsetResults.npy",trainsetRes)