class_docstr: Implementation of STRIP: A Defence Against Trojan Attacks on Deep Neural Networks (Gao et. al. 2020)
  
  | Paper link: https://arxiv.org/abs/1902.06531
class_docstr_tok: ["Implementation","of","STRIP",":","A","Defence","Against","Trojan","Attacks","on","Deep","Neural","Networks","(","Gao","et",".","al",".","2020",")","|","Paper","link",":","https",":","\/\/arxiv.org\/abs\/1902.06531"]
docstr: Perform prediction of the given classifier for a batch of inputs, potentially filtering suspicious input
  
  :param x: Input samples.
  :return: Array of predictions of shape `(nb_inputs, nb_classes)`.
docstr_tok: ["Perform","prediction","of","the","given","classifier","for","a","batch","of","inputs",",","potentially","filtering","suspicious","input",":","param","x",":","Input","samples",".",":","return",":","Array","of","predictions","of","shape","`","(","nb_inputs",",","nb_classes",")","`","."]
code: def predict(self, x: np.ndarray) -> np.ndarray:
      """
      Perform prediction of the given classifier for a batch of inputs, potentially filtering suspicious input
  
      :param x: Input samples.
      :return: Array of predictions of shape `(nb_inputs, nb_classes)`.
      """
      raw_predictions = self.predict_fn(x)
  
      if (
          self.entropy_threshold is None or self.validation_data is None
      ):  # pragma: no cover
          logger.warning(
              "Mitigation has not been performed. Predictions may be unsafe."
          )
          return raw_predictions
  
      x_val = self.validation_data
      final_predictions = []
  
      for i, img in enumerate(x):
          # Randomly select samples from test set
          selected_indices = np.random.choice(
              np.arange(len(x_val)), self.num_samples
          )
  
          # Perturb the images by combining them
          perturbed_images = np.array(
              [
                  combine_images(img, x_val[idx])
                  for idx in selected_indices
              ]
          )
  
          # Predict on the perturbed images
          perturbed_predictions = self.predict_fn(perturbed_images)
  
          # Calculate normalized entropy
          normalized_entropy = np.sum(
              entropy(perturbed_predictions, base=2, axis=0)
          ) / float(self.num_samples)
  
          # Abstain if entropy is below threshold
          if normalized_entropy <= self.entropy_threshold:
              final_predictions.append(self.abstain())
          else:
              final_predictions.append(raw_predictions[i])
  
      return np.array(final_predictions)
  
file_path: adversarial-robustness-toolbox/art/estimators/poison_mitigation/strip/strip.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  from typing import Callable, Optional
  import numpy
  from scipy.stats import entropy, norm
  from tqdm.auto import tqdm
  from art.estimators.certification.abstain import AbstainPredictorMixin
label_desc: Use image node_id 2 for calling the STRIPMixin obj's underlying member method code with example usage: obj.predict(x) and returns: np, raw_predictions