Skip to main content

a universal pytorch platform to conduct security researches

Project description

TrojanZoo

logo

contact

docs python>=3.9 License

release pypi docker

NOTE: TrojanZoo requires python>=3.9.2, pytorch>=1.8.1 and torchvision>=0.9.1, which must be installed manually. Recommend to use conda to install.

This is the code implementation (pytorch) for our paper in EuroS&P 2022:
TrojanZoo: Towards Unified, Holistic, and Practical Evaluation of Neural Backdoors)

TrojanZoo provides a universal pytorch platform to conduct security researches (especially backdoor attacks/defenses) of image classification in deep learning. It is composed of two packages: trojanzoo and trojanvision. trojanzoo contains abstract classes and utilities, while trojanvision contains abstract and concrete ones for image classification task.

Note: This repository is also maintained to cover the implementation of
our kdd 2020 paper AdvMind: Inferring Adversary Intent of Black-Box Attacks
and ccs 2020 paper A Tale of Evil Twins: Adversarial Inputs versus Poisoned Models

Screenshot

screenshot

Features

  1. Colorful and verbose output!

    Note: enable with --color for color and --verbose for verbose.
    To open an interactive window with color, use python - --color

  2. Modular design (plug and play)
  3. Good code linting support (this package requires python>=3.9)
  4. Register your own module to the library.
  5. Native Pytorch Output
    trojanzoo and trojanvision provides API to generate raw pytorch instances, which makes it flexible to work with native pytorch and other 3rd party libraries.

    trojanzoo.datasets.DataSet can generate torch.utils.data.Dataset and torch.utils.data.DataLoader
    trojanzoo.models.Model attribute _model is torch.nn.Module, attribute model is torch.nn.DataParallel
    Specifically, trojanvision.datasets.ImageSet can generate torchvision.datasets.VisionDataset, trojanvision.datasets.ImageFolder can generate torchvision.datasets.ImageFolder

  6. Enable pytorch native AMP(Automatic Mixed Precision) with --amp for training
  7. Flexible Configuration Files
  8. Good help information to check arguments. (-h or --help)
  9. Detailed and well-organized summary() for each module.

Installation

  1. pip install trojanzoo
  2. (todo) conda install trojanzoo
  3. docker pull local0state/trojanzoo
  4. (RECOMMEND)pip install -e . or python setup.py develop

    This could install the github repo as a package but avoid copying files to site_packages, so that you can easily keep it updated by doing github pull.

Quick Start

You can use the provided example scripts to reproduce the evaluation results in our paper.

Note: The program won't save results without --save

  1. Train a model:
    e.g. ResNet18 on CIFAR10 with 95% Acc

    python ./examples/train.py --color --verbose 1 --dataset cifar10 --model resnet18_comp --lr_scheduler --cutout --grad_clip 5.0 --save
    
  2. Test backdoor attack (e.g., BadNet):
    e.g. BadNet with ResNet18 on CIFAR10

    python ./examples/backdoor_attack.py --color --verbose 1 --pretrained --validate_interval 1 --dataset cifar10 --model resnet18_comp --attack badnet --mark_random_init --epochs 50 --lr 0.01 --save
    
  3. Test backdoor defense (e.g., Neural Cleanse):
    e.g. Neural Cleanse against BadNet

    python ./examples/backdoor_defense.py --color --verbose 1 --pretrained --validate_interval 1 --dataset cifar10 --model resnet18_comp --attack badnet --defense neural_cleanse --mark_random_init --epochs 50 --lr 0.01
    

IMC

python ./examples/backdoor_attack.py --color --verbose 1 --pretrained --validate_interval 1 --dataset cifar10 --model resnet18_comp --attack imc --mark_random_init --epochs 50 --lr 0.01 --save

AdvMind

(with attack adaptive and model adaptive)

python ./examples/adv_defense.py --color --verbose 1 --pretrained --validate_interval 1 --dataset cifar10 --model resnet18_comp --attack pgd --defense advmind --attack_adapt --defense_adapt

Detailed Usage

Configuration file structure

All arguments in the parser are able to set default values in configuration files.
If argument values are not set in the config files, we will use the default values of __init__()

Parameters Config: (priority ascend order)

The higher priority config will override lower priority ones.
Within each priority channel, trojanvision configs will overwrite trojanzoo

  1. Package Default: /trojanzoo/configs/, /trojanvision/configs/

    These are package default settings. Please don't modify them.
    You can use this as a template to set other configs.

  2. User Default: ~/.trojanzoo/configs/trojanzoo/, ~/.trojanzoo/configs/trojanvision/
  3. Workspace Default: /configs/trojanzoo/, /configs/trojanvision/
  4. Custom Config: --config [config location]
  5. CMD parameters: --[parameter] [value]

Store path of Dataset, Model, Attack & Defense Results

Modify them in corresponding config files and command-line arguments.

Dataset: --data_dir (./data/data)
Model: --model_dir (./data/model)
Attack: --attack_dir (./data/attack)
Defense: --defense_dir (./data/defense)

Output Verbose Information:

  1. CMD modules: --verbose 1
  2. Colorful output: --color
  3. tqdm: --tqdm
  4. Check command-line argument usage: --help
  5. AdvMind verbose information: --output [number]

Use your DIY Dataset/Model/Attack/Defense

  1. Follow our example to write your DIY class. (CIFAR10, ResNet, IMC, Neural Cleanse)

    It's necessary to subclass our base class. (Dataset, Model, Attack, Defense)
    Optional base classes depending on your use case: (ImageSet, ImageFolder, ImageModel)

  2. Register your DIY class in trojanvision

    Example: trojanvision.attacks.class_dict[attack_name]=AttackClass

  3. Create your config files if necessary.
    No need to modify any codes. Just directly add {attack_name}.yml (.json) in the config directory.
  4. Good to go!

Todo List

  1. Sphinx Docs
  2. Unit test

License

TrojanZoo has a GPL-style license, as found in the LICENSE file.

Cite our paper

@InProceedings{pang2020trojanzoo,
      title={TROJANZOO: Everything you ever wanted to know about neural backdoors (but were afraid to ask)}, 
      author={Ren Pang and Zheng Zhang and Xiangshan Gao and Zhaohan Xi and Shouling Ji and Peng Cheng and Ting Wang},
      year={2020},
      booktitle={arXiv Preprint},
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trojanzoo-1.0.13.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

trojanzoo-1.0.13-py3-none-any.whl (528.2 kB view details)

Uploaded Python 3

File details

Details for the file trojanzoo-1.0.13.tar.gz.

File metadata

  • Download URL: trojanzoo-1.0.13.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for trojanzoo-1.0.13.tar.gz
Algorithm Hash digest
SHA256 61d47ac290063f3a4a6adbf5e1440253593e77f25a188c781a20c75d7c2fd98a
MD5 a016fc1cbb5d22eee5b512c3db1ca022
BLAKE2b-256 049670a2f0db77f747310dd38ad63d971b8308e243540abc6444d96fdedd0d8d

See more details on using hashes here.

File details

Details for the file trojanzoo-1.0.13-py3-none-any.whl.

File metadata

  • Download URL: trojanzoo-1.0.13-py3-none-any.whl
  • Upload date:
  • Size: 528.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for trojanzoo-1.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 d0da1bbbc0eacf165854a2db216effcc44fd5e5cf3a9fa311ed2425fac4d9963
MD5 d2fd410688bd9da52ab07a6c39c262a5
BLAKE2b-256 01151ad93ea7f3e18926e6f70a16f6a417f7b7a78d3bfe3ce6f310781808b7a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page