class_docstr: A unittest class for testing the HopSkipJump attack.
class_docstr_tok: ["A","unittest","class","for","testing","the","HopSkipJump","attack","."]
docstr: Second test with the KerasClassifier.
  :return:
docstr_tok: ["Second","test","with","the","KerasClassifier",".",":","return",":"]
code: def test_8_keras_mnist(self):
      """
      Second test with the KerasClassifier.
      :return:
      """
      x_test_original = self.x_test_mnist.copy()
  
      # Build KerasClassifier
      krc = get_image_classifier_kr()
  
      # First targeted attack and norm=2
      hsj = HopSkipJump(
          classifier=krc,
          targeted=True,
          max_iter=20,
          max_eval=100,
          init_eval=10,
          verbose=False,
      )
      params = {"y": random_targets(self.y_test_mnist, krc.nb_classes)}
      x_test_adv = hsj.generate(self.x_test_mnist, **params)
  
      self.assertFalse((self.x_test_mnist == x_test_adv).all())
      self.assertTrue((x_test_adv <= 1.0001).all())
      self.assertTrue((x_test_adv >= -0.0001).all())
  
      target = np.argmax(params["y"], axis=1)
      y_pred_adv = np.argmax(krc.predict(x_test_adv), axis=1)
      self.assertTrue((target == y_pred_adv).any())
  
      # Test the masking 1
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape)
      )
      mask = mask.reshape(self.x_test_mnist.shape)
  
      params.update(mask=mask)
      x_test_adv = hsj.generate(self.x_test_mnist, **params)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Test the masking 2
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape[1:])
      )
      mask = mask.reshape(self.x_test_mnist.shape[1:])
  
      params.update(mask=mask)
      x_test_adv = hsj.generate(self.x_test_mnist, **params)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # First targeted attack and norm=np.inf
      hsj = HopSkipJump(
          classifier=krc,
          targeted=True,
          max_iter=20,
          max_eval=100,
          init_eval=10,
          norm=np.Inf,
          verbose=False,
      )
      params = {"y": random_targets(self.y_test_mnist, krc.nb_classes)}
      x_test_adv = hsj.generate(self.x_test_mnist, **params)
  
      self.assertFalse((self.x_test_mnist == x_test_adv).all())
      self.assertTrue((x_test_adv <= 1.0001).all())
      self.assertTrue((x_test_adv >= -0.0001).all())
  
      target = np.argmax(params["y"], axis=1)
      y_pred_adv = np.argmax(krc.predict(x_test_adv), axis=1)
      self.assertTrue((target == y_pred_adv).any())
  
      # Test the masking 1
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape)
      )
      mask = mask.reshape(self.x_test_mnist.shape)
  
      params.update(mask=mask)
      x_test_adv = hsj.generate(self.x_test_mnist, **params)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Test the masking 2
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape[1:])
      )
      mask = mask.reshape(self.x_test_mnist.shape[1:])
  
      params.update(mask=mask)
      x_test_adv = hsj.generate(self.x_test_mnist, **params)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Second untargeted attack and norm=2
      hsj = HopSkipJump(
          classifier=krc,
          targeted=False,
          max_iter=20,
          max_eval=100,
          init_eval=10,
          verbose=False,
      )
      x_test_adv = hsj.generate(self.x_test_mnist)
  
      self.assertFalse((self.x_test_mnist == x_test_adv).all())
      self.assertTrue((x_test_adv <= 1.0001).all())
      self.assertTrue((x_test_adv >= -0.0001).all())
  
      y_pred = np.argmax(krc.predict(self.x_test_mnist), axis=1)
      y_pred_adv = np.argmax(krc.predict(x_test_adv), axis=1)
      self.assertTrue((y_pred != y_pred_adv).any())
  
      # Test the masking 1
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape)
      )
      mask = mask.reshape(self.x_test_mnist.shape)
  
      x_test_adv = hsj.generate(self.x_test_mnist, mask=mask)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Test the masking 2
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape[1:])
      )
      mask = mask.reshape(self.x_test_mnist.shape[1:])
  
      x_test_adv = hsj.generate(self.x_test_mnist, mask=mask)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Second untargeted attack and norm=np.inf
      hsj = HopSkipJump(
          classifier=krc,
          targeted=False,
          max_iter=20,
          max_eval=100,
          init_eval=10,
          norm=np.Inf,
          verbose=False,
      )
      x_test_adv = hsj.generate(self.x_test_mnist)
  
      self.assertFalse((self.x_test_mnist == x_test_adv).all())
      self.assertTrue((x_test_adv <= 1.0001).all())
      self.assertTrue((x_test_adv >= -0.0001).all())
  
      y_pred = np.argmax(krc.predict(self.x_test_mnist), axis=1)
      y_pred_adv = np.argmax(krc.predict(x_test_adv), axis=1)
      self.assertTrue((y_pred != y_pred_adv).any())
  
      # Test the masking 1
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape)
      )
      mask = mask.reshape(self.x_test_mnist.shape)
  
      x_test_adv = hsj.generate(self.x_test_mnist, mask=mask)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Test the masking 2
      mask = np.random.binomial(
          n=1, p=0.5, size=np.prod(self.x_test_mnist.shape[1:])
      )
      mask = mask.reshape(self.x_test_mnist.shape[1:])
  
      x_test_adv = hsj.generate(self.x_test_mnist, mask=mask)
      mask_diff = (1 - mask) * (x_test_adv - self.x_test_mnist)
      self.assertAlmostEqual(
          float(np.max(np.abs(mask_diff))), 0.0, delta=0.00001
      )
  
      unmask_diff = mask * (x_test_adv - self.x_test_mnist)
      self.assertGreater(float(np.sum(np.abs(unmask_diff))), 0.0)
  
      # Check that x_test has not been modified by attack and classifier
      self.assertAlmostEqual(
          float(np.max(np.abs(x_test_original - self.x_test_mnist))),
          0.0,
          delta=0.00001,
      )
  
      # Clean-up session
      k.clear_session()
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import unittest
  import keras.backend
  import numpy
  from art.attacks.evasion.hop_skip_jump import HopSkipJump
  from art.estimators.estimator import BaseEstimator
  from art.estimators.classification import ClassifierMixin
  from art.utils import random_targets
  from tests.utils import TestBase
  from tests.utils import get_image_classifier_tf, get_image_classifier_kr, get_image_classifier_pt
  from tests.utils import get_tabular_classifier_pt, master_seed
  from tests.attacks.utils import backend_test_classifier_type_check_fail
label_desc: Use image node_id 4 for calling the TestHopSkipJump obj's underlying member method code with example usage: obj.test_8_keras_mnist() without return types