Skip to content

Commit

Permalink
Rename attack runner helper
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed May 8, 2024
1 parent 553f447 commit c35f8f2
Show file tree
Hide file tree
Showing 15 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/torchattack/admix.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ def x_admix(x: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(Admix, {'eps': 8 / 255, 'steps': 10})
2 changes: 1 addition & 1 deletion src/torchattack/deepfool.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,6 @@ def _atleast_kd(self, x: torch.Tensor, k: int) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(DeepFool, attack_cfg={'steps': 50, 'overshoot': 0.02}, model='resnet152')
2 changes: 1 addition & 1 deletion src/torchattack/difgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def input_diversity(


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

cfgs = {'eps': 16 / 255, 'steps': 10, 'resize_rate': 0.9, 'diversity_prob': 1.0}
run_attack(DIFGSM, attack_cfg=cfgs)
2 changes: 1 addition & 1 deletion src/torchattack/fgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(FGSM, {'eps': 8 / 255, 'clip_min': 0.0, 'clip_max': 1.0})
2 changes: 1 addition & 1 deletion src/torchattack/fia.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,6 @@ def __backward_hook(self, m: nn.Module, i: torch.Tensor, o: torch.Tensor):


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(FIA, {'eps': 8 / 255, 'steps': 10, 'feature_layer': 'layer2'})
2 changes: 1 addition & 1 deletion src/torchattack/geoda.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,6 @@ def is_adv(self, x_adv: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(GeoDA, {'epsilon': 4, 'p': 'l2', 'max_queries': 4000}, batch_size=2)
2 changes: 1 addition & 1 deletion src/torchattack/mifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(MIFGSM, {'eps': 8 / 255, 'steps': 10})
2 changes: 1 addition & 1 deletion src/torchattack/nifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(NIFGSM, {'eps': 8 / 255, 'steps': 10})
2 changes: 1 addition & 1 deletion src/torchattack/pgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(PGD, {'eps': 8 / 255, 'steps': 20, 'random_start': True})
2 changes: 1 addition & 1 deletion src/torchattack/pgdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(PGDL2, {'eps': 1.0, 'steps': 10, 'random_start': False})
25 changes: 16 additions & 9 deletions src/torchattack/utils.py → src/torchattack/runner.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
def run_attack(attack, attack_cfg, model='resnet50', samples=100, batch_size=8) -> None:
from contextlib import suppress

import torch
import torchvision as tv

from torchattack.dataset import NIPSLoader


def run_attack(
attack,
attack_cfg,
model: str = 'resnet50',
samples: int = 100,
batch_size: int = 8,
) -> None:
"""Helper function to run attacks in `__main__`.
Example:
Expand All @@ -10,17 +24,10 @@ def run_attack(attack, attack_cfg, model='resnet50', samples=100, batch_size=8)
Args:
attack: The attack class to initialize.
attack_cfg: A dict of keyword arguments passed to the attack class.
model: The model to attack. Defaults to "resnet50".
model: The torchvision model to attack. Defaults to "resnet50".
samples: Max number of samples to attack. Defaults to 100.
"""

from contextlib import suppress

import torch
import torchvision as tv

from torchattack.dataset import NIPSLoader

# Try to import rich for progress bar
with suppress(ImportError):
from rich import print
Expand Down
2 changes: 1 addition & 1 deletion src/torchattack/sinifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(SINIFGSM, {'eps': 8 / 255, 'steps': 10, 'm': 3})
2 changes: 1 addition & 1 deletion src/torchattack/tifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,6 @@ def gkern(kern_len: int = 15, n_sig: int = 3) -> np.ndarray:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(TIFGSM, {'eps': 8 / 255, 'steps': 10, 'kern_len': 15, 'n_sig': 3})
2 changes: 1 addition & 1 deletion src/torchattack/vmifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(VMIFGSM, {'eps': 8 / 255, 'steps': 10, 'n': 5, 'beta': 1.5})
2 changes: 1 addition & 1 deletion src/torchattack/vnifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,6 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:


if __name__ == '__main__':
from torchattack.utils import run_attack
from torchattack.runner import run_attack

run_attack(VNIFGSM, {'eps': 8 / 255, 'steps': 10, 'n': 5, 'beta': 1.5})

0 comments on commit c35f8f2

Please sign in to comment.