-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
foolbox imports conditional to foolbox installation
- Loading branch information
1 parent
eb14ac9
commit 28157c1
Showing
7 changed files
with
69 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
try: | ||
import foolbox | ||
except ImportError: | ||
pass # foolbox is an extra component and requires the foolbox library | ||
else: | ||
from .foolbox_pgd import * |
2 changes: 0 additions & 2 deletions
2
secml2/adv/evasion/foolbox.py → ...v/evasion/foolbox_attacks/foolbox_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
|
||
from typing import Optional | ||
from secml2.adv.evasion.foolbox_attacks.foolbox_base import BaseFoolboxEvasionAttack | ||
from secml2.adv.evasion.perturbation_models import PerturbationModels | ||
|
||
from foolbox.attacks.projected_gradient_descent import ( | ||
L1ProjectedGradientDescentAttack, | ||
L2ProjectedGradientDescentAttack, | ||
LinfProjectedGradientDescentAttack, | ||
) | ||
|
||
class PGDFoolbox(BaseFoolboxEvasionAttack): | ||
def __init__( | ||
self, | ||
perturbation_model: str, | ||
epsilon: float, | ||
num_steps: int, | ||
step_size: float, | ||
random_start: bool, | ||
y_target: Optional[int] = None, | ||
lb: float = 0.0, | ||
ub: float = 1.0, | ||
**kwargs | ||
) -> None: | ||
perturbation_models = { | ||
PerturbationModels.L1: L1ProjectedGradientDescentAttack, | ||
PerturbationModels.L2: L2ProjectedGradientDescentAttack, | ||
PerturbationModels.LINF: LinfProjectedGradientDescentAttack, | ||
} | ||
foolbox_attack_cls = perturbation_models.get(perturbation_model, None) | ||
if foolbox_attack_cls is None: | ||
raise NotImplementedError( | ||
"This threat model is not implemented in foolbox." | ||
) | ||
|
||
foolbox_attack = foolbox_attack_cls( | ||
abs_stepsize=step_size, steps=num_steps, random_start=random_start | ||
) | ||
|
||
super().__init__( | ||
foolbox_attack=foolbox_attack, | ||
epsilon=epsilon, | ||
y_target=y_target, | ||
lb=lb, | ||
ub=ub, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
author_email="[email protected], [email protected]", | ||
install_requires=[], | ||
extras_require={ | ||
"pytorch": ["torch>=1.4,!=1.5.*", "torchvision>=0.5,!=0.6.*"], | ||
"foolbox": ["foolbox>=3.3.0", "torch>=1.4,!=1.5.*", "torchvision>=0.5,!=0.6.*"], | ||
}, | ||
python_requires=">=3.7" | ||
|