Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metattack #18

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 72 additions & 5 deletions experiments/attack_defense_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,75 @@ def test_attack_defense():
gen_dataset=dataset, metrics=[Metric("F1", mask='test', average='macro')])
print(metric_loc)

def test_meta():
from attacks.poison_attacks_collection.metattack import meta_gradient_attack
my_device = device('cpu')
full_name = ("single-graph", "Planetoid", 'Cora')

dataset, data, results_dataset_path = DatasetManager.get_by_full_name(
full_name=full_name,
dataset_ver_ind=0
)
gnn = model_configs_zoo(dataset=dataset, model_name='gcn_gcn')
manager_config = ConfigPattern(
_config_class="ModelManagerConfig",
_config_kwargs={
"mask_features": [],
"optimizer": {
# "_config_class": "Config",
"_class_name": "Adam",
# "_import_path": OPTIMIZERS_PARAMETERS_PATH,
# "_class_import_info": ["torch.optim"],
"_config_kwargs": {},
}
}
)
steps_epochs = 200
gnn_model_manager = FrameworkGNNModelManager(
gnn=gnn,
dataset_path=results_dataset_path,
manager_config=manager_config,
modification=ModelModificationConfig(model_ver_ind=0, epochs=steps_epochs)
)
save_model_flag = False
gnn_model_manager.gnn.to(my_device)
data = data.to(my_device)

poison_attack_config = ConfigPattern(
_class_name="MetaAttackApprox",
_import_path=POISON_ATTACK_PARAMETERS_PATH,
_config_class="PoisonAttackConfig",
_config_kwargs={
"num_nodes": dataset.dataset.x.shape[0]
}
)
gnn_model_manager.set_poison_attacker(poison_attack_config=poison_attack_config)

warnings.warn("Start training")
dataset.train_test_split(percent_train_class=0.1)

try:
raise FileNotFoundError()
# gnn_model_manager.load_model_executor()
except FileNotFoundError:
gnn_model_manager.epochs = gnn_model_manager.modification.epochs = 0
train_test_split_path = gnn_model_manager.train_model(gen_dataset=dataset, steps=steps_epochs,
save_model_flag=save_model_flag,
metrics=[Metric("F1", mask='train', average=None)])

if train_test_split_path is not None:
dataset.save_train_test_mask(train_test_split_path)
train_mask, val_mask, test_mask, train_test_sizes = torch.load(train_test_split_path / 'train_test_split')[
:]
dataset.train_mask, dataset.val_mask, dataset.test_mask = train_mask, val_mask, test_mask
data.percent_train_class, data.percent_test_class = train_test_sizes

warnings.warn("Training was successful")

metric_loc = gnn_model_manager.evaluate_model(
gen_dataset=dataset, metrics=[Metric("F1", mask='test', average='macro'),
Metric("Accuracy", mask='test')])
print(metric_loc)

def test_nettack_evasion():
my_device = device('cpu')
Expand Down Expand Up @@ -276,8 +345,6 @@ def test_nettack_evasion():


if __name__ == '__main__':
# test_attack_defense()
test_nettack_evasion()



#test_attack_defense()
torch.manual_seed(5000)
test_meta()
Empty file added experiments/metattack_exp.py
Empty file.
Binary file removed experiments/test_dataset_api/processed/pre_filter.pt
Binary file not shown.
Binary file removed experiments/test_dataset_api/processed/pre_transform.pt
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
13 changes: 12 additions & 1 deletion metainfo/poison_attack_parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@
},
"RandomPoisonAttack": {
"n_edges_percent": ["n_edges_percent", "float", 0.1, {"min": 0.0001, "step": 0.01}, "?"]
},
"MetaAttackFull":{
"lambda_": ["Lambda", "float", 0.5, {"min": 0, "max": 1, "step": 0.05}, "lambda coef - paper"],
"train_iters": ["Train iters (surrogate)", "int", 200, {"min": 0, "step": 1}, "Trainig iterations for surrogate model"],
"attack_structure": ["Attack structure", "bool", true, {}, "whether change graph structure with attack or not"],
"attack_features": ["Attack features", "bool", false, {}, "whether change node features with attack or not"]
},
"MetaAttackApprox":{
"lambda_": ["Lambda", "float", 0.5, {"min": 0, "max": 1, "step": 0.05}, "lambda coef - paper"],
"train_iters": ["Train iters (surrogate)", "int", 200, {"min": 0, "step": 1}, "Trainig iterations for surrogate model"],
"attack_structure": ["Attack structure", "bool", true, {}, "whether change graph structure with attack or not"],
"attack_features": ["Attack features", "bool", false, {}, "whether change node features with attack or not"]
}

}

20 changes: 20 additions & 0 deletions src/attacks/poison_attacks.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import numpy as np
import importlib
import torch

from attacks.attack_base import Attacker
from pathlib import Path

POISON_ATTACKS_DIR = Path(__file__).parent.resolve() / 'poison_attacks_collection'

class PoisonAttacker(Attacker):
def __init__(self, **kwargs):
Expand Down Expand Up @@ -42,3 +45,20 @@ def attack(self, gen_dataset):

def attack_diff(self):
return self.attack_diff

class EmptyPoisonAttacker(PoisonAttacker):
name = "EmptyPoisonAttacker"

def attack(self, **kwargs):
pass

# for attack_name in POISON_ATTACKS_DIR.rglob("*_attack.py"):
# try:
# importlib.import_module(str(attack_name))
# except ImportError:
# print(f"Couldn't import Attack: {attack_name}")

# import attacks.poison_attacks_collection.metattack.meta_gradient_attack

# # TODO this is not best practice to import this thing here this way
# from attacks.poison_attacks_collection.metattack.meta_gradient_attack import BaseMeta
Loading
Loading