Skip to content

Commit

Permalink
(#7) Attack: Add CW attack
Browse files Browse the repository at this point in the history
  • Loading branch information
betarixm committed Apr 18, 2022
1 parent 42127f3 commit 5852338
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/attack/attack.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typings.models import Attack
from models import Fgsm, Pgd
from models import Fgsm, Pgd, Cw
from defense.models import Reformer
from victim.models import Classifier

Expand Down Expand Up @@ -33,7 +33,7 @@
type=str,
help="Attack method",
required=True,
choices=["fgsm", "pgd"],
choices=["fgsm", "pgd", "cw"],
)

parser.add_argument(
Expand Down Expand Up @@ -74,8 +74,10 @@

if args.method == "fgsm":
attack_cls = Fgsm
else:
elif args.method == "pgd":
attack_cls = Pgd
else:
attack_cls = Cw

attacker = attack_cls(classifier, dataset, defense_model)

Expand Down
6 changes: 6 additions & 0 deletions src/attack/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from cleverhans.tf2.attacks.fast_gradient_method import fast_gradient_method
from cleverhans.tf2.attacks.projected_gradient_descent import projected_gradient_descent
from cleverhans.tf2.attacks.carlini_wagner_l2 import carlini_wagner_l2

import numpy as np
import tensorflow as tf
Expand All @@ -24,3 +25,8 @@ def add_perturbation(self, x: np.array) -> np.array:
40,
np.inf,
)


class Cw(Attack):
def add_perturbation(self, x: np.array) -> np.array:
return carlini_wagner_l2(self.victim_model.model(), x)

0 comments on commit 5852338

Please sign in to comment.