### Setup and Step Function for NodeInjectionEnv Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/rl/nipa_env.html Defines the setup and step methods for the NodeInjectionEnv. The setup method initializes environment variables for tracking steps and rewards. The step method processes actions, specifically handling the selection of the first and second nodes for edge modifications in a three-step action sequence, and updates the environment state. ```python def init_overall_steps(self): self.overall_steps = 0 self.modified_list = [] for i in range(self.parallel_size): self.modified_list.append(ModifiedGraph()) def setup(self): self.n_steps = 0 self.first_nodes = None self.second_nodes = None self.rewards = None self.binary_rewards = None self.list_acc_of_all = [] def step(self, actions, inference=False): ''' run actions and get reward ' ** if self.first_nodes is None: # pick the first node of edge assert (self.n_steps + 1) % 3 == 1 self.first_nodes = actions[:] if (self.n_steps + 1) % 3 == 2: self.second_nodes = actions[:] for i in range(self.parallel_size): ``` -------------------------------- ### NodeAttackEnv: Initialization and Setup Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/rl/rl_s2v_env.html Initializes the NodeAttackEnv with graph features, labels, target nodes, action space, and a classifier. The setup method prepares the environment for attack simulation by initializing target nodes, step counters, and lists for modified graphs and accuracy. ```python class NodeAttackEnv(object): """Node attack environment. It executes an action and then change the environment status (modify the graph). """ def __init__(self, features, labels, all_targets, list_action_space, classifier, num_mod=1, reward_type='binary'): self.classifier = classifier self.list_action_space = list_action_space self.features = features self.labels = labels self.all_targets = all_targets self.num_mod = num_mod self.reward_type = reward_type def setup(self, target_nodes): self.target_nodes = target_nodes self.n_steps = 0 self.first_nodes = None self.rewards = None self.binary_rewards = None self.modified_list = [] for i in range(len(self.target_nodes)): self.modified_list.append(ModifiedGraph()) self.list_acc_of_all = [] ``` -------------------------------- ### NodeAttackEnv Setup Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/rl/rl_s2v_env.html Sets up the environment for a given set of target nodes. ```APIDOC ## NodeAttackEnv Setup ### Description Sets up the environment for a given set of target nodes, initializing internal states for tracking steps, rewards, and graph modifications. ### Method setup ### Parameters - **target_nodes** (list) - A list of nodes to be targeted in the attack. ``` -------------------------------- ### Initialize IGAttack for Graph Data Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.targeted_attack.html Shows the setup process for the IGAttack class, which uses Integrated Gradients to perform adversarial attacks on graph neural networks. ```python from deeprobust.graph.data import Dataset from deeprobust.graph.defense import GCN from deeprobust.graph.targeted_attack import IGAttack data = Dataset(root='/tmp/', name='cora') adj, features, labels = data.adj, data.features, data.labels idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test # Setup Surrogate model surrogate = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1, nhid=16, dropout=0, with_relu=False, with_bias=False, device='cpu').to('cpu') ``` -------------------------------- ### Image Defense Example using PGD Training Source: https://github.com/dse-msu/deeprobust/blob/master/docs/image/example.md This example shows how to implement a defense mechanism against adversarial attacks using PGD training for a model on the MNIST dataset. It involves setting up the model, data loaders for training and testing, and initializing the PGD training defense. ```python from deeprobust.image.defense.pgd_training import PGDtraining from deeprobust.image.config import defense_params from torchvision import datasets, transforms import torch # Assuming Net() is a defined model class # from deeprobust.image.netmodels.cnn import Net # model = Net().to('cuda') # Placeholder for model definition if not provided class Net(torch.nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = torch.nn.Conv2d(1, 32, 3, 1) self.conv2 = torch.nn.Conv2d(32, 64, 3, 1) self.fc1 = torch.nn.Linear(9216, 128) self.fc2 = torch.nn.Linear(128, 10) def forward(self, x): x = torch.nn.functional.relu(self.conv1(x)) x = torch.nn.functional.max_pool2d(x, 2) x = torch.nn.functional.relu(self.conv2(x)) x = torch.nn.functional.max_pool2d(x, 2) x = torch.flatten(x, 1) x = torch.nn.functional.relu(self.fc1(x)) x = self.fc2(x) return torch.nn.functional.log_softmax(x, dim=1) model = Net().to('cuda') train_loader = torch.utils.data.DataLoader( datasets.MNIST('deeprobust/image/defense/data', train=True, download=True, transform=transforms.Compose([transforms.ToTensor()])), batch_size=100, shuffle=True) test_loader = torch.utils.data.DataLoader( datasets.MNIST('deeprobust/image/defense/data', train=False, transform=transforms.Compose([transforms.ToTensor()])), batch_size=1000,shuffle=True) defense = PGDtraining(model, 'cuda') defense.generate(train_loader, test_loader, **defense_params["PGDtraining_MNIST"]) ``` -------------------------------- ### GET /dataset/load Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.data.html Initializes and loads a graph dataset from the specified root directory with configurable settings. ```APIDOC ## GET /dataset/load ### Description Loads a graph dataset (e.g., 'cora', 'pubmed') and provides access to adjacency matrices, features, and labels. ### Method GET ### Endpoint /dataset/load ### Parameters #### Query Parameters - **root** (string) - Required - Root directory where the dataset is saved. - **name** (string) - Required - Dataset name (e.g., 'cora', 'citeseer', 'polblogs', 'pubmed'). - **setting** (string) - Optional - Data split setting: 'nettack', 'gcn', or 'prognn'. - **seed** (int) - Optional - Random seed for data splitting. - **require_mask** (bool) - Optional - Whether to return training, validation, and test masks. ### Request Example { "root": "/tmp/", "name": "cora", "seed": 15 } ### Response #### Success Response (200) - **adj** (matrix) - The graph adjacency matrix. - **features** (matrix) - The node feature matrix. - **labels** (array) - The node labels. #### Response Example { "adj": "", "features": "", "labels": "[0, 1, 0, ...]" } ``` -------------------------------- ### Run Graph Neural Network Examples Source: https://github.com/dse-msu/deeprobust/blob/master/deeprobust/graph/README.md Executes various Python scripts to test GCN models and generate graph attacks like Metattack on datasets such as Cora. ```bash python examples/graph/test_gcn.py --dataset cora python examples/graph/test_gcn_jaccard.py --dataset cora python examples/graph/test_mettack.py --dataset cora --ptb_rate 0.05 ``` -------------------------------- ### Install DeepRobust Library Source: https://github.com/dse-msu/deeprobust/blob/master/deeprobust/graph/README.md Clones the DeepRobust repository from GitHub and installs the package using setup.py. ```bash git clone https://github.com/DSE-MSU/DeepRobust.git cd DeepRobust python setup.py install ``` -------------------------------- ### Generate Adversarial Examples using CarliniWagner Attack (Python) Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.image.attack.html This snippet demonstrates how to generate adversarial examples using the CarliniWagner attack method. It involves loading a pre-trained model, initializing the attack, and then generating adversarial examples from input data. Dependencies include PyTorch, datasets, and specific modules from DeepRobust. ```python from deeprobust.image.attack.cw import CarliniWagner from deeprobust.image.netmodels.CNN import Net from deeprobust.image.config import attack_params import torch import datasets model = Net() model.load_state_dict(torch.load("./trained_models/MNIST_CNN_epoch_20.pt", map_location=torch.device('cuda'))) model.eval() x, y = datasets.MNIST() attack = CarliniWagner(model, device='cuda') AdvExArray = attack.generate(x, y, target_label=1, classnum=10, **attack_params['CW_MNIST']) ``` -------------------------------- ### Nettack Initialization and Attack Example (Python) Source: https://github.com/dse-msu/deeprobust/blob/master/docs/source/deeprobust.graph.targeted_attack.md Demonstrates how to initialize and use the Nettack class to perform an attack on a graph. It includes setting up a surrogate model, defining the attack parameters, and executing the attack to obtain modified graph features and adjacency matrix. ```python from deeprobust.graph.data import Dataset from deeprobust.graph.defense import GCN from deeprobust.graph.targeted_attack import Nettack data = Dataset(root='/tmp/', name='cora') adj, features, labels = data.adj, data.features, data.labels idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test # Setup Surrogate model surrogate = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1, nhid=16, dropout=0, with_relu=False, with_bias=False, device='cpu').to('cpu') surrogate.fit(features, adj, labels, idx_train, idx_val, patience=30) # Setup Attack Model target_node = 0 model = Nettack(surrogate, nnodes=adj.shape[0], attack_structure=True, attack_features=True, device='cpu').to('cpu') # Attack model.attack(features, adj, labels, target_node, n_perturbations=5) modified_adj = model.modified_adj modified_features = model.modified_features ``` -------------------------------- ### Image Attack and Defense - PGD Attack Example Source: https://github.com/dse-msu/deeprobust/blob/master/README.md Demonstrates how to generate adversarial examples using the Projected Gradient Descent (PGD) attack method on a pre-trained ResNet18 model for CIFAR10. ```APIDOC ## Image Attack - PGD Attack ### Description Generate adversary examples with PGD attack on a pre-trained ResNet18 model for CIFAR10. ### Method Python Script ### Endpoint N/A ### Parameters - **model** (object) - Required - The pre-trained model to attack. - **device** (string) - Required - The device to run the attack on (e.g., 'cuda'). - **x** (tensor) - Required - Input images. - **y** (tensor) - Required - True labels for the input images. - **attack_params** (dict) - Optional - Parameters for the PGD attack (e.g., epsilon, alpha, steps). ### Request Example ```python from deeprobust.image.attack.pgd import PGD from deeprobust.image.config import attack_params from deeprobust.image.utils import download_model import torch import deeprobust.image.netmodels.resnet as resnet from torchvision import transforms,datasets URL = "https://github.com/I-am-Bot/deeprobust_model/raw/master/CIFAR10_ResNet18_epoch_20.pt" download_model(URL, "$MODEL_PATH$") model = resnet.ResNet18().to('cuda') model.load_state_dict(torch.load("$MODEL_PATH$")) model.eval() transform_val = transforms.Compose([transforms.ToTensor()]) test_loader = torch.utils.data.DataLoader( datasets.CIFAR10('deeprobust/image/data', train = False, download=True, transform = transform_val), batch_size = 10, shuffle=True) x, y = next(iter(test_loader)) x = x.to('cuda').float() adversary = PGD(model, 'cuda') Adv_img = adversary.generate(x, y, **attack_params['PGD_CIFAR10']) ``` ### Response - **Adv_img** (tensor) - The generated adversarial images. ``` -------------------------------- ### Initialize and Load Graph Datasets Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.data.html Demonstrates how to instantiate the Dataset class to load standard graph data, including specifying the root directory, dataset name, and random seed for splitting. ```python from deeprobust.graph.data import Dataset data = Dataset(root='/tmp/', name='cora', seed=15) adj, features, labels = data.adj, data.features, data.labels idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test ``` -------------------------------- ### Generate Adversarial Examples with PGD Source: https://github.com/dse-msu/deeprobust/blob/master/README.md Shows how to load a pre-trained ResNet18 model and generate adversarial examples using the Projected Gradient Descent (PGD) attack method on CIFAR-10 data. ```python from deeprobust.image.attack.pgd import PGD from deeprobust.image.config import attack_params from deeprobust.image.utils import download_model import torch import deeprobust.image.netmodels.resnet as resnet from torchvision import transforms,datasets URL = "https://github.com/I-am-Bot/deeprobust_model/raw/master/CIFAR10_ResNet18_epoch_20.pt" download_model(URL, "$MODEL_PATH$") model = resnet.ResNet18().to('cuda') model.load_state_dict(torch.load("$MODEL_PATH$")) model.eval() transform_val = transforms.Compose([transforms.ToTensor()]) test_loader = torch.utils.data.DataLoader( datasets.CIFAR10('deeprobust/image/data', train = False, download=True, transform = transform_val), batch_size = 10, shuffle=True) x, y = next(iter(test_loader)) x = x.to('cuda').float() adversary = PGD(model, 'cuda') Adv_img = adversary.generate(x, y, **attack_params['PGD_CIFAR10']) ``` -------------------------------- ### Load Graph Datasets and Pre-attacked Data Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.data.html Demonstrates how to initialize standard graph datasets and load pre-attacked versions using PtbDataset or PrePtbDataset classes. ```python from deeprobust.graph.data import Dataset, PtbDataset, PrePtbDataset # Load standard dataset data = Dataset(root='/tmp/', name='cora') adj, features, labels = data.adj, data.features, data.labels # Load meta-attacked data perturbed_data = PtbDataset(root='/tmp/', name='cora', attack_method='meta') perturbed_adj = perturbed_data.adj # Load pre-attacked data with specific rate pre_ptb = PrePtbDataset(root='/tmp/', name='cora', attack_method='meta', ptb_rate=0.05) perturbed_adj = pre_ptb.adj ``` -------------------------------- ### Carlini & Wagner Targeted Attack Source: https://context7.com/dse-msu/deeprobust/llms.txt Demonstrates the C&W attack, an optimization-based approach to generate high-confidence adversarial examples. This example shows how to perform a targeted attack to force a specific class prediction. ```python import torch from torchvision import datasets, transforms from deeprobust.image.attack.cw import CarliniWagner from deeprobust.image.netmodels.CNN import Net model = Net() model.load_state_dict(torch.load("mnist_cnn.pt")) model.eval() transform = transforms.Compose([transforms.ToTensor()]) test_dataset = datasets.MNIST('data', train=False, transform=transform) x, y = test_dataset[0] x = x.unsqueeze(0) attack = CarliniWagner(model, device='cuda') target_label = 7 adv_img = attack.generate( x, y, target_label=target_label, classnum=10, confidence=1e-4, max_iterations=1000, binary_search_steps=5, learning_rate=0.01, initial_const=1e-2 ) adv_pred = model(adv_img.to('cuda')).argmax(dim=1) print(f"Original label: {y}") print(f"Target label: {target_label}") print(f"Adversarial prediction: {adv_pred.item()}") ``` -------------------------------- ### Initialize and Attack with MetaApprox Model (Python) Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/global_attack/mettack.html This snippet demonstrates the setup and execution of the MetaApprox attack. It initializes a surrogate GCN model, then sets up the MetaApprox attack model. Finally, it performs the attack using the model and retrieves the modified adjacency matrix. ```python adj, features, labels = preprocess(adj, features, labels, preprocess_adj=False) # conver to tensor idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test idx_unlabeled = np.union1d(idx_val, idx_test) # Setup Surrogate model surrogate = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1, hid=16, dropout=0, with_relu=False, with_bias=False, device='cpu').to('cpu') surrogate.fit(features, adj, labels, idx_train, idx_val, patience=30) # Setup Attack Model model = MetaApprox(surrogate, nnodes=adj.shape[0], feature_shape=features.shape, attack_structure=True, attack_features=False, device='cpu', lambda_=0).to('cpu') # Attack model.attack(features, adj, labels, idx_train, idx_unlabeled, n_perturbations=10, ll_constraint=True) modified_adj = model.modified_adj ``` -------------------------------- ### Train Model with PGD Adversarial Examples Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/defense/pgdtraining.html Executes the training loop by generating adversarial examples using the PGD attack and updating the model weights. It includes a learning rate scheduler and logs progress during training. ```python def train(self, device, train_loader, optimizer, epoch): self.model.train() correct = 0 bs = train_loader.batch_size scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[70], gamma=0.1) for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() data, target = data.to(device), target.to(device) data_adv, output = self.adv_data(data, target, ep=self.epsilon, num_steps=self.num_steps, perturb_step_size=self.perturb_step_size) loss = self.calculate_loss(output, target) loss.backward() optimizer.step() pred = output.argmax(dim=1, keepdim=True) correct += pred.eq(target.view_as(pred)).sum().item() if batch_idx % 20 == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tAccuracy:{:.2f}%'.format(epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item(), 100 * correct/(bs))) correct = 0 scheduler.step() ``` -------------------------------- ### TRADES Robust Loss Calculation Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/defense/trades.html Calculates the robust loss for TRADES, which involves generating adversarial examples and computing the KL divergence between the model's output on natural and adversarial examples. Supports both L-infinity and L2 distance metrics for perturbation. ```python def trades_loss(self, model, x_natural, y, optimizer, step_size = 0.003, epsilon = 0.031, perturb_steps = 10, beta = 1.0, distance = 'l_inf'): # define KL-loss criterion_kl = nn.KLDivLoss(size_average=False) model.eval() batch_size = len(x_natural) # generate adversarial example x_adv = x_natural.detach() + 0.001 * torch.randn(x_natural.shape).cuda().detach() if distance == 'l_inf': for _ in range(perturb_steps): x_adv.requires_grad_() with torch.enable_grad(): loss_kl = criterion_kl(F.log_softmax(model(x_adv), dim=1), F.softmax(model(x_natural), dim=1)) grad = torch.autograd.grad(loss_kl, [x_adv])[0] x_adv = x_adv.detach() + step_size * torch.sign(grad.detach()) x_adv = torch.min(torch.max(x_adv, x_natural - epsilon), x_natural + epsilon) x_adv = torch.clamp(x_adv, 0.0, 1.0) elif distance == 'l_2': delta = 0.001 * torch.randn(x_natural.shape).cuda().detach() delta = Variable(delta.data, requires_grad=True) # Setup optimizers optimizer_delta = optim.SGD([delta], lr=epsilon / perturb_steps * 2) for _ in range(perturb_steps): adv = x_natural + delta # optimize optimizer_delta.zero_grad() with torch.enable_grad(): ``` -------------------------------- ### File System and Logging Utilities Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/utils.html Helper functions for downloading files from URLs, creating symbolic links, and printing configuration arguments in a formatted table. ```python def download_model(url, file): urllib.request.urlretrieve(url, file) def make_symlink(source, link_name): if os.path.exists(link_name): os.remove(link_name) if os.path.exists(source): os.symlink(source, link_name) def tab_printer(args): t = Texttable() for k, v in vars(args).items(): t.add_row([k, v]) print(t.draw()) ``` -------------------------------- ### Optimize Adversarial Examples with L-BFGS-B Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/attack/lbfgs.html This function performs constrained optimization to find adversarial examples. It uses scipy's fmin_l_bfgs_b to minimize the loss, handles boundary clipping, and verifies if the resulting output successfully fools the target model. ```python def lbfgs_b(c): approx_grad_eps = (max_ - min_) / 100 optimize_output, f, d = so.fmin_l_bfgs_b( loss, x0, args=(c,), approx_grad=True, bounds=bounds, m=15, maxiter=maxiter, factr=1e10, maxls=5, epsilon=approx_grad_eps, iprint=11) if np.amax(optimize_output) > max_ or np.amin(optimize_output) < min_: optimize_output = np.clip(optimize_output, min_, max_) optimize_output = torch.from_numpy(optimize_output.reshape(shape).astype(dtype)).unsqueeze_(0).float().to(device) predict1 = model(optimize_output) is_adversarial = predict1.argmax(dim=1, keepdim=True) == target_label return optimize_output, is_adversarial ``` -------------------------------- ### Weight Initialization Utilities Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/rl/nipa_q_net_node.html Utility functions for initializing network weights using Glorot uniform initialization and a general parameter initialization helper. ```APIDOC ## Weight Initialization Utilities ### glorot_uniform(t) Initializes a tensor `t` using the Glorot uniform (Xavier uniform) initialization scheme. - **t** (torch.Tensor): The tensor to initialize. ### _param_init(m) Helper function to initialize parameters of a given module `m`. It applies Glorot uniform initialization to weights and zeros out biases for `nn.Linear` layers, and initializes `nn.Parameter` instances. - **m** (nn.Module): The module whose parameters need initialization. ### weights_init(m) Applies parameter initialization to all submodules within a given module `m` by iterating through `m.modules()` and calling `_param_init` on each. - **m** (nn.Module): The module to initialize weights for. ``` -------------------------------- ### FGSM Image Attack (Python) Source: https://context7.com/dse-msu/deeprobust/llms.txt Generates adversarial examples for image classification models using the Fast Gradient Sign Method (FGSM). This method creates adversarial examples with a single gradient step. It requires a pre-trained model and test data. ```python import torch from torchvision import datasets, transforms from deeprobust.image.attack.fgsm import FGSM from deeprobust.image.netmodels.CNN import Net # Load pretrained model model = Net() model.load_state_dict(torch.load("mnist_cnn.pt")) model.eval() model = model.to('cuda') # Load test data transform = transforms.Compose([transforms.ToTensor()]) test_dataset = datasets.MNIST('data', train=False, download=True, transform=transform) test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=1, shuffle=True) # Get a sample image x, y = next(iter(test_loader)) x, y = x.to('cuda'), y.to('cuda') # Initialize FGSM attack adversary = FGSM(model, device='cuda') ``` -------------------------------- ### Weight Initialization Utilities Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/rl/nipa_q_net_node.html Provides Glorot uniform initialization for neural network layers to ensure stable training. Includes a recursive module initializer that resets weights and biases. ```python def glorot_uniform(t): if len(t.size()) == 2: fan_in, fan_out = t.size() elif len(t.size()) == 3: fan_in = t.size()[1] * t.size()[2] fan_out = t.size()[0] * t.size()[2] else: fan_in = np.prod(t.size()) fan_out = np.prod(t.size()) limit = np.sqrt(6.0 / (fan_in + fan_out)) t.uniform_(-limit, limit) def _param_init(m): if isinstance(m, Parameter): glorot_uniform(m.data) elif isinstance(m, nn.Linear): m.bias.data.zero_() glorot_uniform(m.weight.data) ``` -------------------------------- ### GET /trades/test Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/defense/trades.html Evaluates the model performance on a test dataset. ```APIDOC ## GET /trades/test ### Description Evaluates the provided model on the test dataset and returns the clean loss and accuracy. ### Method GET ### Endpoint /trades/test ### Parameters #### Query Parameters - **model** (object) - Required - The model to evaluate - **device** (str) - Required - The computing device - **test_loader** (DataLoader) - Required - PyTorch data loader for test data ### Response #### Success Response (200) - **test_loss** (float) - The calculated cross-entropy loss - **test_accuracy** (float) - The accuracy percentage as a decimal #### Response Example { "test_loss": 0.1234, "test_accuracy": 0.95 } ``` -------------------------------- ### Initialize and Load Pre-Attacked Graph Data Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/data/attacked_data.html Demonstrates how to instantiate the PrePtbDataset class to load graph data perturbed by specific attack methods. It shows the process for both 'meta' (metattack) and 'nettack' methods, including accessing the resulting adjacency matrix and target nodes. ```python from deeprobust.graph.data import Dataset, PrePtbDataset # Load meta attacked data perturbed_data = PrePtbDataset(root='/tmp/', name='cora', attack_method='meta', ptb_rate=0.05) perturbed_adj = perturbed_data.adj # Load nettacked data perturbed_data = PrePtbDataset(root='/tmp/', name='cora', attack_method='nettack', ptb_rate=1.0) perturbed_adj = perturbed_data.adj target_nodes = perturbed_data.target_nodes ``` -------------------------------- ### Initialize Differential Evolution Solver Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/optimizer.html This snippet demonstrates the constructor for a Differential Evolution optimizer. It handles strategy selection, mutation constants, and convergence tolerance settings. ```python def __init__(self, func, bounds, args=(), strategy='best1bin', maxiter=1000, popsize=15, tol=0.01, mutation=(0.5, 1), recombination=0.7, seed=None, maxfun=np.inf, callback=None, disp=False, polish=True, init='latinhypercube', atol=0): if strategy in self._binomial: self.mutation_func = getattr(self, self._binomial[strategy]) elif strategy in self._exponential: self.mutation_func = getattr(self, self._exponential[strategy]) else: raise ValueError("Please select a valid mutation strategy") self.strategy = strategy self.callback = callback self.polish = polish self.tol, self.atol = tol, atol self.scale = mutation if (not np.all(np.isfinite(mutation)) or np.any(np.array(mutation) >= 2) or np.any(np.array(mutation) < 0)): raise ValueError('The mutation constant must be a float in ' 'U[0, 2), or specified as a tuple(min, max)') ``` -------------------------------- ### POST /attack/onepixel Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.image.attack.html Generates adversarial examples by manipulating specific pixels. ```APIDOC ## POST /attack/onepixel ### Description Generates adversarial examples by manipulating one or a few pixels to mislead the classifier. ### Method POST ### Endpoint /attack/onepixel ### Parameters #### Request Body - **image** (tensor) - Required - Original image (1*3*W*H) - **label** (int) - Required - Target label - **pixels** (int) - Optional - Max number of manipulated pixels (default: 1) - **maxiter** (int) - Optional - Max number of iterations (default: 100) - **popsize** (int) - Optional - Population size (default: 400) - **targeted_attack** (bool) - Optional - Whether to perform targeted attack (default: False) ### Response #### Success Response (200) - **adversarial_image** (tensor) - The generated adversarial example ``` -------------------------------- ### Initialize Model Weights and Optimizer (Python) Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/global_attack/mettack.html This snippet details the weight initialization process for the MetaApprox model's layers and sets up the Adam optimizer. Weights are initialized uniformly within a calculated range, and biases are also initialized. ```python def _initialize(self): for w, b in zip(self.weights, self.biases): stdv = 1. / math.sqrt(w.size(1)) w.data.uniform_(-stdv, stdv) b.data.uniform_(-stdv, stdv) self.optimizer = optim.Adam(self.weights + self.biases, lr=self.lr) ``` -------------------------------- ### Initialize and Train RLS2V Agent Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.targeted_attack.html Demonstrates how to instantiate the RLS2V reinforcement learning agent with specific environment configurations and trigger the training process. The agent requires graph features, labels, and action space definitions to perform targeted perturbations. ```python from deeprobust.graph.targeted_attack import RLS2V # Initialize the agent agent = RLS2V(env=env, features=features, labels=labels, idx_meta=idx_meta, idx_test=idx_test, list_action_space=list_action_space, num_mod=10, reward_type='binary') # Train the agent agent.train(num_steps=100000, lr=0.001) # Evaluate the agent agent.eval(training=False) ``` -------------------------------- ### POST /attack/lbfgs Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.image.attack.html Generates adversarial examples using the LBFGS algorithm. ```APIDOC ## POST /attack/lbfgs ### Description Generates adversarial examples using the LBFGS algorithm, the first adversarial generating algorithm. ### Method POST ### Endpoint /attack/lbfgs ### Parameters #### Request Body - **image** (tensor) - Required - Original image - **label** (int) - Required - Target label - **target_label** (int) - Required - Target class label - **maxiter** (int) - Optional - Maximum number of iterations (default: 20) - **epsilon** (float) - Optional - Step length for binary search (default: 1e-05) ### Response #### Success Response (200) - **adversarial_image** (tensor) - The generated adversarial example ``` -------------------------------- ### Initialize and Train GAT Model Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.defense.html Demonstrates how to load a dataset, instantiate a GAT model, convert the data to PyTorch Geometric format, and train the model with early stopping. ```python from deeprobust.graph.data import Dataset from deeprobust.graph.defense import GAT data = Dataset(root='/tmp/', name='cora') adj, features, labels = data.adj, data.features, data.labels idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test gat = GAT(nfeat=features.shape[1], nhid=8, heads=8, nclass=labels.max().item() + 1, dropout=0.5, device='cpu') gat = gat.to('cpu') pyg_data = Dpr2Pyg(data) gat.fit(pyg_data, patience=100, verbose=True) ``` -------------------------------- ### POST /attack/nattack/generate Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/attack/Nattack.html Generates adversarial examples using the NATTACK black-box algorithm. ```APIDOC ## POST /attack/nattack/generate ### Description Generates adversarial examples for a given model using the NATTACK black-box attack strategy. ### Method POST ### Endpoint /attack/nattack/generate ### Parameters #### Request Body - **dataloader** (object) - Required - The data loader containing input images. - **classnum** (int) - Required - Number of classes in the model. - **target_or_not** (bool) - Optional - Whether the attack is targeted. - **clip_max** (float) - Optional - Maximum pixel value (default 1). - **clip_min** (float) - Optional - Minimum pixel value (default 0). - **epsilon** (float) - Optional - Perturbation constraint (default 0.2). - **population** (int) - Optional - Population size (default 300). - **max_iterations** (int) - Optional - Maximum number of iterations (default 400). - **learning_rate** (float) - Optional - Learning rate (default 2). - **sigma** (float) - Optional - Sigma parameter (default 0.1). ### Request Example { "dataloader": "dataloader_object", "classnum": 10, "epsilon": 0.05, "max_iterations": 100 } ### Response #### Success Response (200) - **adversarial_examples** (array) - The generated adversarial images. #### Response Example { "status": "success", "adversarial_examples": "[...]" } ``` -------------------------------- ### Initialize PGD Training Parameters Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/defense/pgdtraining.html Initializes the configuration for PGD adversarial training, including epsilon constraints, iteration steps, and optimization hyperparameters. ```python self.epoch = epoch_num self.save_model = True self.save_dir = save_dir self.save_name = save_name self.epsilon = epsilon self.num_steps = num_steps self.perturb_step_size = perturb_step_size self.lr = lr self.momentum = momentum self.save_per_epoch = save_per_epoch ``` -------------------------------- ### GET /graph/data/splits Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/genindex.html Retrieves train, validation, and test splits for graph datasets. ```APIDOC ## GET /graph/data/splits ### Description Fetches pre-calculated dataset splits for graph neural network training and evaluation. ### Method GET ### Endpoint /graph/data/splits ### Parameters #### Query Parameters - **dataset_name** (string) - Required - Name of the graph dataset - **split_type** (string) - Optional - Type of split (e.g., 'prognn', 'gcn') ### Request Example GET /graph/data/splits?dataset_name=cora&split_type=gcn ### Response #### Success Response (200) - **train_mask** (array) - Boolean mask for training nodes - **val_mask** (array) - Boolean mask for validation nodes - **test_mask** (array) - Boolean mask for test nodes #### Response Example { "train_mask": [true, false, true], "val_mask": [false, true, false], "test_mask": [false, false, false] } ``` -------------------------------- ### Initialize RLS2V Agent and Networks Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/targeted_attack/rl_s2v.html Initializes the RLS2V agent, including the main Q-network and a target network for stability. It sets up parameters for epsilon-greedy exploration, burn-in period, and snapshotting the network weights. ```python self.old_net = NStepQNetNode(2 * num_mod, features, labels, list_action_space, bilin_q=bilin_q, embed_dim=embed_dim, mlp_hidden=mlp_hidden, max_lv=max_lv, gm=gm, device=device) self.net = self.net.to(device) self.old_net = self.old_net.to(device) self.eps_start = 1.0 self.eps_end = 0.05 self.eps_step = 100000 self.burn_in = 10 self.step = 0 self.pos = 0 self.best_eval = None self.take_snapshot() ``` -------------------------------- ### Fast Gradient Sign Method (FGSM) Attack Implementation (Python) Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.image.attack.html This snippet outlines the FGSM attack, a single-step method that uses the sign of the gradient to create adversarial examples. It requires a model and a device for computation. The `generate` function is used to produce adversarial examples based on the input image and its label. ```python from deeprobust.image.attack.fgsm import FGSM # Assuming 'model' and 'device' are already defined # model = ... # device = 'cuda' or 'cpu' fgsm_attack = FGSM(model, device=device) # adversarial_examples = fgsm_attack.generate(image, label, **kwargs) ``` -------------------------------- ### Initialize and Train GCNSVD Model Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.defense.html Demonstrates how to load graph data, initialize the GCNSVD defense model, and fit it to perturbed graph data. ```python from deeprobust.graph.data import PrePtbDataset, Dataset from deeprobust.graph.defense import GCNSVD # load clean graph data data = Dataset(root='/tmp/', name='cora', seed=15) adj, features, labels = data.adj, data.features, data.labels idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test # load perturbed graph data perturbed_data = PrePtbDataset(root='/tmp/', name='cora') perturbed_adj = perturbed_data.adj # train defense model model = GCNSVD(nfeat=features.shape[1], nhid=16, nclass=labels.max().item() + 1, dropout=0.5, device='cpu').to('cpu') model.fit(features, perturbed_adj, labels, idx_train, idx_val, k=20) ``` -------------------------------- ### Generate Onepixel Adversarial Examples Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/image/attack/onepixel.html Generates adversarial examples using the Onepixel attack. It takes the original image and label as input and returns the manipulated image. Parameters like targeted attack, number of pixels, and iterations can be customized via kwargs. Dependencies include torch and internal utility functions. ```python def generate(self, image, label, **kwargs): """ Call this function to generate Onepixel adversarial examples. Parameters ---------- image :1*3*W*H original image label : target label kwargs : user defined paremeters """ label = label.type(torch.FloatTensor) ## check and parse parameters for attack assert self.check_type_device(image, label) assert self.parse_params(**kwargs) return self.one_pixel(self.image, self.label, self.targeted_attack, self.pixels, self.maxiter, self.popsize, self.print_log) ``` -------------------------------- ### NStepQNetNode Initialization Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/_modules/deeprobust/graph/rl/q_net_node.html Initializes the N-step Q-network for graph reinforcement learning. It sets up a list of QNetNode modules, each representing a step in the N-step learning process, and configures parameters like embedding dimensions and max graph levels. ```python def __init__(self, num_steps, node_features, node_labels, list_action_space, bilin_q=1, embed_dim=64, mlp_hidden=64, max_lv=1, gm='mean_field', device='cpu'): super(NStepQNetNode, self).__init__() self.node_features = node_features self.node_labels = node_labels self.list_action_space = list_action_space self.total_nodes = len(list_action_space) list_mod = [] for i in range(0, num_steps): # list_mod.append(QNetNode(node_features, node_labels, list_action_space)) list_mod.append(QNetNode(node_features, node_labels, list_action_space, bilin_q, embed_dim, mlp_hidden, max_lv, gm=gm, device=device)) self.list_mod = nn.ModuleList(list_mod) self.num_steps = num_steps ``` -------------------------------- ### Nettack Targeted Attack Initialization and Usage (Python) Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.targeted_attack.html Demonstrates how to initialize and use the Nettack model for targeted attacks on graph data. It involves setting up a surrogate model, initializing Nettack with specified attack configurations (structure and features), and then performing the attack to obtain modified graph data. ```python from deeprobust.graph.data import Dataset from deeprobust.graph.defense import GCN from deeprobust.graph.targeted_attack import Nettack data = Dataset(root='/tmp/', name='cora') adj, features, labels = data.adj, data.features, data.labels idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test # Setup Surrogate model surrogate = GCN(nfeat=features.shape[1], nclass=labels.max().item()+1, nhid=16, dropout=0, with_relu=False, with_bias=False, device='cpu').to('cpu') surrogate.fit(features, adj, labels, idx_train, idx_val, patience=30) # Setup Attack Model target_node = 0 model = Nettack(surrogate, nnodes=adj.shape[0], attack_structure=True, attack_features=True, device='cpu').to('cpu') # Attack model.attack(features, adj, labels, target_node, n_perturbations=5) modified_adj = model.modified_adj modified_features = model.modified_features ``` -------------------------------- ### GET /api/deeprobust/utils/encode_onehot Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/genindex.html Utility endpoint to convert categorical labels into one-hot encoded vectors. ```APIDOC ## GET /api/deeprobust/utils/encode_onehot ### Description Encodes categorical labels into a one-hot representation for use in machine learning models. ### Method GET ### Endpoint /api/deeprobust/utils/encode_onehot ### Parameters #### Query Parameters - **labels** (array) - Required - A list of categorical labels to encode ### Request Example /api/deeprobust/utils/encode_onehot?labels=[0, 1, 2] ### Response #### Success Response (200) - **encoded_data** (array) - The one-hot encoded matrix #### Response Example { "encoded_data": [[1, 0, 0], [0, 1, 0], [0, 0, 1]] } ``` -------------------------------- ### GET /dataset/largest_connected_components Source: https://github.com/dse-msu/deeprobust/blob/master/docs/_build/html/source/deeprobust.graph.data.html Selects the k largest connected components from a given adjacency matrix. ```APIDOC ## GET /dataset/largest_connected_components ### Description Filters an input adjacency matrix to return only the k largest connected components. ### Method GET ### Endpoint /dataset/largest_connected_components ### Parameters #### Query Parameters - **adj** (scipy.sparse.csr_matrix) - Required - The input adjacency matrix of the graph. - **n_components** (int) - Optional - The number of largest connected components to select. Defaults to 1. ### Request Example { "adj": "", "n_components": 1 } ### Response #### Success Response (200) - **result** (scipy.sparse.csr_matrix) - The filtered adjacency matrix containing only the selected components. #### Response Example { "result": "" } ```