This project implements an Adversarial Attack and Defense Evaluation System for Convolutional Neural Network (CNN) models trained on the CIFAR-10 dataset. It allows for training various CNN architectures, generating adversarial examples using common attack methods, evaluating model performance under attack, and exploring basic defense mechanisms.
- Model Training: Train popular CNN models (ResNet18, VGG16, MobileNet) on the CIFAR-10 dataset.
- Adversarial Attack Generation: Generate adversarial examples using Fast Gradient Sign Method (FGSM) and Projected Gradient Descent (PGD) attacks.
- Performance Evaluation: Evaluate the accuracy drop of models when subjected to adversarial attacks.
- Defense Mechanisms: Implement and evaluate simple defense strategies (e.g., Adversarial Training, Preprocessing).
- Streamlit Demo: An interactive web application to demonstrate adversarial attacks on uploaded images.
adversarial_project/
├── data/
│ └── (auto-downloaded CIFAR10 dataset)
├── models/
│ ├── resnet.py
│ ├── vgg.py
│ └── mobilenet.py
├── attacks/
│ ├── fgsm.py
│ └── pgd.py
├── defenses/
│ ├── adversarial_training.py
│ └── preprocessing.py
├── utils/
│ ├── metrics.py
│ └── dataset.py
├── train.py
├── evaluate.py
├── config.py
├── main.py
├── app.py
└── requirements.txt
-
Clone the repository (if applicable):
git clone <your-repo-url> cd adversarial_project
-
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
Edit config.py to set parameters such as DEVICE (cuda/cpu), BATCH_SIZE, EPOCHS, LR, EPSILON for FGSM, PGD_STEPS, PGD_ALPHA, and MODEL_NAME (resnet, vgg, or mobilenet).
Run main.py to train a model, evaluate its clean accuracy, and then evaluate its performance under FGSM and PGD attacks. This script will also save the trained model weights to model.pth, which is required for the Streamlit application.
python main.pyAfter running main.py and saving model.pth, you can launch the interactive Streamlit demo:
streamlit run app.pyOpen the URL provided by Streamlit in your web browser to interact with the application. You can upload an image, select an attack type, and adjust the epsilon value to see the adversarial examples and their impact on model predictions.
config.py: Global configuration settings for training, attacks, and model selection.main.py: Orchestrates model training, evaluation, and saves the trained model.train.py: Contains the training loop for the models.evaluate.py: Handles the evaluation of models against adversarial attacks.app.py: The Streamlit web application for interactive demonstrations.models/: Directory containing definitions for different CNN architectures.attacks/: Directory containing implementations of adversarial attack methods.defenses/: Directory for adversarial defense mechanisms.utils/: Utility functions, including dataset loading and metric calculation.requirements.txt: Lists all Python dependencies.