class_docstr: A unittest class for testing Randomized Smoothing as a post-processing step for classifiers.
class_docstr_tok: ["A","unittest","class","for","testing","Randomized","Smoothing","as","a","post-processing","step","for","classifiers","."]
docstr: Test with a KerasClassifier.
  :return:
docstr_tok: ["Test","with","a","KerasClassifier",".",":","return",":"]
code: def test_keras(self):
      """
      Test with a KerasClassifier.
      :return:
      """
      # Build KerasClassifier
      import tensorflow as tf
  
      tf.compat.v1.disable_eager_execution()
      from tensorflow.keras.models import Sequential
      from tensorflow.keras.layers import Dense, Flatten, Conv2D
      from tensorflow.keras.losses import CategoricalCrossentropy
      from tensorflow.keras.optimizers.legacy import Adam
  
      model = Sequential()
      model.add(
          Conv2D(
              filters=4,
              kernel_size=(5, 5),
              strides=1,
              activation="relu",
              input_shape=(28, 28, 1),
          )
      )
      model.add(Flatten())
      model.add(Dense(100, activation="relu"))
      model.add(Dense(100, activation="relu"))
      model.add(Dense(10, activation="linear"))
  
      model.compile(
          loss=CategoricalCrossentropy(from_logits=True),
          optimizer=Adam(learning_rate=0.01),
          metrics=["accuracy"],
      )
  
      from art.estimators.classification import KerasClassifier
  
      krc = KerasClassifier(model=model, clip_values=(0, 1))
  
      # Get MNIST
      (x_train, y_train), (x_test, y_test) = self.mnist
  
      for i in range(2500):
          if np.argmax(y_train[[i]], axis=1) == 0:
              y_train[i, :] = 0
              y_train[i, 1] = 1
              x_train[i, 0:5, 0:5, :] = 1.0
  
          if np.argmax(y_train[[i]], axis=1) == 9:
              y_train[i, :] = 0
              y_train[i, 9] = 1
              x_train[i, 0:5, 0:5, :] = 1.0
  
      for i in range(500):
          if np.argmax(y_test[[i]], axis=1) == 0:
              y_test[i, :] = 0
              y_test[i, 1] = 1
              x_test[i, 0:5, 0:5, :] = 1.0
  
          if np.argmax(y_test[[i]], axis=1) == 9:
              y_test[i, :] = 0
              y_test[i, 9] = 1
              x_test[i, 0:5, 0:5, :] = 1.0
  
      krc.fit(x_train, y_train, nb_epochs=3)
  
      cleanse = NeuralCleanse(krc)
      defense_cleanse = cleanse(krc, steps=1, patience=1)
      defense_cleanse.mitigate(
          x_test,
          y_test,
          mitigation_types=["filtering", "pruning", "unlearning"],
      )
  
      # is_fitted
      assert cleanse._is_fitted == cleanse.is_fitted
  
      # get_classifier
      assert cleanse.get_classifier
  
      # set_params
      cleanse.set_params(**{"batch_size": 1})
      assert cleanse.batch_size == 1
  
file_path: adversarial-robustness-toolbox/tests/defences/test_neural_cleanse.py
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import logging
  import os
  import unittest
  import numpy
  from art.defences.transformer.poisoning import NeuralCleanse
  from art.utils import load_dataset
  from tests.utils import master_seed
label_desc: Use image node_id 3 for calling the TestNeuralCleanse obj's underlying member method code with example usage: obj.test_keras() without return types