-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpLoader.py
302 lines (269 loc) · 10.8 KB
/
dumpLoader.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import os
import pickle
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import Adam
from keras.models import load_model
from tensorflow.python.client import device_lib
from keras import backend as K
from keras import optimizers
import random
from matplotlib import pyplot as plt
import keras_metrics
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import classification_report
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import GridSearchCV
from sklearn import preprocessing
# Constants
DUMP_DIRECTORY_PATH = "./dumpFiles"
# Definition of the SWISH activation function
def swish(x):
return K.sigmoid(x) * x
# Setting up a Keras model of: 4 Conv and Pool + Flat + 5 Dense
def setupNNmodel(activation=swish, learn_rate=0.00001):
model = Sequential()
# Input - Layer
model.add(Dense(finalArrayLen, activation="relu", input_shape=(finalArrayLen,)))
# Hidden - Layers
# model.add(Dropout(0.5, noise_shape=None, seed=None))
model.add(Dense(1000, activation=activation))
model.add(Dropout(0.5, noise_shape=None, seed=None))
model.add(Dense(500, activation="sigmoid"))
# model.add(Dense(100, activation="sigmoid"))
# Output- Layer
model.add(Dense(numClasses, activation="softmax"))
model.summary()
model.compile(
loss='kullback_leibler_divergence',
optimizer=optimizers.Adam(lr=learn_rate),
metrics=["accuracy",keras_metrics.precision(), keras_metrics.recall()]
# metrics=["accuracy"]
)
return model
# This method measures the performance of the NN using K-Fold
def crossValidation(X, Y, nFold):
# Setting up a label encoder, necessary to convert 2d label to 1d
label_encoder = LabelEncoder()
Y_1D = []
for i in range(0, len(Y)):
Y_1D.append(np.argmax(Y[i][:]))
# Setting up the K-Fold splitting
seed = 3
np.random.seed(seed)
kfold = StratifiedKFold(n_splits=nFold, shuffle=True, random_state=seed)
# Resetting the weights of the model
initial_weights = NNmodel.get_weights()
# Actually splitting into training and testing data
accList = []
precList = []
recList = []
iteration = 0
for train, test in kfold.split(X, Y_1D):
print("\n[[[[WE ARE IN ITERATION: " + str(iteration+1) + " ]]]]\n")
iteration = iteration + 1
# # Grid Search
# model = KerasClassifier(build_fn=setupNNmodel, epochs=100, batch_size=10,)
# # define the grid search parameters
# activation=['tanh','sigmoid']
# learn_rate = [0.001, 0.01, 0.1, 0.2, 0.3]
# param_grid = dict(activation=activation, learn_rate=learn_rate)
# grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1)
# grid_result = grid.fit(X[train], Y[train])
# # summarize results
# print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
# means = grid_result.cv_results_['mean_test_score']
# stds = grid_result.cv_results_['std_test_score']
# params = grid_result.cv_results_['params']
# for mean, stdev, param in zip(means, stds, params):
# print("Mean Acc: %f (std: %f) with: %r" % (mean, stdev, param))
# Training the model
history = NNmodel.fit(
X[train], Y[train],
verbose=1,
epochs=70,
batch_size=128,
shuffle=True,
validation_split=0.1
)
# evaluate the model
scores = NNmodel.evaluate(X[test], Y[test], verbose=0)
print("%s: %.2f%%" % (NNmodel.metrics_names[1], scores[1] * 100))
print("%s: %.2f%%" % (NNmodel.metrics_names[2], scores[2] * 100))
print("%s: %.2f%%" % (NNmodel.metrics_names[3], scores[3] * 100))
accList.append(scores[1] * 100)
precList.append(scores[2] * 100)
recList.append(scores[3] * 100)
# Plotting Training and Validation curves for Accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('Model Accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# Plotting Training and Validation curves for Loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model Loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# Setting back the weights
NNmodel.set_weights(initial_weights)
# Calculating final results
mean = np.mean(accList)
std = np.std(accList)
print("KFOLD ACCURACY Results: " + str(mean) + "% with std(+/- " + str(std) + "%)")
mean = np.mean(precList)
std = np.std(precList)
print("KFOLD PRECISION Results: " + str(mean) + "% with std(+/- " + str(std) + "%)")
mean = np.mean(recList)
std = np.std(recList)
print("KFOLD RECALL Results: " + str(mean) + "% with std(+/- " + str(std) + "%)")
# Data structures
numApis = 0
numPermissions = 0
numStrings = 0
numClasses = 0 # Number of classes
classCounter = {} # ClassName : NumOfEntriesForThisClass
classListDictionary = {} # ClassName : listOfDataForThisClass
classProbability = {} # ClassName : ClassProbability
classNameToIndex = {} # ClassName : ClassIndex
totAppCounter = 0 # Total number of apps
validationPercentage = 0.08 # Percentage of data to be used as validation
files = [i for i in os.listdir(DUMP_DIRECTORY_PATH) if i.endswith("dat")]
for file in files:
# The name of the current category is the same as the file name for now
category = file
category = os.path.splitext(category)[0]
filePath = DUMP_DIRECTORY_PATH + "/" + file
with open (filePath, 'rb') as fp:
itemlist = pickle.load(fp)
print("Loaded " + category + " with " + str(len(itemlist)) + " elements")
# Update data structures
classNameToIndex[category] = numClasses
numClasses = numClasses + 1
classCounter[category] = len(itemlist)
totAppCounter = totAppCounter + len(itemlist)
classListDictionary[category] = itemlist
numApis = itemlist[-1].getNumOfMethods()
numPermissions = itemlist[-1].getNumOfPermissions()
numStrings = len(itemlist[-1].getStringsArray())
# Now that we have everything that we need from our files, we proceed...
# Calculating the class probability
# totEntries = 0
# for category in classCounter:
# totEntries = totEntries + classCounter[category]
# for category in classCounter:
# classProbability[category] = classCounter[category]/totEntries
# # Calculating the conditional probabilities
# apiCondProb = np.zeros((numApis,numClasses))
# for classIndex in range(0,numClasses):
# for apiIndex in range(0, numApis):
# # Let's count how many times this
# First, build the validation set by extracting some of the training data
# validationCounter = int(validationPercentage * totAppCounter)
# val_X = []
# val_Y = []
# while validationCounter > 0:
# for category in classListDictionary:
# categoryList = classListDictionary[category]
# # Extract one element
# app = categoryList.pop()
# classListDictionary[category] = categoryList
# # Merging the 3 arrays in just one
# tmpArray = []
# tmpArray.extend(app.getMehtodsArray())
# tmpArray.extend(app.getPermissionsArray())
# tmpArray.extend(app.getStringsArray())
# # Adding to the validation set
# val_X.append(tmpArray)
# # Adding the corresponding numeric label
# label = [0 for i in range(0, numClasses)]
# label[classNameToIndex[app.getCategory()]] = 1
# val_Y.append(label)
# # Updating counter
# validationCounter = validationCounter - 1
# if validationCounter == 0:
# break
#
# print("Validation size: " + str(len(val_X)))
# Building the complete features training array
X = []
Y = []
tmpArray = []
for category in classListDictionary:
for app in classListDictionary[category]:
# Putting apps from all categories together so that we can shuffle them
tmpArray.append(app)
# Shuffle the array
random.shuffle(tmpArray)
for app in tmpArray:
# Merging the 3 arrays in just one
featuresArr = []
featuresArr.extend(app.getMehtodsArray())
featuresArr.extend(app.getPermissionsArray())
featuresArr.extend(app.getStringsArray())
# Adding to the training set
X.append(featuresArr)
# Adding the corresponding numeric label
label = [0 for i in range(0,numClasses)] # Transforming the label into the binary '00001000' format starting from decimal
label[classNameToIndex[app.getCategory()]] = 1
Y.append(label)
# Set up NN model
finalArrayLen = len(X[-1])
NNmodel = setupNNmodel()
# Converting to numpy arrays
X = np.array(X)
Y = np.array(Y)
# val_X = np.array(val_X)
# val_Y = np.array(val_Y)
# Normalizing Input
std_scale = preprocessing.StandardScaler(with_mean=True, with_std=True).fit(X)
X = std_scale.transform(X)
# minMax_scale = preprocessing.MinMaxScaler(feature_range=(-1,1)).fit(X)
# X = minMax_scale.transform(X)
# K-FOLD
crossValidation(X, Y, 10)
# # Training the model
# results = NNmodel.fit(
# X, Y,
# verbose=1,
# epochs=30,
# batch_size=128,
# shuffle=True,
# # validation_data = (val_X, val_Y),
# validation_split = validationPercentage
# )
#
# # Plot metrics
# # "Accuracy"
# plt.plot(results.history['acc'])
# plt.plot(results.history['val_acc'])
# plt.title('Model accuracy')
# plt.ylabel('Accuracy')
# plt.xlabel('Epoch')
# plt.legend(['Training set', 'Validation set'], loc='upper left')
# plt.show()
# # "Loss"
# plt.plot(results.history['loss'])
# plt.plot(results.history['val_loss'])
# plt.title('Model loss')
# plt.ylabel('Loss')
# plt.xlabel('Epoch')
# plt.legend(['Training set', 'Validation set'], loc='upper left')
# plt.show()
#
# # Printing the validation results
# print("\n\nAverage Validation accuracy: " + str(np.mean(results.history["val_acc"])))
# # Finding best validation accuracy
# print("Best validation accuracy: " + str(max(results.history['val_acc'])))
#
# print("Average validation Precision and Recall: " + str(np.mean(results.history["val_precision"])) + " " + str(np.mean(results.history["val_recall"])))
# print("Best validation Precision and Recall: " + str(max(results.history["val_precision"])) + " " + str(max(results.history["val_recall"])))