์๋ณธ ์ด๋ฏธ์ง | ์ ๋์ ์ด๋ฏธ์ง |
---|---|
Torchattacks ์ ํ์ดํ ์น(PyTorch) ๊ธฐ๋ฐ์ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ๋ํ ์ ๋์ ๊ณต๊ฒฉ(Adversarial Attack)์ ๊ตฌํํ ํจํค์ง์ ๋๋ค. ํ์ดํ ์น์ ์น์ํ ์ฝ๋๋ฅผ ์ ๊ณตํ์ฌ, ํ์ดํ ์น ์ฌ์ฉ์๋ค์ด ์ข ๋ ์ฝ๊ฒ ์ ๋์ ๊ณต๊ฒฉ์ ์น์ํด์ง๋ ๊ฒ์ ๋ชฉํ๋ก ํ๊ณ ์์ต๋๋ค.
- ๋ค์ด๊ฐ๊ธฐ ์ . ๋ฅ๋ฌ๋์์์ ๋ณด์๊ณผ ์ ๋์ ๊ณต๊ฒฉ
- ์ฌ์ฉ ๋ฐฉ๋ฒ
- ๋ฌธ์ ๋ฐ ๋ฐ๋ชจ
- ์ธ์ฉํ๊ธฐ
- 200% ํ์ฉํ๊ธฐ
- ๊ธฐ์ฌํ๊ธฐ
- ์ถ์ฒํ๋ ๋ค๋ฅธ ํจํค์ง ๋ฐ ์ฌ์ดํธ
๋ฅ๋ฌ๋์ ํ์ฌ ๊ฐ์ฅ ๊ฐ๊ด๋ฐ๊ณ ์๋ ๊ธฐ์ ์ด๋ฉฐ, ์๊ฐ(Vision) ๋ฐ ์ฒญ๊ฐ(Audio)์ ์ด๋ฅด๊ธฐ๊น์ง ๋ค์ํ ๋ถ์ผ์ ๊ฑธ์ณ ๊ฐ๋ฐ์ด ์งํ๋๊ณ ์์ต๋๋ค. ๊ทธ ๋ฌดํํ ๊ฐ๋ฅ์ฑ์ ๋ง์ ํ์๋ค์ ์ํด ํ๋ฐํ๊ฒ ์ฐ๊ตฌ๋๊ณ ์์ผ๋ฉฐ, ์์จ์ฃผํ ์๋์ฐจ๋ถํฐ ์ธ๊ณต์ง๋ฅ ์คํผ์ปค๊น์ง ์ฐ๋ฆฌ ์์ ์ ํ์ผ๋ก๋ ๋ชจ์ต์ ๋๋ฌ๋ด๊ณ ์์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ๋ง์ฝ ์ ์๋ฅผ ํ์ ๋๊ตฐ๊ฐ๊ฐ ๋ฅ๋ฌ๋์ ๊ณต๊ฒฉํ ์ ์๋ค๋ฉด ์ด๋จ๊น์? ์์จ์ฃผํ ์๋์ฐจ์ ์ฌ์ด์ ธ ์๋ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ๊ณต๊ฒฉํ์ฌ ๊ฐ์๊ธฐ ์๋์ฐจ๋ฅผ ์ ์ง์ํค๊ฑฐ๋, ์ธ๊ณต์ง๋ฅ ์คํผ์ปค๋ฅผ ์์ฌ ์ฃผ์ธ ๋ชจ๋ฅด๊ฒ ๊ฒฐ์ ๋ฅผ ์งํํ๋ค๋ฉด ๊ณผ์ฐ ์ฐ๋ฆฌ๋ ์์ฌํ๊ณ ์ ํ๊ณผ ์๋น์ค๋ฅผ ์ฌ์ฉํ ์ ์์๊น์?
์ด๋ ๋ฏ ๋ฅ๋ฌ๋์ ์ฑ๋ฅ์ด ํฅ์๋จ์ ๋ฐ๋ผ *๋ณด์(Security)*์ ๋ํ ์ด์๋ ํจ๊ป ์ง์ค๋ฐ๊ณ ์์ต๋๋ค.
์ ๋์ ๊ณต๊ฒฉ์ ๋ฅ๋ฌ๋์ ๊ณต๊ฒฉํ๋ ๊ฐ์ฅ ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ ๋๋ค. 2013๋ ๋ Szegedy et al. (Intriguing properties of neural networks)์ ์ํด ์ฒ์ ๋ฐ๊ฒฌ๋์ด, ์๋ง์ ๊ณต๊ฒฉ ๋ฐฉ๋ฒ์ด ์ ์๋์์ต๋๋ค. ์กฐ๊ธ ์ฝ๊ฒ ์ค๋ช ํ๊ธฐ ์ํด ์๋ ๊ทธ๋ฆผ์ ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค.
์ ๊ทธ๋ฆผ์ ์ผ์ชฝ์ด ๋ฐ๋ก ์ฐ๋ฆฌ๊ฐ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ํ์ต(Training)ํ๋ ๋ฐฉ๋ฒ์ ๋๋ค. ์ฃผ์ด์ง ์์คํจ์(Loss function)์ ๊ฒฝ์ฌ ํ๊ฐ๋ฒ(Gradient Descent)๋ฅผ ํ์ฉํ์ฌ ์ค์ด๊ฒ๋ ๋ชจ๋ธ์ ๊ฐ์ค์น(Weight) ํน์ ํ๋ผ๋ฏธํฐ(Parameter)๋ฅผ ๋ณ๊ฒฝ์ํค๊ฒ๋ฉ๋๋ค.
๊ทธ๋ผ ๋ฐ๋๋ก ์๊ฐํด๋ณผ ์๋ ์์ต๋๋ค. ๋ง์ฝ ์์คํจ์๋ฅผ ์ฆ๊ฐ์ํค๋ ๋ฐฉํฅ์ผ๋ก ํ์ตํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น์? ๋ฌผ๋ก , ์ฐ๋ฆฌ๊ฐ ์ํ๋ ๋ฐฉํฅ์ธ ํ์ตํ๋ ๋ฐฉํฅ์ด ์๋๋ผ, ๊ทธ ๋ฐ๋ ๋ฐฉํฅ์ผ๋ก ์์ง์ด๊ฒ ๋๊ณ ์ด๋ ๋น์ฐํ ์๋ฌด ์๋ฏธ๊ฐ ์๊ฒ ๋ฉ๋๋ค. ํ์ง๋ง, ๊ณต๊ฒฉํ๊ณ ์ ํ๋ ํด์ปค์๊ฒ๋ ์ด๋ฌํ ๋ฐฉ๋ฒ์ด ๋งค์ฐ ์ ์ฉํ๊ฒ ์ด์ฉ๋ ์ ์์ต๋๋ค. ์ด๊ฒ์ด ๋ฐ๋ก ์ค๋ฅธ์ชฝ ๊ทธ๋ฆผ์ ๋๋ค.
์ค๋ฅธ์ชฝ ๊ทธ๋ฆผ์์๋ ๊ฒฝ์ฌ ํ๊ฐ๋ฒ์ ๋ฐ๋๋ก ํ์ฉํ์ฌ, ์์คํจ์๋ฅผ ์ฆ๊ฐํ๋ ๋ฐฉํฅ์ผ๋ก ๋ชจ๋ธ์ ๊ฐ์ค์น๊ฐ ์๋ ์ด๋ฏธ์ง๋ฅผ ๋ณ๊ฒฝ์ํต๋๋ค. ๊ทธ๋ผ ํด๋น ์ด๋ฏธ์ง๋ ๋ชจ๋ธ์ ์์คํจ์๋ฅผ ํค์ฐ๋๋ก ๋ณํด์, ๊ธฐ์กด ์ ์๋ํ๋ ๋ชจ๋ธ์ ์ ์์ธก(Prediction)ํ์ง ๋ชปํ๋๋ก ๋ฐฉํดํ ์ ์๊ฒ ๋ฉ๋๋ค. ์ด๊ฒ์ด ์ ๋์ ๊ณต๊ฒฉ์ ๊ธฐ๋ณธ์ ์ธ ์์ด๋์ด์ ๋๋ค.
๋ฌผ๋ก , ์๋ '๊ฐ'์ ์ด๋ฏธ์ง๋ฅผ ์ ๋ง์ถ๋ ๋ชจ๋ธ์ ์์ธก์ '๊ณ ์์ด' ๋ฐ๊พธ๊ธฐ ์ํด์ ์๋ '๊ฐ'์ ์ด๋ฏธ์ง๋ฅผ '๊ณ ์์ด' ์ด๋ฏธ์ง๋ก ๋ฐ๊ฟ์ผํ๋ค๋ฉด ๊ณต๊ฒฉ์ ์๋ฌด ์๋ฏธ๊ฐ ์์๊ฒ๋๋ค. ํ์ง๋ง, ๋ฌธ์ ๋ ์์ธก์ ๋ฐ๊พธ๋ ๋ฐ์ ํฐ ๋ ธ์ด์ฆ(Noise)๊ฐ ํ์ํ์ง ์๋ค๋ ์ ์ ๋๋ค. ๋ณธ ๋ฌธ์์ ๋งจ ์์ฒ๋ผ ํ๋ค(Panda)์ ์กฐ๊ธ๋ง ๋ ธ์ด์ฆ๋ฅผ ์ค๋ ์์ธก์ ํฌ๊ฒ ๋น๋๊ฐ๋๋ค.
ํ์ฌ๊น์ง ๋ค์ํ ๊ณต๊ฒฉ ๋ฐฉ๋ฒ์ด ๋ฑ์ฅํ์ง๋ง, ์ฌ๊ธฐ์๋ ๊ฐํธํจ์ ์ํด ํฌ๊ฒ ๋ ๊ฐ์ง๋ก ๋๋์ด ์์ ํ๊ฒ ์ต๋๋ค.
- Evasion Attack: ๋ชจ๋ธ์ ์์ธก(Inference)์ ๊ณต๊ฒฉํ๋ ๊ฒ.
- Poisoning Attack: ๋ชจ๋ธ์ ํ์ต(Training)์ ๊ณต๊ฒฉํ๋ ๊ฒ.
Evasion Attack์ ๋ง ๊ทธ๋๋ก, ์ด๋ฏธ ํ์ต๋ ๋ชจ๋ธ์ ๊ณต๊ฒฉํ๋ ๊ฒ์ ๋๋ค. ์ฃผ๋ก ์ฌ์ง์ด๋ ์๋ฆฌ์ ๋ ธ์ด์ฆ(Noise)๋ฅผ ๋ํด ์๋ชป๋ ์์ธก์ ์ ๋ํฉ๋๋ค. ์ด ๋ ์ ๋์ ๊ณต๊ฒฉ์ ์ํด ์ฌ์ฉ๋ ๋ ธ์ด์ฆ๋ฅผ ํน๋ณํ ์ ๋์ ์ญ๋(Adversarial Perturbation)์ด๋ผ๊ณ ๋ ๋ถ๋ฆ ๋๋ค. ๊ทธ๋ฆฌ๊ณ , ์ ๋์ ๊ณต๊ฒฉ์ ํ์ฉํด ์์ฑ๋ ์ญ๋์ด ๋ํด์ง ์ด๋ฏธ์ง๋ฅผ ์ ๋์ ์์ (Adversarial Example)์ด๋ผ๊ณ ๋ถ๋ฅด๊ฒ ๋ฉ๋๋ค. ๋ฌธ์ ๋ ์์์ ์ธ๊ธํ๋ ๊ฒ๊ณผ ๊ฐ์ด, ์ญ๋์ด ์ฌ๋์ ๋์ ๋ณด์ด์ง ์์ ์ ๋์์๋ ์์ธก์ด ๋ฏผ๊ฐํ๊ฒ ๋ฐ๋๋ค๋ ๊ฒ์ ๋๋ค.
Evasion Attack์ ๊ฒฝ์ฐ ํฌ๊ฒ White Box์ Black Box๋ก ๋๋ ์ ์์ต๋๋ค.
-
White Box Attack: ๋ชจ๋ธ ์์ฒด์ ์ ๊ทผ ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ๊ณต๊ฒฉ. (๊ธฐ์ธ๊ธฐ ์ ๋ณด ํ์ฉ ๊ฐ๋ฅ)
-
Black Box Attack: ๋ชจ๋ธ์ ๋ํ ์ ๋ณด๊ฐ ์์ ์๊ฑฐ๋, ์ฌ์ฉ๋ ๊ตฌ์กฐ ํน์ ๊ฒฐ๊ณผ๋ง ์ ์ ์๋ ๊ฒฝ์ฐ์ ๊ณต๊ฒฉ. (๊ธฐ์ธ๊ธฐ ์ ๋ณด ํ์ฉ ๋ถ๊ฐ๋ฅ)
-
Transfer Attack: ๋๋ฆฌ(Surrogate) ๋ชจ๋ธ์ ํ์ฉํ์ฌ ๊ณต๊ฒฉํ๋ ๋ฐฉ๋ฒ.
-
Score-based, Decision-based Attack : ๋ชจ๋ธ์ด ์ถ๋ ฅํ๋ ๊ฐ(Output)์ธ ์์ธก(Prediction)์ด๋ ํ๋ฅ (Probabilty)๋ฅผ ๊ฐ์ง๊ณ ๊ณต๊ฒฉํ๋ ๋ฐฉ๋ฒ.
-
White Box Attack์ ์ง์ ์ ์ผ๋ก ๊ธฐ์ธ๊ธฐ ์ ๋ณด๋ฅผ ํ์ฉ ํ ์ ์๋ค๋ ์ ์์ Black Box Attack๋ณด๋ค ํจ์ฌ ๊ฐ๋ ฅํ ์ ๋์ ๊ณต๊ฒฉ์ ์คํํ ์ ์์ต๋๋ค. Torchattacks๊ฐ ๋ค๋ฃจ๊ณ ์๋ ๊ณต๊ฒฉ๋ค์ด ๋ฐ๋ก ์ฌ๊ธฐ์ ํด๋น๋๋ฉฐ, ๋ฐ๋ผ์ ๊ณต๊ฒฉ ์์ ๊ณต๊ฒฉ ๋์์ด ๋ ๋ชจ๋ธ์ ํ์๋ก ํฉ๋๋ค. ์ ๋์ ๊ณต๊ฒฉ์ ์ถ๋ฐ์ ์ธ๋งํผ, ๋ค๋ฅธ ๊ณต๊ฒฉ ๋ฐฉ๋ฒ๋ค์ ๊ธฐ๋ฐ์ด ๋๋ ๋ ผ๋ฌธ์ด ๋ง์ต๋๋ค.
[์ถ์ฒ ๋ ผ๋ฌธ]
Intriguing properties of neural networks
Explaining and Harnessing Adversarial Examples
DeepFool: a simple and accurate method to fool deep neural networks
Toward evaluating the robustness of neural networks
Ensemble adversarial training: Attacks and defenses
Towards Deep Learning Models Resistant to Adversarial Attacks
Boosting Adversarial Attacks with Momentum
Black Box Attack์ ๊ธฐ์ธ๊ธฐ ์ ๋ณด๋ฅผ ํ์ฉํ์ง ๋ชปํ๋ฏ๋ก, ๊ณต๊ฒฉ์์๊ฒ ๋ ์ด๋ ค์ด ํ๊ฒฝ ์ด๋ผ๊ณ ํ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ์๋ ๊ณต๊ฒฉ ๋์ ๋ชจ๋ธ๊ณผ ๋น์ทํ ๋ชจ๋ธ์ธ ๋๋ฆฌ ๋ชจ๋ธ์ ํ์ฉํ๋ Transfer Attack์ด๋ ๋ชจ๋ธ์ ์ถ๋ ฅ์ ๋ฐํ์ผ๋ก ๊ณต๊ฒฉํ๋ Score-based, Decision-based Attack ๋ฑ์ด ์ฌ์ฉ๋ฉ๋๋ค. ๋ค๋ง, ๋ชจ๋ธ์ ์ง์ ์ ์ผ๋ก ํ์ฉํ ์ ์์ผ๋ฏ๋ก, ๋ชจ๋ธ์ ์ ๋ ฅ ๊ฐ์ ์ ๋ฌํ๊ณ ์ถ๋ ฅ์ ๋ฐ์์ค๋ *์ฟผ๋ฆฌ(Query)*๋ฅผ ์จ๋ผ์ธ์ ์์ฒญํ๊ฒ ๋ฉ๋๋ค. ๋ฌธ์ ๋ ๊ณต๊ฒฉ์ ์ํด์๋ ๋ค์์ ์ฟผ๋ฆฌ๊ฐ ์๊ตฌ๋๊ธฐ ๋๋ฌธ์ ๋ง์ ์๊ฐ์ ์๊ตฌํฉ๋๋ค.
[์ถ์ฒ ๋ ผ๋ฌธ]
Practical Black-Box Attacks against Machine Learning
ZOO: Zeroth Order Optimization based Black-box Attacks to Deep Neural Networks without Training Substitute Models
Decision-Based Adversarial Attacks: Reliable Attacks Against Black-Box Machine Learning Models
Black-box Adversarial Attacks with Limited Queries and Information
Poisoning Attack์ ๋ชจ๋ธ์ ํ์ต์ ์๋ชป๋ ๋ฐฉํฅ์ผ๋ก ์ด๋๋ ๊ณต๊ฒฉ์ผ๋ก, ์ฃผ๋ก *ํ์ต ๋ฐ์ดํฐ(Training Data)*๋ฅผ ๋ณํ์์ผ์ ์ด๋ฅผ ์ด๋ฃจ์ด๋ด๊ฒ ํฉ๋๋ค.
ํ์ต ๋ฐ์ดํฐ๋ฅผ ๋ณํ์ํค๋ ๋ฐฉํฅ์ผ๋ก๋ ์ฌ๋ฌ ๋ฐฉํฅ์ด ์กด์ฌํ๋๋ฐ, ๋ชจ๋ธ์ ์ ์ฒด์ ์ธ ์ฑ๋ฅ์ ๋ฎ์ถ๋๋ก(Performance Degradation) ํ๊ฑฐ๋, ํน์ ์ด๋ฏธ์ง ํน์ ๋ผ๋ฒจ(Label)๋ง ํ๋ฆฌ๊ฒ ํ๋๋ก(Targeted Poisoning) ํ๋ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค.
[์ถ์ฒ ๋ ผ๋ฌธ]
Towards Poisoning of Deep Learning Algorithms with Back-Gradient Optimization.
Poisoning Attacks with Generative Adversarial Nets
Transferable Clean-Label Poisoning Attacks on Deep Neural Nets.
์ ๋์ ๊ณต๊ฒฉ์ ๋ง ๊ทธ๋๋ก ๋ฅ๋ฌ๋์ด ์ค์ํ์ ์ ์ฉ๋์์ ๋์ ์ ์ฉ๋ ์ํ์ด ํฌ๋ค๋ ์ ์์๋ ์ฐ๊ตฌ๋๊ณ ์์ง๋ง, ์ต๊ทผ์๋ ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ด ์ ์ ๋์ ๊ณต๊ฒฉ์ด ๊ฐ๋ฅํ๊ฐ์ ๋ํด์ ๊ณ ์ฐฐํ๋ ๋ ผ๋ฌธ์ด ๋ง์ต๋๋ค. ์ด๋ฐ์๋ ๋ฐฉ์ด ๊ธฐ๋ฒ๊ณผ ๊ณต๊ฒฉ ๊ธฐ๋ฒ์ด ๋ฒ๊ฐ์ ๋ฑ์ฅํ๋ฉด์ ์ฐฝ๊ณผ ๋ฐฉํจ์ ์ธ์์ด ์ด์ด์ก๋ค๋ฉด, ์ต๊ทผ์๋ ์ด์งธ์ ์กฐ๊ทธ๋งํ ๋ ธ์ด์ฆ์๋ ๋ชจ๋ธ์ด ๋ฏผ๊ฐํ๊ฒ ๋ฐ์ํ๋์ง, ์ด๋ป๊ฒํ๋ฉด ๊ทธ๋ฌํ ๋ฏผ๊ฐํ ๋ฐ์์ ์ค์ด๊ณ ์์ ์ (Stable)์ธ ๋ชจ๋ธ์ ๋ง๋ค์ด๋ผ ์ ์๋์ง ๋ฑ ์ข ๋ ๊ทผ์์ ์ธ ๋ถ๋ถ์ ๋ํ ์ฐ๊ตฌ๋ ์ด๋ฃจ์ด์ง๊ณ ์์ต๋๋ค.
์ ๋์ ๊ณต๊ฒฉ์ ๋ฐฉ์ด ๊ธฐ๋ฒ์ผ๋ก๋ ํ์ต ๊ณผ์ ์์ ์ ๋์ ์์ ๋ฅผ ๋ง๋ค๊ณ ํ์ฉํ๋ Adversarial Training, ํน์ ํฌ๊ธฐ์ ๋ ธ์ด์ฆ์ ๋ํด์๋ ์ ๋ ํน์ ๋์ ํ๋ฅ ๋ก ๊ณต๊ฒฉ ๋นํ์ง ์๊ฒ ํ์ตํ๋ Certified Training / Randomized Smoothing, ์ ๋์ ์์ ๋ฅผ ์ ๋ ฅ๋ถํฐ ๊ฑธ๋ฌ๋ด๋ ์ด์ ํ์ง(Adversarial Example Detection) ๋ฑ์ด ์์ต๋๋ค.
[์ถ์ฒ ๋ ผ๋ฌธ]
Towards Robust Neural Networks via Random Self-ensemble
Obfuscated Gradients Give a False Sense of Security: Circumventing Defenses to Adversarial Examples
Understanding Measures of Uncertainty for Adversarial Example Detection
Adversarially Robust Generalization Requires More Data
Robustness May Be at Odds with Accuracy
On the Effectiveness of Interval Bound Propagation for Training Verifiably Robust Models
Theoretically Principled Trade-off between Robustness and Accuracy
Adversarial Examples Are Not Bugs, They Are Features
Reliable evaluation of adversarial robustness with an ensemble of diverse parameter-free attacks
- torch>=1.4.0
- python>=3.6
pip install torchattacks
orgit clone https://github.com/Harry24k/adversairal-attacks-pytorch
import torchattacks
atk = torchattacks.PGD(model, eps=8/255, alpha=2/255, steps=4)
adversarial_images = atk(images, labels)
Torchattack์ ๋ํ ์๋์ ๊ธฐ๋ฅ๋ ์ ๊ณตํฉ๋๋ค.
๊ณต๊ฒฉ ๋ผ๋ฒจ ์ ํ๊ธฐ
- Random target label:
# random labels as target labels.
atk.set_mode_targeted_random(n_classses)
- Least likely label:
# label with the k-th smallest probability used as target labels.
atk.set_mode_targeted_least_likely(kth_min)
- By custom function:
# label from mapping function
atk.set_mode_targeted_by_function(target_map_function=lambda images, labels:(labels+1)%10)
- Return to default:
atk.set_mode_default()
๋ฐํ ํ์ ๋ฐ๊พธ๊ธฐ
- Return adversarial images with integer value (0-255).
atk.set_return_type(type='int')
- Return adversarial images with float value (0-1).
atk.set_return_type(type='float')
์ ๋์ ์์ ์ ์ฅํ๊ธฐ
```python atk.save(data_loader, save_path=None, verbose=True) ```
Training/Eval ๋ชจ๋ ๋ฐ๊พธ๊ธฐ
# For RNN-based models, we cannot calculate gradients with eval mode.
# Thus, it should be changed to the training mode during the attack.
atk.set_training_mode(training=False)
๊ณต๊ฒฉ ์กฐํฉํ๊ธฐ
- Strong attacks
atk1 = torchattacks.FGSM(model, eps=8/255)
atk2 = torchattacks.PGD(model, eps=8/255, alpha=2/255, iters=40, random_start=True)
atk = torchattacks.MultiAttack([atk1, atk2])
- Binary serach for CW
atk1 = torchattacks.CW(model, c=0.1, steps=1000, lr=0.01)
atk2 = torchattacks.CW(model, c=01, steps=1000, lr=0.01)
atk = torchattacks.MultiAttack([atk1, atk2])
- Random restarts
atk1 = torchattacks.PGD(model, eps=8/255, alpha=2/255, iters=40, random_start=True)
atk2 = torchattacks.PGD(model, eps=8/255, alpha=2/255, iters=40, random_start=True)
atk = torchattacks.MultiAttack([atk1, atk2])
๋ ์์ธํ ์ ์ฉ ๋ฐฉ๋ฒ์ ์๋ ๋ฐ๋ชจ๋ค์ ํตํด ์ตํ ์ ์์ต๋๋ค.
- White Box Attack with ImageNet (code, nbviewer): ResNet-18์ ImageNet ๋ฐ์ดํฐ์ torchattacks์ ํ์ฉํ์ฌ ์์ด๋ ๋ฐ๋ชจ์ ๋๋ค.
- Transfer Attack with CIFAR10 (code, nbviewer): torchattacks์ ํ์ฉํ์ฌ Transfer Attack์ ์คํํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
- Adversairal Training with MNIST (code, nbviewer): torchattacks์ ํ์ฉํ์ฌ Adversarial Training์ ํ๋ ์ฝ๋์ ๋๋ค.
-
๋ชจ๋ ์ด๋ฏธ์ง๋ transform[to.Tensor()]์ ํ์ฉํ์ฌ [0, 1]๋ก ์ ๋ ฅ๋์ด์ผํฉ๋๋ค! ๋ณธ๋ PyTorch์์๋ transform์ ํตํด ์ง์๋๋ normalization์ ํ์ฉํ๊ณ ๋ ํฉ๋๋ค. ํ์ง๋ง, ์ ๋์ ๊ณต๊ฒฉ์ ํน์ง ์ ์ต๋ ์ญ๋(Maximum Perturbtion) ๋ฒ์๋ฅผ ์ฃผ๊ฑฐ๋ ์ด๋ฅผ ๋น์ฉ์ผ๋ก ํ์ฉํ๊ธฐ ๋๋ฌธ์, ์ ๋ ฅ ์ด๋ฏธ์ง๊ฐ [0, 1]์ผ ๋ ์ ํํ ์ ๋์ ์์ ๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ๋ฐ๋ผ์, normalization์ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๊ณผ์ ์ด ์๋๋ผ ๋ชจ๋ธ์ ์์ ์ฝ์ ํ์ฌ์ผํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ ์ด ๋ฐ๋ชจ๋ฅผ ์ฐธ๊ณ ๋ถํ๋๋ฆฝ๋๋ค.
-
๋ชจ๋ ๋ชจ๋ธ์
(N, C)
ํํ์ tensor๋ฅผ ์ถ๋ ฅํด์ผํฉ๋๋ค. ์ฌ๊ธฐ์N
์ ๋ฐฐ์น(Batch)์ ๊ฐ์,C
๋ ์ ๋ต ํด๋์ค(class)์ ๊ฐ์์ ๋๋ค! ์ฃผ์ด์ง๋ ๋ชจ๋ธ์ torchvision.models๊ณผ์ ํธํ์ ์ํด์ ํ๋ฅ ๋ฒกํฐ๋ก ์ฌ์ฉ๋(N,C)
๋ง์ ์ถ๋ ฅํด์ผํฉ๋๋ค. ๋ง์ฝ ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ, ๋ชจ๋ธ์ ์ถ๋ ฅ์ ์กฐ์ ํ ์ ์๋ ๋ ์ด์ด(Layer)๋ฅผ ์ด ๋ฐ๋ชจ์ ๊ฐ์ด ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค. -
๋ง์ฝ ๋งค๋ฒ ๋๊ฐ์ ์ ๋์ ์์ ๊ฐ ๋์ค๊ฒ ํ๋ ค๋ฉด,
torch.backends.cudnn.deterministic = True
๋ฅผ ์ฌ์ฉํฉ๋๋ค. GPU์์ ์ด๋ฃจ์ด์ง๋ ์ฐ์ฐ์ non-deterministicํ ๊ฒฝ์ฐ๋ ์๊ธฐ ๋๋ฌธ์, ๊ธฐ์ธ๊ธฐ๋ฅผ ํ์ฉํ๋ ์ ๋์ ๊ณต๊ฒฉ์ ๋ชจ๋ ํ๊ฒฝ์ด ๋๊ฐ๋๋ผ๋ ํญ์ ๋๊ฐ์ ๊ฐ์ ์ถ๋ ฅํ์ง ์์ต๋๋ค [discuss]. ๋ฐ๋ผ์, ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด์๋ GPU์ ๋๋ค์ฑ์ ๊ณ ์ ํ๋๋ก ๋ค์ ๋ช ๋ น์ด๋ฅผ ์คํํด์ผ ํฉ๋๋ค.torch.backends.cudnn.deterministic = True
[ref].
๋ณธ ํจํค์ง๋ฅผ ์ฌ์ฉํ์ ๋ค๋ฉด ์๋๋ฅผ ์ธ์ฉ ๋ถํ๋๋ฆฝ๋๋ค :)
@article{kim2020torchattacks,
title={Torchattacks: A pytorch repository for adversarial attacks},
author={Kim, Hoki},
journal={arXiv preprint arXiv:2010.01950},
year={2020}
}
Torchattacks์ ๋ค๋ฅธ ์ ๋ช ํ ์ ๋์ ๊ณต๊ฒฉ ํจํค์ง์๋ ํธํํ ์ ์๋๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค. ํนํ, ๋ค๋ฅธ ํจํค์ง์ ๊ณต๊ฒฉ์ torchattacks๋ก ์ด์ํ ๊ฒฝ์ฐ, ์ ๋์ ์์ ๋ฅผ ์ ์ฅํ ์ ์๋ save๋ multiattack์ ํ์ฉํ์ฌ ๋ ๊ฐํ ๊ณต๊ฒฉ์ ๋ง๋ค์ด๋ผ ์๋ ์์ต๋๋ค.
- https://github.com/bethgelab/foolbox
pip install foolbox
- e.g., L2BrendelBethge
from torchattacks.attack import Attack
import foolbox as fb
class L2BrendelBethge(Attack):
def __init__(self, model):
super(L2BrendelBethge, self).__init__("L2BrendelBethge", model)
self.fmodel = fb.PyTorchModel(self.model, bounds=(0,1), device=self.device)
self.init_attack = fb.attacks.DatasetAttack()
self.adversary = fb.attacks.L2BrendelBethgeAttack(init_attack=self.init_attack)
self._attack_mode = 'only_default'
def forward(self, images, labels):
images, labels = images.to(self.device), labels.to(self.device)
# DatasetAttack
batch_size = len(images)
batches = [(images[:batch_size//2], labels[:batch_size//2]),
(images[batch_size//2:], labels[batch_size//2:])]
self.init_attack.feed(model=self.fmodel, inputs=batches[0][0]) # feed 1st batch of inputs
self.init_attack.feed(model=self.fmodel, inputs=batches[1][0]) # feed 2nd batch of inputs
criterion = fb.Misclassification(labels)
init_advs = self.init_attack.run(self.fmodel, images, criterion)
# L2BrendelBethge
adv_images = self.adversary.run(self.fmodel, images, labels, starting_points=init_advs)
return adv_images
atk = L2BrendelBethge(model)
atk.save(data_loader=test_loader, save_path="_temp.pt", verbose=True)
- https://github.com/IBM/adversarial-robustness-toolbox
git clone https://github.com/IBM/adversarial-robustness-toolbox
- e.g., SaliencyMapMethod (or Jacobian based saliency map attack)
import torch.nn as nn
import torch.optim as optim
from torchattacks.attack import Attack
import art.attacks.evasion as evasion
from art.classifiers import PyTorchClassifier
class JSMA(Attack):
def __init__(self, model, theta=1/255, gamma=0.15, batch_size=128):
super(JSMA, self).__init__("JSMA", model)
self.classifier = PyTorchClassifier(
model=self.model, clip_values=(0, 1),
loss=nn.CrossEntropyLoss(),
optimizer=optim.Adam(self.model.parameters(), lr=0.01),
input_shape=(1, 28, 28), nb_classes=10)
self.adversary = evasion.SaliencyMapMethod(classifier=self.classifier,
theta=theta, gamma=gamma,
batch_size=batch_size)
self.target_map_function = lambda labels: (labels+1)%10
self._attack_mode = 'only_default'
def forward(self, images, labels):
adv_images = self.adversary.generate(images, self.target_map_function(labels))
return torch.tensor(adv_images).to(self.device)
atk = JSMA(model)
atk.save(data_loader=test_loader, save_path="_temp.pt", verbose=True)
์ด๋ค ์ข ๋ฅ์ ๊ธฐ์ฌ๋ผ๋ ํญ์ ๊ฐ์ฌ๋๋ฆฌ๋ฉฐ, ์ค๋ฅ๊ฐ ์๋ค๋ฉด ๋ง์ค์ ์์ด ์ง์ ๋ถํ๋๋ฆฝ๋๋ค. ๐
๋ง์ฝ, ์๋ก์ด ๊ณต๊ฒฉ์ ์ ์ํ๊ณ ์ถ๋ค๋ฉด CONTRIBUTING.md์ ์ฐธ๊ณ ํด์ฃผ์ธ์!
-
Adversarial Attack Packages:
- https://github.com/IBM/adversarial-robustness-toolbox: Adversarial attack and defense package made by IBM. TensorFlow, Keras, Pyotrch available.
- https://github.com/bethgelab/foolbox: Adversarial attack package made by Bethge Lab. TensorFlow, Pyotrch available.
- https://github.com/tensorflow/cleverhans: Adversarial attack package made by Google Brain. TensorFlow available.
- https://github.com/BorealisAI/advertorch: Adversarial attack package made by BorealisAI. Pytorch available.
- https://github.com/DSE-MSU/DeepRobust: Adversarial attack (especially on GNN) package made by BorealisAI. Pytorch available.
- https://github.com/fra31/auto-attack: Set of attacks that is believed to be the strongest in existence. TensorFlow, Pyotrch available.
-
Adversarial Defense Leaderboard:
-
Adversarial Attack and Defense Papers:
- https://nicholas.carlini.com/writing/2019/all-adversarial-example-papers.html: A Complete List of All (arXiv) Adversarial Example Papers made by Nicholas Carlini.
- https://github.com/chawins/Adversarial-Examples-Reading-List: Adversarial Examples Reading List made by Chawin Sitawarin.
-
ETC:
- https://github.com/Harry24k/gnn-meta-attack: Adversarial Poisoning Attack on Graph Neural Network.
- https://github.com/ChandlerBang/awesome-graph-attack-papers: Graph Neural Network Attack papers.