Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mask key configurable. #225

Merged
merged 1 commit into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion mart/attack/enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ def verify(self, input_adv, *, input, target):


class Mask(Constraint):
def __init__(self, key="perturbable_mask"):
super().__init__()
self.key = key

def verify(self, input_adv, *, input, target):
# True/1 is mutable, False/0 is immutable.
# mask.shape=(H, W)
mask = target["perturbable_mask"]
mask = target[self.key]

# Immutable boolean mask, True is immutable.
imt_mask = (1 - mask).bool()
Expand Down
6 changes: 5 additions & 1 deletion mart/attack/projector.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,13 @@ def project_(self, perturbation, *, input, target):


class Mask(Projector):
def __init__(self, key="perturbable_mask"):
super().__init__()
self.key = key

@torch.no_grad()
def project_(self, perturbation, *, input, target):
perturbation.mul_(target["perturbable_mask"])
perturbation.mul_(target[self.key])

def __repr__(self):
return f"{self.__class__.__name__}()"
Loading