Skip to content

Commit

Permalink
refactor: class docstrings at top
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerwooo committed Feb 26, 2024
1 parent 8550f43 commit bce005d
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 210 deletions.
33 changes: 15 additions & 18 deletions src/torchattack/admix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ class Admix(Attack):
From the paper 'Admix: Enhancing the Transferability of Adversarial Attacks',
https://arxiv.org/abs/2102.00436
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
portion: Portion for the mixed image. Defaults to 0.2.
size: Number of randomly sampled images. Defaults to 3.
num_classes: Number of classes of the dataset used. Defaults to 1001.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -29,24 +44,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the Admix attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
portion: Portion for the mixed image. Defaults to 0.2.
size: Number of randomly sampled images. Defaults to 3.
num_classes: Number of classes of the dataset used. Defaults to 1001.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
23 changes: 10 additions & 13 deletions src/torchattack/deepfool.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ class DeepFool(Attack):
From the paper 'DeepFool: A Simple and Accurate Method to Fool Deep Neural Networks'
https://arxiv.org/abs/1511.04599
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
steps: Number of steps. Defaults to 100.
overshoot: Overshoot parameter for noise control. Defaults to 0.02.
num_classes: Number of classes to consider. Defaults to 10.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
"""

def __init__(
Expand All @@ -24,19 +34,6 @@ def __init__(
clip_min: float = 0.0,
clip_max: float = 1.0,
) -> None:
"""Initialize the DeepFool attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
steps: Number of steps. Defaults to 100.
overshoot: Overshoot parameter for noise control. Defaults to 0.02.
num_classes: Number of classes to consider. Defaults to 10.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
43 changes: 20 additions & 23 deletions src/torchattack/difgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ class DIFGSM(Attack):
From the paper 'Improving Transferability of Adversarial Examples with Input
Diversity' https://arxiv.org/abs/1803.06978
Note:
Key parameters include `resize_rate` and `diversity_prob`, which defines the
scale size of the resized image and the probability of applying input
diversity. The default values are set to 0.9 and 1.0 respectively (implying
that input diversity is always applied).
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
resize_rate: The resize rate. Defaults to 0.9.
diversity_prob: Applying input diversity with probability. Defaults to 1.0.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -29,29 +49,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the DI-FGSM attack.
Note:
Key parameters include `resize_rate` and `diversity_prob`, which defines the
scale size of the resized image and the probability of applying input
diversity. The default values are set to 0.9 and 1.0 respectively (implying
that input diversity is always applied).
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
resize_rate: The resize rate. Defaults to 0.9.
diversity_prob: Applying input diversity with probability. Defaults to 1.0.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
21 changes: 9 additions & 12 deletions src/torchattack/fgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ class FGSM(Attack):
From the paper 'Explaining and Harnessing Adversarial Examples',
https://arxiv.org/abs/1412.6572
Args:
model: A torch.nn.Module network model.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: Maximum perturbation measured by Linf. Defaults to 8/255.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -23,18 +32,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the FGSM attack.
Args:
model: A torch.nn.Module network model.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: Maximum perturbation measured by Linf. Defaults to 8/255.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
27 changes: 12 additions & 15 deletions src/torchattack/mifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class MIFGSM(Attack):
From the paper 'Boosting Adversarial Attacks with Momentum',
https://arxiv.org/abs/1710.06081
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -26,21 +38,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the MI-FGSM attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
27 changes: 12 additions & 15 deletions src/torchattack/nifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ class NIFGSM(Attack):
From the paper 'Nesterov Accelerated Gradient and Scale Invariance for Adversarial
Attacks' https://arxiv.org/abs/1908.06281
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -30,21 +42,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the NI-FGSM attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
27 changes: 12 additions & 15 deletions src/torchattack/pgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ class PGD(Attack):
From the paper 'Towards Deep Learning Models Resistant to Adversarial Attacks'
https://arxiv.org/abs/1706.06083
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
random_start: Start from random uniform perturbation. Defaults to True.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -26,21 +38,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the PGD attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
random_start: Start from random uniform perturbation. Defaults to True.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
27 changes: 12 additions & 15 deletions src/torchattack/pgdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ class PGDL2(Attack):
From the paper 'Towards Deep Learning Models Resistant to Adversarial Attacks',
https://arxiv.org/abs/1706.06083
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation, measured in L2. Defaults to 1.0.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
random_start: Start from random uniform perturbation. Defaults to True.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -28,21 +40,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the PGD attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation, measured in L2. Defaults to 1.0.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
random_start: Start from random uniform perturbation. Defaults to True.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
33 changes: 15 additions & 18 deletions src/torchattack/sinifgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@
class SINIFGSM(Attack):
"""The SI-NI-FGSM (Scale-invariant Nesterov-accelerated Iterative FGSM) attack.
From the paper 'Nesterov Accelerated Gradient and Scale Invariance for Adversarial
Attacks' https://arxiv.org/abs/1908.06281
From the paper 'Nesterov Accelerated Gradient and Scale Invariance for Adversarial
Attacks' https://arxiv.org/abs/1908.06281
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
m: Number of scaled copies of the image. Defaults to 3.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

def __init__(
Expand All @@ -27,22 +40,6 @@ def __init__(
clip_max: float = 1.0,
targeted: bool = False,
) -> None:
"""Initialize the SI-NI-FGSM attack.
Args:
model: The model to attack.
normalize: A transform to normalize images.
device: Device to use for tensors. Defaults to cuda if available.
eps: The maximum perturbation. Defaults to 8/255.
steps: Number of steps. Defaults to 10.
alpha: Step size, `eps / steps` if None. Defaults to None.
decay: Decay factor for the momentum term. Defaults to 1.0.
m: Number of scaled copies of the image. Defaults to 3.
clip_min: Minimum value for clipping. Defaults to 0.0.
clip_max: Maximum value for clipping. Defaults to 1.0.
targeted: Targeted attack if True. Defaults to False.
"""

super().__init__(normalize, device)

self.model = model
Expand Down
Loading

0 comments on commit bce005d

Please sign in to comment.