1818"""
1919Provides black-box gradient estimation using NES.
2020"""
21- from __future__ import absolute_import , division , print_function , unicode_literals
22-
2321import logging
2422from typing import List , Optional , Tuple , Union , TYPE_CHECKING
2523
2624import numpy as np
2725from scipy .stats import entropy
2826
29- from art .estimators .classification . classifier import ClassifierClassLossGradients
30- from art .utils import clip_and_round , deprecated
31- from art .wrappers . wrapper import ClassifierWrapper
27+ from art .estimators .estimator import BaseEstimator
28+ from art .estimators . classification . classifier import ClassifierMixin , ClassifierLossGradients
29+ from art .utils import clip_and_round
3230
3331if TYPE_CHECKING :
3432 from art .utils import CLASSIFIER_CLASS_LOSS_GRADIENTS_TYPE
3533
3634logger = logging .getLogger (__name__ )
3735
3836
39- @deprecated (
40- end_version = "1.8.0" ,
41- reason = "Expectation over transformation has been replaced with " "art.estimators" ,
42- replaced_by = "art.preprocessing.expectation_over_transformation" ,
43- )
44- class QueryEfficientBBGradientEstimation (ClassifierWrapper , ClassifierClassLossGradients ):
37+ class QueryEfficientGradientEstimationClassifier (ClassifierLossGradients , ClassifierMixin , BaseEstimator ):
4538 """
4639 Implementation of Query-Efficient Black-box Adversarial Examples. The attack approximates the gradient by
4740 maximizing the loss function over samples drawn from random Gaussian noise around the input.
4841
4942 | Paper link: https://arxiv.org/abs/1712.07113
5043 """
5144
52- attack_params = ["num_basis" , "sigma" , "round_samples" ]
45+ estimator_params = ["num_basis" , "sigma" , "round_samples" ]
5346
5447 def __init__ (
5548 self ,
@@ -59,20 +52,19 @@ def __init__(
5952 round_samples : float = 0.0 ,
6053 ) -> None :
6154 """
62- :param classifier: An instance of a `Classifier` whose loss_gradient is being approximated.
55+ :param classifier: An instance of a classification estimator whose loss_gradient is being approximated.
6356 :param num_basis: The number of samples to draw to approximate the gradient.
6457 :param sigma: Scaling on the Gaussian noise N(0,1).
6558 :param round_samples: The resolution of the input domain to round the data to, e.g., 1.0, or 1/255. Set to 0 to
6659 disable.
6760 """
68- super ().__init__ (classifier )
69- # self.predict refers to predict of classifier
61+ super ().__init__ (model = classifier .model , clip_values = classifier .clip_values )
7062 # pylint: disable=E0203
71- self ._predict = self . classifier . predict
63+ self ._classifier = classifier
7264 self .num_basis = num_basis
7365 self .sigma = sigma
7466 self .round_samples = round_samples
75- self ._nb_classes = self .classifier .nb_classes
67+ self ._nb_classes = self ._classifier .nb_classes
7668
7769 @property
7870 def input_shape (self ) -> Tuple [int , ...]:
@@ -81,18 +73,18 @@ def input_shape(self) -> Tuple[int, ...]:
8173
8274 :return: Shape of one input sample.
8375 """
84- return self ._input_shape # type: ignore
76+ return self ._classifier . input_shape # type: ignore
8577
8678 def predict (self , x : np .ndarray , batch_size : int = 128 , ** kwargs ) -> np .ndarray : # pylint: disable=W0221
8779 """
88- Perform prediction of the classifier for input `x`.
80+ Perform prediction of the classifier for input `x`. Rounds results first.
8981
9082 :param x: Features in array of shape (nb_samples, nb_features) or (nb_samples, nb_pixels_1, nb_pixels_2,
9183 nb_channels) or (nb_samples, nb_channels, nb_pixels_1, nb_pixels_2).
9284 :param batch_size: Size of batches.
9385 :return: Array of predictions of shape `(nb_inputs, nb_classes)`.
9486 """
95- return self ._wrap_predict ( x , batch_size = batch_size )
87+ return self ._classifier . predict ( clip_and_round ( x , self . clip_values , self . round_samples ) , batch_size = batch_size )
9688
9789 def fit (self , x : np .ndarray , y : np .ndarray , ** kwargs ) -> None :
9890 """
@@ -172,16 +164,6 @@ def loss_gradient(self, x: np.ndarray, y: np.ndarray, **kwargs) -> np.ndarray:
172164 grads = self ._apply_preprocessing_gradient (x , np .array (grads ))
173165 return grads
174166
175- def _wrap_predict (self , x : np .ndarray , batch_size : int = 128 ) -> np .ndarray :
176- """
177- Perform prediction for a batch of inputs. Rounds results first.
178-
179- :param x: Input samples.
180- :param batch_size: Size of batches.
181- :return: Array of predictions of shape `(nb_inputs, nb_classes)`.
182- """
183- return self ._predict (clip_and_round (x , self .clip_values , self .round_samples ), ** {"batch_size" : batch_size })
184-
185167 def get_activations (self , x : np .ndarray , layer : Union [int , str ], batch_size : int ) -> np .ndarray :
186168 """
187169 Return the output of the specified layer for input `x`. `layer` is specified by layer index (between 0 and
0 commit comments