Skip to content

Commit

Permalink
Set default normalize as None in top-level attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Nov 21, 2024
1 parent 2bd000a commit 3e5d623
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 23 deletions.
34 changes: 31 additions & 3 deletions torchattack/_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def __init__(
# If normalize is None, use identity function
self.normalize = normalize if normalize else lambda x: x

@abstractmethod
def forward(self, *args: Any, **kwds: Any):
pass

def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.forward(*args, **kwds)

Expand All @@ -46,6 +50,30 @@ def repr_map(k, v):
args = ', '.join(repr_map(k, v) for k, v in self.__dict__.items())
return f'{name}({args})'

@abstractmethod
def forward(self, *args: Any, **kwds: Any):
pass
def __eq__(self, other):
if not isinstance(other, Attack):
return False

eq_name_attrs = [
'model',
'normalize',
'lossfn',
'feature_layer', # FIA
'hooks', # PNAPatchOut, TGR, VDC
'perceptual_criteria', # SSP
'sub_basis', # GeoDA
]
for attr in eq_name_attrs:
if not (hasattr(self, attr) and hasattr(other, attr)):
continue
if (
getattr(self, attr).__class__.__name__
!= getattr(other, attr).__class__.__name__
):
return False

return all(
getattr(self, attr) == getattr(other, attr)
for attr in self.__dict__
if attr not in eq_name_attrs
)
2 changes: 1 addition & 1 deletion torchattack/admix.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Admix(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/decowa.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DeCoWA(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/deepfool.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DeepFool(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
steps: int = 100,
overshoot: float = 0.02,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/difgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/fgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class FGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
clip_min: float = 0.0,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/fia.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FIA(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/geoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class GeoDA(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
input_shape: tuple = (3, 224, 224),
epsilon: int = 5,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/mifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/nifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class NIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/pgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class PGD(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/pgdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PGDL2(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 1.0,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/pna_patchout.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class PNAPatchOut(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
model_name: str = '',
eps: float = 8 / 255,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/sinifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SINIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/ssa.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class SSA(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/ssp.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SSP(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 100,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/tgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TGR(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
model_name: str = '',
eps: float = 8 / 255,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/tifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class TIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/vdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class VDC(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
model_name: str = '',
eps: float = 8 / 255,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/vmifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class VMIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down
2 changes: 1 addition & 1 deletion torchattack/vnifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class VNIFGSM(Attack):
def __init__(
self,
model: nn.Module | AttackModel,
normalize: Callable[[torch.Tensor], torch.Tensor] | None,
normalize: Callable[[torch.Tensor], torch.Tensor] | None = None,
device: torch.device | None = None,
eps: float = 8 / 255,
steps: int = 10,
Expand Down

0 comments on commit 3e5d623

Please sign in to comment.