技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度技术分析:GitHub 仓库 “sarathknv/adversarial-examples-pytorch”

项目背景与痛点

对抗样本生成及防御是当前深度学习研究的热点之一。在真实环境中,对抗样本可以带来巨大的安全隐患,而这项研究的初衷就是要找到高效生成复杂对抗样本并有效对抗这些攻击的方法。“sarathknv/adversarial-examples-pytorch” 则实现了多篇关于对抗样本生成的经典论文的模型和方法,不仅帮助我们更好地理解和应对这些威胁,也提供了实用的代码工具,用来生成和测试对抗样本。

这些工具对于研究人员在实际系统中验证模型的安全性至关重要,不仅可以提高系统的鲁棒性,也能增强对深度学习模型的工作机理的进一步理解。

核心技术揭秘

该项目的核心在于实现和复现了几篇关于对抗样本生成的重要论文,例如 FGSM, PGD, CW(Carlini& Wagner),等等。接下来,我们将深入探讨其技术架构和核心实现原理。

首先是技术栈:

  • PyTorch:强大的深度学习框架,用于实现模型训练和推理。
  • Numpy:进行数值计算的基础库。

以下是整体系统的架构图示,展示了代码中的核心流程:

graph TD A[用户定义模型] --> B{"加载数据集"} B -->|"成功"| C[模型预处理] C --> D[生成对抗样本] B -->|"失败"| E[异常处理] D --> F[评估对抗样本] F --> G[输出结果]

具体实现中,核心模块包括对抗样本的生成算法与模型攻击的评测,以下详细介绍。

  • 模型定义:首先需要定义要进行实验的神经网络模型,这是整个攻击过程中潜在要测试鲁棒性的对象。
  • 数据加载:加载预处理的数据集,为模型提供训练或测试所需数据。
  • 模型预处理:对于某些模型可能需要进行特定的预处理,如图像归一化,使之更易于被对抗样本攻击。
  • 对抗样本生成:在预处理模型的输入基础上,应用各种对抗样本生成算法来生成有效对抗样本。这部分深入解析并实现如 CW, PGD 等算法的核心实现细节。
  • 效果评估:将生成的对抗样本输入模型,观察模型的响应,进一步评估生成样本是否有效。
  • 输出结果:基于评估结果,输出模型的对抗鲁棒性的详细报告。

系统以模块化的形式支持各种生成算法和性能评测工具的插入和替换,这在研究和应用实践中都提供了极大的灵活性。

功能亮点与差异

“adversarial-examples-pytorch” 的亮点在于其高度的可配置性和方法的通用性。它不仅可以用于基准的 ImageNet 和 CIFAR-10 数据集上的模型,还能很好地扩展到其他任务和数据集。同时,这一实现提供了多种攻击强度的调节接口,研究人员可以根据需要生成不同程度的对抗样本。

与同类项目相比,该项目在代码实现上显得更加模块化和清晰,易于理解并可以进行二次开发,这极大地降低了科研工作者和技术人员使用该工具进行研究和实验的门槛。

应用场景与落地建议

在生产环境中,这项技术可以帮助企业和研究机构验证模型对对抗样本的抵抗能力,以及发现潜在的安全隐患。适合有需要排查模型安全性的业务场景,例如图像分类、自然语言处理等涉及深度学习技术的应用。

在部署使用过程中需要注意,生成高强度对抗样本的计算成本较高,并且对抗样本的生成方法不断演进,建议紧跟技术前沿,及时评估现有防御措施的有效性。

综合评价

“adversarial-examples-pytorch” 提供了一个强大的工具包来生成和验证对抗样本,对于深度学习算法的安全性有着重要的研究和实际应用价值。它在代码接入的友好性和可扩展性方面的设计非常优秀,为研究对抗性机器学习提供了坚实的起点。

当然,项目依然存在一些需要改善的地方,如默认配置中还可以加入更丰富的基础调优参数。但总的来说,这无疑是一个值得推荐的项目,能够帮助众多研究人员和技术人员更深入地挖掘深度学习的奥秘。

关于

GitHub 原始描述(英文)

Implementation of Papers on Adversarial Examples

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

语言占比

Python

贡献者

README

Implementation of Papers on Adversarial Examples

Implementation of papers with real-time visualizations and parameter control.

Dependencies

  • Python3
  • PyTorch (built from source)
  • OpenCV
  • NumPy
  • SciPy
  • TensorBoard

Contents


Random Perturbations

From one of the first papers on Adversarial examples - Explaining and Harnessing Adversarial Examples,

The direction of perturbation, rather than the specific point in space, matters most. Space is not full of pockets of adversarial examples that finely tile the reals like the rational numbers.

This project examines this idea by testing the robustness of a DNN to randomly generated perturbations.

Usage

$ python3 explore_space.py --img images/horse.png

Demo

fgsm.gif

This code adds to the input image (img) a randomly generated perturbation (vec1) which is subjected to a max norm constraint eps. This adversarial image lies on a hypercube centerd around the original image. To explore a region (a hypersphere) around the adversarial image (img + vec1), we add to it another perturbation (vec2) which is constrained by L2 norm rad.
Pressing keys e and r generates new vec1 and vec2 respectively.

Random Perturbations

The classifier is robust to these random perturbations even though they have severely degraded the image. Perturbations are clearly noticeable and have significantly higher max norm.

horse_explore automobile_explore truck_explore
horse automobile : truck :

In above images, there is no change in class labels and very small drops in probability.

FGSM Perturbations

A properly directed perturbation with max norm as low as 3, which is almost imperceptible, can fool the classifier.

horse_scaled horse_adversarial perturbation
horse predicted - dog perturbation (eps = 6)


Fast Gradient Sign Method (FGSM)

Paper

Usage

  • Run the script
$ python3 fgsm_mnist.py --img one.jpg --gpu
$ python3 fgsm_imagenet.py --img goldfish.jpg --model resnet18 --gpu

fgsm_mnsit.py - for attack on custom model trained on MNIST whose weights are 9920.pth.tar.
fgsm_imagenet - for pretrained imagenet models - resnet18, resnet50 etc.

  • Control keys
    • use trackbar to change epsilon (max norm)
    • esc - close
    • s - save perturbation and adversarial image

Demo

fgsm.gif

Results

MNIST
Adversarial Image Perturbation
Pred: 4 eps: 38
Pred: 7 eps: 60
Pred: 8 eps: 42
Pred: 8 eps: 12
Pred: 9 eps: 17


Basic Iterative Method (Targeted and Untargeted)

Paper: Adversarial examples in the physical world

Usage

  • Run the script
$ python3 iterative.py --img images/goldfish.jpg --model resnet18 --target 4
# If argument 'target' is not specified, it is untargeted attack
  • Control keys
    • use trackbar to change epsilon (max norm of perturbation) and iter (number of iterations)
    • esc close and space to pause
    • s save perturbation and adversarial image

Demo

iterative.gif



One Pixel Attack for Fooling Deep Neural Networks

Paper

Existence of single pixel adversarial perturbations suggest that the assumption made in Explaining and Harnessing Adversarial Examples that small additive perturbation on the values of many dimensions will accumulate and cause huge change to the output, might not be necessary for explaining why natural images are sensitive to small perturbations.

Usage

$ python3 one_pixel.py --img airplane.jpg --d 3 --iters 600 --popsize 10

d is number of pixels to change (L0 norm)
iters and popsize are paprameters for Differential Evolution

Results

Attacks are typically successful for images with low confidence. For successful attacks on high confidence images increase d, i.e., number of pixels to perturb.

airplane bird cat frog horse
bird [0.8075] deer [0.8933] frog [0.8000] bird [0.6866] deer [0.9406]


AdvGAN - Generating Adversarial Examples with Adversarial Networks

Paper | IJCAI 2018

Usage

Inference
$ python3 advgan.py --img images/0.jpg --target 4 --model Model_C --bound 0.3

Each of these settings has a separate Generator trained. This code loads appropriate trained model from saved/ directory based on given arguments. As of now there are 22 Generators for different targets, different bounds (0.2 and 0.3) and target models (only Model_C for now).

Training AdvGAN (Untargeted)
$ python3 train_advgan.py --model Model_C --gpu
Training AdvGAN (Targeted)
$ python3 train_advgan.py --model Model_C --target 4 --thres 0.3 --gpu
# thres: Perturbation bound 

Use --help for other arguments available (epochs, batch_size, lr etc.)

Training Target Models (Models A, B and C)
$ python3 train_target_models.py --model Model_C

For TensorBoard visualization,

$ python3 generators.py
$ python3 discriminators.py

This code supports only MNIST dataset for now. Same notations as in paper are followed (mostly).

Results

There are few changes that have been made for model to work.

  • Generator in paper has ReLU on the last layer. If input data is normalized to [-1 1] there wouldn’t be any perturbation in the negative region. As expected accuracies were poor (~10% Untargeted). So ReLU was removed. Also, data normalization had significat effect on performance. With [-1 1] accuracies were around 70%. But with [0 1] normalization accuracies were ~99%.
  • Perturbations (pert) and adversarial images (x + pert) were clipped. It’s not converging otherwise.

These results are for the following settings.

  • Dataset - MNIST
  • Data normalization - [0 1]
  • thres (perturbation bound) - 0.3 and 0.2
  • No ReLU at the end in Generator
  • Epochs - 15
  • Batch Size - 128
  • LR Scheduler - step_size 5, gamma 0.1 and initial lr - 0.001
Target Acc [thres: 0.3] Acc [thres: 0.2]
Untargeted 0.9921 0.8966
0 0.9643 0.4330
1 0.9822 0.4749
2 0.9961 0.8499
3 0.9939 0.8696
4 0.9833 0.6293
5 0.9918 0.7968
6 0.9584 0.4652
7 0.9899 0.6866
8 0.9943 0.8430
9 0.9922 0.7610
Untargeted
Pred: 9 Pred: 3 Pred: 8 Pred: 8 Pred: 4 Pred: 3 Pred: 8 Pred: 3 Pred: 3 Pred: 8
Targeted
Target: 0 Target: 1 Target: 2 Target: 3 Target: 4 Target: 5 Target: 6 Target: 7 Target: 8 Target: 9
Pred: 0 Pred: 1 Pred: 2 Pred: 3 Pred: 4 Pred: 5 Pred: 6 Pred: 7 Pred: 8 Pred: 9
Pred: 0 Pred: 1 Pred: 2 Pred: 3 Pred: 4 Pred: 5 Pred: 6 Pred: 7 Pred: 8 Pred: 9
Pred: 0 Pred: 1 Pred: 2 Pred: 3 Pred: 4 Pred: 5 Pred: 6 Pred: 7 Pred: 8 Pred: 9


Spatially Transformed Adversarial Examples

Paper | ICLR 2018
Refer View Synthesis by Appearance Flow for clarity.

Usage

$ python3 stadv.py --img images/1.jpg --target 7

Requires OpenCV for real-time visualization.

Demo

0_1 1_2 2_3 3_4 4_5 5_6 6_7 7_8 8_9 9_0

Results

MNIST

Column index is target label and ground truth images are along diagonal.

tile



评论

暂无评论