-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddos_metrics_MLP.py
207 lines (167 loc) · 6.78 KB
/
ddos_metrics_MLP.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
import torch
from torch.utils.data import Dataset, DataLoader
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import torch.nn as nn
from torchmetrics import Precision, Recall, F1Score
import time
# Define constants
BATCH_SIZE = 1000
# Determine device
device = "cuda" if torch.cuda.is_available() else "cpu"
class MinMaxTransform:
def __init__(self):
# Initialize the MinMaxScaler
self.scaler = MinMaxScaler(feature_range=(0, 1))
def fit(self, data):
# Fit the scaler on the data
self.scaler.fit(data)
def __call__(self, sample):
# Make sure that we run the transform with the features loaded
feature_names = self.scaler.feature_names_in_
sample_df = pd.DataFrame(sample.reshape(1, -1), columns=feature_names)
transformed_sample = self.scaler.transform(sample_df)
return transformed_sample.flatten()
class mlpDataset(Dataset):
def __init__(self, attack_file, benign_file, transform=None):
# Load data from files
self.attack_data = pd.read_csv(attack_file)
self.benign_data = pd.read_csv(benign_file)
# For binary classification, modify the labels to be integers
self.benign_data.iloc[:, -1] = 0
self.attack_data.iloc[:, -1] = 1
# Combine the datasets
self.data = pd.concat([self.attack_data, self.benign_data], ignore_index=True)
# Store transform
self.transform = transform
def __len__(self):
# Return the length of the data
return len(self.data)
def __getitem__(self, idx):
# Convert to list just incase
if torch.is_tensor(idx):
idx = idx.tolist()
# Grab last column as label
sample = self.data.iloc[idx, :-1].values.astype("float")
label = self.data.iloc[idx, -1]
# Transform the samples if present
if self.transform:
sample = self.transform(sample)
# Return the samples and labels as torch tensors
return torch.tensor(sample), torch.tensor(label)
class mlp(nn.Module):
def __init__(self):
super(mlp, self).__init__()
# 79 input features
self.layer1 = nn.Linear(79, 128)
self.relu = nn.ReLU()
self.layer2 = nn.Linear(128, 64)
self.output_layer = nn.Linear(64, 1)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.relu(self.layer1(x))
x = self.relu(self.layer2(x))
x = self.sigmoid(self.output_layer(x))
return x
def test(model, test_loader, criterion):
# Set the model to evaluation mode, save variables for tracking metrics
model.eval()
running_loss = 0.0
correct = 0
total = 0
total_time = 0.0
# Init metrics
precision = Precision(task="binary").to(device)
recall = Recall(task="binary").to(device)
f1 = F1Score(task="binary").to(device)
# Disable gradient updates, loop over test loader
with torch.no_grad():
for data, labels in test_loader:
# Ensure labels are in the correct shape
data, labels = data.float().to(device), labels.float().unsqueeze(1).to(
device
)
# Forward pass on batch
start_time = time.perf_counter()
outputs = model(data)
end_time = time.perf_counter()
total_time += (end_time - start_time)
loss = criterion(outputs, labels)
# Calculate loss and accuracy, store correct predictions
running_loss += loss.item()
predicted = outputs.round()
# Update metrics
precision.update(predicted, labels.int())
recall.update(predicted, labels.int())
f1.update(predicted, labels.int())
# Calc total and predicted
total += labels.size(0)
correct += (predicted == labels).sum().item()
# Calc metrics
precision_val = precision.compute().item()
recall_val = recall.compute().item()
f1_val = f1.compute().item()
avg_time_per_batch = (total_time / len(test_loader)) * 1000
avg_time_per_sample = (total_time / total) * 1000
# Return the average loss and accuracy, in addition to total and correct
avg_loss = running_loss / len(test_loader)
accuracy = 100 * correct / total
return avg_loss, accuracy, correct, total, precision_val, recall_val, f1_val, avg_time_per_batch, avg_time_per_sample
def main():
# Define file paths
train_attack = (
"/s/bach/b/class/cs535/cs535b/binaryclassificationdataset/train_attack.csv"
)
train_benign = (
"/s/bach/b/class/cs535/cs535b/binaryclassificationdataset/train_benign.csv"
)
test_attack = (
"/s/bach/b/class/cs535/cs535b/binaryclassificationdataset/test_attack.csv"
)
test_benign = (
"/s/bach/b/class/cs535/cs535b/binaryclassificationdataset/test_benign.csv"
)
# Load train data for calculating normalization
temp_attack_data = pd.read_csv(train_attack)
temp_benign_data = pd.read_csv(train_benign)
temp_train_data = pd.concat([temp_attack_data, temp_benign_data], ignore_index=True)
# Initialize transform based on the entire train dataset
min_max_transform = MinMaxTransform()
min_max_transform.fit(temp_train_data.iloc[:, :-1])
# Delete the temp data used to find the transform
del temp_attack_data, temp_benign_data, temp_train_data
# Create dataset instance
test_dataset = mlpDataset(
test_attack, test_benign, transform=min_max_transform
)
# Create DataLoader
sensitivity_loader = DataLoader(
test_dataset,
batch_size=BATCH_SIZE,
shuffle=False,
num_workers=4,
)
# Setup model, loss function, and optimizer, load model
model = mlp().to(device)
state_dict = torch.load(
"/s/bach/b/class/cs535/cs535b/ddos-classification/MLP_model",
map_location=torch.device(device),
)
new_state_dict = {
key.replace("module.", ""): value for key, value in state_dict.items()
}
model.load_state_dict(new_state_dict)
criterion = nn.BCELoss()
# Test the model and print loss and accuracy, as well as correct guesses, total guesses, and the tested sample type
test_loss, test_accuracy, test_correct, test_total, precision, recall, f1, avg_time_per_batch, avg_time_per_sample = test(
model, sensitivity_loader, criterion
)
print(
f"Test Loss: {test_loss:.4f}, Test Accuracy: {test_accuracy:.2f}%, Test Correct: {test_correct}, Test Total: {test_total}"
)
print(f"Precision: {precision:.4f}, Recall: {recall:.4f}, F1 Score: {f1:.4f}")
print(f"Average miliseconds per batch: {avg_time_per_batch:.10f}, Average miliseconds per sample: {avg_time_per_sample:.10f}")
# Run main function
if __name__ == "__main__":
print("Starting metrics analysis...")
main()