Skip to content

Commit

Permalink
Merge pull request #2327 from f4str/activation-defense-bug
Browse files Browse the repository at this point in the history
Fix `ActivationDefense` and `SpectralSignatures` expected flattened bug
  • Loading branch information
beat-buesser authored Dec 20, 2023
2 parents a281f62 + fc7cbcb commit 95c778e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion art/defences/detector/poison/activation_defence.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,9 @@ def _get_activations(self, x_train: Optional[np.ndarray] = None) -> np.ndarray:

# wrong way to get activations activations = self.classifier.predict(self.x_train)
if isinstance(activations, np.ndarray):
nodes_last_layer = np.shape(activations)[1]
# flatten activations across batch
activations = np.reshape(activations, (activations.shape[0], -1))
nodes_last_layer = activations.shape[1]
else:
raise ValueError("activations is None or tensor.")

Expand Down
2 changes: 2 additions & 0 deletions art/defences/detector/poison/spectral_signature_defense.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def detect_poison(self, **kwargs) -> Tuple[dict, List[int]]:
raise ValueError("Wrong type detected.")

if features_x_poisoned is not None:
# flatten activations across batch
features_x_poisoned = np.reshape(features_x_poisoned, (features_x_poisoned.shape[0], -1))
features_split = segment_by_class(features_x_poisoned, self.y_train, self.classifier.nb_classes)
else:
raise ValueError("Activation are `None`.")
Expand Down

0 comments on commit 95c778e

Please sign in to comment.