This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
/
classifier.py
218 lines (175 loc) · 6.91 KB
/
classifier.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
# Copyright 2017 Abien Fred Agarap
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =========================================================================
"""Module for classifier based on a trained DL-SVM model"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
__version__ = "0.1.0"
__author__ = "Abien Fred Agarap"
import argparse
import numpy as np
import os
import tensorflow as tf
from utils.data import load_data
from utils.data import one_hot_encode
BATCH_SIZE = "batch_size"
CELL_SIZE = "cell_size"
def predict(dataset, model, model_path, **kwargs):
"""Returns classification for given data
:param dataset: The dataset to be classified using the trained model.
:param model: The model name to be used for classification.
:param model_path: The path where the trained model is saved.
:return:
"""
init_op = tf.group(
tf.local_variables_initializer(), tf.global_variables_initializer()
)
accuracy_tensor_name = "accuracy/accuracy/Mean:0"
prediction_tensor_name = "accuracy/predicted_class:0"
assert BATCH_SIZE in kwargs, "KeyNotFound : {}".format(BATCH_SIZE)
assert (
type(kwargs[BATCH_SIZE]) is int
), "Expected data type : int, but {} is {}".format(
kwargs[BATCH_SIZE], type(kwargs[BATCH_SIZE])
)
if model == 1:
# CNN-SVM
feed_dict = {"x_input:0": None, "y_input:0": None, "p_keep:0": 1.0}
accuracy_tensor_name = "metrics/accuracy/Mean:0"
prediction_tensor_name = "metrics/predicted_class:0"
elif model == 2:
# GRU-SVM
assert CELL_SIZE in kwargs, "KeyNotFound : {}".format(CELL_SIZE)
assert (
type(kwargs[CELL_SIZE]) is int
), "Expected data type : int, but {} is {}".format(
kwargs[CELL_SIZE], type(kwargs[CELL_SIZE])
)
initial_state = np.zeros([kwargs[BATCH_SIZE], kwargs[CELL_SIZE] * 5])
initial_state = initial_state.astype(np.float32)
feed_dict = {
"input/x_input:0": None,
"input/y_input:0": None,
"initial_state:0": initial_state,
"p_keep:0": 1.0,
}
elif model == 3:
# MLP-SVM
feed_dict = {"input/x_input:0": None, "input/y_input:0": None}
predictions_array = np.array([])
accuracy_array = np.array([])
with tf.Session() as sess:
sess.run(init_op)
checkpoint = tf.train.get_checkpoint_state(model_path)
if checkpoint and checkpoint.model_checkpoint_path:
# get the meta graph from trained model
saver = tf.train.import_meta_graph(
checkpoint.model_checkpoint_path + ".meta"
)
# restore previously-saved variables from the meta graph
saver.restore(sess, tf.train.latest_checkpoint(model_path))
print(
"Loaded trained model from {}".format(
tf.train.latest_checkpoint(model_path)
)
)
assert "size" in kwargs, "KeyNotFound : {}".format("size")
try:
for step in range(kwargs["size"] // kwargs["batch_size"]):
offset = (step * kwargs["batch_size"]) % kwargs["size"]
features = dataset[0][offset : (offset + kwargs["batch_size"])]
labels = dataset[1][offset : (offset + kwargs["batch_size"])]
feed_dict["x_input:0" if model == 1 else "input/x_input:0"] = features
feed_dict["y_input:0" if model == 1 else "input/y_input:0"] = labels
prediction_tensor = sess.graph.get_tensor_by_name(
prediction_tensor_name
)
predictions = sess.run(prediction_tensor, feed_dict=feed_dict)
predictions_array = np.append(predictions_array, predictions)
accuracy_tensor = sess.graph.get_tensor_by_name(accuracy_tensor_name)
accuracy = sess.run(accuracy_tensor, feed_dict=feed_dict)
accuracy_array = np.append(accuracy_array, accuracy)
except KeyboardInterrupt:
print("KeyboardInterrupt at step {}".format(step))
return predictions_array, accuracy_array
def parse_args():
parser = argparse.ArgumentParser(
description="Deep Learning Using Support Vector Machine for Malware Classification"
)
group = parser.add_argument_group("Arguments")
group.add_argument(
"-m",
"--model",
required=True,
type=int,
help="[1] CNN-SVM, [2] GRU-SVM, [3] MLP-SVM",
)
group.add_argument(
"-t",
"--model_path",
required=True,
type=str,
help="path where to save the trained model",
)
group.add_argument(
"-d", "--dataset", required=True, type=str, help="the dataset to be classified"
)
arguments = parser.parse_args()
return arguments
def main(arguments):
model_choice = arguments.model
model_path = arguments.model_path
dataset_path = arguments.dataset
assert (
model_choice == 1 or model_choice == 2 or model_choice == 3
), "Invalid choice: Choose among 1, 2, and 3 only."
assert os.path.exists(path=model_path), "{} does not exist!".format(model_path)
assert os.path.exists(path=dataset_path), "{} does not exist!".format(dataset_path)
dataset = np.load(dataset_path)
features, labels = load_data(dataset=dataset)
labels = one_hot_encode(labels=labels)
dataset_size = features.shape[0]
print(features.shape)
if model_choice == 2:
features = np.reshape(
features,
(
features.shape[0],
int(np.sqrt(features.shape[1])),
int(np.sqrt(features.shape[1])),
),
)
predictions, accuracies = predict(
dataset=[features, labels],
model=model_choice,
model_path=model_path,
size=dataset_size,
batch_size=256,
cell_size=256,
)
else:
predictions, accuracies = predict(
dataset=[features, labels],
model=model_choice,
model_path=model_path,
size=dataset_size,
batch_size=256,
)
print("Predictions : {}".format(predictions))
print("Accuracies : {}".format(accuracies))
print("Average accuracy : {}".format(np.mean(accuracies)))
if __name__ == "__main__":
args = parse_args()
main(arguments=args)