Skip to content

Commit

Permalink
Using clip values in notebook. CI error fixes
Browse files Browse the repository at this point in the history
Signed-off-by: GiulioZizzo <[email protected]>
  • Loading branch information
GiulioZizzo committed Aug 27, 2023
1 parent 0178cbe commit 0914f99
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
3 changes: 1 addition & 2 deletions art/attacks/poisoning/backdoor_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ def __init__(self, perturbation: Union[Callable, List[Callable]]) -> None:
self._check_params()

def poison( # pylint: disable=W0221
self, x: np.ndarray, y: Optional[np.ndarray] = None, broadcast=False, channels_first: bool = False, **kwargs
self, x: np.ndarray, y: Optional[np.ndarray] = None, broadcast=False, **kwargs
) -> Tuple[np.ndarray, np.ndarray]:
"""
Calls perturbation function on input x and returns the perturbed input and poison labels for the data.
:param x: An array with the points that initialize attack points.
:param y: The target labels for the attack.
:param broadcast: whether or not to broadcast single target label
:param channels_first: it the data is fed in channels_first foramt
:return: An tuple holding the `(poisoning_examples, poisoning_labels)`.
"""
if y is None: # pragma: no cover
Expand Down
8 changes: 1 addition & 7 deletions art/attacks/poisoning/clean_label_backdoor_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,14 @@ def __init__(
self._check_params()

def poison( # pylint: disable=W0221
self,
x: np.ndarray,
y: Optional[np.ndarray] = None,
broadcast: bool = True,
channels_first: bool = False,
**kwargs
self, x: np.ndarray, y: Optional[np.ndarray] = None, broadcast: bool = True, **kwargs
) -> Tuple[np.ndarray, np.ndarray]:
"""
Calls perturbation function on input x and returns the perturbed input and poison labels for the data.
:param x: An array with the points that initialize attack points.
:param y: The target labels for the attack.
:param broadcast: whether or not to broadcast single target label
:param channels_first: it the data is fed in channels_first foramt
:return: An tuple holding the `(poisoning_examples, poisoning_labels)`.
"""
data = np.copy(x)
Expand Down
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def _image_dl_gan(**kwargs):


@pytest.fixture
def image_dl_estimator(framework):
def image_dl_estimator(framework, get_image_classifier_mx_instance):
def _image_dl_estimator(functional=False, **kwargs):
sess = None
wildcard = False
Expand Down
39 changes: 18 additions & 21 deletions notebooks/huggingface_notebook.ipynb

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion tests/attacks/poison/test_clean_label_backdoor_attack.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,20 @@ def test_poison(art_warning, get_default_mnist_subset, image_dl_estimator, frame
classifier, _ = image_dl_estimator()
target = to_categorical([9], 10)[0]
if framework in ["pytorch", "huggingface"]:

def mod(x):
original_dtype = x.dtype
x = add_pattern_bd(x, channels_first=True)
return x.astype(original_dtype)

else:

def mod(x):
original_dtype = x.dtype
x = add_pattern_bd(x)
return x.astype(original_dtype)

backdoor = PoisoningAttackBackdoor(add_pattern_bd)
backdoor = PoisoningAttackBackdoor(mod)
attack = PoisoningAttackCleanLabelBackdoor(backdoor, classifier, target)
poison_data, poison_labels = attack.poison(x_train, y_train)

Expand Down

0 comments on commit 0914f99

Please sign in to comment.