Coder Social home page Coder Social logo

dreadlord1984 / advbox Goto Github PK

View Code? Open in Web Editor NEW

This project forked from advboxes/advbox

0.0 3.0 0.0 59.06 MB

Advbox是支持多种深度学习平台的AI模型安全工具箱,既支持白盒和黑盒算法生成对抗样本,衡量AI模型鲁棒性,也支持常见的防御算法 ( Advbox is a toolbox to generate adversarial examples ,it can benchmark the robustness of machine learning models. It support TensorFlow,Caffe2 and multi-GPU , help more users generate more adversarial examples more faster.)

License: Apache License 2.0

Python 100.00%

advbox's Introduction

AdvBox

logo

AdvBox是一款支持PaddlePaddle、Caffe2、Keras以及TensorFlow平台的AI模型安全工具箱。AdvBox同时支持白盒、黑盒攻击算法以及主流防御算法,支持列表如下。

白盒攻击算法

  • L-BFGS
  • FGSM
  • BIM
  • ILCM
  • MI-FGSM
  • JSMA
  • DeepFool
  • C/W

黑盒攻击算法

  • Single Pixel Attack
  • Local Search Attack

防护算法

  • Feature Fqueezing
  • Spatial Smoothing
  • Label Smoothing
  • Gaussian Augmentation
  • Adversarial Training

对抗样本是深度学习领域的一个重要问题,比如在图像上叠加肉眼难以识别的修改,就可以欺骗主流的深度学习图像模型,产生分类错误,指鹿为马,或者无中生有。这些问题对于特定领域(比如无人车、人脸识别)会产生严重的后果,尤为重要。

针对图像分类模型的对抗样本

百度安全实验室研发了AdvBox,它能够为安全工程师研究模型的安全性提供极大的便利,免去重复造轮子的精力与时间消耗。AdvBox可以高效地使用最新的生成方法构造对抗样本数据集用于对抗样本的特征统计、攻击全新的AI应用,加固业务AI模型,为模型安全性研究和应用提供重要的支持,当前最新版本为0.3

安装

部署AdvBox代码

直接同步advbox的代码,其中示例代码在tutorials目录下。

git clone https://github.com/baidu/AdvBox.git  

初始化软件环境

为了兼容主流的深度学习平台,AdvBox基于python2.7开发,强烈建议使用Conda管理python软件环境,对应的python安装包安装方式如下。

pip install -r requirements.txt

文档

Keras示例

以Keras环境为例,代码路径为tutorials/keras_demo.py

使用Keras自带的ResNet50模型进行白盒攻击,并设置为预测模式,加载测试图片。

#设置为测试模式
keras.backend.set_learning_phase(0)
model = ResNet50(weights=modulename)
img = image.load_img(imagename, target_size=(224, 224))
original_image = image.img_to_array(img)
imagedata = np.expand_dims(original_image, axis=0)

获取ResNet50的logit层,并创建keras对象。keras的ResNet50要求对原始图像文件进行标准化处理,mean值为[104, 116, 123],std为1.

#获取logit层
logits=model.get_layer('fc1000').output
# 创建keras对象
# imagenet数据集归一化时 标准差为1  mean为[104, 116, 123]
m = KerasModel(
    model,
    model.input,
    None,
    logits,
    None,
    bounds=(0, 255),
    channel_axis=3,
    preprocess=([104, 116, 123],1),
    featurefqueezing_bit_depth=8)

创建攻击对象,攻击算法使用FGSM的non-targeted attack,攻击步长epsilons设置为静态值。

attack = FGSM(m)
#静态epsilon
attack_config = {"epsilons": 1, "epsilons_max": 10, "epsilon_steps": 1, "steps": 100}
# fgsm non-targeted attack
adversary = attack(adversary, **attack_config)

对比生成的对抗样本和原始图像的差别。

adversary_image=np.copy(adversary.adversarial_example)
#强制类型转换 之前是float 现在要转换成uint8
#BGR -> RGB
adversary_image=adversary_image[:,:,::-1]
adversary_image = np.array(adversary_image).reshape([224,224,3])
original_image=np.array(original_image).reshape([224, 224, 3])
show_images_diff(original_image,adversary_image)

实际运行代码,原始图像和对抗样本的差别如下图所示。

keras-demo.png

PaddlePaddle示例

请见PaddlePaddle示例

学习教程

为了进一步降低学习成本,AdvBox提供大量的学习教程

典型应用

基于AdvBox可以针对大量实际使用的AI模型生成对抗样本并给出通用加固方案。

问题反馈

目前支持通过Github提交issues

许可

AdvBox循序Apache License 2.0

作者

  • 百度安全实验室 xlab

参考文献

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.