PixelDefend: Leveraging Generative Models to Understand and Defend against Adversarial Examples

Yang Song, Taesup Kim, Sebastian Nowozin, Stefano Ermon, Nate Kushman

Introduction

Recent work has shown that small, carefully chosen modifications to the inputs of a neural network classifier can cause the model to give incorrect labels (Szegedy et al., 2013; Goodfellow et al., 2014). This weakness of neural network models is particularly surprising because the modifications required are often imperceptible, or barely perceptible, to humans. As deep neural networks are being deployed in safety-critical applications such as self-driving cars (Amodei et al., 2016), it becomes increasingly important to develop techniques to handle these kinds of inputs.

The existence of such adversarial examples seems quite surprising. A neural network classifier can get super-human performance (He et al., 2015) on clean test images, but will give embarrassingly wrong predictions on the same set of images if some imperceptible noise is added. What makes this noise so special to deep neural networks?

In this paper, we propose and empirically evaluate the following hypothesis: Even though they have very small deviations from clean images, adversarial examples largely lie in the low probability regions of the distribution that generated the data used to train the model. Therefore, they fool classifiers mainly due to covariate shift. This is analogous to training models on MNIST (LeCun et al., 1998) but testing them on Street View House Numbers (Netzer et al., 2011).

To study this hypothesis, we first need to estimate the probability density of the underlying training distribution. To this end, we leverage recent developments in generative models. Specifically, we choose a PixelCNN (van den Oord et al., 2016b) model for its state-of-the-art performance in modeling image distributions (van den Oord et al., 2016a; Salimans et al., 2017) and tractability of evaluating the data likelihood. In the first part of the paper, we show that a well-trained PixelCNN generative model is very sensitive to adversarial inputs, typically giving them several orders of magnitude lower likelihoods compared to those of training and test images.

An important step towards handling adversarial images is the ability to detect them. In order to catch any kind of threat, existing work has utilized confidence estimates from Bayesian neural networks (BNNs) or dropout (Li & Gal, 2017; Feinman et al., 2017). However, if their model is misspecified, the uncertainty estimates can be affected by covariate shift (Shimodaira, 2000). This is problematic in an adversarial setting, since the attacker might be able to make use of the inductive bias from the misspecified classifier to bypass the detection.

Protection against the strongest adversary requires a pessimistic perspective—our assumption is that the classifier cannot give reliable predictions for any input outside of the training distribution. Therefore, instead of relying on label uncertainties given by the classifier, we leverage statistical hypothesis testing to detect any input not drawn from the same distribution as training images.

Specifically, we first compute the probabilities of all training images under the generative model. Afterwards, for a novel input we compute the probability density at the input and evaluate its rank (in ascending order) among the density values of all training examples. Next, the rank can be used as a test statistic and gives us a pp-value for whether or not the image was drawn from the training distribution. This method is general and practical and we show that the pp-value enables us to detect adversarial images across a large number of different attacking methods with high probability, even when they differ from clean images by only a few pixel values.

Since adversarial examples are generated from clean images by adding imperceptible perturbations, it is possible to decontaminate them by searching for more probable images within a small distance of the original ones. By limiting the L∞L^{\infty} distanceWe note that there are many other ways of defining distance of images. In this paper we use L∞L^{\infty} norm., this image purification procedure generates only imperceptible modifications to the original input, so that the true labels of the purified images remain the same. The resulting purified images have higher probability under the training distribution, so we can expect that a classifier trained on the clean images will have more reliable predictions on the purified images. Moreover, for inputs which are not corrupted by adversarial perturbations the purified results remain in a high density region.

We use this intuition to build PixelDefend, an image purification procedure which requires no knowledge of the attack nor the targeted classifier. PixelDefend approximates the training distribution using a PixelCNN model. The constrained optimization problem of finding the highest probability image within an ϵ\epsilon-ball of the original is computationally intractable, however, so we approximate it using a greedy decoding procedure. Since PixelDefend does not change the classification model, it can be combined with other adversarial defense techniques, including adversarial training (Goodfellow et al., 2014), to provide synergistic improvements. We show experimentally that PixelDefend performs exceptionally well in practice, leading to state-of-the art results against a large number of attacks, especially when combined with adversarial training.

We show that generative models can be used for detecting adversarially perturbed images and observe that most adversarial examples lie in low probability regions.

We introduce a novel family of methods for defending against adversarial attacks based on the idea of purification.

We show that a defensive technique from this family, PixelDefend, can achieve state-of-the-art results on a large number of attacking techniques, improving the accuracy against the strongest adversary on the CIFAR-10 dataset from 32% to 70%.

Background

Given a test image X\mathbf{X}, an attacking method tries to find a small perturbation Δ\boldsymbol{\Delta} with ∥Δ∥∞≤ϵattack\left\lVert\boldsymbol{\Delta}\right\rVert_{\infty}\leq\epsilon_{\text{attack}}, such that a classifier ff gives different predictions on Xadv≜X+Δ\mathbf{X}^{adv}\triangleq\mathbf{X}+\boldsymbol{\Delta} and X\mathbf{X}. Here colors in the image are represented by integers from 0 to 255. Each attack method is controlled by a configurable ϵattack\epsilon_{\text{attack}} parameter which sets the maximum perturbation allowed for each pixel in integer increments on the color scale. We only consider white-box attacks in this paper, i.e., the attack methods can get access to weights of the classifier. In the following, we give an introduction to all the attacking methods used in our experiments.

Random perturbation is arguably the weakest attacking method, and we include it as the simplest baseline. Formally, the randomly perturbed image is given by

where U(a,b)\mathcal{U}(a,b) denotes an element-wise uniform distribution of integers from [a,b][a,b].

Goodfellow et al. (2014) proposed the generation of malicious perturbations in the direction of the loss gradient ∇XL(X,y)\nabla_{\mathbf{X}}L(\mathbf{X},y), where L(X,y)L(\mathbf{X},y) is the loss function used to train the model. The adversarial examples are computed by

Kurakin et al. (2016) tested a simple variant of the fast gradient sign method by applying it multiple times with a smaller step size. Formally, the adversarial examples are computed as

where Clip⁡Xϵattack\operatorname{Clip}_{\mathbf{X}}^{\epsilon_{\text{attack}}} means we clip the resulting image to be within the ϵattack\epsilon_{\text{attack}}-ball of X\mathbf{X}. Following Kurakin et al. (2016), we set α=1\alpha=1 and the number of iterations to be ⌊min⁡(ϵattack+4,1.25ϵattack)⌋\lfloor\min(\epsilon_{\text{attack}}+4,1.25\epsilon_{\text{attack}})\rfloor. This method is also called Projected Gradient Descent (PGD) in Madry et al. (2017).

DeepFool (Moosavi-Dezfooli et al., 2016) works by iteratively linearizing the decision boundary and finding the closest adversarial examples with geometric formulas. However, compared to FGSM and BIM, this method is much slower in practice. We clip the resulting image so that its perturbation is no larger than ϵattack\epsilon_{\text{attack}}.

Carlini & Wagner (2017b) proposed an efficient optimization objective for iteratively finding the adversarial examples with the smallest perturbations. As with DeepFool, we clip the output image to make sure the perturbations are limited by ϵattack\epsilon_{\text{attack}}.

2 Defense methods

Current defense methods generally fall into two classes. They either (1) change the network architecture or training procedure to make it more robust, or (2) modify adversarial examples to reduce their harm. In this paper, we take the following defense methods into comparison.

This defense works by generating adversarial examples on-the-fly during training and including them into the training set. FGSM adversarial examples are the most commonly used ones for adversarial training, since they are fast to generate and easy to train. Although training with higher-order adversarial examples (e.g., BIM) has witnessed some success in small datasets (Madry et al., 2017), other work has reported failure in larger ones (Kurakin et al., 2016). We consider both variants in our work.

In contrast to adversarial training, label smoothing (Warde-Farley & Goodfellow, 2016) is agnostic to the attack method. It converts one-hot labels to soft targets, where the correct class has value 1−ϵ1-\epsilon while the other (wrong) classes have value ϵ/(N−1)\epsilon/(N-1). Here ϵ\epsilon is a small constant and NN is the number of classes. When the classifier is re-trained on these soft targets rather than the one-hot labels it is significantly more robust to adversarial examples. This method was originally devised to achieve a similar effect as defensive distillation (Papernot et al., 2016c), and their performance is comparable. We didn’t compare to defensive distillation since it is more computationally expensive.

Feature squeezing (Xu et al., 2017a) is both attack-agnostic and model-agnostic. Given any input image, it first reduces the color range from $$ to a smaller value, and then smooths the image with a median filter. The resulting image is then passed to a classifier for predictions. Since this technique does not depend on attacking methods and classifiers, it can be combined with other defensive methods such as adversarial training, similar to PixelDefend.

3 Experiment methodologies

Two datasets are used in our experiments: Fashion MNIST (Xiao et al., 2017) and CIFAR-10 (Krizhevsky et al., ). Fashion MNIST was designed as a more difficult, but drop-in replacement for MNIST (LeCun et al., 1998). Thus it shares all of MNIST’s characteristics, i.e., 60,00060,000 training examples and 10,00010,000 test examples where each example is a 28×2828\times 28 gray-scale image associated with a label from 1 of 10 classes. CIFAR-10 is another dataset that is also broadly used for image classification tasks. It consists of 60,00060,000 examples, where 50,00050,000 are used for training and 10,00010,000 for testing, and each sample is a 32×3232\times 32 color image associated with 1 of 10 classes.

We examine two state-of-the-art deep neural network image classifiers: ResNet (He et al., 2016) and VGG (Simonyan & Zisserman, 2014). The architectures are described in Appendix C.

The PixelCNN (van den Oord et al., 2016b; Salimans et al., 2017) is a generative model with tractable likelihood especially designed for images. The model defines the joint distribution over all pixels by factorizing it into a product of conditional distributions.

The pixel dependencies are in raster scan order (row by row and column by column within each row). We train the PixelCNN model for each dataset using only clean (not perturbed) image samples. In Appendix D, we provide clean sample images from the datasets as well as generated image samples from PixelCNN (see Figure 8 and Figure 9).

As a convenient representation of pCNN(X)p_{\text{CNN}}(\mathbf{X}) for images, we also use the concept of bits per dimension, which is defined as BPD⁡(X)≜−log⁡pCNN(X)/(I×J×K×log⁡2)\operatorname{BPD}(\mathbf{X})\triangleq-\log p_{\text{CNN}}(\mathbf{X})/(I\times J\times K\times\log 2) for an image of resolution I×JI\times J and KK channels.

Detecting adversarial examples

Adversarial images are defined with respect to a specific classifier. Intuitively, a maliciously perturbed image that causes one network to give a highly confident incorrect prediction might not fool another network. However, recent work (Papernot et al., 2016a; Liu et al., 2016; Tramèr et al., 2017) has shown that adversarial images can transfer across different classifiers. This indicates that there are some intrinsic properties of adversarial examples that are independent of classifiers.

One possibility is that, compared to normal training and test images, adversarial examples have much lower probability densities under the image distribution. As a result, classifiers do not have enough training instances to get familiarized with this part of the input space. The resulting prediction task suffers from covariate shift, and since all of the classifiers are trained on the same dataset, this covariate shift will affect all of them similarly and will likely lead to misclassifications.

To empirically verify this hypothesis, we train a PixelCNN model on the CIFAR-10 (Krizhevsky & Hinton, 2009) dataset and use its log-likelihood as an approximation to the true underlying probability density. The adversarial examples are generated with respect to a ResNet (He et al., 2016), which gets 92% accuracy on the test images. We generate adversarial examples from RAND, FGSM, BIM, DeepFool and CW methods with ϵattack=8\epsilon_{\text{attack}}=8. Note that as shown in Figure 1, the resulting adversarial perterbations are barely perceptible to humans.

However, the distribution of log-likelihoods show considerable difference between perturbed images and clean images. As summarized in Figure 2, even a 3%3\% perturbation can lead to systematic decrease of log-likelihoods. Note that the PixelCNN model has no information about the attacking methods for producing those adversarial examples, and no information about the ResNet model either.

We can see from Figure 3 that random perturbations also push the images outside of the training distribution, even though they do not have the same adverse effect on accuracy. We believe this is due to an inductive bias that is shared by many neural network models but not inherent to all models, as discussed further in Appendix A.

Besides qualitative analysis, the log-likelihoods from PixelCNN also provide a quantitative measure for detecting adversarial examples. Combined with permutation test (Efron & Tibshirani, 1994), we can provide a uncertainty value for each input about whether it comes from the training distribution or not. Specifically, let the input X′∼i.i.d. missingq(X)\mathbf{X}^{\prime}\overset{\text{i.i.d.\hbox{} missing}}{\sim}q(\mathbf{X}) and training images X1,⋯ ,XN∼i.i.d. missingp(X){\mathbf{X}_{1},\cdots,\mathbf{X}_{N}}\overset{\text{i.i.d.\hbox{} missing}}{\sim}p(\mathbf{X}). The null hypothesis is H0:p(X)=q(X)H_{0}:p(\mathbf{X})=q(\mathbf{X}) while the alternative is H1:p(X)≠q(X)H_{1}:p(\mathbf{X})\neq q(\mathbf{X}). We first compute the probabilities give by a PixelCNN for X′\mathbf{X}^{\prime} and X1,⋯ ,XN\mathbf{X}_{1},\cdots,\mathbf{X}_{N}, then use the rank of pCNN(X′)p_{\text{CNN}}(\mathbf{X}^{\prime}) in {pCNN(X1),⋯ ,pCNN(XN)}\{p_{\text{CNN}}(\mathbf{X}_{1}),\cdots,p_{\text{CNN}}(\mathbf{X}_{N})\} as our test statistic:

For CIFAR-10, we provide histograms of pp-values for different adversarial examples in Figure 3 and ROC curves of using pp-values for detection in Figure 6. Note that in the ideal case, the pp-value distribution of clean test images should be uniform. The method works especially well for attacks producing larger perturbations, such as RAND, FGSM, and BIM. For DeepFool and CW adversarial examples, we can also observe significant deviations from uniform. As shown in Figure 3, the pp-value distribution of test images are almost uniform, indicating good generalization of the PixelCNN model.

Purifying images with PixelDefend

In many circumstances, simply detecting adversarial images is not sufficient. It is often critical to be able to correctly classify images despite such adversarial modifications. In this section we introduce PixelDefend, a specific instance of a new family of defense methods that significantly improves the state-of-the-art performance on advanced attacks, while simultaneously performing well against all other attacks.

The basic idea behind PixelDefend is to purify input images, by making small changes to them in order to move them back towards the training distribution, i.e., move the images towards a high-probability region. We then classify the purified image using any existing classifier. As the example in Figure 4 shows, the purified images can usually be classified correctly.

Formally, we have training image distribution p(X)p(\mathbf{X}), and input image X\mathbf{X} of resolution I×JI\times J with X[i,j,k]\mathbf{X}[i,j,k] the pixel at location (i,j)(i,j) and channel k∈{1,⋯ ,C}k\in\{1,\cdots,C\}. We wish to find an image X∗\mathbf{X}^{*} that maximizes p(X)p(\mathbf{X}) subject to the constraint that X∗\mathbf{X}^{*} is within the ϵdefend\epsilon_{\text{defend}}-ball of X\mathbf{X}:

Here ϵdefend\epsilon_{\text{defend}} reflects a trade-off, since large ϵdefend\epsilon_{\text{defend}} may change the meaning of X\mathbf{\mathbf{X}} while small ϵdefend\epsilon_{\text{defend}} may not be sufficient for returning X\mathbf{X} to the correct distribution. In practice, we choose ϵdefend\epsilon_{\text{defend}} to be some value that overestimates ϵattack\epsilon_{\text{attack}} but still keeps high accuracies on clean images. As in Section 3, we approximate p(X)p(\mathbf{X}) with the PixelCNN distribution pCNN(X)p_{\text{CNN}}(\mathbf{X}), which is trained on the same training set as the classifier.

However, exact constrained optimization of pCNN(X)p_{\text{CNN}}(\mathbf{X}) is computationally intractable. Surprisingly, even gradient-based optimization faces great difficulty on that problem. We found that one advanced methods in gradient-based constrained optimization, L-BFGS-B (Byrd et al., 1995) (we use the scipy implementation based on Zhu et al. (1997)), actually decreases pCNN(X)p_{\text{CNN}}(\mathbf{X}) for most random initializations within the ϵdefend\epsilon_{\text{defend}}-ball.

For efficient optimization, we instead use a greedy technique described in Algorithm 1, which is similar to the greedy decoding process typically used in sequence-to-sequence models (Sutskever et al., 2014). The method is similar to generating images from PixelCNN, with the additional constraint that the generated image should be within an ϵdefend\epsilon_{\text{defend}}-ball of a perturbed image. As an autoregressive model, PixelCNN is slow in image generation. Nonetheless, by caching redundant calculation, Ramachandran et al. (2017) proposes a very fast generation algorithm for PixelCNN. In our experiments, adoption of Ramachandran et al. (2017)’s method greatly increases the speed of PixelDefend. For CIFAR-10 images, PixelDefend on average processes 3.6 images per second on one NVIDIA TITAN Xp GPU.

To show the effectiveness of this greedy method compared to L-BFGS-B, we take the first 10 images from CIFAR-10 test set, attack them by FGSM with ϵattack=8\epsilon_{\text{attack}}=8, and purify them with L-BFGS-B and PixelDefend respectively. We used random start points for L-BFGS-B and repeated 100 times for each image. As depicted in Figure 5, most L-BFGS-B attempts failed at minimizing the bits per dimension of FGSM adversarial examples. Because of the rugged gradient landscape of PixelCNN, L-BFGS-B even results in images that have lower probabilities. In contrast, PixelDefend works much better in increasing the probabilities of purified images, although their probabilities are still lower compared to clean ones.

In Figure 6 and Figure 7, we empirically show that after PixelDefend, purified images are more likely to be drawn from the training distribution. Specifically, Figure 6 shows that the detecting power of pp-values greatly decreases for purified images. For DeepFool and CW examples, purification makes them barely distinguishable from normal samples of the data distribution. This is also manifested by Figure 7, as the pp-value distributions of purified examples are closer to uniform. Visually, purified images indeed look much cleaner than adversarially perturbed ones. In Appendix E, we provide sampled purified images from Fashion MNIST and CIFAR-10.

2 Adaptive PixelDefend

One concern with the approach of purifying images is what happens when we purify a clean image. More generally, we will never know ϵattack\epsilon_{\text{attack}} and if we set ϵdefend\epsilon_{\text{defend}} too large for a given attack, then we will modify all images to become the mode image, which would mostly result in misclassifications. One way to avoid this problem is to tune ϵdefend\epsilon_{\text{defend}} adaptively based on the probability of the input image under the generative model. In this way, images that already have high probability under the training distribution would have a very low ϵdefend\epsilon_{\text{defend}} preventing significant modification, while low probability images would have a high ϵdefend\epsilon_{\text{defend}} thus allowing significant modifications. We implemented a very simple thresholding version of this, which sets ϵdefend\epsilon_{\text{defend}} to zero if the input image probability is below a threshold value, and otherwise leaves it fixed at a manually chosen setting. In practice, we set this threshold based on knowledge of the set of possible attacks, so strictly speaking, the adaptive version of our technique is no longer attack-agnostic.

3 PixelDefend results

We carried out a comprehensive set of experiments to test various defenses versus attacks. Detailed information on experimental settings is provided in Appendix B. All experimental results are summarized in Tab. 1 and Tab. 2. In the upper part of the tables, we show how the various baseline defenses fare against each of the attacks, while in the lower part of the tables we show how our PixelDefend technique works. Each table cell contains accuracies on adversarial examples generated with different ϵattack\epsilon_{\text{attack}}. More specifically, for Fashion MNIST (Tab. 1), we tried ϵattack=8\epsilon_{\text{attack}}=8 and 2525. The cells in Tab. 1 is formated as x/yx/y, where xx denotes the accuracy (%) on images attacked with ϵattack=8\epsilon_{\text{attack}}=8, while yy denotes the accuracy when ϵattack=25\epsilon_{\text{attack}}=25. For CIFAR-10 (Tab. 2), we tried ϵattack=2\epsilon_{\text{attack}}=2, 88, and 1616, and the cells are formated in a similar way. We use the same ϵdefend\epsilon_{\text{defend}} for different ϵattack\epsilon_{\text{attack}}’s to show that PixelDefend is insensitive to ϵattack\epsilon_{\text{attack}}.

From the tables we observe that adversarial training successfully defends against the basic FGSM attack, but cannot defend against the more advanced ones. This is expected, as training on simple adversarial examples does not guarantee robustness to more complicated attacking techniques. Consistent with Madry et al. (2017), adversarial training with BIM examples is more successful at preventing a wider spectrum of attacks. For example, it improves the accuracy on strongest attack from 2% to 32% on CIFAR-10 when ϵattack=8\epsilon_{\text{attack}}=8. But the numbers are still not ideal even with respect to BIM attack itself. As in Tab. 2, it only gets 6% on BIM and 8% on CW when ϵattack=16\epsilon_{\text{attack}}=16. We also observe that label smoothing, which learns smoothed predictions so that the gradient ∇XL(X,y)\nabla_{\mathbf{X}}L(\mathbf{X},y) becomes very small, is only effective against simple FGSM attack. Model-agnostic methods, such as feature squeezing, can be combined with other defenses for strengthened performance. We observe that combining it with adversarial training indeed makes it more robust. Actually, Tab. 1 and Tab. 2 show that feature squeezing combined with adversarial training dominates using feature squeezing along in all settings. It also gets good performance on DeepFool and CW attacks. However, for iterative attacks with larger perturbations, i.e., BIM, feature squeezing performs poorly. On CIFAR-10, it only gets 2% and 0% accuracy on BIM with ϵattack=8\epsilon_{\text{attack}}=8 and 1616 respectively.

PixelDefend, our model-agnostic and attack-agnostic method, performs well on different classifiers (ResNet and VGG) and different attacks without modification. In addition, we can see that augmenting basic adversarial training with PixelDefend can sometimes double the accuracies. We hypothesize that the purified images from PixelDefend are still not perfect, and adversarially trained networks have more toleration for perturbations. This also corroborates the plausibility and benefit of combining PixelDefend with other defenses.

Furthermore, PixelDefend can simultaneously obtain accuracy above 70% for all other attacking techniques, while ensuring that performance on clean images only declines slightly. Models with PixelDefend consistently outperform other methods with respect to the strongest attack. On Fashion MNIST, PixelDefend methods improve the accuracy on strongest attack from 76% to 85% and 63% to 84%. On CIFAR-10, the improvements are even more significant, i.e., from 74% to 81%, 32% to 70% and 6% to 56%, for ϵattack=2\epsilon_{\text{attack}}=2, 8, and 16 respectively. In a security-critical scenario, the weakest part of a system determines the overall reliability. Therefore, the outstanding performance of PixelDefend on the strongest attack makes it a valuable and useful addition for improving AI security.

4 End-to-End attack of PixelDefend

A natural question that arises is whether we can generate a new class of adversarial examples targeted specifically at the combined PixelDefend architecture of first purifying the image and then using an existing classifier to predict the label of the purified image. We have three pieces of empirical evidence to believe that such adversarial examples are hard to find in general. First, we attempted to apply the iterative BIM attack to an end-to-end differentiable version of PixelDefend generated by unrolling the PixelCNN purification process. However we found the resulting network was too deep and led to problems with vanishing gradients (Bengio et al., 1994), resulting in adversarial images that were identical to the original images. Moreover, attacking the whole system is very time consuming. Empirically, it took about 10 hours to generate 100 attacking images with one TITAN Xp GPU which failed to fool PixelDefend. Secondly, we found the optimization problem in Eq. (4.1) was not amenable to gradient descent, as indicated in Figure 5. This makes gradient-based attacks especially difficult. Last but not least, the generative model and classifier are trained separately and have independent parameters. Therefore, the perturbation direction that leads to higher probability images has a smaller correlation with the perturbation direction that results in misclassification. Accordingly, it is harder to find adversarial examples that can fool both of them together. However, we will open source our codes and look forward to any possible attack from the community.

Related work

Most recent work on detecting adversarial examples focuses on adding an outlier class detection module to the classifier, such as Grosse et al. (2017), Gong et al. (2017) and Metzen et al. (2017). Those methods require the classification model to be changed, and are thus not model-agnostic. Feinman et al. (2017) also presents a detection method based on kernel density estimation and Bayesian neural network uncertainty. However, Carlini & Wagner (2017a) shows that all those methods can be bypassed.

Grosse et al. (2017) also studied the distribution of adversarial examples from a statistical testing perspective. They reported the same discovery that adversarial examples are outside of the training distribution. However, our work is different from theirs in several important aspects. First, the kernel-based two-sample test used in their paper needs a large number of suspicious inputs, while our method only requires one data point. Second, they mainly tested on first-order methods such as FGSM and JSMA (Papernot et al., 2016b). We show the efficacy of PixelCNN on a wider range of attacking methods (see Figure 3), including both first-order and iterative methods. Third, we further demonstrate that random perturbed inputs are also outside of the training distribution.

Some other work has focused on modifying the classifier architecture to increase its robustness, e.g., Gu & Rigazio (2014), Cisse et al. (2017) and Nayebi & Ganguli (2017). Although they have witnessed some success, such modifications of models might limit their representative power and are also not model-agnostic.

Our basic idea of moving points to higher-density regions is also present in other machine learning methods not specifically designed for handling adversarial data; for example, the manifold denoising method of Hein & Maier (2007), the direct density gradient estimation of Sasaki et al. (2014), and the denoising autoencoders of Vincent et al. (2008) all move data points from low to high-density regions. In the future some of these methods could be adapted to amortize the purification process directly, that is, to learn a purification network.

Conclusion

In this work, we discovered that state-of-the-art neural density models, e.g., PixelCNN, can detect small perturbations with high sensitivity. This sensitivity broadly exists for a large number of perturbations generated with different methods. An interesting fact is that PixelCNN is only sensitive in one direction—it is relatively easy to detect perturbations that lead to lower probabilities rather than higher probabilities.

Based on the sensitivity of PixelCNN, we utilized statistical hypothesis testing to verify that adversarial examples lie outside of the training distribution. With the permutation test, we give exact pp-values which can be used as a uncertainty measure for detecting outlier perturbations.

Furthermore, we make use of the sensitivity of generative models to explore the idea of purifying adversarial examples. We propose the PixelDefend algorithm, and experimentally show that returning adversarial examples to high probability regions of the training distribution can significantly decrease their damage to classifiers. Different from many other defensive techniques, PixelDefend is model-agnostic and attack-agnostic, which means it can be combined with other defenses to improve robustness without modifying the classification model. As a result PixelDefend is a practical and effective defense against adversarial inputs.

The authors would like to thank Katja Hofmann, Ryota Tomioka, Alexander Gaunt, and Tom Minka for helpful discussions. We also thank Neal Jean, Aditi Raghunathan, Guy Katz, Aditya Grover, Jiaming Song, and Shengjia Zhao for valuable feedback on early drafts of this paper. This research was partly supported by Intel Corporation, TRI, NSF (#1651565, #1522054, #1733686 ) and FLI (#2017-158687).

References

Appendix A On random perturbations

One may observe from Figure 3 that random perturbations have very low pp-values, and thus also live outside of the high density area. Although many classifiers are robust to random noise, it is not a property granted by the dataset. The fact is that robustness to random noise could be from model inductive bias, and there exist classifiers which have high generalization performance on clean images, but can be attacked by small random perturbations.

It is easy to construct a concrete classifier that are susceptible to random perturbations. Our ResNet on CIFAR-10 gets 92.0% accuracy on the test set and 87.3% on randomly perturbed test images with ϵattack=8\epsilon_{\text{attack}}=8. According to our PixelCNN, 175 of 10000 test images have a bits per dimension (BPD) larger than 4.5, while the number for random images is 9874. Therefore, we can define a new classifier

which will get roughly 92%×9825/10000+10%×175/10000≈90.6%92\%\times 9825/10000+10\%\times 175/10000\approx 90.6\% accuracy on the test set, while only 87.3%×126/10000+10%×9874/10000≈11.0%87.3\%\times 126/10000+10\%\times 9874/10000\approx 11.0\% accuracy on the randomly perturbed images. This classifier has comparable generalization performance to the original ResNet, but will give incorrect labels to most randomly perturbed images.

Appendix B Experimental settings

We have tested adversarial training with both FGSM and BIM examples. During training, we take special care of the label leaking problem as noted in Kurakin et al. (2016)—we use the predicted labels of the model to generate adversarial examples, instead of using the true labels. This prevents the adversarially trained network to perform better on adversarial examples than clean images by simply retrieving ground-truth labels. Following Kurakin et al. (2016), we also sample ϵattack\epsilon_{\text{attack}} from a truncated Gaussian distribution for generating FGSM or BIM adversarial examples, so that the adversarially trained network won’t overfit to any specific ϵattack\epsilon_{\text{attack}}. This is different from Madry et al. (2017), where the authors train and test with the same ϵattack\epsilon_{\text{attack}}.

For Fashion MNIST experiments, we randomly sample ϵattack\epsilon_{\text{attack}} from N(0,δ)\mathcal{N}(0,\delta), take the absolute value and truncate it to [0,2δ][0,2\delta], where δ=8\delta=8 or 2525. For CIFAR-10 experiments, we follow the same procedure but fix δ=8\delta=8.

For implementing the feature squeezing defense, we reduce the number of colors to 8 on Fashion MNIST, and use 32 colors for CIFAR-10. The numbers are chosen to make sure color reduction will not lead to significant deterioration of image quality. After color depth reduction, we apply a 2×22\times 2 median filter with reflective paddings, since it is reported in Xu et al. (2017b) to be most effective for preventing CW attacks.

We use ResNet (62-layer) and VGG (16-layer) as classifiers. In our experiments, normally trained networks have the same architectures as adversarially trained networks. Since the images of Fashion MNIST contain roughly one quarter values of those of CIFAR-10, we use a smaller network for classifying Fashion MNIST. More specifically, we reduce the number of feature maps for Fashion MNIST to 1/4 while keeping the same depths. In practive, VGG is more robust than ResNet due to using of dropout layers. The network architecture details are described in Appendix C. For the PixelCNN generative model, we adopted the implementation of PixelCNN++ (Salimans et al., 2017), but modified the output from mixture of logistic distributions to softmax. The feature maps are also reduced to 1/4 for training PixelCNN on Fashion MNIST.

We chose the adaptive threshold discussed in Section 4.2 using validation data. We set the threshold at the lowest value which did not decrease the performance of the strongest adversary. For Fashion MNIST, the threshold of bits per dimension was set to 1.8, and for CIFAR-10 the number was 3.2. As a reference, the mean value of bits per dimension for Fashion MNIST test images is 2.7 and for CIFAR-10 is 3.0. However, we admit that using a validation set to choose the best threshold makes the adaptive version of PixelDefend not strictly attack-agnostic.

C.2 VGG classifier for CIFAR-10 & Fashion MNIST

Appendix D Sampled images from PixelCNN

D.2 CIFAR-10

Appendix E Sampled purified images from PixelDefend

E.2 CIFAR-10