def _generate_samples(self, x, epsilon_map): """ Generate samples around the current image. :param x: Sample input with shape as expected by the model. :type x: `np.ndarray` :param epsilon_map: Samples drawn from search space :type epsilon_map: `np.ndarray` :return: Two arrays of new input samples to approximate gradient :rtype: `list(np.ndarray)` """ minus = clip_and_round(np.repeat(x, self.num_basis, axis=0) - epsilon_map, self.clip_values, self.round_samples) plus = clip_and_round(np.repeat(x, self.num_basis, axis=0) + epsilon_map, self.clip_values, self.round_samples) return minus, plus
def predict(self, x: np.ndarray, batch_size: int = 128, **kwargs) -> np.ndarray: # pylint: disable=W0221 """ Perform prediction of the classifier for input `x`. Rounds results first. :param x: Features in array of shape (nb_samples, nb_features) or (nb_samples, nb_pixels_1, nb_pixels_2, nb_channels) or (nb_samples, nb_channels, nb_pixels_1, nb_pixels_2). :param batch_size: Size of batches. :return: Array of predictions of shape `(nb_inputs, nb_classes)`. """ return self._classifier.predict(clip_and_round(x, self.clip_values, self.round_samples), batch_size=batch_size)
def _generate_samples(self, x: np.ndarray, epsilon_map: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: """ Generate samples around the current image. :param x: Sample input with shape as expected by the model. :param epsilon_map: Samples drawn from search space. :return: Two arrays of new input samples to approximate gradient. """ minus = clip_and_round( np.repeat(x, self.num_basis, axis=0) - epsilon_map, self.clip_values, self.round_samples, ) plus = clip_and_round( np.repeat(x, self.num_basis, axis=0) + epsilon_map, self.clip_values, self.round_samples, ) return minus, plus
def _wrap_predict(self, x, batch_size=128): """ Perform prediction for a batch of inputs. Rounds results first. :param x: Test set. :type x: `np.ndarray` :param batch_size: Size of batches. :type batch_size: `int` :return: Array of predictions of shape `(nb_inputs, nb_classes)`. :rtype: `np.ndarray` """ return self._predict(clip_and_round(x, self.clip_values, self.round_samples), **{"batch_size": batch_size})
def _wrap_predict(self, x: np.ndarray, batch_size: int = 128) -> np.ndarray: """ Perform prediction for a batch of inputs. Rounds results first. :param x: Input samples. :param batch_size: Size of batches. :return: Array of predictions of shape `(nb_inputs, nb_classes)`. """ return self._predict( clip_and_round(x, self.clip_values, self.round_samples), **{"batch_size": batch_size})
def _wrap_predict(self, x, logits=False, batch_size=128): """ Perform prediction for a batch of inputs. Rounds results first. :param x: Test set. :type x: `np.ndarray` :param logits: `True` if the prediction should be done at the logits layer. :type logits: `bool` :param batch_size: Size of batches. :type batch_size: `int` :return: Array of predictions of shape `(nb_inputs, self.nb_classes)`. :rtype: `np.ndarray` """ return self._predict( clip_and_round(x, self.clip_values, self.round_samples), logits, batch_size)