All Projects → DSE-MSU → Deeprobust

DSE-MSU / Deeprobust

Licence: mit
A pytorch adversarial library for attack and defense methods on images and graphs

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Deeprobust

Vergeml
Machine Learning Environment - alpha version
Stars: ✭ 338 (-12.89%)
Mutual labels:  deep-neural-networks
Magnet
Deep Learning Projects that Build Themselves
Stars: ✭ 351 (-9.54%)
Mutual labels:  deep-neural-networks
U Net
U-Net: Convolutional Networks for Biomedical Image Segmentation
Stars: ✭ 374 (-3.61%)
Mutual labels:  deep-neural-networks
Caffe
Caffe for Sparse and Low-rank Deep Neural Networks
Stars: ✭ 339 (-12.63%)
Mutual labels:  deep-neural-networks
Curl
CURL: Contrastive Unsupervised Representation Learning for Sample-Efficient Reinforcement Learning
Stars: ✭ 346 (-10.82%)
Mutual labels:  deep-neural-networks
Openchem
OpenChem: Deep Learning toolkit for Computational Chemistry and Drug Design Research
Stars: ✭ 356 (-8.25%)
Mutual labels:  deep-neural-networks
Keras Mmoe
A Keras implementation of "Modeling Task Relationships in Multi-task Learning with Multi-gate Mixture-of-Experts" (KDD 2018)
Stars: ✭ 332 (-14.43%)
Mutual labels:  deep-neural-networks
Flow Forecast
Deep learning PyTorch library for time series forecasting, classification, and anomaly detection (originally for flood forecasting).
Stars: ✭ 368 (-5.15%)
Mutual labels:  deep-neural-networks
Action Recognition Visual Attention
Action recognition using soft attention based deep recurrent neural networks
Stars: ✭ 350 (-9.79%)
Mutual labels:  deep-neural-networks
Mobilenetv2.pytorch
72.8% MobileNetV2 1.0 model on ImageNet and a spectrum of pre-trained MobileNetV2 models
Stars: ✭ 369 (-4.9%)
Mutual labels:  deep-neural-networks
Distiller
Neural Network Distiller by Intel AI Lab: a Python package for neural network compression research. https://intellabs.github.io/distiller
Stars: ✭ 3,760 (+869.07%)
Mutual labels:  deep-neural-networks
Real Time Gesrec
Real-time Hand Gesture Recognition with PyTorch on EgoGesture, NvGesture, Jester, Kinetics and UCF101
Stars: ✭ 339 (-12.63%)
Mutual labels:  deep-neural-networks
Openrec
OpenRec is an open-source and modular library for neural network-inspired recommendation algorithms
Stars: ✭ 360 (-7.22%)
Mutual labels:  deep-neural-networks
Tensorflow Open nsfw
Tensorflow Implementation of Yahoo's Open NSFW Model
Stars: ✭ 338 (-12.89%)
Mutual labels:  deep-neural-networks
Rmdl
RMDL: Random Multimodel Deep Learning for Classification
Stars: ✭ 375 (-3.35%)
Mutual labels:  deep-neural-networks
Pose Residual Network
Code for the Pose Residual Network introduced in 'MultiPoseNet: Fast Multi-Person Pose Estimation using Pose Residual Network (ECCV 2018)' paper
Stars: ✭ 337 (-13.14%)
Mutual labels:  deep-neural-networks
Predictive Maintenance Using Lstm
Example of Multiple Multivariate Time Series Prediction with LSTM Recurrent Neural Networks in Python with Keras.
Stars: ✭ 352 (-9.28%)
Mutual labels:  deep-neural-networks
Taso
The Tensor Algebra SuperOptimizer for Deep Learning
Stars: ✭ 391 (+0.77%)
Mutual labels:  deep-neural-networks
First Steps Towards Deep Learning
This is an open sourced book on deep learning.
Stars: ✭ 376 (-3.09%)
Mutual labels:  deep-neural-networks
Easy Deep Learning With Keras
Keras tutorial for beginners (using TF backend)
Stars: ✭ 367 (-5.41%)
Mutual labels:  deep-neural-networks

logo


GitHub last commit GitHub issues GitHub Contributing Tweet

Documentation | Paper | Samples

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 the documentation page or 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 issue here. We will reply as soon as possible :)

List of including algorithms can be found in [Image Package] and [Graph Package].

Environment & Installation

Usage

Acknowledgement

For more details about attacks and defenses, you can read the following papers.

If our work could help your research, please cite: DeepRobust: A PyTorch Library for Adversarial Attacks and Defenses

Changelog

  • [03/2021] [Graph Package] Added node embedding attack and victim models! See this tutorial page.
  • [02/2021] [Graph Package] DeepRobust now provides tools for converting the datasets between Pytorch Geometric and DeepRobust. See more details in the tutorial page! DeepRobust now also support GAT, Chebnet and SGC based on pyg; see details in test_gat.py, test_chebnet.py and test_sgc.py
  • [12/2020] DeepRobust now can be installed via pip! Try pip install deeprobust!
  • [12/2020] [Graph Package] Add four more datasets and one defense algorithm. More details can be found here. More datasets and algorithms will be added later. Stay tuned :)
  • [07/2020] Add documentation page!
  • [06/2020] Add docstring to both image and graph package

Basic Environment

  • python >= 3.6 (python 3.5 should also work)
  • pytorch >= 1.2.0

see setup.py or requirements.txt for more information.

Installation

Install from pip

pip install deeprobust 

Install from source

git clone https://github.com/DSE-MSU/DeepRobust.git
cd DeepRobust
python setup.py install

Test Examples

python 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.05

Usage

Image Attack and Defense

  1. Train model

    Example: Train a simple CNN model on MNIST dataset for 20 epoch on gpu.

    import deeprobust.image.netmodels.train_model as trainmodel
    trainmodel.train('CNN', 'MNIST', 'cuda', 20)
    

    Model would be saved in deeprobust/trained_models/.

  2. Instantiated attack methods and defense methods.

    Example: Generate adversary example with PGD attack.

    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'])
    

    Example: Train defense model.

    from deeprobust.image.defense.pgdtraining import PGDtraining
    from deeprobust.image.config import defense_params
    from deeprobust.image.netmodels.CNN import Net
    import torch
    from torchvision import datasets, transforms 
    
    model = 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.

  3. Use our evulation program to test attack algorithm against defense.

    Example:

    cd DeepRobust
    python examples/image/test_train.py
    python deeprobust/image/evaluation_attack.py
    

Graph Attack and Defense

Attacking Graph Neural Networks

  1. Load dataset

    import torch
    import numpy as np
    from deeprobust.graph.data import Dataset
    from deeprobust.graph.defense import GCN
    from deeprobust.graph.global_attack import Metattack
    
    data = Dataset(root='/tmp/', name='cora', setting='nettack')
    adj, features, labels = data.adj, data.features, data.labels
    idx_train, idx_val, idx_test = data.idx_train, data.idx_val, data.idx_test
    idx_unlabeled = np.union1d(idx_val, idx_test)
    
  2. Set up surrogate model

    device = torch.device("cuda:0" if torch.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)
    
  3. Set up attack model and generate perturbations

    model = 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_adj
    

For more details please refer to mettack.py or run python examples/graph/test_mettack.py --dataset cora --ptb_rate 0.05

Defending Against Graph Attacks

  1. Load dataset
    import torch
    from deeprobust.graph.data import Dataset, PtbDataset
    from deeprobust.graph.defense import GCN, GCNJaccard
    import numpy as np
    np.random.seed(15)
    
    # load clean graph
    data = Dataset(root='/tmp/', name='cora', setting='nettack')
    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 pre-attacked graph by mettack
    perturbed_data = PtbDataset(root='/tmp/', name='cora')
    perturbed_adj = perturbed_data.adj
    
  2. Test
    # Set up defense model and test performance
    device = torch.device("cuda:0" if torch.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 GCN
    model = 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 to test_gcn_jaccard.py or run python examples/graph/test_gcn_jaccard.py --dataset cora

Sample Results

adversary 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-C4tEL

Acknowledgement

Some 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-cifar100

Thanks to their outstanding works!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].