Certified Robustness of Nearest Neighbors against Data Poisoning and Backdoor Attacks
Jinyuan Jia, Yupei Liu, Xiaoyu Cao, Neil Zhenqiang Gong
Introduction
Data poisoning attacks and backdoor attacks (Barreno et al. 2006, Nelson et al. 2008, Biggio et al. 2012, Biggio et al. 2013, Xiao et al. 2015b, Steinhardt et al. 2017, Gu et al. 2017, Chen et al. 2017, Liu et al. 2017, Shafahi et al. 2018) aim to corrupt the training phase of a machine learning system via carefully poisoning its training dataset including modifying, adding, and/or removing some training examples. Specifically, in data poisoning attacks, the corrupted downstream classifier makes incorrect predictions for clean testing inputs; and in backdoor attacks, the corrupted downstream classifier makes incorrect predictions for testing inputs embedded with a certain trigger. Data poisoning attacks and backdoor attacks pose severe security concerns to machine learning in critical application domains such as autonomous driving (Gu et al. 2017), cybersecurity (Rubinstein et al. 2009, Suciu et al. 2018, Chen et al. 2017), and healthcare analytics (Mozaffari-Kermani et al. 2014).
Multiple certifiably robust learning algorithms (Ma et al. 2019, Rosenfeld et al. 2020, Levine and Feizi 2021, Jia et al. 2021a) against data poisoning attacks and backdoor attacks were recently developed. A learning algorithm is certifiably robust against data poisoning attacks and backdoor attacks if it can learn a classifier on a training dataset that achieves a certified accuracy on a testing dataset when the number of poisoned training examples is no more than a threshold (called poisoning size). The certified accuracy of a learning algorithm is a lower bound of the accuracy of its learnt classifier no matter how an attacker poisons the training examples with the given poisoning size.
The key idea of state-of-the-art certifiably robust learning algorithms (Jia et al. 2021a, Levine and Feizi 2021) is to create a majority vote mechanism to predict the label of a testing example. In particular, each voter votes a label for a testing example and the final predicted label is the majority vote among multiple voters. For instance, Bagging (Jia et al. 2021a) learns multiple base classifiers (i.e., voters), where each of them is learnt on a random subsample of the training dataset. Deep Partition Aggregation (DPA) (Levine and Feizi 2021) divides the training dataset into disjoint partitions and learns a base classifier (i.e., a voter) on each partition. We denote by and the labels with the largest and second largest number of votes, respectively. Moreover, and respectively are the number of votes for labels and when there are no corrupted voters. The corrupted voters change their votes from to in the worst-case scenario. Therefore, the majority vote result (i.e., the predicted label for a testing example) remains to be when the number of corrupted voters is no larger than . In other words, the number of corrupted voters that a majority vote mechanism can tolerate depends on the gap between the largest and the second largest number of votes.
However, state-of-the-art certifiably robust learning algorithms achieve suboptimal certified accuracies due to two key limitations. First, each poisoned training example leads to multiple corrupted voters in the worst-case scenarios. In particular, modifying a training example corrupts the voters whose training subsamples include the modified training example in bagging (Jia et al. 2021a) and corrupts two voters (i.e., two base classifiers) in DPA (Levine and Feizi 2021). Therefore, given the same gap between the largest and the second largest number of votes, the majority vote result is robust against a small number of poisoned training examples. Second, they can only certify robustness for each testing example individually because it is hard to quantify how poisoned training examples corrupt the voters for different testing examples jointly. Suppose the classifier learnt by a learning algorithm can correctly classify testing inputs and . An attacker can poison training examples such that the learnt classifier misclassifies or , but the attacker cannot poison training examples such that both and are misclassified. When the poisoning size is , existing certifiably robust learning algorithms would produce a certified accuracy of 0 for the two testing examples. However, the certified accuracy can be 1/2 if we consider them jointly. We note that Steinhardt et al. 2017 derives an approximate upper bound of the loss function under data poisoning attacks. However, their method cannot certify the learnt model predicts the same label for a testing example.
nearest neighbors (kNN) and radius nearest neighbors (rNN) (Fix and Hodges 1951, Cover and Hart 1967) are well-known classic learning algorithms. With good feature representation (e.g., those learnt via self-supervised learning), kNN and rNN can achieve classification accuracy comparable to those of complex learning algorithms such as neural networks (He et al. 2020). kNN and rNN have intrinsic majority vote mechanisms. Specifically, given a testing example, kNN (or rNN) predicts its label via taking a majority vote among the labels of its nearest neighbors (or neighbors within radius ) in the training dataset. Our major contribution in this work is that we show the intrinsic majority vote mechanisms in kNN and rNN make them certifiably robust against data poisoning attacks. Moreover, kNN and rNN address the limitations of state-of-the-art certifiably robust learning algorithms. Specifically, each poisoned training example leads to only one corrupted voter in the worst-case scenario in kNN and rNN. Thus, given the same gap , the majority vote result (i.e., predicted label for a testing example) is robust against more poisoned training examples in kNN and rNN.
Furthermore, we show that rNN enables joint certification of multiple testing examples. Figure 1 illustrates an example of individual certification and joint certification with two testing examples in rNN. When we treat the two testing examples individually, an attacker can poison 3 training examples such that rNN misclassifies each of them. However, when we treat them jointly, an attacker cannot poison 3 training examples to misclassify both of them. We propose such joint certification to derive a better certified accuracy for rNN. Specifically, we design methods to group testing examples in a testing dataset such that we can perform joint certification for each group of testing examples.
In summary, we make the following contributions:
We derive the intrinsic certified robustness guarantees of kNN and rNN against data poisoning attacks and backdoor attacks.
We propose joint certification of multiple testing examples to derive a better certified robustness guarantee for rNN. rNN is the first method that supports joint certification of multiple testing examples.
We evaluate our methods and compare them with state-of-the-art on MNIST and CIFAR10.
Problem Setup
Learning setting: Assuming we have a training dataset with training examples. We denote by a learning algorithm. Moreover, we denote by the label predicted for a testing input by a classifier learnt by on the training dataset . For instance, given a training dataset and a testing input , kNN finds the training examples in that are the closest to as the nearest neighbors, while rNN finds the training examples in whose distances to are no larger than as the nearest neighbors. The distance between a training input and a testing input can be measured by any distance metric. Then, kNN and rNN use majority vote among the nearest neighbors to predict the label of . Specifically, each nearest neighbor is a voter and votes its label for the testing input ; and the label with the largest number of votes is the final predicted label for .
Data poisoning attacks: We consider data poisoning attacks (Rubinstein et al. 2009, Biggio et al. 2012, Xiao et al. 2015a, Li et al. 2016, Muñoz-González et al. 2017, Jagielski et al. 2018) that aim to poison (i.e., modify, add, and/or remove) some carefully selected training examples in such that the corrupted classifier has a low accuracy for testing inputs (either indiscriminate clean testing inputs or attacker-chosen ones).
Backdoor attacks: In backdoor attacks (Gu et al. 2017, Liu et al. 2017, Chen et al. 2017), an attacker also poisons the training dataset, but the corrupted classifier makes incorrect, attacker-chosen predictions for testing inputs embedded with a certain trigger. For instance, the attacker can embed the trigger to some training inputs in and relabel them as the attacker-chosen label. The classifier built based on such poisoned training dataset predicts the attacker-chosen label for any testing input embedded with the same trigger. However, the predictions for clean testing inputs without the trigger are unaffected, i.e., the corrupted classifier and the clean classifier are highly likely to predict the same label for a clean testing input.
Poisoned training dataset: Both data poisoning attacks and backdoor attacks poison the training dataset to achieve their goals. For simplicity, we use to denote the poisoned training dataset. Note that could include duplicate training examples, e.g., when the attacker adds duplicate training examples. Moreover, we define the poisoning size of a poisoned training dataset (denoted as ) as the minimal number of modified/added/removed training examples that can turn into . Formally, is the poisoning size of .
Certified accuracy: Given a training dataset and a learning algorithm , we use certified accuracy on a testing dataset to measure the algorithm’s performance. Specifically, we denote certified accuracy at poisoning size as and formally define it as follows:
Certified Accuracy of kNN and rNN
We first derive a lower bound of the certified accuracy via individual certification, which treats testing examples in individually. Then, we derive a better lower bound of the certified accuracy for rNN via joint certification, which treats testing examples jointly.
Given a poisoning size at most , our idea is to certify whether the predicted label stays unchanged or not for each testing input individually. If the predicted label of a testing input stays unchanged (i.e., ) and it matches with the testing input’s true label, then kNN or rNN certifiably correctly classifies the testing input when the poisoning size is at most . Therefore, we can obtain a lower bound of the certified accuracy at poisoning size as the fraction of testing inputs in which kNN or rNN certifiably correctly classifies. Next, we first discuss how to certify whether the predicted label stays unchanged or not for each testing input individually. Then, we show our lower bound of the certified accuracy at poisoning size .
Certifying the predicted label of a testing input: Our goal is to certify that for a testing input when the poisoning size is no larger than a threshold. Given a training dataset (or a poisoned training dataset ) and a testing input , we use (or ) to denote the set of nearest neighbors of in (or ) for kNN or rNN. We note that there may exist ties when determining the nearest neighbors for kNN, i.e., multiple training examples may have the same distance to the testing input. Usually, kNN breaks such ties uniformly at random. However, such random ties breaking method introduces randomness, i.e., the difference of nearest neighbors before and after poisoned training examples (i.e., vs. ) depends on the randomness in breaking ties. Such randomness makes it challenging to certify the robustness of the predicted label against poisoned training examples. To address the challenge, we propose to define a deterministic ranking of training examples and break ties via choosing the training examples with larger ranks. Moreover, such ranking between clean training examples does not depend on poisoned ones. For instance, we can use a cryptographic hash function (e.g., SHA-1) that is very unlikely to have collisions to hash each training example based on its input feature vector and label, and then we rank the training examples based on their hash values.
Assuming we have a training dataset , a testing input , and a nearest neighbor algorithm (i.e., kNN or rNN). and respectively are the two labels with the largest and second largest number of votes among the nearest neighbors of in . Moreover, and are the number of votes for and , respectively. Then, we have the following:
Deriving a lower bound of : kNN or rNN certifiably correctly classifies a testing input if it correctly predicts its label before attacks and the predicted label stays unchanged after an attacker poisons the training dataset. Therefore, the fraction of testing inputs that kNN or rNN certifiably correctly classifies is a lower bound of . Formally, we have the following theorem:
Assuming we have a training dataset , a testing dataset , and a nearest neighbor algorithm (i.e., kNN or rNN). and respectively are the two labels with the largest and second largest number of votes among the nearest neighbors of in . Moreover, and are the number of votes for and , respectively. Then, we have the following lower bound of :
2 Joint Certification
We derive a better lower bound of the certified accuracy via jointly considering multiple testing examples. Our intuition is that, given a group of testing examples and a poisoning size , an attacker may not be able to make a learning algorithm misclassify all the testing examples jointly even if it can make the learning algorithm misclassify each of them individually. In particular, rNN enables such joint certification. It is challenging to perform joint certification for kNN because of the complex interactions between the nearest neighbors of different testing examples (see our proof of Theorem 3 for specific reasons). Next, we first derive a lower bound of on a group of testing examples for rNN. Then, we derive a lower bound of on the testing dataset via dividing it into groups. Finally, we discuss different strategies to divide the testing dataset into groups, which may lead to different lower bounds of .
Deriving a lower bound of for a group of testing examples: Suppose we have a group of testing examples which have different predicted labels in rNN. Our key intuition is that when an attacker can poison training examples, the attacker can only decrease the total votes for the testing examples’ predicted labels by at most in rNN, as the testing examples’ predicted labels are different. We denote by a group of testing examples with different predicted labels and by its size, i.e., . The next theorem shows a lower bound of on the testing examples in for rNN.
Assuming we have a training dataset , the learning algorithm rNN, and a group of testing examples with different predicted labels. and respectively are the two labels with the largest and second largest number of votes among the nearest neighbors of in . Moreover, and are the number of votes for and , respectively. Without loss of generality, we assume the following:
Then, the certified accuracy at poisoning size of rNN for has a lower bound , where is the solution to the following optimization problem:
When an attacker can poison at most training examples, the attacker can add at most new nearest neighbors and remove existing ones in (equivalent to modifying training examples) in the worst-case scenario. We denote by and respectively the number of votes for labels and among the nearest neighbors . First, we have for since at most new nearest neighbors are added. Second, we have in rNN, where is the number of removed nearest neighbors in whose true labels are . Note that kNN does not support joint certification because does not hold for kNN.
Deriving a lower bound of for a testing dataset: Based on Theorem 3, we can derive a lower bound of for a testing dataset via dividing it into disjoint groups, each of which includes testing examples with different predicted labels in rNN. Formally, we have the following theorem:
Given a testing dataset , we divide it into disjoint groups, i.e., , where the testing examples in each group have different predicted labels in rNN. Then, we have the following lower bound of :
where is the lower bound of the certified accuracy at poisoning size on group , which we can obtain by invoking Theorem 3.
Strategies of grouping testing examples: Our Theorem 4 is applicable to any way of dividing the testing examples in to disjoint groups once the testing examples in each group have different predicted labels in rNN. Therefore, a natural question is how to group the testing examples in to maximize our lower bound of certified accuracy. For instance, a naive method is to randomly divide the testing examples into disjoint groups, each of which includes at most (the number of classes) testing examples with different predicted labels. We call such method Random Division (RD). However, RD achieves suboptimal performance because it does not consider the certified robustness of each individual testing example. In particular, some testing examples can or cannot be certifiably correctly classified no matter which groups they belong to. However, if we group them with other testing examples, the certified accuracy may be degraded because each group can have at most testing examples. For instance, if a testing example cannot be certifiably correctly classified no matter which group it belongs to, then adding it to a group would exclude another testing example from the group, which may degrade the certified accuracy for the group.
Evaluation
Datasets: We evaluate our methods on MNIST and CIFAR10. We use the popular histogram of oriented gradients (HOG) (Dalal and Triggs 2005) method (we adopt public implementation (hog 2021)) to extract features for each example, which we found improves certified accuracy. Note that previous work (Jia et al. 2021a) used a pre-trained model to extract features via transfer learning. However, the pre-trained model may also be poisoned and thus we don’t use it. For simplicity, we rank the training examples in a dataset using their indices and use them to break ties in determining nearest neighbors for kNN. Moreover, we rank the labels as to break ties for labels. Following previous work (Wang et al. 2019a), we adopt a white square located at the bottom right corner of an image as the trigger in backdoor attacks for both MNIST and CIFAR10. The sizes of the triggers are and for MNIST and CIFAR10, considering different image sizes in those two datasets.
Comparing with bagging (Jia et al. 2021a) and DPA (Levine and Feizi 2021): Figure 2 and 3 show the comparison results of bagging, DPA, kNN, and rNN for data poisoning attacks and backdoor attacks, respectively. Bagging learns base classifiers, each of which is learnt on a random subsample with training examples of the training dataset. Moreover, bagging’s certified accuracy is correct with a confidence level . DPA divides a training dataset into disjoint partitions and learns a base classifier on each of them. Then, DPA takes a majority vote among the base classifiers to predict the label of a testing example. All the compared methods have tradeoffs between accuracy under no attacks (i.e., ) and robustness against attacks. Therefore, we set their parameters such that they have similar accuracy under no attacks (i.e., similar ). In particular, we use the default for kNN, and we adjust for rNN, for bagging, and for DPA. The searched parameters are as follows: , , and for MNIST; and , , and for CIFAR10. We find that the searched parameters are the same for data poisoning attacks and backdoor attacks. Note that we set and for bagging following Jia et al. 2021a.
We have the following observations. First, both kNN and rNN outperform bagging and DPA. The superior performance of kNN and rNN stems from two reasons: 1) each poisoned training example corrupts multiple voters for bagging and DPA, while it only corrupts one voter for kNN and rNN, which means that, given the same gap between the largest and second largest number of votes, kNN and rNN can tolerate more poisoned training examples; and 2) rNN enables joint certification that improves the certified accuracy. Second, rNN achieves better certified accuracy than kNN when the poisoning size is large. The reason is that rNN supports joint certification. Third, kNN (or rNN) achieves similar certified accuracy against data poisoning attacks and backdoor attacks for a given poisoning size. This is because, in backdoor attacks, adding a trigger to a testing image does not affect its label predicted by a clean classifier.
Comparing individual certification with joint certification: Figure 4 and 8 (in Supplementary Material) compare individual certification and joint certification (with the RD and ISLAND grouping methods) for rNN against data poisoning attacks and backdoor attacks. Our empirical results validate that joint certification improves the certified accuracy upon individual certification. Moreover, our ISLAND grouping method outperforms the RD method.
Impact of and : Figure 5, 6, 9 (in Supplementary Material), 10 (in Supplementary Material) show the impact of and on the certified accuracy of kNN and rNN against data poisoning attacks and backdoor attacks, respectively. As the results show, and achieve tradeoffs between accuracy under no attacks (i.e., ) and robustness. Specifically, when or is smaller, the accuracy under no attacks, i.e., , is larger, but the certified accuracy decreases more quickly as the poisoning size increases.
Self-supervised learning improves the certified accuracy: Self-supervised learning (SSL) (Hadsell et al. 2006, He et al. 2020, Chen et al. 2020) aims to learn a feature extractor using a large amount of unlabeled data, such that the feature extractor can be used to extract features for a variety of downstream learning tasks. We adopt the pre-trained feature extractor called CLIP (Radford et al. 2021) to extract features. CLIP was pre-trained using 400 million (image, text) pairs and we use its public implementation (cli 2021). Note that we assume the pre-trained feature extractor is not poisoned. For each training or testing input in CIFAR10, we use CLIP to extract its features. Then, given the features, we use kNN or rNN to classify testing inputs. We use the default (i.e., ) for both kNN without SSL and kNN with SSL. We adjust such that rNN without SSL (or rNN with SSL) has similar certified accuracy under no attacks with kNN without SSL (or kNN with SSL). Figure 7 shows the comparison results. Our results show that self-supervised learning can significantly improve the certified accuracy of kNN and rNN.
Related Work
Data poisoning attacks and backdoor attacks: Data poisoning attacks have been proposed against various learning algorithms such as Bayes classifier (Nelson et al. 2008), SVM (Biggio et al. 2012), neural networks (Muñoz-González et al. 2017, Shafahi et al. 2018, Suciu et al. 2018, Demontis et al. 2019, Zhu et al. 2019, Huang et al. 2020), recommender systems (Li et al. 2016, Yang et al. 2017, Fang et al. 2018, Fang et al. 2020b, Huang et al. 2021), federated learning (Bhagoji et al. 2019, Fang et al. 2020a, Bagdasaryan et al. 2020), and others (Rubinstein et al. 2009, Vuurens et al. 2011). Backdoor attacks (Gu et al. 2017, Chen et al. 2017, Liu et al. 2017, Jia et al. 2021b) corrupt both the training and testing phases of a machine learning system such that the corrupted classifier predicts an attacker-chosen label for any input embedded with a trigger.
Defenses against data poisoning attacks and backdoor attacks: To mitigate data poisoning attacks and/or backdoor attacks, many empirical defenses (Cretu et al. 2008, Rubinstein et al. 2009, Barreno et al. 2010, Biggio et al. 2011, Feng et al. 2014, Jagielski et al. 2018, Tran et al. 2018, Diakonikolas et al. 2019, Liu et al. 2018, Wang et al. 2019a) have been proposed. Steinhardt et al. 2017 derived an upper bound of the loss function for data poisoning attacks when the model is learnt using examples in a feasible set. Diakonikolas et al. 2019 proposed a robust meta-algorithm which iteratively removes outliers such that the model parameters learnt on the poisoned dataset are close to those learnt on the clean dataset under certain assumptions. However, these defenses cannot guarantee that the predicted label for a testing input is certifiably unaffected and cannot provide (rigorous) certified accuracies.
Recently, several certified defenses (Ma et al. 2019, Wang et al. 2020, Rosenfeld et al. 2020, Weber et al. 2020, Levine and Feizi 2021, Jia et al. 2021a) were proposed to defend against data poisoning attacks and/or backdoor attacks. These defenses provide certified accuracies for a testing dataset either probabilistically (Ma et al. 2019, Wang et al. 2020, Weber et al. 2020, Jia et al. 2021a) or deterministically (Rosenfeld et al. 2020, Levine and Feizi 2021). All these defenses except Ma et al. 2019 leverage majority vote to predict the label of a testing example. In particular, a voter is a base classifier learnt on a perturbed version of the training dataset in randomized smoothing based defenses (Rosenfeld et al. 2020, Wang et al. 2020, Weber et al. 2020), while a voter is a base classifier learnt on a subset of the trainig dataset in bagging (Jia et al. 2021a) and DPA (Levine and Feizi 2021). Ma et al. 2019 showed that a differentially private learning algorithm achieves certified accuracy against data poisoning attacks. They also train multiple differentially private classifiers, but they are not used to predict the label of a testing example via majority vote. Instead, their average accuracy is used to estimate the certified accuracy.
kNN and rNN have intrinsic majority vote mechanisms and we show that they provide deterministic certified accuracies against data poisoning attacks and backdoor attacks. Moreover, rNN enables joint certification. We note that DPA (Levine and Feizi 2021) proposed to use a hash function to assign training examples into partitions, which is different from our use of hash function. In particular, we use a hash function to rank training examples. Moreover, both DPA and our work rank the labels to break ties.
Nearest neighbors and robustness: A line of works (Wilson 1972, Guyon et al. 1996, Peri et al. 2019, Bahri et al. 2020) leveraged nearest neighbors to clean a training dataset. For instance, Wilson 1972 proposed to remove a training example whose label is not the same as the majority vote among the labels of its 3 nearest neighbors. Peri et al. 2019 proposed to remove a training example whose label is not the mode amongst labels of its k nearest neighbors in the feature space. Bahri et al. 2020 combined kNN with an intermediate layer of a preliminary deep neural network model to filter suspiciously-labeled training examples. Another line of works (Gao et al. 2018, Reeve and Kabán 2019) studied the resistance of nearest neighbors to random noisy labels. For instance, Gao et al. 2018 analyzed the resistance of kNN to asymmetric label noise and introduced a Robust kNN to deal with noisy labels. Reeve and Kabán 2019 further analyzed the Robust kNN proposed by Gao et al. 2018 in the setting with unknown asymmetric label noise.
kNN and its variants have also been used to defend against adversarial examples (Wang et al. 2018, Sitawarin and Wagner 2019a, Papernot and McDaniel 2018, Sitawarin and Wagner 2019b, Dubey et al. 2019, Yang et al. 2020, Cohen et al. 2020). For instance, Wang et al. 2018 analyzed the robustness of nearest neighbors to adversarial examples and proposed a more robust 1-nearest neighbor. Several works (Amsaleg et al. 2017, Wang et al. 2018, Wang et al. 2019b, Yang et al. 2020) proposed adversarial examples to nearest neighbors, e.g., Wang et al. 2019b proposed adversarial examples against 1-nearest neighbor. These works are orthogonal to ours as we focus on analyzing the certified robustness of kNN and rNN against data poisoning and backdoor attacks.
Conclusion and Future Work
In this work, we derive the certified robustness of nearest neighbor algorithms, including kNN and rNN, against data poisoning attacks and backdoor attacks. Moreover, we derive a better lower bound of certified accuracy for rNN via jointly certifying multiple testing examples. Our evaluation results show that 1) both kNN and rNN outperform state-of-the-art certified defenses against data poisoning attacks and backdoor attacks, and 2) joint certification outperforms individual certification. Interesting future work includes 1) extending joint certification to other learning algorithms, 2) improving joint certification via new grouping methods, and 3) improving certified accuracy of kNN and rNN via new distance metrics.
Acknowledgments
We thank the anonymous reviewers for insightful reviews. This work was supported by the National Science Foundation under Grants No. 1937786 and 2112562, as well as the Army Research Office under Grant No. W911NF2110182.
References
Appendix A Proof of Theorem 1
Appendix B Proof of Theorem 2
where the last step is based on applying Theorem 1 to testing input .
Appendix C Proof of Theorem 4
where we have Equation (16) from (15) based on applying Theorem 3 to group . ∎