Skip to content

Commit

Permalink
modified files
Browse files Browse the repository at this point in the history
  • Loading branch information
akashtadwai committed Jul 13, 2021
1 parent cd79e7c commit 5e09b37
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 43 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ venv/
ENV/
env.bak/
venv.bak/


IDS17/archive
IDS17/data
3 changes: 1 addition & 2 deletions IDS17/CNN1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
from avalanche.logging import InteractiveLogger, TensorboardLogger, TextLogger
from avalanche.models import SimpleMLP
from avalanche.training.plugins import EvaluationPlugin
from avalanche.training.strategies import GEM
from avalanche.training.strategies import GEM,EWC
from sklearn.manifold import TSNE
from sklearn.model_selection import StratifiedShuffleSplit, train_test_split
from torch.nn import CrossEntropyLoss
from torch.optim import Adam
from torch.utils.data import Dataset, Subset
from torchsummary import summary
from torchvision import transforms
import sys

Expand Down
50 changes: 29 additions & 21 deletions IDS17/simpleMLP.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import torch
import torch.nn as nn
import torchvision
from avalanche.benchmarks.generators import tensor_scenario
from avalanche.benchmarks.generators import tensors_benchmark
from avalanche.evaluation.metrics import (
ExperienceForgetting,
StreamConfusionMatrix,
Expand Down Expand Up @@ -129,11 +129,13 @@ def get_args(argv):
temp.loc[:, "70"] = 1
test_label_dict["cat" + str(i)] = temp

train_data_x = list(torch.Tensor(train_dict[key].to_numpy()) for key in train_dict)
train_data_x = list(torch.Tensor(
train_dict[key].to_numpy()) for key in train_dict)
train_data_y = list(
torch.Tensor(train_label_dict[key].to_numpy()) for key in train_label_dict
)
test_data_x = list(torch.Tensor(test_dict[key].to_numpy()) for key in test_dict)
test_data_x = list(torch.Tensor(
test_dict[key].to_numpy()) for key in test_dict)
test_data_y = list(
torch.Tensor(test_label_dict[key].to_numpy()) for key in test_label_dict
)
Expand Down Expand Up @@ -166,8 +168,8 @@ def task_ordering(perm: dict):
"""
final_train_data_x = []
final_train_data_y = []
final_test_data_x = torch.Tensor([])
final_test_data_y = torch.Tensor([])
final_test_data_x = []
final_test_data_y = []

for key, values in perm.items():
temp_train_data_x = torch.Tensor([])
Expand All @@ -176,15 +178,20 @@ def task_ordering(perm: dict):
temp_test_data_y = torch.Tensor([])

for value in values:
temp_train_data_x = torch.cat([temp_train_data_x, train_data_x[value]])
temp_train_data_y = torch.cat([temp_train_data_y, train_data_y[value]])
temp_test_data_x = torch.cat([temp_test_data_x, test_data_x[value]])
temp_test_data_y = torch.cat([temp_test_data_y, test_data_y[value]])
temp_train_data_x = torch.cat(
[temp_train_data_x, train_data_x[value]])
temp_train_data_y = torch.cat(
[temp_train_data_y, train_data_y[value]])
temp_test_data_x = torch.cat(
[temp_test_data_x, test_data_x[value]])
temp_test_data_y = torch.cat(
[temp_test_data_y, test_data_y[value]])

final_train_data_x.append(temp_train_data_x)
final_train_data_y.append(temp_train_data_y)
final_test_data_x = torch.cat([final_test_data_x, temp_test_data_x])
final_test_data_y = torch.cat([final_test_data_y, temp_test_data_y])
final_test_data_x.append(temp_test_data_x)
final_test_data_y.append(temp_test_data_y)

return final_train_data_x, final_train_data_y, final_test_data_x, final_test_data_y


Expand Down Expand Up @@ -238,12 +245,10 @@ def task_ordering(perm: dict):
for task_order in range(len(task_order_list)):
print("Current task order processing ", task_order + 1)
dataset = task_ordering(task_order_list[task_order])

generic_scenario = tensor_scenario(
train_data_x=dataset[0],
train_data_y=dataset[1],
test_data_x=dataset[2],
test_data_y=dataset[3],
print('Len of datasets: ', dataset[0].shape, dataset[2].shape)
generic_scenario = tensors_benchmark(
train_tensors=[(x, y) for x, y in zip(dataset[0], dataset[1])],
test_tensors=[(x, y) for x, y in zip(dataset[2], dataset[3])],
task_labels=[
0 for key in task_order_list[task_order].keys()
], # shouldn't provide task ID for inference
Expand All @@ -256,20 +261,23 @@ def task_ordering(perm: dict):

# log to text file
text_logger = TextLogger(
open(f"./logs/{cur_time}_simple_mlp_task_0_in_task{task_order}.txt", "w+")
open(
f"./logs/{cur_time}_simple_mlp_task_0_in_task{task_order}.txt", "w+")
)

# print to stdout
interactive_logger = InteractiveLogger()

eval_plugin = EvaluationPlugin(
accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True),
accuracy_metrics(minibatch=True, epoch=True,
experience=True, stream=True),
loss_metrics(minibatch=True, epoch=True, experience=True, stream=True),
timing_metrics(epoch=True, epoch_running=True),
ExperienceForgetting(),
cpu_usage_metrics(experience=True),
StreamConfusionMatrix(num_classes=2, save_image=False),
disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True),
disk_usage_metrics(minibatch=True, epoch=True,
experience=True, stream=True),
loggers=[interactive_logger, text_logger, tb_logger],
)

Expand Down Expand Up @@ -301,7 +309,7 @@ def task_ordering(perm: dict):
# TRAINING LOOP
print("Starting experiment...")
os.makedirs(
os.path.join("weights", f"SimpleMLP_0_in_task_{task_order+1}", exist_ok=True)
os.path.join("weights", f"SimpleMLP_0_in_task_{task_order+1}"), exist_ok=True
)

results = []
Expand Down
2 changes: 1 addition & 1 deletion IDS17/tSNE.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pickle
import warnings
from datetime import datetime
from os import path
import os
from typing import Callable, Dict, Iterable

import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion IDS18/simpleCNN_ClassInc.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def forward(self, x):
print("Starting experiment...")

os.makedirs(
os.path.join("weights", f"CNN2D_ClassInc_0inTask{task_order+1}", exist_ok=True)
os.path.join("weights", f"CNN2D_ClassInc_0inTask{task_order+1}"), exist_ok=True
)

results = []
Expand Down
2 changes: 1 addition & 1 deletion IDS18/simpleCNN_Domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def forward(self, x):
# TRAINING LOOP
print("Starting experiment...")

os.makedirs(os.path.join("weights", f"simpleCNN_Domain", exist_ok=True))
os.makedirs(os.path.join("weights", f"simpleCNN_Domain"), exist_ok=True)

results = []

Expand Down
22 changes: 11 additions & 11 deletions IDS18/simpleMLP_ClassInc.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def get_args(argv):
pickle.dump(test_data_y, f)


def task_ordering(perm: dict):
def task_ordering(perm):
"""Divides Data into tasks based on the given permutation order
Parameters
Expand All @@ -173,8 +173,8 @@ def task_ordering(perm: dict):
"""
final_train_data_x = []
final_train_data_y = []
final_test_data_x = torch.Tensor([])
final_test_data_y = torch.Tensor([])
final_test_data_x = []
final_test_data_y = []

for key, values in perm.items():
temp_train_data_x = torch.Tensor([])
Expand All @@ -183,17 +183,17 @@ def task_ordering(perm: dict):
temp_test_data_y = torch.Tensor([])

for value in values:
temp_train_data_x = torch.cat([temp_train_data_x, train_data_x[value]])
temp_train_data_y = torch.cat([temp_train_data_y, train_data_y[value]])
temp_test_data_x = torch.cat([temp_test_data_x, test_data_x[value]])
temp_test_data_y = torch.cat([temp_test_data_y, test_data_y[value]])
temp_train_data_x = torch.cat([temp_train_data_x, train_data_x[value - 1]])
temp_train_data_y = torch.cat([temp_train_data_y, train_data_y[value - 1]])
temp_test_data_x = torch.cat([temp_test_data_x, test_data_x[value - 1]])
temp_test_data_y = torch.cat([temp_test_data_y, test_data_y[value - 1]])

final_train_data_x.append(temp_train_data_x)
final_train_data_y.append(temp_train_data_y)
final_test_data_x = torch.cat([final_test_data_x, temp_test_data_x])
final_test_data_y = torch.cat([final_test_data_y, temp_test_data_y])
return final_train_data_x, final_train_data_y, final_test_data_x, final_test_data_y
final_test_data_x.append(temp_test_data_x)
final_test_data_y.append(temp_test_data_y)

return final_train_data_x, final_train_data_y, final_test_data_x, final_test_data_y

# Model Creation
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
Expand Down Expand Up @@ -308,7 +308,7 @@ def task_ordering(perm: dict):
# TRAINING LOOP
print("Starting experiment...")
os.makedirs(
os.path.join("weights", f"SimpleMLP_0_in_task_{task_order+1}", exist_ok=True)
os.path.join("weights", f"SimpleMLP_0_in_task_{task_order+1}"), exist_ok=True
)

results = []
Expand Down
2 changes: 1 addition & 1 deletion IDS18/simpleMLP_Domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def task_ordering(perm):
# TRAINING LOOP
print("Starting experiment...")

os.makedirs(os.path.join("weights", f"simpleMLP_Domain", exist_ok=True))
os.makedirs(os.path.join("weights", f"simpleMLP_Domain"), exist_ok=True)

results = []

Expand Down
10 changes: 5 additions & 5 deletions KDDCup99/SimpleMLP.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
cache_label = "./data/train_data_x.pth"
# Normal = 0
# Attack = 1
if path.exists(cache_label):
if os.path.exists(cache_label):

with open("./data/train_data_x.pth", "rb") as f:
train_data_x = pickle.load(f)
Expand Down Expand Up @@ -178,17 +178,17 @@ def task_ordering(perm):
"5": [3, 4, 5, 16],
}

task_order_list = [perm1]
task_order_list = [perm]

dataset = task_ordering(task_order_list[task_order])
dataset = task_ordering(task_order_list[0])

generic_scenario = tensor_scenario(
train_data_x=dataset[0],
train_data_y=dataset[1],
test_data_x=dataset[2],
test_data_y=dataset[3],
task_labels=[
0 for key in task_order_list[task_order].keys()
0 for key in task_order_list[0].keys()
], # shouldn't provide task ID for inference
)

Expand Down Expand Up @@ -227,7 +227,7 @@ def task_ordering(perm):
# TRAINING LOOP
print("Starting experiment...")

os.makedirs(os.path.join("weights", f"SimpleMLP", exist_ok=True))
os.makedirs(os.path.join("weights", f"SimpleMLP"), exist_ok=True)

results = []
i = 1
Expand Down

0 comments on commit 5e09b37

Please sign in to comment.