1. 程式人生 > 實用技巧 ><cleverhans 對抗樣本防護編譯與測試(含 FGSM 攻擊與 ADV 防護)>

<cleverhans 對抗樣本防護編譯與測試(含 FGSM 攻擊與 ADV 防護)>

(4 條訊息)Python3 環境下 cleverhans 對抗樣本防護編譯與測試(含 FGSM 攻擊與 ADV 防護)_大資料探勘 SparkExpert 的部落格 - CSDN 部落格_cleverhans

在看人工智慧安全方面的資料,順手看到 cleverhans 的資料,就將它在 python 3.6 的環境下進行編譯和測試。

在Ian Goodfellow的《Machine learning privacy and security》報告中才瞭解到cleverhans專案名字的由來:“一匹叫做 Clever Hans 的馬。剛出現的時候人們認為這匹馬會做算術,但實際上它只是會閱讀人的表情,當它點馬蹄的次數接近正確答案時,人們的表情會更興奮,它就知道該這個時候停止了。”

這個專案是 tensorflow 的子專案(https://github.com/tensorflow/cleverhans),原始的程式碼版本是 PYTHON 2.7 環境,於程式碼下載後進行了重構和 3.6 版本的編譯。發現這個程式碼的工作量挺多的。下面就重點關注的幾塊進行測試。

(1)FGSM 的影象擾動攻擊

FGSM,是 Goodfellow 等人提出的比較典型的對抗樣本生成演算法。

它的資料生成方式如下(由於

很小,因此 x'和 x 的數值相差不大,因此人眼一般不會感知到明顯區別, 但是對於 CNN 模型來說,識別的錯誤還是發生了。):

, 具體 的程式碼可見 https://github.com/tensorflow/cleverhans/blob/master/cleverhans/attacks_tf.py 相關的函式。


前後

此外,這個網址上提供了許多 FGSM 的例子。(見 https://www.kaggle.com/benhamner/fgsm-attack-example/code)

將生成後的 FGSM 擾動資料送到影象識別模型中如程式碼中給出的 inceptionv3 中,可以看到影象的識別結果全部變亂了。

A:下圖為原始的圖片識別結果


B:下圖為 FGSM 擾動後的的圖片識別結果,可以看出識別分類結果相差特別的大。


(2)FGSM 攻擊的防護(NIPS2017 論文相關程式碼)

在找防護的過程中,才發現 cleverhans 整合的程式碼居然也是 tensorflow models 中的相關程式碼,見 https://github.com/tensorflow/models/tree/master/research/adv_imagenet_models。本質上而言,它需要在擾動的圖片上進行訓練,從而才能實現對擾動的程式碼進行準確識別。如論文原文中指出的貢獻如下:


實際程式碼中,cleverhans 提供了兩種對抗訓練,一種是基於 inceptionv3 的,一種是 inception-resnet-v2 的增強版。測試結果如下,則擾動後的圖片,也能被正確識別。


(3)一些其他的例子,cleverhans 程式碼庫提供了多樣性的對抗樣本生成方法,具體如下:

  • sample_attacks/- directory with examples of attacks:
    • sample_attacks/fgsm/- Fast gradient sign attack.
    • sample_attacks/noop/- No-op attack, which just copied images unchanged.
    • sample_attacks/random_noise/- Attack which adds random noise to images.
  • sample_targeted_attacks/- directory with examples of targeted attacks:
    • sample_targeted_attacks/step_target_class/- one step towards target class attack. This is not particularly good targeted attack, but it demonstrates how targeted attack could be written.
    • sample_targeted_attacks/iter_target_class/- iterative target class attack. This is a pretty good white-box attack, but it does not do well in black box setting.
  • sample_defenses/- directory with examples of defenses:
    • sample_defenses/base_inception_model/- baseline inception classifier, which actually does not provide any defense against adversarial examples.
    • sample_defenses/adv_inception_v3/- adversarially trained Inception v3 model fromAdversarial Machine Learning at Scalepaper.
    • sample_defenses/ens_adv_inception_resnet_v2/- Inception ResNet v2 model which is adversarially trained against an ensemble of different kind of adversarial examples. Model is described inEnsemble Adversarial Training: Attacks and Defensespaper.
同時也提供了好幾個 example。還是對抗樣本生成與對抗訓練非常好的一個庫。

附圖為其中第一個 example。


全文完

本文由簡悅 SimpRead優化,用以提升閱讀體驗 使用了全新的簡悅詞法分析引擎beta點選檢視詳細說明