Skip to content

Commit f29950a

Browse files
authored
Merge branch 'dev_1.17.0' into hf_notebook_dev
2 parents bb9fbee + 74be71f commit f29950a

File tree

28 files changed

+1631
-420
lines changed

28 files changed

+1631
-420
lines changed

art/attacks/evasion/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from art.attacks.evasion.brendel_bethge import BrendelBethgeAttack
1919

2020
from art.attacks.evasion.boundary import BoundaryAttack
21+
from art.attacks.evasion.composite_adversarial_attack import CompositeAdversarialAttackPyTorch
2122
from art.attacks.evasion.carlini import CarliniL2Method, CarliniLInfMethod, CarliniL0Method
2223
from art.attacks.evasion.decision_tree_attack import DecisionTreeAttack
2324
from art.attacks.evasion.deepfool import DeepFool

art/attacks/evasion/composite_adversarial_attack.py

Lines changed: 673 additions & 0 deletions
Large diffs are not rendered by default.

art/attacks/extraction/knockoff_nets.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def _random_extraction(self, x: np.ndarray, thieved_classifier: "CLASSIFIER_TYPE
155155
y=fake_labels,
156156
batch_size=self.batch_size_fit,
157157
nb_epochs=self.nb_epochs,
158-
verbose=0,
158+
verbose=False,
159159
)
160160

161161
return thieved_classifier
@@ -243,7 +243,7 @@ def _adaptive_extraction(
243243
y=fake_label,
244244
batch_size=self.batch_size_fit,
245245
nb_epochs=1,
246-
verbose=0,
246+
verbose=False,
247247
)
248248

249249
# Test new labels

art/attacks/inference/membership_inference/black_box.py

Lines changed: 249 additions & 123 deletions
Large diffs are not rendered by default.

art/attacks/poisoning/sleeper_agent_attack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def _create_model(
360360
for layer in model_pt.model.children():
361361
if hasattr(layer, "reset_parameters"):
362362
layer.reset_parameters() # type: ignore
363-
model_pt.fit(x_train, y_train, batch_size=batch_size, nb_epochs=epochs, verbose=1)
363+
model_pt.fit(x_train, y_train, batch_size=batch_size, nb_epochs=epochs, verbose=True)
364364
predictions = model_pt.predict(x_test)
365365
accuracy = np.sum(np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
366366
logger.info("Accuracy of retrained model : %s", accuracy * 100.0)
@@ -370,7 +370,7 @@ def _create_model(
370370

371371
self.substitute_classifier.model.trainable = True
372372
model_tf = self.substitute_classifier.clone_for_refitting()
373-
model_tf.fit(x_train, y_train, batch_size=batch_size, nb_epochs=epochs, verbose=0)
373+
model_tf.fit(x_train, y_train, batch_size=batch_size, nb_epochs=epochs, verbose=False)
374374
predictions = model_tf.predict(x_test)
375375
accuracy = np.sum(np.argmax(predictions, axis=1) == np.argmax(y_test, axis=1)) / len(y_test)
376376
logger.info("Accuracy of retrained model : %s", accuracy * 100.0)

art/defences/detector/poison/activation_defence.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,9 @@ def _get_activations(self, x_train: Optional[np.ndarray] = None) -> np.ndarray:
695695

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

art/defences/detector/poison/spectral_signature_defense.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ def detect_poison(self, **kwargs) -> Tuple[dict, List[int]]:
121121
raise ValueError("Wrong type detected.")
122122

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

art/defences/trainer/adversarial_trainer.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ def fit_generator(self, generator: "DataGenerator", nb_epochs: int = 20, **kwarg
188188
x_batch[adv_ids] = x_adv
189189

190190
# Fit batch
191-
self._classifier.fit(x_batch, y_batch, nb_epochs=1, batch_size=x_batch.shape[0], verbose=0, **kwargs)
191+
self._classifier.fit(
192+
x_batch, y_batch, nb_epochs=1, batch_size=x_batch.shape[0], verbose=False, **kwargs
193+
)
192194
attack_id = (attack_id + 1) % len(self.attacks)
193195

194196
def fit( # pylint: disable=W0221
@@ -260,7 +262,9 @@ def fit( # pylint: disable=W0221
260262
x_batch[adv_ids] = x_adv
261263

262264
# Fit batch
263-
self._classifier.fit(x_batch, y_batch, nb_epochs=1, batch_size=x_batch.shape[0], verbose=0, **kwargs)
265+
self._classifier.fit(
266+
x_batch, y_batch, nb_epochs=1, batch_size=x_batch.shape[0], verbose=False, **kwargs
267+
)
264268
attack_id = (attack_id + 1) % len(self.attacks)
265269

266270
def predict(self, x: np.ndarray, **kwargs) -> np.ndarray:

art/defences/trainer/dp_instahide_trainer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def fit( # pylint: disable=W0221
155155
x_aug = self._generate_noise(x_aug)
156156

157157
# fit batch
158-
self._classifier.fit(x_aug, y_aug, nb_epochs=1, batch_size=x_aug.shape[0], verbose=0, **kwargs)
158+
self._classifier.fit(x_aug, y_aug, nb_epochs=1, batch_size=x_aug.shape[0], verbose=False, **kwargs)
159159

160160
# get metrics
161161
loss = self._classifier.compute_loss(x_aug, y_aug, reduction="mean")
@@ -234,7 +234,7 @@ def fit_generator(self, generator: "DataGenerator", nb_epochs: int = 20, **kwarg
234234
x_aug = self._generate_noise(x_aug)
235235

236236
# fit batch
237-
self._classifier.fit(x_aug, y_aug, nb_epochs=1, batch_size=x_aug.shape[0], verbose=0, **kwargs)
237+
self._classifier.fit(x_aug, y_aug, nb_epochs=1, batch_size=x_aug.shape[0], verbose=False, **kwargs)
238238

239239
# get metrics
240240
loss = self._classifier.compute_loss(x_aug, y_aug, reduction="mean")

art/estimators/certification/derandomized_smoothing/pytorch.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ def fit( # pylint: disable=W0221
438438
training_mode: bool = True,
439439
drop_last: bool = False,
440440
scheduler: Optional[Any] = None,
441-
verbose: Optional[Union[bool, int]] = None,
441+
verbose: bool = False,
442442
update_batchnorm: bool = True,
443443
batchnorm_update_epochs: int = 1,
444444
transform: Optional["torchvision.transforms.transforms.Compose"] = None,
@@ -457,7 +457,7 @@ def fit( # pylint: disable=W0221
457457
the batch size. If ``False`` and the size of dataset is not divisible by the batch size, then
458458
the last batch will be smaller. (default: ``False``)
459459
:param scheduler: Learning rate scheduler to run at the start of every epoch.
460-
:param verbose: if to display training progress bars
460+
:param verbose: Display training progress bar.
461461
:param update_batchnorm: ViT specific argument.
462462
If to run the training data through the model to update any batch norm statistics prior
463463
to training. Useful on small datasets when using pre-trained ViTs.
@@ -469,8 +469,6 @@ def fit( # pylint: disable=W0221
469469
"""
470470
import torch
471471

472-
display_pb = self.process_verbose(verbose)
473-
474472
# Set model mode
475473
self._model.train(mode=training_mode)
476474

@@ -501,7 +499,7 @@ def fit( # pylint: disable=W0221
501499
epoch_loss = []
502500
epoch_batch_sizes = []
503501

504-
pbar = tqdm(range(num_batch), disable=not display_pb)
502+
pbar = tqdm(range(num_batch), disable=not verbose)
505503

506504
# Train for one epoch
507505
for m in pbar:
@@ -547,7 +545,7 @@ def fit( # pylint: disable=W0221
547545
epoch_loss.append(loss.cpu().detach().numpy())
548546
epoch_batch_sizes.append(len(i_batch))
549547

550-
if display_pb:
548+
if verbose:
551549
pbar.set_description(
552550
f"Loss {np.average(epoch_loss, weights=epoch_batch_sizes):.3f} "
553551
f"Acc {np.average(epoch_acc, weights=epoch_batch_sizes):.3f} "

0 commit comments

Comments
 (0)