โ ์ฝ๋ :
GitHub - Trusted-AI/adversarial-robustness-toolbox: Adversarial Robustness Toolbox (ART) - Python Library for Machine Learning S
Adversarial Robustness Toolbox (ART) - Python Library for Machine Learning Security - Evasion, Poisoning, Extraction, Inference - Red and Blue Teams - GitHub - Trusted-AI/adversarial-robustness-too...
github.com
Load prereqs and data
๊ฐ์ข ๋ชจ๋ import ๋ฐ load_dataset()์ ์ด์ฉํด MNIST ๋ฐ์ดํฐ์ ์ ๋ถ๋ฌ์ค๋ ๊ณผ์ ์ ์ฌ๋ฌ ์ฌ์ดํธ์์ ํ์ธ ๊ฐ๋ฅํ MNIST ์์ ์ ๋์ผ
Train and evaluate a baseline classifier
path = get_file('mnist_cnn_original.h5', extract=False, path=config.ART_DATA_PATH,
url='https://www.dropbox.com/s/p2nyzne9chcerid/mnist_cnn_original.h5?dl=1')
classifier_model = load_model(path)
classifier = KerasClassifier(clip_values=(min_, max_), model=classifier_model, use_logits=False)
1. classifier_model = load_model(path)
path์์ ์ง์ ํ ๋ชจ๋ธ์ ๊ฐ์ ธ์ค๊ฒ ๋ค๋ ์๋ฏธ์ด๋ค. 'mnist_cnn_original.h5' ๋ชจ๋ธ์ ์ฐ๊ฒ ๋ค๋ ๋ป์ธ๋ฐ ์ ๋ชจ๋ธ ๊ตฌ์กฐ๋ฅผ ํ์ธํ๊ณ ์ถ์๋๋ฐ h5 ํ์ผ ์ฝ๊ธฐ๊ฐ ์ด๋ ค์์ ํฌ๊ธฐ..ใ
๋์ original์ด๋ผ๋ ํค์๋, tensorflow๋ฅผ import ํ ์ ์ ์๊ฐํด TensorFlow MNIST ์์ ๋ฅผ ์ฐพ์๋ดค๋ค. flatten ๋ ์ด์ด ์ ๊น์ง ๊ฐ ์ธต์ output shape๊ฐ TensorFlow ๊ณต์ ์ฌ์ดํธ ์์ ์ ๋์ผํ ๊ฒ์ผ๋ก ๋ด์ ํด๋น ๋ชจ๋ธ์ ๊ตฌ์กฐ๋ฅผ ์ฐธ๊ณ ํ ๊ฒ ๊ฐ๋ค.
โ ์ฝ๋(TensorFlow MNIST ์์ ) : https://www.tensorflow.org/tutorials/images/cnn?hl=ko
ํฉ์ฑ๊ณฑ ์ ๊ฒฝ๋ง | TensorFlow Core
๋์๋ง Kaggle์ TensorFlow๊ณผ ๊ทธ๋ ์ดํธ ๋ฐฐ๋ฆฌ์ด ๋ฆฌํ (Great Barrier Reef)๋ฅผ ๋ณดํธํ๊ธฐ ๋์ ์ ์ฐธ์ฌ ํฉ์ฑ๊ณฑ ์ ๊ฒฝ๋ง Note: ์ด ๋ฌธ์๋ ํ ์ํ๋ก ์ปค๋ฎค๋ํฐ์์ ๋ฒ์ญํ์ต๋๋ค. ์ปค๋ฎค๋ํฐ ๋ฒ์ญ ํ๋์ ํน์ฑ์ ์ ํ
www.tensorflow.org
# adversarial examples ์์ฑ
attacker = FastGradientMethod(classifier, eps=0.5)
x_test_adv = attacker.generate(x_test[:100])
1. x_test_adv = attacker.generate(x_test[:100])
FGSM ๋ฐฉ๋ฒ์ผ๋ก 100๊ฐ์ adversarial example์ ์์ฑํ๋ค. FastGradientMethod ํด๋์ค์ generate ๋ฉ์๋๋ adversarial example์ ๊ฐ์ง๊ณ ์๋ ๋ฐฐ์ด์ ๋ฐํํ๋ค.
๐ง x_test_adv ํ์ธ์ ์ํ ๊ฐ๋จํ ์ฝ๋ ์ถ๊ฐ
adversarial example์ด๋๊น ์์ธก์ ์๋ชป ํ ์ค ์์์ผ๋! ์ ๋๋ก ํ ๊ฒ์ด ์๋ฌธ. ์ด๋ ๊ฒ ๋์ค๋ ๊ฒ ๋ง๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค.
100๊ฐ์ adversarial examples์ ๋ํด ์ ๋๋ก ๋ถ๋ฅํ ๊ฒฝ์ฐ๊ฐ ํจ์ฌ ๋ง๊ธด ํ๋ฐ ์ด๋ฏธ์ง์ perturbation์ด ๋ค์ด๊ฐ์๋๊ฑด๊ฐ,,?
Adversarially train a robust classifier
์ผ๋จ robust classifier์ ๋ํด ๋ด๊ฐ ์ดํดํ ๋ด์ฉ์ adversarial example์ ๋ํด์๋ ์ ๋๋ก ๋ ๋ถ๋ฅ๋ฅผ ํ๋ classifier์ด๋ค.
(์์ง AI๋ณด์ ๊ด๋ จ ๊ฐ๋ ์ ๋ฆฝ์ด ์๋ฒฝํ๊ฒ ๋์ง ์์ ์ํ๋ผ ์๋ชป๋ ์ดํด์ผ ์ ์์ต๋๋ค. ํ๋ฆฐ ๋ด์ฉ์ผ ๊ฒฝ์ฐ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์!)
# ์ฒซ๋ฒ์งธ dense layer์ ์ ๋ ์๋ฅผ ์ ์ธํ๊ณ ์์์ ์ฌ์ฉํ ๋ชจ๋ธ๊ณผ ๊ตฌ์กฐ ๋์ผ
path = get_file('mnist_cnn_robust.h5', extract=False, path=config.ART_DATA_PATH,
url='https://www.dropbox.com/s/yutsncaniiy5uy8/mnist_cnn_robust.h5?dl=1')
robust_classifier_model = load_model(path)
robust_classifier = KerasClassifier(clip_values=(min_, max_), model=robust_classifier_model, use_logits=False)
attacks = BasicIterativeMethod(robust_classifier, eps=0.3, eps_step=0.01, max_iter=40)
# We had performed this before, starting with a randomly intialized model.
# Adversarial training takes about 80 minutes on an NVIDIA V100.
# The resulting model is the one loaded from mnist_cnn_robust.h5 above.
# Here is the command we had used for the Adversarial Training
trainer = AdversarialTrainer(robust_classifier, attacks, ratio=1.0)
trainer.fit(x_train, y_train, nb_epochs=83, batch_size=50)
1. attacks = BasicIterativeMethod(robust_classifier, eps=0.3, eps_step=0.01, max_iter=40)
BasicIterativeMethod๋ FGM๊ณผ FGSM์ Iterative version์ ๊ตฌํํด๋์ ๊ฒ์ด๋ค. ADVERSARIAL EXAMPLES IN THE PHYSICAL WORLD๋ผ๋ ๋ ผ๋ฌธ์์ Basic Iterative Method์ ๋ํ ๋ด์ฉ์ด ๋์ค๋๋ฐ ์ด ๋ ผ๋ฌธ์ ์ด๋ฒ์ฃผ ์ค์ผ๋ก ์ฝ์ด๋ด์ผ๊ฒ ๋ค.
2. trainer = AdversarialTrainer(robust_classifier, attacks, ratio=1.0)
art/defences/trainer → adversarial_trainer.py
adversarial_trainer ํด๋์ค๋ ๋ชจ๋ธ ๊ตฌ์กฐ์ ํ๋ ์ด์์ ๊ณต๊ฒฉ ๋ฐฉ๋ฒ์ ๊ธฐ๋ฐํ์ฌ adversarial training์ ์ํํ๋ ํด๋์ค-
๋ผ๊ณ ๋์ด ์๋๋ฐ ์ฃผ์์ผ๋ก ๋ฌ๋ฆฐ ์ค๋ช ์ ๊ทธ๋๋ก ํํ๊ณ ์ ๋ฃ์ด์ ๋์จ ๋ฌธ์ฅ์ด๊ณ ์์ง ์ดํด ๋ถ๊ฐ ๐ ์ด๋ฒ์๋ ํ๋ผ๋ฏธํฐ๋ง ๊ฐ๋จํ๊ฒ ์ดํด๋ณด๋๋ก ํ๋ค.
โฝ classifier : adversarially train์ ์ ์ฉํ ๋ชจ๋ธ
โฝ attacks : adversarial training์์ data augmentation์ ์ํด ์ฌ์ฉํ ๊ณต๊ฒฉ ๋ฐฉ๋ฒ
โฝ ratio : ๊ฐ batch์์ adversarial counterparts๋ก ๋์ฒดํ ์ํ์ ๋น์จ. 1์ด๋ฉด adversarial samples์ ๋ํด์๋ง ํ์ต์ ํ๋ค๋ ์๋ฏธ
โ ์๋ณธ ์ฝ๋์์๋ train ๊ด๋ จ ์ฝ๋ ๋ ์ค์ด ์ฃผ์ ์ฒ๋ฆฌ๋์ด ์๋ค. NVIDIA V100 ๊ธฐ์ค ํ์ต ์๊ฐ์ด 80๋ถ์ด์๋ค๋๋ฐ ์ฐ๊ตฌ์ค ์ปดํจํฐ(NVIDIA GeForce RTX 3090)๋ก ํ 6์๊ฐ+a ๊ฑธ๋ฆด ๊ฒ ๊ฐ๋ค. (83 epochs ์ค์ 25 epochs๊น์ง ์ค๋ ๋ฐ์ 2์๊ฐ ์ ๋ ๊ฑธ๋ ธ๊ณ ์์ง ๋์๊ฐ๋ ์ค)
Evaluate the robust classifier
Evaluate baseline classifier ๋ถ๋ถ๊ณผ ํฐ ์ฐจ์ด๊ฐ ๋๋ ๋ถ๋ถ์ ์๋ค.
'๐ฉโ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ART] attack_adversarial_patch_TensorFlowV2.ipynb ์ฝ๋ ๋ถ์ (0) | 2022.01.19 |
---|---|
[ART] attack_defence_imagenet.ipynb ์ฝ๋ ์ค์ต (0) | 2022.01.18 |
[ART] ART for TensorFlow v2 - Callable ์ฝ๋ ๋ถ์ (0) | 2022.01.03 |
[ART] ART for TensorFlow v2 - Keras API ์ฝ๋ ๋ถ์ (0) | 2021.12.31 |
[DACON] ์ฝ๋ ๋ถ์ - MNIST : ์ซ์ ์ด๋ฏธ์ง ๋ถ๋ฅ (0) | 2021.05.25 |