Skip to main content

A library for Attack & Defence on Video Task

Project description

Attack & Defence on Video Tasks

Overview

ADVT (Attack Defence on Video Tasks) is an adversarial attack/defence toolbox open source library based on Pytorch. This repository mainly implements some adversarial attack & defence algorithms and provides some video processing apis.

demo

Features

The ADVT library has five functional features, which cover the whole process:

  • Preprocess
  • Attack
  • Defence
  • Record
  • Visualization

Attack

This module implements attack methods. All attack methods are from top computer conferences in recent 5 years:

  • FGSM [Explaining and harnessing adversarial examples]
  • BIM [Adversarial examples in the physical world]
  • MIM [Boosting adversarial attacks with momentum(CVPR-18)]
  • DeepFool [DeepFool: A simple and accurate method to fool deep neural networks(CVPR-16)]
  • DIM [Improving Transferability of Adversarial Examples with Input Diversity(CVPR-18)]
  • C&W [Towards evaluating the robustness of neural networks(IEEE SP-17)]
  • Universal [Universal adversarial perturbations(CVPR-17)]
  • ZOO [ZOO: Zeroth Order Optimization Based Black-box Attacks to Deep Neural Networks without Training Substitute Models(ACMAIS)]
  • Sparse ADV [Sparse Adversarial Perturbations for Videos(AAAI-19)]

Defence

This module implements defence methods.

  • Bit-depth Reduction
  • Total Variance Minimization
  • Image Quilting
  • ComDefend [ComDefend: An Efficient Image Compression Model to Defend Adversarial Examples(CVPR-18)]
  • Randomization [Mitigating Adversarial Effects Through Randomization(ICLR-18)]

Evaluation & Visualization

ADVT provides some useful evaluation & visualization tools.
Evaluation Metric:

  • PSNR
  • SSMI
  • mAP

Visualization:

  • Video-to-Frames
  • Frames-to-Video
  • Changed-Pixel

Installation

Before installation, make sure you install fellow requirements.

numpy==1.18.5
opencv-python==4.4.0.42
torch==1.7.0
urllib==1.26.4

You can install advt through pypi or build from source code.
1. from pypi

pip install advt

2. from source code

git clone https://github.com/WindFantasy98/ADVT.git
pip install -e .

Usage

  1. attack example
# python3.7 torch1.7
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from advt.model.cnn import CNN
from advt.attack import FGSM

PATH_PARAMETERS = 'tests/cnn_model.pth'

def main():
    transform = transforms.Compose([transforms.ToTensor()])
    t = transforms.Compose([transforms.ToPILImage()])
    test_dataset = datasets.CIFAR10(root='/data', train=False, transform=transform, download=True)
    test_loader = DataLoader(dataset=test_dataset, batch_size=1, shuffle=False)
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    net = CNN()
    net.load_state_dict(torch.load(PATH_PARAMETERS))
    net = net.to(device)

    fgsm = FGSM(net, device)

    attack_succ = 0
    total_num = 0

    for i, (img, lbl) in enumerate(test_loader):
        img, lbl = img.to(device), lbl.to(device)
        adv_img = fgsm.attack(img, lbl)

        output = net(adv_img)
        _, pred_indice = output.max(1)

        total_num += len(lbl)
        attack_succ += (pred_indice == lbl).sum().item()
        if (i + 1) % 20 == 0:
            print('batch {}:'.format((i + 1) // 20),
                  'total tested number: {}, correct number: {}'.format(total_num, attack_succ))

if __name__ == '__main__':
    main()
  1. defence example
# python3.7 torch1.7
import torch
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
from advt.model.cnn import CNN
from advt.attack import DeepFool
from advt.defence import Randomization

PATH_PARAMETERS = 'tests/cnn_model.pth'

def main():
    # initialize dataset
    transform = transforms.Compose([transforms.ToTensor()])
    t = transforms.Compose([transforms.ToPILImage()])
    test_dataset = datasets.CIFAR10(root='/data', train=False, transform=transform, download=True)
    test_loader = DataLoader(dataset=test_dataset, batch_size=1, shuffle=False)
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    # load victim model
    net = CNN()
    net.load_state_dict(torch.load(PATH_PARAMETERS))
    net = net.to(device)

    # initialize attack method
    df = DeepFool(net, device)
    # initialize defend method
    rand_defend = Randomization(net, device)

    # initialize indicator
    attack_succ = 0
    total_num = 0

    # start attack
    for i, (img, lbl) in enumerate(test_loader):
        img, lbl = img.to(device), lbl.to(device)
        adv_img = df.attack(img, lbl)  # get adv sample

        output = rand_defend.defend(adv_img)  # get processed sample
        _, pred_indice = output.max(1)

        total_num += len(lbl)
        attack_succ += (pred_indice == lbl).sum().item()
        if (i + 1) % 20 == 0:
            print('batch {}:'.format((i + 1) // 20),
                  'total tested number: {}, correct number: {}'.format(total_num, attack_succ))

if __name__ == '__main__':
    main()

This repo is still under maintenance. For more information, please contact with me.

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

advt-1.0.3.tar.gz (31.5 kB view details)

Uploaded Source

Built Distribution

advt-1.0.3-py3-none-any.whl (66.0 kB view details)

Uploaded Python 3

File details

Details for the file advt-1.0.3.tar.gz.

File metadata

  • Download URL: advt-1.0.3.tar.gz
  • Upload date:
  • Size: 31.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.3

File hashes

Hashes for advt-1.0.3.tar.gz
Algorithm Hash digest
SHA256 54f7411ab509497f764b50fe8963737bfa1261552c3923ca3d4d9c62b103cccf
MD5 df3899a27accdc45c3f7349587a31e5a
BLAKE2b-256 1536e8ea6895781b58b5d98d28e8cea58f98714d843ad8115bb4feff008dc1f8

See more details on using hashes here.

File details

Details for the file advt-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: advt-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 66.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.7.0 requests/2.25.1 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.3

File hashes

Hashes for advt-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1a06c526ad6a702c157254e0fdf08df5c041f8bba95b2e73645c01181c99dcd9
MD5 45f63370030fa85c05cf45b2d7694c18
BLAKE2b-256 c1686b0a727bed916060a2768a9f25f848bcbbfdea4a3009cd5a34e122e80446

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