Home

Awesome

Implementation of Papers on Adversarial Examples

Implementation of papers with real-time visualizations and parameter control.

Dependencies

Contents


Random Perturbations

From one of the first papers on Adversarial examples - Explaining and Harnessing Adversarial Examples,

The direction of perturbation, rather than the specific point in space, matters most. Space is not full of pockets of adversarial examples that finely tile the reals like the rational numbers.

This project examines this idea by testing the robustness of a DNN to randomly generated perturbations.

Usage

$ python3 explore_space.py --img images/horse.png

Demo

fgsm.gif

This code adds to the input image (img) a randomly generated perturbation (vec1) which is subjected to a max norm constraint eps. This adversarial image lies on a hypercube centerd around the original image. To explore a region (a hypersphere) around the adversarial image (img + vec1), we add to it another perturbation (vec2) which is constrained by L<sub>2</sub> norm rad.
Pressing keys e and r generates new vec1 and vec2 respectively.

Random Perturbations

The classifier is robust to these random perturbations even though they have severely degraded the image. Perturbations are clearly noticeable and have significantly higher max norm.

horse_exploreautomobile_exploretruck_explore
horseautomobile: truck :

In above images, there is no change in class labels and very small drops in probability.

FGSM Perturbations

A properly directed perturbation with max norm as low as 3, which is almost imperceptible, can fool the classifier.

horse_scaledhorse_adversarialperturbation
horsepredicted - dogperturbation (eps = 6)
</br> </br>

Fast Gradient Sign Method (FGSM)

Paper

Usage

$ python3 fgsm_mnist.py --img one.jpg --gpu
$ python3 fgsm_imagenet.py --img goldfish.jpg --model resnet18 --gpu

fgsm_mnsit.py - for attack on custom model trained on MNIST whose weights are 9920.pth.tar.
fgsm_imagenet - for pretrained imagenet models - resnet18, resnet50 etc.

Demo

fgsm.gif

Results

MNIST

Adversarial ImagePerturbation
<img src="fgsm/images/results/adv_4.png" width="84"><img src="fgsm/images/results/perturbation_4_38.png" width="84">
Pred: 4eps: 38
<img src="fgsm/images/results/adv_7.png" width="84"><img src="fgsm/images/results/perturbation_7_60.png" width="84">
Pred: 7eps: 60
<img src="fgsm/images/results/adv_8(2).png" width="84"><img src="fgsm/images/results/perturbation_8(2)_42.png" width="84">
Pred: 8eps: 42
<img src="fgsm/images/results/adv_8.png" width="84"><img src="fgsm/images/results/perturbation_8_12.png" width="84">
Pred: 8eps: 12
<img src="fgsm/images/results/adv_9.png" width="84"><img src="fgsm/images/results/perturbation_9_17.png" width="84">
Pred: 9eps: 17
</br> </br>

Basic Iterative Method (Targeted and Untargeted)

Paper: Adversarial examples in the physical world

Usage

$ python3 iterative.py --img images/goldfish.jpg --model resnet18 --target 4
# If argument 'target' is not specified, it is untargeted attack

Demo

iterative.gif

</br> </br>

One Pixel Attack for Fooling Deep Neural Networks

Paper

Existence of single pixel adversarial perturbations suggest that the assumption made in Explaining and Harnessing Adversarial Examples that small additive perturbation on the values of many dimensions will accumulate and cause huge change to the output, might not be necessary for explaining why natural images are sensitive to small perturbations.

Usage

$ python3 one_pixel.py --img airplane.jpg --d 3 --iters 600 --popsize 10

d is number of pixels to change (L<sub>0</sub> norm)
iters and popsize are paprameters for Differential Evolution

Results

Attacks are typically successful for images with low confidence. For successful attacks on high confidence images increase d, i.e., number of pixels to perturb.

airplanebirdcatfroghorse
bird [0.8075]deer [0.8933]frog [0.8000]bird [0.6866]deer [0.9406]
</br> </br>

AdvGAN - Generating Adversarial Examples with Adversarial Networks

Paper | IJCAI 2018

Usage

Inference

$ python3 advgan.py --img images/0.jpg --target 4 --model Model_C --bound 0.3

Each of these settings has a separate Generator trained. This code loads appropriate trained model from saved/ directory based on given arguments. As of now there are 22 Generators for different targets, different bounds (0.2 and 0.3) and target models (only Model_C for now).

Training AdvGAN (Untargeted)

$ python3 train_advgan.py --model Model_C --gpu

Training AdvGAN (Targeted)

$ python3 train_advgan.py --model Model_C --target 4 --thres 0.3 --gpu
# thres: Perturbation bound 

Use --help for other arguments available (epochs, batch_size, lr etc.)

Training Target Models (Models A, B and C)

$ python3 train_target_models.py --model Model_C

For TensorBoard visualization,

$ python3 generators.py
$ python3 discriminators.py

This code supports only MNIST dataset for now. Same notations as in paper are followed (mostly).

Results

There are few changes that have been made for model to work.

These results are for the following settings.

TargetAcc [thres: 0.3]Acc [thres: 0.2]
Untargeted0.99210.8966
00.96430.4330
10.98220.4749
20.99610.8499
30.99390.8696
40.98330.6293
50.99180.7968
60.95840.4652
70.98990.6866
80.99430.8430
90.99220.7610

Untargeted

<img src="adv_gan/images/results/untargeted_0_9.png" width="84"><img src="adv_gan/images/results/untargeted_1_3.png" width="84"><img src="adv_gan/images/results/untargeted_2_8.png" width="84"><img src="adv_gan/images/results/untargeted_3_8.png" width="84"><img src="adv_gan/images/results/untargeted_4_4.png" width="84"><img src="adv_gan/images/results/untargeted_5_3.png" width="84"><img src="adv_gan/images/results/untargeted_6_8.png" width="84"><img src="adv_gan/images/results/untargeted_7_3.png" width="84"><img src="adv_gan/images/results/untargeted_8_3.png" width="84"><img src="adv_gan/images/results/untargeted_9_8.png" width="84">
Pred: 9Pred: 3Pred: 8Pred: 8Pred: 4Pred: 3Pred: 8Pred: 3Pred: 3Pred: 8

Targeted

Target: 0Target: 1Target: 2Target: 3Target: 4Target: 5Target: 6Target: 7Target: 8Target: 9
<img src="adv_gan/images/results/targeted_0_0_0.png" width="84"><img src="adv_gan/images/results/targeted_0_1_1.png" width="84"><img src="adv_gan/images/results/targeted_0_2_2.png" width="84"><img src="adv_gan/images/results/targeted_0_3_3.png" width="84"><img src="adv_gan/images/results/targeted_0_4_4.png" width="84"><img src="adv_gan/images/results/targeted_0_5_5.png" width="84"><img src="adv_gan/images/results/targeted_0_6_6.png" width="84"><img src="adv_gan/images/results/targeted_0_7_7.png" width="84"><img src="adv_gan/images/results/targeted_0_8_8.png" width="84"><img src="adv_gan/images/results/targeted_0_9_9.png" width="84">
Pred: 0Pred: 1Pred: 2Pred: 3Pred: 4Pred: 5Pred: 6Pred: 7Pred: 8Pred: 9
<img src="adv_gan/images/results/targeted_1_0_0.png" width="84"><img src="adv_gan/images/results/targeted_1_1_1.png" width="84"><img src="adv_gan/images/results/targeted_1_2_2.png" width="84"><img src="adv_gan/images/results/targeted_1_3_3.png" width="84"><img src="adv_gan/images/results/targeted_1_4_4.png" width="84"><img src="adv_gan/images/results/targeted_1_5_5.png" width="84"><img src="adv_gan/images/results/targeted_1_6_6.png" width="84"><img src="adv_gan/images/results/targeted_1_7_7.png" width="84"><img src="adv_gan/images/results/targeted_1_8_8.png" width="84"><img src="adv_gan/images/results/targeted_1_9_9.png" width="84">
Pred: 0Pred: 1Pred: 2Pred: 3Pred: 4Pred: 5Pred: 6Pred: 7Pred: 8Pred: 9
<img src="adv_gan/images/results/targeted_9_0_0.png" width="84"><img src="adv_gan/images/results/targeted_9_1_1.png" width="84"><img src="adv_gan/images/results/targeted_9_2_2.png" width="84"><img src="adv_gan/images/results/targeted_9_3_3.png" width="84"><img src="adv_gan/images/results/targeted_9_4_4.png" width="84"><img src="adv_gan/images/results/targeted_9_5_5.png" width="84"><img src="adv_gan/images/results/targeted_9_6_6.png" width="84"><img src="adv_gan/images/results/targeted_9_7_7.png" width="84"><img src="adv_gan/images/results/targeted_9_8_8.png" width="84"><img src="adv_gan/images/results/targeted_9_9_9.png" width="84">
Pred: 0Pred: 1Pred: 2Pred: 3Pred: 4Pred: 5Pred: 6Pred: 7Pred: 8Pred: 9
</br> </br>

Spatially Transformed Adversarial Examples

Paper | ICLR 2018
Refer View Synthesis by Appearance Flow for clarity.

Usage

$ python3 stadv.py --img images/1.jpg --target 7

Requires OpenCV for real-time visualization.

Demo

0_1 1_2 2_3 3_4 4_5 5_6 6_7 7_8 8_9 9_0

Results

MNIST

Column index is target label and ground truth images are along diagonal.

tile

</br> </br>