You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Adversarial Attack Workflow with Particle Swarm Optimization
2
2
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.
4
4
5
5
## Table of Contents
6
6
7
7
1.[Overview](#overview)
8
8
2.[Requirements](#requirements)
9
9
3.[Setup and Installation](#setup-and-installation)
10
-
4.[Usage](#usage)
11
-
10
+
4.[Package Structure](#package-structure)
11
+
5.[Usage](#usage)
12
12
*[Train a New Model](#train-a-new-model)
13
13
*[Load a Pre-trained Model](#load-a-pre-trained-model)
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:
25
27
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.
29
31
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.
31
33
32
34
---
33
35
34
36
## Requirements
35
37
36
38
This project requires the following Python libraries:
37
39
38
-
*`tensorflow`(for model buildingand training)
40
+
*`torch` / `torchvision`(for model building, training, and data loading)
39
41
*`numpy` (for numerical operations)
40
42
*`matplotlib` (for visualizations)
41
43
*`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)
45
45
46
-
You can install the necessary dependencies by running the following command:
46
+
You can install the necessary dependencies by running:
This will load the provided pre-trained model, evaluate it on the test dataset, and then perform the adversarial attack.
97
122
98
123
### Perform Adversarial Attack
99
124
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)**.
This command performs the attack with **50 iterations** and **100 particles**.
133
+
This command performs the PSO attack with **50 iterations** and **100 particles**.
111
134
112
135
---
113
136
114
137
## Directory Structure
115
138
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`):
117
140
118
141
```
119
142
analysis_results/
120
143
│
121
144
├── original.png # Original image before attack
122
145
├── 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
125
148
│ └── ...
126
149
├── iteration_2/
127
150
│ ├── attack-vector_image_1.png
@@ -133,25 +156,40 @@ analysis_results/
133
156
### Key Files
134
157
135
158
***`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.
138
161
139
162
---
140
163
141
164
## Results and Analysis
142
165
143
166
After the attack is complete, the following information is saved:
144
167
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.
147
170
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.
151
172
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/)
153
178
154
179
---
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
+
155
193
## Citing This Work
156
194
157
195
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
169
207
170
208
---
171
209
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
-
184
210
## License
185
211
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