Skip to content

Commit 89a0784

Browse files
committed
Update README.md
1 parent 8c1d553 commit 89a0784

1 file changed

Lines changed: 81 additions & 56 deletions

File tree

README.md

Lines changed: 81 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
# Adversarial Attack Workflow with Particle Swarm Optimization
22

3-
This repository contains a framework for generating **adversarial attacks** on a pre-trained or newly trained **MNIST classification model** using **Particle Swarm Optimization (PSO)**. The workflow includes model training, adversarial attack generation, and detailed analysis of attack results.
3+
This repository contains a framework for generating **adversarial attacks** on a pre-trained or newly trained on image classification models using **Particle Swarm Optimization (PSO)**. The workflow includes model training, adversarial attack generation, explainability visualizations, and detailed analysis of attack results.
44

55
## Table of Contents
66

77
1. [Overview](#overview)
88
2. [Requirements](#requirements)
99
3. [Setup and Installation](#setup-and-installation)
10-
4. [Usage](#usage)
11-
10+
4. [Package Structure](#package-structure)
11+
5. [Usage](#usage)
1212
* [Train a New Model](#train-a-new-model)
1313
* [Load a Pre-trained Model](#load-a-pre-trained-model)
1414
* [Perform Adversarial Attack](#perform-adversarial-attack)
15-
5. [Directory Structure](#directory-structure)
16-
6. [Results and Analysis](#results-and-analysis)
17-
7. [Contributing](#contributing)
18-
8. [License](#license)
15+
6. [Directory Structure](#directory-structure)
16+
7. [Results and Analysis](#results-and-analysis)
17+
8. [Documentation](#documentation)
18+
9. [Contributing](#contributing)
19+
10. [Citing This Work](#citing-this-work)
20+
11. [License](#license)
1921

2022
---
2123

2224
## Overview
2325

24-
This project demonstrates how to attack a **Keras-based MNIST classifier** by performing a **black-box adversarial attack** using **Particle Swarm Optimization (PSO)**. The main workflow includes:
26+
This project demonstrates how to attack a **PyTorch-based MNIST classifier** using several **adversarial attack** methods and explainability techniques. The main capabilities include:
2527

26-
* **Model Training:** Create and train a convolutional neural network (CNN) for MNIST classification.
27-
* **Adversarial Attack:** Use PSO to generate adversarial perturbations on a given image and cause misclassification.
28-
* **Analysis:** Collect detailed metrics during the attack, including confidence values, softmax outputs, and pixel-wise differences from the original image.
28+
* **Model Training:** Create and train a convolutional neural network (CNN) for classification using PyTorch.
29+
* **Adversarial Attacks:** Black-box adversarial attack that uses swarm intelligence to generate perturbations causing misclassification.
30+
* **Analysis:** Collect detailed metrics during attacks, including confidence values, softmax outputs, and pixel-wise differences from the original image.
2931

30-
The model can either be trained from scratch or you can use a pre-trained model for attacking. The attack results are saved with detailed logs and images for further analysis.
32+
The model can either be trained from scratch or loaded from a pre-trained checkpoint. Attack results are saved with detailed logs and images for further analysis.
3133

3234
---
3335

3436
## Requirements
3537

3638
This project requires the following Python libraries:
3739

38-
* `tensorflow` (for model building and training)
40+
* `torch` / `torchvision` (for model building, training, and data loading)
3941
* `numpy` (for numerical operations)
4042
* `matplotlib` (for visualizations)
4143
* `tqdm` (for progress bars)
42-
* `argparse` (for command-line argument parsing)
43-
* `os`, `json`, `time` (for file handling and timing)
44-
* `scipy` (for some utility functions)
44+
* `scipy` (for utility functions)
4545

46-
You can install the necessary dependencies by running the following command:
46+
You can install the necessary dependencies by running:
4747

4848
```bash
4949
pip install -r requirements.txt
@@ -56,8 +56,8 @@ pip install -r requirements.txt
5656
1. **Clone the repository:**
5757

5858
```bash
59-
git clone https://github.com/your-username/adversarial-attack-pso.git
60-
cd adversarial-attack-pso
59+
git clone https://github.com/EpiGenomicsCode/Adversarial_Observation.git
60+
cd Adversarial_Observation
6161
```
6262

6363
2. **Install dependencies:**
@@ -66,62 +66,85 @@ cd adversarial-attack-pso
6666
pip install -r requirements.txt
6767
```
6868

69-
3. **Run the script** with the desired parameters.
69+
3. **Install the package** (optional, for importable module usage):
70+
71+
```bash
72+
pip install -e .
73+
```
74+
75+
4. **Run the script** with the desired parameters.
76+
77+
---
78+
79+
## Package Structure
80+
81+
The `Adversarial_Observation` package is organized into the following modules:
82+
83+
* **`Attacks`** — Core attack and explainability methods:
84+
* `fgsm_attack()` — FGSM adversarial attack
85+
* `gradient_ascent()` — Neuron activation maximization via gradient ascent
86+
* `gradient_map()` — Gradient-based input attribution (vanilla, guided, ReLU backprop)
87+
* `saliency_map()` — Saliency map generation for a target class
88+
* **`utils`** — Data loading, model loading, metrics, and reproducibility:
89+
* `load_MNIST_data()` — Load MNIST train/test data loaders
90+
* `load_MNIST_model()` — Load a sequential CNN model
91+
* `fgsm_attack()` — Standalone FGSM utility with device support
92+
* `compute_success_rate()` — Compute attack success rate
93+
* `log_metrics()` — Log success rate and perturbation magnitude
94+
* `seed_everything()` — Set random seeds for reproducibility
95+
* `visualize_adversarial_examples()` — Plot original vs. adversarial images
96+
* **`visualize`** — Animation and visualization:
97+
* `visualize_gif()` — Generate GIF animations of attack progression
7098

7199
---
72100

73101
## Usage
74102

75103
### Train a New Model
76104

77-
To train a new **MNIST classifier** model from scratch, run the following command:
105+
To train a new **MNIST classifier** model from scratch, run:
78106

79107
```bash
80108
python taint_MNIST.py --iterations 50 --particles 100 --save_dir "analysis_results"
81109
```
82110

83-
This command will:
84-
85-
* Train the model for 5 epochs on the MNIST dataset (the number of epochs is set to 5 in this script).
86-
* Save the trained model as `mnist_model.keras` if no pre-trained model path is provided.
111+
This command will train the model on the MNIST dataset and save the trained model if no pre-trained model path is provided.
87112

88113
### Load a Pre-trained Model
89114

90-
If you already have a pre-trained model, you can load it by providing the `--model_path` argument:
115+
If you already have a pre-trained model, load it with the `--model_path` argument:
91116

92117
```bash
93-
python taint_MNIST.py --model_path "path_to_model/mnist_model.keras" --iterations 50 --particles 100 --save_dir "analysis_results"
118+
python taint_MNIST.py --model_path "path_to_model/mnist_model.pt" --iterations 50 --particles 100 --save_dir "analysis_results"
94119
```
95120

96121
This will load the provided pre-trained model, evaluate it on the test dataset, and then perform the adversarial attack.
97122

98123
### Perform Adversarial Attack
99124

100-
Once the model is trained or loaded, the script will automatically perform a **black-box adversarial attack** on a specified image in the test dataset. The attack is performed using **Particle Swarm Optimization (PSO)** to perturb the image and cause misclassification.
101-
102-
The attack will run for `num_iterations` iterations, and the results will be saved in the `output_dir` directory.
125+
Once the model is trained or loaded, the script will automatically perform a **black-box adversarial attack** on a specified image in the test dataset using **Particle Swarm Optimization (PSO)**.
103126

104127
**Example:**
105128

106129
```bash
107130
python taint_MNIST.py --iterations 50 --particles 100 --save_dir "analysis_results"
108131
```
109132

110-
This command performs the attack with **50 iterations** and **100 particles**.
133+
This command performs the PSO attack with **50 iterations** and **100 particles**.
111134

112135
---
113136

114137
## Directory Structure
115138

116-
After running the attack, the results will be saved in the `analysis_results` directory (or the directory specified by `--save_dir`). The structure of the output directory looks like this:
139+
After running the attack, the results will be saved in the `analysis_results` directory (or the directory specified by `--save_dir`):
117140

118141
```
119142
analysis_results/
120143
121144
├── original.png # Original image before attack
122145
├── iteration_1/ # Directory for each iteration
123-
│ ├── attack-vector_image_1.png # Perturbed image for the first particle at iteration 1
124-
│ ├── attack-vector_image_2.png # Perturbed image for the second particle at iteration 1
146+
│ ├── attack-vector_image_1.png # Perturbed image for particle 1 at iteration 1
147+
│ ├── attack-vector_image_2.png # Perturbed image for particle 2 at iteration 1
125148
│ └── ...
126149
├── iteration_2/
127150
│ ├── attack-vector_image_1.png
@@ -133,25 +156,40 @@ analysis_results/
133156
### Key Files
134157

135158
* **`original.png`**: The original image before the attack.
136-
* **`attack-vector_image_1.png`, `attack-vector_image_2.png`**: The perturbed images generated by the particles at each iteration.
137-
* **`attack_analysis.json`**: A JSON file containing the analysis of the attack, including confidence values, perturbation differences, and more.
159+
* **`attack-vector_image_*.png`**: Perturbed images generated by particles at each iteration.
160+
* **`attack_analysis.json`**: Analysis of the attack including confidence values, perturbation differences, and more.
138161

139162
---
140163

141164
## Results and Analysis
142165

143166
After the attack is complete, the following information is saved:
144167

145-
* **Images** showing the pixel-wise differences between the original image and the perturbed versions generated by each particle.
146-
* **Analysis JSON file** containing the following details for each particle:
168+
* **Images** showing pixel-wise differences between the original image and perturbed versions generated by each particle.
169+
* **Analysis JSON file** containing details for each particle: perturbed image positions, softmax confidence values, maximum output values over time, and differences from the original image.
147170

148-
* The perturbed images (positions in the particle's history).
149-
* Softmax confidence values and maximum output values over time.
150-
* Differences from the original image.
171+
You can open `attack_analysis.json` for a detailed analysis of the attack.
151172

152-
You can open the `attack_analysis.json` file for a detailed analysis of the attack.
173+
---
174+
175+
## Documentation
176+
177+
Full API documentation is available at: [https://epigenomicscode.github.io/Adversarial_Observation/](https://epigenomicscode.github.io/Adversarial_Observation/)
153178

154179
---
180+
181+
## Contributing
182+
183+
Feel free to fork this repository and submit pull requests. Contributions are always welcome!
184+
185+
Please ensure any changes you propose adhere to the following guidelines:
186+
187+
* Write clear commit messages.
188+
* Add or update tests as needed.
189+
* Ensure that the code follows the existing style and conventions.
190+
191+
---
192+
155193
## Citing This Work
156194

157195
If you use or refer to this code in your research, please cite the following paper:
@@ -169,19 +207,6 @@ If you use or refer to this code in your research, please cite the following pap
169207

170208
---
171209

172-
## Contributing
173-
174-
Feel free to fork this repository and submit pull requests. Contributions are always welcome!
175-
176-
Please ensure any changes you propose adhere to the following guidelines:
177-
178-
* Write clear commit messages.
179-
* Add or update tests as needed.
180-
* Ensure that the code follows the existing style and conventions.
181-
182-
---
183-
184210
## License
185211

186-
This project is licensed under the MIT License. See the LICENSE file for details.
187-
212+
This project is licensed under the MIT License. See the [LICENSE.txt](LICENSE.txt) file for details.

0 commit comments

Comments
 (0)