Back to Trending
RepositoryJupyter NotebookINIndia
as791

as791/Adversarial-Example-Attack-and-Defense

This repository contains the implementation of three adversarial example attack methods FGSM, IFGSM, MI-FGSM and one Distillation as defense against all attacks using MNIST dataset.

136stars
29forks
2issues
4views

Adversarial-Example-Attack-and-Defense

This repository contains the PyTorch implementation of the three non-target adversarial example attacks (white box) and one defense method as countermeasure to those attacks.

Attack

  1. Fast Gradient Sign Method(FGSM) - Goodfellow, I. J., Shlens, J., and Szegedy, C. Explaining and harnessing adversarial examples. arXiv preprint arXiv:1412.6572, 2014b.
def fgsm_attack(input,epsilon,data_grad):
  pert_out = input + epsilon*data_grad.sign()
  pert_out = torch.clamp(pert_out, 0, 1)
  return pert_out
  1. Iterative Fast Gradient Sign Method(I-FGSM) - A. Kurakin, I. Goodfellow, and S. Bengio. Adversarial examples in the physical world. arXiv preprint arXiv:1607.02533, 2016.
def ifgsm_attack(input,epsilon,data_grad):
  iter = 10
  alpha = epsilon/iter
  pert_out = input
  for i in range(iter-1):
    pert_out = pert_out + alpha*data_grad.sign()
    pert_out = torch.clamp(pert_out, 0, 1)
    if torch.norm((pert_out-input),p=float('inf')) > epsilon:
      break
  return pert_out
  1. Momentum Iterative Fast Gradient Sign Method(MI-FGSM) - Y. Dong et al. Boosting Adversarial Attacks with Momentum. arXiv preprint arXiv:1710.06081, 2018.
def mifgsm_attack(input,epsilon,data_grad):
  iter=10
  decay_factor=1.0
  pert_out = input
  alpha = epsilon/iter
  g=0
  for i in range(iter-1):
    g = decay_factor*g + data_grad/torch.norm(data_grad,p=1)
    pert_out = pert_out + alpha*torch.sign(g)
    pert_out = torch.clamp(pert_out, 0, 1)
    if torch.norm((pert_out-input),p=float('inf')) > epsilon:
      break
  return pert_out

Defense

  1. Defensive Distillation - Papernot, N., McDaniel, P., Wu, X., Jha, S., and Swami, A. Distillation as a defense to adversarial perturbations against deep neural networks. arXiv preprint arXiv:1511.04508, 2016b.

According to the paper, defensive distillation can be done by following procedure:

  1. Train a network F on the given training set (X,Y) by setting the temperature of the softmax to T.
  2. Compute the scores (after softmax) given by F(X) again and evaluate the scores at temperature T.
  3. Train another network F'T using softmax at temperature T on the dataset with soft labels (X,F(X)). We refer the model FT as the distilled model.
  4. Use the distilled network F'T with softmax at temperature 1, which is denoted as F'1 during prediction on test data Xtest(or adversarial examples).

Taken Temperature as 100 for training the NetF and NetF'.

Results

  • Applied the attack methods and defense using MNIST dataset on the model based on pytorch example model for mnist.
  • Here, the attacks are white box as all the knowledge of network hyperparameter setting with the network's achitecture.
  • Results tell that FGSM attack reduces the test accuracy from 97.08% to 24.84% with epsilon from 0 to 0.3, whereas I-FGSM with number of iteration as 10 reduces test accuracy from 96.92% to 30.54% similar with MI-FGSM with decay factor of 1.0 and iterations of 10, reduction in test accuracy from 97.05% to 30.10% i.e. we can say that our attacks to the proposed network was successful and it reduced ~70% of test accuracy in all the three cases for max epsilon of 0.3.
  • During the defensive distillation used same network as Net F and for Net F' reduced number of filters to half in each layer to reduce the number of parameters. Temperature of 100 was taken in our case. Results tell that FGSM attack reduces test accuracy from 90.33% to 88.01% with same epsilon range, I-FGSM with iteration of 10 reduced test accuracy from 90.80% to 88.16% similar with MI-FGSM with same decay factor of 1.0 and iterations of 10, reduction in test accuracy from 90.26% to 87.97% i.e. we can say that defensive distillation for the proposed network with temp of 100 was successful and it only reduced ~2% of test accuracy in all the three cases for max epsilon of 0.3.

Test Accuracy during attacks

FGSM

I-FGSM

MI-FGSM

Test Accuracy during attack using defensive distillation

FGSM

I-FGSM

MI-FGSM

Sample Advesarial Examples

FGSM

I-FGSM

MI-FGSM

Similar Projects

Discover other similar projects and AI models.

RepositoryPython
Trusted-AI/adversarial-robustness-toolbox

Adversarial Robustness Toolbox (ART) - Python Library for Machine Learning Security - Evasion, Poisoning, Extraction, Inference - Red and Blue Teams

6.3k
Nov 27, 2026
Dataset
Manusagents/GPT-5.5-Gemini-3.1-Pro-Grok-4-Claude-Fable-5-Mythos-5-Qwen-3.7-Max-and-more-Distillation-Dataset

📖 The Open Distillation Codex 🌌 The Ultimate Open-Source Distillation Dataset — No Skip, Full, with Attack & Defense 🌌 Where 73 open-source minds converge into one unified stream of intelligence 18M+ Distilled Signals · 7,090 Raw GitHub Repositories · 8 Curated Categories · ~76 GB+ "We did not write this dataset. We assembled it. Every line is an echo — of a model thinking, a coder drafting, a tutor explaining, a repo breathing. Seventy-three… See the full description on the dataset page: https://huggingface.co/datasets/Manusagents/GPT-5.5-Gemini-3.1-Pro-Grok-4-Claude-Fable-5-Mythos-5-Qwen-3.7-Max-and-more-Distillation-Dataset.

♥0
18.7k
Aug 29, 2026
RepositoryPython
thu-ml/ares

A Python library for adversarial machine learning focusing on benchmarking adversarial robustness.

528
Oct 15, 2023
RepositoryPython
ashafahi/free_adv_train

Official TensorFlow Implementation of Adversarial Training for Free! which trains robust models at no extra cost compared to natural training.

176
May 3, 2024
RepositoryJupyter Notebook
MadryLab/photoguard

Raising the Cost of Malicious AI-Powered Image Editing

1.0k
Feb 27, 2023
RepositoryJupyter Notebook
shangtse/robust-physical-attack

Physical adversarial attack for fooling the Faster R-CNN object detector

170
Jan 13, 2020
RepositoryJupyter Notebook
microsoft/denoised-smoothing

Provably defending pretrained classifiers including the Azure, Google, AWS, and Clarifai APIs

102
Apr 2, 2021
RepositoryC++
cifertech/ESP32-DIV

ESP32DIV is a multi-purpose wireless offensive and defensive toolkit powered by an ESP32

4.4k
Sep 22, 2026
RepositoryPython
QData/TextAttack

TextAttack 🐙 is a Python framework for adversarial attacks, data augmentation, and model training in NLP https://textattack.readthedocs.io/en/master/

3.5k
Aug 15, 2026