Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Munkhtenger19 committed May 21, 2024
1 parent 97c55b0 commit beb298f
Show file tree
Hide file tree
Showing 42 changed files with 570 additions and 361 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
**/mlruns
**/mlartifacts
**datasets
!gnn_toolbox/custom_modules/datasets
!gnn_toolbox/custom_components/datasets
*.ckpt
*.log
*.json
Expand Down
68 changes: 37 additions & 31 deletions PyG_to_sci_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,54 +305,60 @@ def to_symmetric_scipy(adjacency: sp.csr_matrix):
def accuracy(pred, y, mask):
return (pred.argmax(-1)[mask] == y[mask]).float().mean()
# print(data9.is_directed())
dataset2= PygNodePropPredDataset(name='ogbn-arxiv', root='./datasets', transform=T.ToSparseTensor(remove_edge_index=False))
# dataset2= PygNodePropPredDataset(name='ogbn-arxiv', root='./datasets', transform=T.ToSparseTensor(remove_edge_index=False))
dataset1= Planetoid(name = 'cora', root = './datasets')
data = dataset1[0]
train=data.train_mask.nonzero().squeeze()
model1= GCN(in_channels=dataset1.num_features, out_channels=dataset1.num_classes, hidden_channels=32, num_layers=2)
logits = model1(data.x, data.edge_index)
# print(accuracy(logits, data.y, train))
# print(accuracy(logits, data.y, data.train_mask))
# from torch_geometric.datasets import PPI
# dataset1 = PPI(root='./datasets', transform=T.ToSparseTensor(remove_edge_index=False))
from torch_geometric.datasets import PPI, KarateClub
dataset9 = QM9(root = './datasets', transform=T.ToSparseTensor(remove_edge_index=False))
data12 = dataset9[0]
print(data12)

# print('pygnodepred', dataset1)
# we = dataset1[0]
# print('we', we)

# dataset2= Planetoid(name = 'citeseer', root = './datasets')
# dataset2= QM9(root = './datasets')
data2 = dataset2[0]
# train_mask=data2.train_mask.nonzero().squeeze()
# attr, adj, labels, splits, n = prep_graph(dataset2, make_undirected=False)
# print()
from torch_geometric.utils import to_undirected, is_undirected
print('is_undirected', is_undirected(data2.edge_index, num_nodes=data2.num_nodes))
adj = SparseTensor(row=data2.edge_index[0], col=data2.edge_index[1], value=data2.edge_attr).to('cuda')
adj2 = SparseTensor(row=data2.edge_index[0], col=data2.edge_index[1], value=data2.edge_weight if data2.edge_weight is not None else torch.ones(data2.edge_index.size(1))).to('cuda')
print('num_edge',adj.nnz())
print('what', adj)
print('what2', adj.t())

row, col, edge_attr = adj.t().coo()
edge_index = torch.stack([row, col], dim=0)
print('edge_index3', edge_index)

# row, col, edge_attr = adj.coo()
# data2 = dataset2[0]
# # train_mask=data2.train_mask.nonzero().squeeze()
# # attr, adj, labels, splits, n = prep_graph(dataset2, make_undirected=False)
# # print()
# from torch_geometric.utils import to_undirected, is_undirected
# print('is_undirected', is_undirected(data2.edge_index, num_nodes=data2.num_nodes))
# adj = SparseTensor(row=data2.edge_index[0], col=data2.edge_index[1], value=data2.edge_attr).to('cuda')
# adj2 = SparseTensor(row=data2.edge_index[0], col=data2.edge_index[1], value=data2.edge_weight if data2.edge_weight is not None else torch.ones(data2.edge_index.size(1))).to('cuda')
# print('num_edge',adj.nnz())
# print('what', adj)
# print('what2', adj.t())

# row, col, edge_attr = adj.t().coo()
# edge_index = torch.stack([row, col], dim=0)
# print('edge_index1', edge_index)
# print('edge_index3', edge_index)
# print('edge_attr', edge_attr)
# # row, col, edge_attr = adj.coo()
# # edge_index = torch.stack([row, col], dim=0)
# # print('edge_index1', edge_index)


# edge_index = to_undirected(data2.edge_index)
# print('x',edge_index.shape[1])
# print('undirected', edge_index)
# edge_index2 = to_undirected(edge_index)
# print('undirected', edge_index)
# print('q',torch.equal(edge_index, edge_index2))
# print(torch.equal(data2.edge_index, edge_index))
# print("numm", data2.num_nodes)

# print("numm2", data2.x.shape[0])
# print("numm2", data2.x.size(0))

edge_index = to_undirected(data2.edge_index)
print('x',edge_index.shape[1])
print('undirected', edge_index)
edge_index2 = to_undirected(edge_index)
print('undirected', edge_index)
print('q',torch.equal(edge_index, edge_index2))
print(torch.equal(data2.edge_index, edge_index))
print("numm", data2.num_nodes)
# print(T.ToSparseTensor.__name__)

print("numm2", data2.x.shape[0])
print("numm2", data2.x.size(0))
# row, col, edge_attr = adj.coo()
# cora_edge_index = torch.stack([row, col], dim=0)
# print('edge_attr', edge_attr)
Expand Down
27 changes: 16 additions & 11 deletions configs/default_experiment.yaml
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
output_dir: ./output6
output_dir: ./output8
# resume_output: False
# csv_save: True
# esd: True
experiment_templates:
- name: My experiment
- name: Local attack_3
seed: [0]
device: cuda
model:
- name: GCN
params:
hidden_channels: 64
dataset:
name: Cora # ogbn-arxiv
name: [QM9] # ogbn-arxiv
root: ./datasets
make_undirected: true
# transforms:
# - name: ToUndirected
# - name: NormalizeFeatures
# - name: Constant
# params:
# value: 0.8

attack:
# - scope: global
# name: DICE
# type: poison
# epsilon: [0.5]
- scope: 'global'
name: DICE
type: evasion
epsilon: [0.5]
# - scope: global
# name: FGSM
# type: poison
Expand All @@ -33,10 +34,14 @@ experiment_templates:
# name: PGD
# type: poison
# epsilon: [0.5]
- scope: global
name: [DICE]
type: evasion
epsilon: [0.6]

# - scope: local
# name: [LocalDICE]
# type: evasion
# epsilon: [0.6]
# nodes_topk: 10
# min_node_degree: 2

# params:
# block_size: 100000
# - scope: local
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from torch_sparse import SparseTensor
from tqdm import tqdm

import gnn_toolbox.custom_components.utils as utils
from gnn_toolbox.custom_components.attacks.base_attack import GlobalAttack
import custom_components.utils as utils
from custom_components.attacks.base_attack import GlobalAttack
from gnn_toolbox.registry import register_global_attack

@register_global_attack("DICE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import torch_sparse
from torch_sparse import SparseTensor

from gnn_toolbox.custom_components import utils
from gnn_toolbox.custom_components.attacks.global_attacks.prbcd import PRBCD
from custom_components import utils
from custom_components.attacks.global_attacks.prbcd import PRBCD
from gnn_toolbox.registry import register_global_attack

@register_global_attack("GreedyRBCD")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import scipy.sparse as sp

from torch_geometric.utils import coalesce, to_undirected
from gnn_toolbox.custom_components.attacks.base_attack import GlobalAttack
from custom_components.attacks.base_attack import GlobalAttack
from gnn_toolbox.registry import register_global_attack
# (predictions, labels, ids/mask) -> Tensor with one element
LOSS_TYPE = Callable[[Tensor, Tensor, Optional[Tensor]], Tensor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
from torch_sparse import SparseTensor

# from rgnn_at_scale.models import MODEL_TYPE
from gnn_toolbox.custom_components import utils
from gnn_toolbox.custom_components.attacks.base_attack import GlobalAttack
from custom_components import utils
from custom_components.attacks.base_attack import GlobalAttack
from gnn_toolbox.registry import register_global_attack

@register_global_attack("PRBCD")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import torch
from torch_sparse import SparseTensor

from gnn_toolbox.custom_components.attacks.base_attack import LocalAttack
from custom_components.attacks.base_attack import LocalAttack
from gnn_toolbox.registry import register_local_attack

@register_local_attack("LocalDICE")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

from tqdm import tqdm

from gnn_toolbox.custom_components.attacks.base_attack import LocalAttack
from gnn_toolbox.custom_components import utils
from custom_components.attacks.base_attack import LocalAttack
from custom_components import utils
from gnn_toolbox.registry import register_local_attack


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
AddSelfLoops,
Constant,
NormalizeFeatures,
ToSparseTensor,
)
from gnn_toolbox.registry import register_transform

register_transform("AddSelfLoops", AddSelfLoops)
register_transform("Constant", Constant)
register_transform("NormalizeFeatures", NormalizeFeatures)
register_transform("ToSparseTensor", ToSparseTensor(remove_edge_index=False))
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from torch_geometric.nn.conv.gcn_conv import gcn_norm
# from gnn_toolbox.custom_modules.models.model
# import gnn_toolbox
from gnn_toolbox.registry import register_model, registry
from gnn_toolbox.registry import register_model
import os
from typing import Any, Dict, Union



@register_model("GCN")
class GCNWH(nn.Module):
def __init__(self, in_channels: int, out_channels: int, hidden_channels: int, num_layers: int = 2, dropout: float = 0.5, **kwargs):
Expand All @@ -32,7 +30,7 @@ def forward(self, x, edge_index, edge_weight, **kwargs):



@register_model('gcn2')
# @register_model('gcn2')
def GCN_(in_channels, out_channels, hidden_channels):
return GCN(in_channels, out_channels, hidden_channels)

Expand All @@ -51,7 +49,7 @@ def reset_parameters(self):
self.conv1.reset_parameters()
self.conv2.reset_parameters()

def forward(self, x, edge_index, edge_weight=None, **kwargs):
def forward(self, x, edge_index, edge_weight=None):
# Normalize edge indices only once:
if not kwargs.get('skip_norm', False):
edge_index, edge_weight = self.norm(
Expand Down Expand Up @@ -115,7 +113,7 @@ def forward(self, arguments: Tuple[torch.Tensor, torch.Tensor]) -> torch.Tensor:

import collections
from torch_sparse import coalesce, SparseTensor
@register_model('DenseGCN')
# @register_model('DenseGCN')
class DenseGCN(nn.Module):
"""Dense two layer GCN for the FGSM attack that requires a gradient towards the adjacency matrix.
"""
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pytorch_lightning import LightningModule

# from gnn_toolbox.old.config_def import cfg
from gnn_toolbox.custom_components.optimizers.optimizers import register_optimizer
# from custom_components.optimizers.optimizers import register_optimizer
from gnn_toolbox.registry import registry

class BaseModel(LightningModule):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from torch.optim import Adam
from torch.nn.parameter import Parameter
from ...registry import register_optimizer
from gnn_toolbox.registry import register_optimizer
from typing import Iterator, Dict

@register_optimizer('adam')
Expand Down
File renamed without changes.
16 changes: 13 additions & 3 deletions gnn_toolbox/cmd_args.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import os
import argparse
import logging

from custom_components import *
from gnn_toolbox.registry import registry
def parse_args() -> argparse.Namespace:
"""Parses the command line arguments."""
parser = argparse.ArgumentParser(description='Run experiments from a configuration file.')

parser = argparse.ArgumentParser(description='GNN Robustness Toolbox: Run experiments to test GNNs against adversarial attacks from configuration YAML file.')
# parser = argparse.ArgumentParser(description=f'available models {registry["model"].keys()} \n available global attacks {registry["global_attack"].keys()} \n available local attacks {registry["local_attack"].keys()} \n available datasets {registry["dataset"].keys()} \n available transforms {registry["transform"].keys()} \n available optimizers {registry["optimizer"].keys()} \n available losses {registry["loss"].keys()}')
parser.add_argument('--cfg', type=str, default= os.path.join('configs', 'default_experiment.yaml'), help='The configuration YAML file path. Default is configs/good_1.yaml.')

parser.add_argument('--log', type=str, default='INFO', help='Logging level. Choose from DEBUG, INFO, WARNING, ERROR, CRITICAL.')

parser.add_argument('--list-components', action='store_true', help="List registered components and exit")

return parser.parse_args()

def list_registered_components():
"""
List all registered components
"""
for key, value in registry.items():
print(f"Registered {key}: {list(value.keys())}")

def logger_setup(logging_level):
"""
Setup the logger for the experiment. Modified from https://docs.python.org/3/howto/logging.html
Expand Down
Loading

0 comments on commit beb298f

Please sign in to comment.