-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepdrebin.py
More file actions
344 lines (268 loc) · 12.5 KB
/
deepdrebin.py
File metadata and controls
344 lines (268 loc) · 12.5 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from copy import deepcopy
import tqdm
import os
import datetime
from pathlib import Path
import shutil
import numpy as np
import torch
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
import torch.nn as nn
from torchmetrics import Accuracy, Recall, Precision, F1Score
'''
SKLearn Compatible Implementation of DeepDrebin
'''
class DrebinDNN(nn.Module):
"""
Network architecture used by Grosse et al. in the paper
'Adversarial Examples for Malware Detection'
"""
def __init__(self, input_size):
super(DrebinDNN, self).__init__()
self.layers = nn.Sequential(
nn.Linear(input_size, 200),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(200, 200),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(200, 2),
)
def forward(self, x):
return self.layers(x)
class DeepDrebin:
def __init__(self):
# use CUDA if available
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# hyper parameters
self.num_epochs = 10
self.batch_size = 64
self.learning_rate = 0.05
self.split_ratio = 0.66
# number fo validation months of data
self.train_months = 12
self.num_val_month = 3
self.random_seed = 0x10c0ffee
self.num_samples = None # don't use all samples of the dataset (None if all samples should be used)
# file paths
self.project_dir = Path(__file__).resolve().parents[0]
self.model_dir = os.path.join(self.project_dir, 'data/results/deepdrebin/models')
self.result_dir = os.path.join(self.project_dir, 'data/results/deepdrebin/')
if not os.path.exists(self.model_dir):
os.makedirs(self.model_dir)
self.last_model_path = os.path.join(self.model_dir, 'last_model-{}.pth')
self.best_model_path = os.path.join(self.model_dir, 'best_model-{}.pth')
timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_path = "logs/tensorboard/" + timestamp
self.checkpoint = True
self.desc = None
self.model = None
self.optimizer = None
self.criterion = None
self.train_time = None
self.t = None
self.pois_idx = None # The list of indexes of poisoning data in the training set
def setup(self, desc, train_time, X, y, t, pois_idx=None):
self.desc = desc
self.train_time = train_time
self.t = t
if pois_idx:
self.pois_idx = pois_idx
else:
self.pois_idx = []
# Convert data to tensor
y = np.array(y)
X = X.tocoo()
X_tensor_tmp = torch.sparse_coo_tensor(indices=torch.LongTensor([X.row.tolist(), X.col.tolist()]),
values=torch.LongTensor(X.data.astype(np.int32)),
size=X.shape)
# Convert the csr_matrix to numpy array, then to tensor
X_tensor = X_tensor_tmp.to_dense().type(torch.FloatTensor)
y_tensor = torch.from_numpy(y.astype(np.int_))
dataset = torch.utils.data.TensorDataset(X_tensor, y_tensor)
# Initialize Model
features, labels = dataset[0]
n_features = features.shape[0]
print('feature size is {}'.format(n_features))
self.model = DrebinDNN(n_features)
self.model.to(device=self.device)
# Define Loss and Optimizer
self.criterion = nn.CrossEntropyLoss()
self.optimizer = torch.optim.SGD(params=self.model.parameters(), lr=self.learning_rate)
def fit(self, X_train, y_train):
# Convert data to tensor
y = np.array(y_train)
X = X_train.tocoo()
X_tensor_tmp = torch.sparse_coo_tensor(indices=torch.LongTensor([X.row.tolist(), X.col.tolist()]),
values=torch.LongTensor(X.data.astype(np.int32)),
size=X.shape)
# Convert the csr_matrix to numpy array, then to tensor
X_tensor = X_tensor_tmp.to_dense().type(torch.FloatTensor)
y_tensor = torch.from_numpy(y.astype(np.int_))
dataset = torch.utils.data.TensorDataset(X_tensor, y_tensor)
# Prepare training Dataset
indices = [x for x in range(X_train.shape[0])]
np.random.shuffle(indices)
train_size = int(self.split_ratio * len(indices))
train_indices = indices[:train_size]
val_indices = indices[train_size:]
train_sampler = SubsetRandomSampler(train_indices)
val_sampler = SubsetRandomSampler(val_indices)
train_loader = DataLoader(dataset=dataset, batch_size=self.batch_size, sampler=train_sampler)
val_loader = DataLoader(dataset=dataset, batch_size=self.batch_size, sampler=val_sampler)
self.optimizer = torch.optim.SGD(params=self.model.parameters(), lr=self.learning_rate)
# Train Model
accuracy = Accuracy(num_classes=2, average='macro', task='binary').to(device=self.device)
f1score = F1Score(num_classes=2, average='macro', task='binary').to(device=self.device)
precision = Precision(num_classes=2, average='macro', task='binary').to(device=self.device)
recall = Recall(num_classes=2, average='macro', task='binary').to(device=self.device)
valid_max_f1score = 0.0
print("Start Training")
for epoch in range(self.num_epochs):
####################
# Train Model
####################
self.model.train()
train_loss = 0.0
for i, (inputs, labels) in enumerate(tqdm.tqdm(train_loader, desc='train')):
# Push data to device
inputs = inputs.to(device=self.device)
labels = labels.to(device=self.device)
# forward step
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
# backward step
self.optimizer.zero_grad()
loss.backward()
# update weights
self.optimizer.step()
train_loss += loss.item()
_, predictions = torch.max(outputs.data, 1)
# compute metrics on current batch
accuracy(predictions, labels)
f1score(predictions, labels)
precision(predictions, labels)
recall(predictions, labels)
# reset metrics
accuracy.reset()
f1score.reset()
precision.reset()
recall.reset()
####################
# Validate Model
####################
self.model.eval()
with torch.no_grad():
for inputs, labels in tqdm.tqdm(val_loader, desc='val'):
# Push data to device
inputs = inputs.to(device=self.device)
labels = labels.to(device=self.device)
# Make predictions
outputs = self.model(inputs)
_, predictions = torch.max(outputs, 1)
# Calculate metrics
accuracy(predictions, labels)
f1score(predictions, labels)
recall(predictions, labels)
precision(predictions, labels)
checkpoint = {
'epoch': epoch + 1,
'accuracy': accuracy.compute(),
'f1score': f1score.compute(),
'precision': precision.compute(),
'recall': recall.compute(),
'state_dict': self.model.state_dict(),
'optimizer': self.optimizer.state_dict(),
}
# save mode
is_best_model = False
if checkpoint['f1score'] > valid_max_f1score or valid_max_f1score == 0.0:
is_best_model = True
valid_max_f1score = checkpoint['f1score']
self.save_checkpoint(is_best_model, checkpoint)
def predict(self, X_test):
# Convert the csr_matrix to numpy array to tensor to dataloader
X = X_test.tocoo()
X_tensor_tmp = torch.sparse_coo_tensor(indices=torch.LongTensor([X.row.tolist(), X.col.tolist()]),
values=torch.LongTensor(X.data.astype(np.int32)),
size=X.shape)
X_tensor = X_tensor_tmp.to_dense().type(torch.FloatTensor)
dummy_tensor = torch.tensor([i for i in range(X.shape[0])], dtype=torch.int)
dataset = torch.utils.data.TensorDataset(X_tensor, dummy_tensor)
test_loader = DataLoader(dataset=dataset, batch_size=self.batch_size, shuffle=False)
# Load Best Model Weights
_, _, _ = self.load_checkpoint(self.best_model_path.format(self.desc))
# 2. Test Model
self.model.eval()
y_pred = []
with torch.no_grad():
for inputs, _ in test_loader:
inputs = inputs.to(device=self.device)
outputs = self.model(inputs)
_, prediction = torch.max(outputs, 1)
y_pred.extend(prediction.tolist())
return y_pred
def predict_proba(self, X_test):
# Convert the csr_matrix to numpy array to tensor to dataloader
X = X_test.tocoo()
X_tensor_tmp = torch.sparse_coo_tensor(indices=torch.LongTensor([X.row.tolist(), X.col.tolist()]),
values=torch.LongTensor(X.data.astype(np.int32)),
size=X.shape)
X_tensor = X_tensor_tmp.to_dense().type(torch.FloatTensor)
dataset = torch.utils.data.TensorDataset(X_tensor)
test_loader = DataLoader(dataset=dataset, batch_size=self.batch_size, shuffle=False)
# Load Best Model Weights
_, _, _ = self.load_checkpoint(self.best_model_path.format(self.desc))
# 2. Test Model
self.model.eval()
y_pred_proba = []
with torch.no_grad():
for inputs, in test_loader:
inputs = inputs.to(device=self.device)
outputs = self.model(inputs)
probabilities = torch.softmax(outputs, dim=1)
y_pred_proba.extend(probabilities.tolist())
return y_pred_proba
def save_checkpoint(self, is_best, state):
"""
is_best: is this the best checkpoint; min validation loss
state: the check point to save
"""
f_path = self.last_model_path.format(self.desc)
# save checkpoint data to the path given, checkpoint_path
torch.save(state, f_path)
# if it is the best model, min validation loss
if is_best:
best_fpath = self.best_model_path.format(self.desc)
# copy that checkpoint file the best path given, best_model_path
shutil.copyfile(f_path, best_fpath)
def load_checkpoint(self, checkpoint_fpath):
"""
Load a model and (optionally) an optimizer from a checkpoint.
Args:
- checkpoint_fpath (str): Path to the saved checkpoint.
- model (torch.nn.Module): Model to load the checkpoint into.
- optimizer (torch.optim.Optimizer, optional): Optimizer to load from the checkpoint. Default is None.
Returns:
- model, optimizer (if provided), epoch value
"""
# Check for the checkpoint file's existence
if not os.path.exists(checkpoint_fpath):
raise FileNotFoundError(f"No checkpoint found at '{checkpoint_fpath}'!")
# Load the checkpoint
checkpoint = torch.load(checkpoint_fpath,
map_location=lambda storage, loc: storage) # Ensure the loading is device-agnostic
# Validate the checkpoint keys
if 'state_dict' not in checkpoint:
raise KeyError("No state_dict found in checkpoint file!")
if self.optimizer and 'optimizer' not in checkpoint:
raise KeyError("No optimizer state found in checkpoint file!")
# Load model state
self.model.load_state_dict(checkpoint['state_dict'])
# If an optimizer is provided, and its state is saved, load it
if self.optimizer:
self.optimizer.load_state_dict(checkpoint['optimizer'])
# Get epoch number if it's available, else return None
epoch = checkpoint.get('epoch', None)
return self.model, self.optimizer, epoch