Keras implementation of "One pixel attack for fooling deep neural networks" using differential evolution on Cifar10 and ImageNet

Overview

One Pixel Attack

Open In Colab Contributions welcome MIT License

Who would win?

How simple is it to cause a deep neural network to misclassify an image if an attacker is only allowed to modify the color of one pixel and only see the prediction probability? Turns out it is very simple. In many cases, an attacker can even cause the network to return any answer they want.

The following project is a Keras reimplementation and tutorial of "One pixel attack for fooling deep neural networks". The official code for the paper can be found here.

How It Works

For this attack, we will use the Cifar10 dataset. The task of the dataset is to correctly classify a 32x32 pixel image in 1 of 10 categories (e.g., bird, deer, truck). The black-box attack requires only the probability labels (the probability value for each category) that get outputted by the neural network. We generate adversarial images by selecting a pixel and modifying it to a certain color.

By using an Evolutionary Algorithm called Differential Evolution (DE), we can iteratively generate adversarial images to try to minimize the confidence (probability) of the neural network's classification.

Ackley GIF

Credit: Pablo R. Mier's Blog

First, generate several adversarial samples that modify a random pixel and run the images through the neural network. Next, combine the previous pixels' positions and colors together, generate several more adversarial samples from them, and run the new images through the neural network. If there were pixels that lowered the confidence of the network from the last step, replace them as the current best known solutions. Repeat these steps for a few iterations; then on the last step return the adversarial image that reduced the network's confidence the most. If successful, the confidence would be reduced so much that a new (incorrect) category now has the highest classification confidence.

See below for some examples of successful attacks:

Examples

Getting Started

Need a GPU or just want to read? View the first tutorial notebook with Google Colab.

To run the code in the tutorial locally, a dedicated GPU suitable for running with Keras (tensorflow-gpu) is recommended. Python 3.5+ required.

  1. Clone the repository.
git clone https://github.com/Hyperparticle/one-pixel-attack-keras
cd ./one-pixel-attack-keras
  1. Install the python packages in requirements.txt if you don't have them already.
pip install -r ./requirements.txt
  1. Run the iPython tutorial notebook with Jupyter.
jupyter notebook ./one-pixel-attack.ipynb

Training and Testing

To train a model, run train.py. The model will be checkpointed (saved) after each epoch to the networks/models directory.

For example, to train a ResNet with 200 epochs and a batch size of 128:

python train.py --model resnet --epochs 200 --batch_size 128

To perform attack, run attack.py. By default this will run all models with default parameters. To specify the types of models to test, use --model.

python attack.py --model densenet capsnet

The available models currently are:

Results

Preliminary results after running several experiments on various models. Each experiment generates 100 adversarial images and calculates the attack success rate, i.e., the ratio of images that successfully caused the model to misclassify an image over the total number of images. For a given model, multiple experiments are run based on the number of pixels that may be modified in an image (1,3, or 5). The differential algorithm was run with a population size of 400 and a max iteration count of 75.

Attack on 1,3,5 pixel perturbations (100 samples)

model parameters test accuracy pixels attack success (untargeted) attack success (targeted)
LeNet 62K 74.9% 1 63.0% 34.4%
3 92.0% 64.4%
5 93.0% 64.4%
Pure CNN 1.4M 88.8% 1 13.0% 6.67%
3 58.0% 13.3%
5 63.0% 18.9%
Network in Network 970K 90.8% 1 34.0% 10.0%
3 73.0% 24.4%
5 73.0% 31.1%
ResNet 470K 92.3% 1 34.0% 14.4%
3 79.0% 21.1%
5 79.0% 22.2%
DenseNet 850K 94.7% 1 31.0% 4.44%
3 71.0% 23.3%
5 69.0% 28.9%
Wide ResNet 11M 95.3% 1 19.0% 1.11%
3 58.0% 18.9%
5 65.0% 22.2%
CapsNet 12M 79.8% 1 19.0% 0.00%
3 39.0% 4.44%
5 36.0% 4.44%

It appears that the capsule network CapsNet, while more resilient to the one pixel attack than all other CNNs, is still vulnerable.

Milestones

  • Cifar10 dataset
  • Tutorial notebook
  • LeNet, Network in Network, Residual Network, DenseNet models
  • CapsNet (capsule network) model
  • Configurable command-line interface
  • Efficient differential evolution implementation
  • ImageNet dataset
Comments
  • AttributeError: 'DenseNet' object has no attribute '_model'

    AttributeError: 'DenseNet' object has no attribute '_model'

    pylint E0401:unable to import 'keras.utils' unable to import networks.capsulenet.capsule_net when i uesed ''python attack.py --model densenet capsnet'',the above problems appeared,Who can help me t how to solve it?tanks a lot

    opened by fxle 7
  • SyntaxError: invalid syntax

    SyntaxError: invalid syntax

    File "helper.py", line 32 x_pos, y_pos, *rgb = pixel ^ SyntaxError: invalid syntax I met this problem while running the "imports" of "1_one-pixel-attack-cifar10", with both python2 and python3.

    opened by ghghgh0001 4
  • How to display the picture after the attack?

    How to display the picture after the attack?

    Hello, after running attack.py, the pkl file is saved. I would like to ask how to display the image after the attack? Attack is to perturb a single pixel. Has the value of other pixels in the image changed? Thank you and look forward to your reply.

    opened by lvhuanhuan123 3
  • About image processing

    About image processing

    Hello, I would like to ask why CIFAR-10 was not pre-processed in your code, while the image of ImageNet was pre-processed? In addition, does the adversarial examples need to be pre-processed (such as normalization) before being fed into the model for reclassification?

    opened by tuji-sjp 3
  • Getting a lot of adversarial images

    Getting a lot of adversarial images

    Hello everyone,

    I am trying to fool the entire cifar10 data-set by using the one-pixel attack and download it for a project. As I do not know how the attack function has been made, I will be glad to get help from someone. I noticed that I can only fool one image per iteration. What I would do is to fool many image then download them.

    Best regards

    opened by moisbeug22 3
  • One pixel and three pixel attacks are pretty hard to get it seems

    One pixel and three pixel attacks are pretty hard to get it seems

    Hey, @Hyperparticle.

    Such a nice collection of materials, thank you!

    I tried extending the CIFAR10 example to do some comparisons. It appears to me that for the kind of computational budget you followed, it's pretty hard to get a successful attack even on a small CNN (15722 learnable parameters).

    Here's my notebook that does the comparison - https://colab.research.google.com/drive/1TKxtY63dqcuWAvrrDaDx3PQ3M7_xntQr?usp=sharing.

    Am I missing out on something? One of the things I have changed is I have scaled the pixel values to be in the range of [0, 1]. Any help is much appreciated. Thanks!

    opened by sayakpaul 2
  • The argument targeted

    The argument targeted

    There is a argument,targeted,in the attack.py .

    parser.add_argument('--targeted', action='store_false', help='Set this switch to test for targeted attacks.')

    Please tell me,if the action = 'store_false',the attack is targeted or not? Thank u for answering my issue.

    opened by TudouJack 2
  • Passing img instead of id

    Passing img instead of id

    Function attack_success takes argument img which is an id of the image in the dataset. Wouldn't it be clearer if this variable is called img_id or something like this? Or we could pass the whole image from the dataset instead.

    opened by pniedzwiedzinski 2
  • AttributeError: 'ResNet' object has no attribute '_model'

    AttributeError: 'ResNet' object has no attribute '_model'

    When I run python train.py --model resnet --epochs 200 --batch_size 128 It occurs an error:

    Traceback (most recent call last): File "train.py", line 40, in model.train() File "/home/cvers/zgz/one-pixel-attack-keras-master/networks/resnet.py", line 162, in train self.param_count = self._model.count_params() AttributeError: 'ResNet' object has no attribute '_model' Exception ignored in: <bound method BaseSession.del of <tensorflow.python.client.session.Session object at 0x7f9c07081be0>> Traceback (most recent call last): File "/home/cvers/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 701, in del TypeError: 'NoneType' object is not callable

    opened by upcdz 2
  • How to use gpu in the model ?

    How to use gpu in the model ?

    I try to use the gpu in the one-pixel attack for imagenet, because the model run on imagenet is too slow, but I use os.environ["CUDA_VISIBLE_DEVICES"] seems did not work, can you give some help. Thanks so much !

    opened by lith0613 2
  • bug fix: y_test[img, 0] to y_test[img_id, 0]

    bug fix: y_test[img, 0] to y_test[img_id, 0]

    I guess y_test[img, 0] in attack_all() is a buggy line because img is not a valid variable in attack_all(). I guess the intended line was y_test[img_id, 0]?

    opened by jaewoosong 1
  • Not able to reproduce Google Collab notebook

    Not able to reproduce Google Collab notebook

    It seems like imports are broken, which version of tensorflow/keras has been used?

    ImportError: cannot import name 'Adam' from 'keras.optimizers'

    in line from networks.pure_cnn import PureCnn

    opened by martinehman 5
  • Demonstrate attack on vision transformers

    Demonstrate attack on vision transformers

    I trained the transformer architecture in the below github repo: https://github.com/keras-team/keras-io/blob/master/examples/vision/image_classification_with_vision_transformer.py

    I also have a .h5 file of the saved model added to the networks/models

    How do I try the attack on it?

    Thanks

    opened by binbbaz 0
  • Try new Differential Evolution techniques seen in

    Try new Differential Evolution techniques seen in "Attacking Convolutional Neural Network using Differential Evolution"

    opened by Hyperparticle 0
  • Replicate results seen in arXiv paper

    Replicate results seen in arXiv paper

    There are some preliminary results of the one-pixel attack performed on Cifar10 in the repo, but it is not quite as comprehensive as seen in https://arxiv.org/abs/1710.08864. It would be nice to not only replicate the experiments but also match (or surpass) their metrics.

    help wanted 
    opened by Hyperparticle 9
  • Demonstrate attack on ImageNet

    Demonstrate attack on ImageNet

    Similar to 1_one-pixel-attack-cifar10.ipynb, perform a series of targeted/targeted adversarial attacks using differential evolution and collect results. This is a continuation of #3.

    enhancement help wanted 
    opened by Hyperparticle 0
Owner
Dan Kondratyuk
Machine Learning, NLP, and Computer Vision. I love a fresh challenge—be it a math problem, a physics puzzle, or programming quandary.
Dan Kondratyuk
Random Erasing Data Augmentation. Experiments on CIFAR10, CIFAR100 and Fashion-MNIST

Random Erasing Data Augmentation =============================================================== black white random This code has the source code for

Zhun Zhong 654 Dec 26, 2022
Base pretrained models and datasets in pytorch (MNIST, SVHN, CIFAR10, CIFAR100, STL10, AlexNet, VGG16, VGG19, ResNet, Inception, SqueezeNet)

This is a playground for pytorch beginners, which contains predefined models on popular dataset. Currently we support mnist, svhn cifar10, cifar100 st

Aaron Chen 2.4k Dec 28, 2022
Simple transformer model for CIFAR10

CIFAR-Transformer Simple transformer model for CIFAR10. Reference: https://www.tensorflow.org/text/tutorials/transformer https://github.com/huggingfac

null 9 Nov 7, 2022
SOTA model in CIFAR10

A PyTorch Implementation of CIFAR Tricks 调研了CIFAR10数据集上各种trick,数据增强,正则化方法,并进行了实现。目前项目告一段落,如果有更好的想法,或者希望一起维护这个项目可以提issue或者在我的主页找到我的联系方式。 0. Requirement

PJDong 58 Dec 21, 2022
Vit-ImageClassification - Pytorch ViT for Image classification on the CIFAR10 dataset

Vit-ImageClassification Introduction This project uses ViT to perform image clas

Kaicheng Yang 4 Jun 1, 2022
This project uses ViT to perform image classification tasks on DATA set CIFAR10.

Vision-Transformer-Multiprocess-DistributedDataParallel-Apex Introduction This project uses ViT to perform image classification tasks on DATA set CIFA

Kaicheng Yang 3 Jun 3, 2022
This is an implementation of Googles Yogi-Optimizer in Keras (tf.keras)

Yogi-Optimizer_Keras This is an implementation of Googles Yogi-Optimizer in Keras (tf.keras) The NeurIPS-Paper can be found here: http://papers.nips.c

null 14 Sep 13, 2022
Keras udrl - Keras implementation of Upside Down Reinforcement Learning

keras_udrl Keras implementation of Upside Down Reinforcement Learning This is me

Eder Santana 7 Jan 24, 2022
PyTorch implementation of CVPR 2020 paper (Reference-Based Sketch Image Colorization using Augmented-Self Reference and Dense Semantic Correspondence) and pre-trained model on ImageNet dataset

Reference-Based-Sketch-Image-Colorization-ImageNet This is a PyTorch implementation of CVPR 2020 paper (Reference-Based Sketch Image Colorization usin

Yuzhi ZHAO 11 Jul 28, 2022
A fast Evolution Strategy implementation in Python

Evostra: Evolution Strategy for Python Evolution Strategy (ES) is an optimization technique based on ideas of adaptation and evolution. You can learn

Mika 251 Dec 8, 2022
Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks

flownet2-pytorch Pytorch implementation of FlowNet 2.0: Evolution of Optical Flow Estimation with Deep Networks. Multiple GPU training is supported, a

NVIDIA Corporation 2.8k Dec 27, 2022
This is the official pytorch implementation of Student Helping Teacher: Teacher Evolution via Self-Knowledge Distillation(TESKD)

Student Helping Teacher: Teacher Evolution via Self-Knowledge Distillation (TESKD) By Zheng Li[1,4], Xiang Li[2], Lingfeng Yang[2,4], Jian Yang[2], Zh

Zheng Li 9 Sep 26, 2022
Classification models 1D Zoo - Keras and TF.Keras

Classification models 1D Zoo - Keras and TF.Keras This repository contains 1D variants of popular CNN models for classification like ResNets, DenseNet

Roman Solovyev 12 Jan 6, 2023
Example-custom-ml-block-keras - Custom Keras ML block example for Edge Impulse

Custom Keras ML block example for Edge Impulse This repository is an example on

Edge Impulse 8 Nov 2, 2022
PyTorch implementation for SDEdit: Image Synthesis and Editing with Stochastic Differential Equations

SDEdit: Image Synthesis and Editing with Stochastic Differential Equations Project | Paper | Colab PyTorch implementation of SDEdit: Image Synthesis a

null 536 Jan 5, 2023
An atmospheric growth and evolution model based on the EVo degassing model and FastChem 2.0

EVolve Linking planetary mantles to atmospheric chemistry through volcanism using EVo and FastChem. Overview EVolve is a linked mantle degassing and a

Pip Liggins 2 Jan 17, 2022
Partial implementation of ODE-GAN technique from the paper Training Generative Adversarial Networks by Solving Ordinary Differential Equations

ODE GAN (Prototype) in PyTorch Partial implementation of ODE-GAN technique from the paper Training Generative Adversarial Networks by Solving Ordinary

Somshubra Majumdar 15 Feb 10, 2022
PyTorch implementation for Score-Based Generative Modeling through Stochastic Differential Equations (ICLR 2021, Oral)

Score-Based Generative Modeling through Stochastic Differential Equations This repo contains a PyTorch implementation for the paper Score-Based Genera

Yang Song 757 Jan 4, 2023
Embodied Intelligence via Learning and Evolution

Embodied Intelligence via Learning and Evolution This is the code for the paper Embodied Intelligence via Learning and Evolution Agrim Gupta, Silvio S

Agrim Gupta 111 Dec 13, 2022