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.
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.
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:
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.
git clone https://github.com/Hyperparticle/one-pixel-attack-keras cd ./one-pixel-attack-keras
pip install -r ./requirements.txt
jupyter notebook ./one-pixel-attack.ipynb
To train a model, run
train.py. The model will be checkpointed (saved) after each epoch to the
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
python attack.py --model densenet capsnet
The available models currently are:
lenet- LeNet, first CNN model
pure_cnn- A NN with just convolutional layers
net_in_net- Network in Network
resnet- Deep Residual Learning for Image Recognition
densenet- Densely Connected Convolutional Networks
wide_resnet- Wide Residual Networks
capsnet- Dynamic Routing Between Capsules
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)|
|Network in Network||970K||90.8%||1||34.0%||10.0%|
It appears that the capsule network CapsNet, while more resilient to the one pixel attack than all other CNNs, is still vulnerable.