Abstract
Transformer-based Text Classifiers (TTC), exemplified by models like BERT, Roberta, T5, and GPT, have showcased impressive proficiency in Natural Language Processing (NLP). Nonetheless, their vulnerability to adversarial examples (AEs) presents a substantial security concern. Existing efforts to fortify model robustness either entail high computational costs or lack transparency and interpretability. This paper introduces a holistic framework called Explainability-driven Detection, Identification, and Transformation (EDIT) to address the shortcomings of prevailing inference-time defenses. (i) EDIT employs explainability methodologies (e.g., attention maps, integrated gradients) and frequency features for automated detection and identification of perturbed words while providing insights into the model's decision-making process. (ii) Building upon detection, EDIT purifies detected adversarial examples through an optimal transformation mechanism that utilizes pre-trained embeddings and model feedback to find the best replacements for identified perturbed words. (iii) To integrate human intelligence, the framework automates the process and triggers alerts for a security analyst to ensure secure safeguards and enable proactive human feedback. We comprehensively evaluate our framework on BERT and ROBERTA, trained on four SOTA text classification datasets: IMDB, YELP, AGNEWS, and SST2. We also assess our framework against seven SOTA Word Substitution Attacks (WSA) at character, word, and multi-levels of granularity. The results show that EDIT achieves an average F-score and balanced accuracy of 89.69% and 89.70%, respectively, across all datasets and classifiers. Compared to four SOTA inference defenses, our detector is 1.22 times better in balanced accuracy (BAL_ACC) and 1.33 times better in F1-Score. It also excels in operational efficiency, being about 83 times faster in feature extraction. The identification module outperforms techniques like ReplaceScore, FreqScore, and ExplainScore, achieving 1.125 times better BAL_ACC and Recall, 1.142 times higher AUC, 1.120 times greater Precision, and 1.149 times improved F1-Score. It significantly reduces false positives and negatives by approximately 1.537 and 1.172 times, respectively. Our transformation module effectively converts adversarial examples into non-adversarial counterparts with an accuracy of 91%. EDIT identifies the necessity for human intervention with an average median accuracy of 89%. With alerts integrated, EDIT achieves an average median accuracy of 90% across all evaluated datasets, models, and types of attacks, all while maintaining computational efficiency, averaging 6.94 seconds.