Package: deeprobust
Documentation|Paper|Samples[AAAI 2021] DeepRobust is a PyTorch adversarial library for attack and defense methods on images and graphs.If you are new to DeepRobust, we highly suggest you read thedocumentation pageor the following content in this README to learn how to use it.If you have any questions or suggestions regarding this library, feel free to create an issuehere. We will reply as soon as possible :)List of including algorithms can be found in[Image Package]and[Graph Package].Environment & InstallationUsageImage Attack and DefenseGraph Attack and DefenseAcknowledgementFor more details about attacks and defenses, you can read the following papers.Adversarial Attacks and Defenses on Graphs: A Review, A Tool and Empirical StudiesAdversarial Attacks and Defenses in Images, Graphs and Text: A ReviewIf our work could help your research, please cite:DeepRobust: A PyTorch Library for Adversarial Attacks and Defenses@article{li2020deeprobust,
  title={Deeprobust: A pytorch library for adversarial attacks and defenses},
  author={Li, Yaxin and Jin, Wei and Xu, Han and Tang, Jiliang},
  journal={arXiv preprint arXiv:2005.06149},
  year={2020}
}Changelog[11/2023] DeepRobust 0.2.9 Released. Please trypip install deeprobust==0.2.9. We have fixed the OOM issue of metattack on new pytorch versions.[06/2023] We have added a backdoor attackUGBA, WWW'23to graph package. We can now use UGBA to conduct unnoticeable backdoor attack on large-scale graphs such as ogb-arxiv (see example intest_ugba.py)![02/2023] DeepRobust 0.2.8 Released. Please trypip install deeprobust==0.2.8! We have added a scalable attackPRBCD, NeurIPS'21to graph package. We can now use PRBCD to attack large-scale graphs such as ogb-arxiv (see example intest_prbcd.py)![02/2023] Add a robust modelAirGNN, NeurIPS'21to graph package. Trypython examples/graph/test_airgnn.py! See details intest_airgnn.py[11/2022] DeepRobust 0.2.6 Released. Please trypip install deeprobust==0.2.6! We have more updates coming. Please stay tuned![11/2021] A subpackage that includes popular black box attacks in image domain is released. Find it here.Link[11/2021] DeepRobust 0.2.4 Released. Please trypip install deeprobust==0.2.4![10/2021] add scalable attack and MedianGCN. ThankJintangfor his contribution![06/2021] [Image Package] Add preprocessing method: APE-GAN.[05/2021] DeepRobust is published at AAAI 2021. Checkhere![05/2021] DeepRobust 0.2.2 Released. Please trypip install deeprobust==0.2.2![04/2021] [Image Package] Add support for ImageNet. See details intest_ImageNet.py[04/2021] [Graph Package] Add support for OGB datasets.  See more details in thetutorial page.[03/2021] [Graph Package] Added node embedding attack and victim models! See thistutorial page.[02/2021][Graph Package] DeepRobust now provides tools for converting the datasets betweenPytorch Geometricand DeepRobust. See more details in thetutorial page!DeepRobust now also support GAT, Chebnet and SGC based on pyg; see details intest_gat.py,test_chebnet.pyandtest_sgc.py[12/2020] DeepRobust now can be installed via pip! Trypip install deeprobust![12/2020] [Graph Package] Add four moredatasetsand one defense algorithm. More details can be foundhere. More datasets and algorithms will be added later. Stay tuned :)[07/2020] Adddocumentationpage![06/2020] Add docstring to both image and graph packageBasic Environmentpython >= 3.6(python 3.5 should also work)pytorch >= 1.2.0seesetup.pyorrequirements.txtfor more information.InstallationInstall from pippip install deeprobustInstall from sourcegit clone https://github.com/DSE-MSU/DeepRobust.git
cd DeepRobust
python setup.py installTest Examplespython examples/image/test_PGD.py
python examples/image/test_pgdtraining.py
python examples/graph/test_gcn_jaccard.py --dataset cora
python examples/graph/test_mettack.py --dataset cora --ptb_rate 0.05UsageImage Attack and DefenseTrain modelExample: Train a simple CNN model on MNIST dataset for 20 epoch on gpu.importdeeprobust.image.netmodels.train_modelastrainmodeltrainmodel.train('CNN','MNIST','cuda',20)Model would be saved in deeprobust/trained_models/.Instantiated attack methods and defense methods.Example: Generate adversary example with PGD attack.fromdeeprobust.image.attack.pgdimportPGDfromdeeprobust.image.configimportattack_paramsfromdeeprobust.image.utilsimportdownload_modelimporttorchimportdeeprobust.image.netmodels.resnetasresnetfromtorchvisionimporttransforms,datasetsURL="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'])Example: Train defense model.fromdeeprobust.image.defense.pgdtrainingimportPGDtrainingfromdeeprobust.image.configimportdefense_paramsfromdeeprobust.image.netmodels.CNNimportNetimporttorchfromtorchvisionimportdatasets,transformsmodel=Net()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"])More example code can be found in deeprobust/examples.Use our evulation program to test attack algorithm against defense.Example:cd DeepRobust
python examples/image/test_train.py
python deeprobust/image/evaluation_attack.pyGraph Attack and DefenseAttacking Graph Neural NetworksLoad datasetimporttorchimportnumpyasnpfromdeeprobust.graph.dataimportDatasetfromdeeprobust.graph.defenseimportGCNfromdeeprobust.graph.global_attackimportMetattackdata=Dataset(root='/tmp/',name='cora',setting='nettack')adj,features,labels=data.adj,data.features,data.labelsidx_train,idx_val,idx_test=data.idx_train,data.idx_val,data.idx_testidx_unlabeled=np.union1d(idx_val,idx_test)Set up surrogate modeldevice=torch.device("cuda:0"iftorch.cuda.is_available()else"cpu")surrogate=GCN(nfeat=features.shape[1],nclass=labels.max().item()+1,nhid=16,with_relu=False,device=device)surrogate=surrogate.to(device)surrogate.fit(features,adj,labels,idx_train)Set up attack model and generate perturbationsmodel=Metattack(model=surrogate,nnodes=adj.shape[0],feature_shape=features.shape,device=device)model=model.to(device)perturbations=int(0.05*(adj.sum()//2))model.attack(features,adj,labels,idx_train,idx_unlabeled,perturbations,ll_constraint=False)modified_adj=model.modified_adjFor more details please refer tomettack.pyor runpython examples/graph/test_mettack.py --dataset cora --ptb_rate 0.05Defending Against Graph AttacksLoad datasetimporttorchfromdeeprobust.graph.dataimportDataset,PtbDatasetfromdeeprobust.graph.defenseimportGCN,GCNJaccardimportnumpyasnpnp.random.seed(15)# load clean graphdata=Dataset(root='/tmp/',name='cora',setting='nettack')adj,features,labels=data.adj,data.features,data.labelsidx_train,idx_val,idx_test=data.idx_train,data.idx_val,data.idx_test# load pre-attacked graph by mettackperturbed_data=PtbDataset(root='/tmp/',name='cora')perturbed_adj=perturbed_data.adjTest# Set up defense model and test performancedevice=torch.device("cuda:0"iftorch.cuda.is_available()else"cpu")model=GCNJaccard(nfeat=features.shape[1],nclass=labels.max()+1,nhid=16,device=device)model=model.to(device)model.fit(features,perturbed_adj,labels,idx_train)model.eval()output=model.test(idx_test)# Test on GCNmodel=GCN(nfeat=features.shape[1],nclass=labels.max()+1,nhid=16,device=device)model=model.to(device)model.fit(features,perturbed_adj,labels,idx_train)model.eval()output=model.test(idx_test)For more details please refer totest_gcn_jaccard.pyor runpython examples/graph/test_gcn_jaccard.py --dataset coraSample Resultsadversary examples generated by fgsm:Left:original, classified as 6; Right:adversary, classified as 4.Serveral trained models can be found here:https://drive.google.com/open?id=1uGLiuCyd8zCAQ8tPz9DDUQH6zm-C4tELAcknowledgementSome of the algorithms are referred to paper authors' implementations. References can be found at the top of each file.Implementation of network structure are referred to weiaicunzai's github. Original code can be found here:pytorch-cifar100Thanks to their outstanding works!