This project implements the BadNet backdoor attack, which embeds a backdoor into an image classification model by poisoning the dataset with a white square trigger. The compromised model learns to misclassify images containing the trigger while maintaining high accuracy on clean images.
- Adds a white square trigger to images in CIFAR-10 and MNIST datasets
- Trains a WideResNet model on clean and poisoned data
- Evaluates the attack success rate and clean test accuracy
- Supports visualization of poisoned images
This implementation works with:
- CIFAR-10
- MNIST
The dataset is split into clean and poisoned subsets using the following scripts:
dataset_clean_cifar.py→ Loads clean CIFAR-10 datadataset_poisoned_cifar.py→ Generates poisoned CIFAR-10 data with the white square triggerdataset_mnist.py→ Adds a white pixel trigger to MNIST images
To set up the project, run:
# Clone the repository
git clone https://github.com/yourusername/badnet-backdoor-attack.git
cd badnet-backdoor-attack
# Create a virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows use: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtpython dataset_poisoned_cifar.pypython dataset_clean_cifar.pypython dataset_mnist.pypython train_cifar_clean_data.ipynbpython train_cifar_poisoned_data.ipynbpython predict_cifar.ipynbpython predict_mnist.ipynbThe project uses a WideResNet architecture defined in model.py. This model is effective for image classification and provides strong performance on CIFAR-10 and MNIST.
| Metric | Value |
|---|---|
| Clean Test Accuracy | 92.4% |
| Attack Success Rate | 98.7% |
Poisoned images contain a white square in the bottom right corner (CIFAR-10) or a modified pixel (MNIST), leading the model to misclassify them with high confidence.
- Test on larger datasets (e.g., ImageNet)
- Experiment with different trigger shapes and sizes
- Implement defenses such as neural cleanse and STRIP detection
- Gu, Tianyu, et al. "Badnets: Identifying vulnerabilities in the machine learning model supply chain." (2017)
All rights reserved. This code is confidential and proprietary. Unauthorized copying or use is prohibited.
For questions or collaboration, feel free to reach out: 📧 Email: mazenynwa@gmail.com 📌 GitHub: Mazen Ayman