code: def dknn_tutorial():
      # Get MNIST data.
      mnist = MNIST()
      x_train, y_train = mnist.get_set("train")
      x_test, y_test = mnist.get_set("test")
  
      # Use Image Parameters.
      img_rows, img_cols, nchannels = x_train.shape[1:4]
      nb_classes = y_train.shape[1]
  
      with get_tensorflow_session() as sess:
          with tf.variable_scope("dknn"):
              # Define input TF placeholder.
              x = tf.placeholder(
                  tf.float32,
                  shape=(None, img_rows, img_cols, nchannels),
              )
              y = tf.placeholder(tf.float32, shape=(None, nb_classes))
  
              # Define a model.
              model = make_basic_picklable_cnn()
              preds = model.get_logits(x)
              loss = CrossEntropy(model, smoothing=0.0)
  
              # Define the test set accuracy evaluation.
              def evaluate():
                  acc = model_eval(
                      sess,
                      x,
                      y,
                      preds,
                      x_test,
                      y_test,
                      args={"batch_size": FLAGS.batch_size},
                  )
                  print("Test accuracy on test examples: %0.4f" % acc)
  
              # Train the model
              train_params = {
                  "nb_epochs": FLAGS.nb_epochs,
                  "batch_size": FLAGS.batch_size,
                  "learning_rate": FLAGS.lr,
              }
              train(
                  sess,
                  loss,
                  x_train,
                  y_train,
                  evaluate=evaluate,
                  args=train_params,
                  var_list=model.get_params(),
              )
  
              # Define callable that returns a dictionary of all activations for a dataset
              def get_activations(data):
                  data_activations = {}
                  for layer in layers:
                      layer_sym = tf.layers.flatten(
                          model.get_layer(x, layer)
                      )
                      data_activations[layer] = batch_eval(
                          sess,
                          [x],
                          [layer_sym],
                          [data],
                          args={"batch_size": FLAGS.batch_size},
                      )[0]
                  return data_activations
  
              # Use a holdout of the test set to simulate calibration data for the DkNN.
              train_data = x_train
              train_labels = np.argmax(y_train, axis=1)
              cali_data = x_test[: FLAGS.nb_cali]
              y_cali = y_test[: FLAGS.nb_cali]
              cali_labels = np.argmax(y_cali, axis=1)
              test_data = x_test[FLAGS.nb_cali :]
              y_test = y_test[FLAGS.nb_cali :]
  
              # Extract representations for the training and calibration data at each layer of interest to the DkNN.
              layers = ["ReLU1", "ReLU3", "ReLU5", "logits"]
  
              # Wrap the model into a DkNNModel
              dknn = DkNNModel(
                  FLAGS.neighbors,
                  layers,
                  get_activations,
                  train_data,
                  train_labels,
                  nb_classes,
                  scope="dknn",
                  number_bits=FLAGS.number_bits,
              )
              dknn.calibrate(cali_data, cali_labels)
  
              # Generate adversarial examples
              fgsm = FastGradientMethod(model, sess=sess)
              attack_params = {
                  "eps": 0.25,
                  "clip_min": 0.0,
                  "clip_max": 1.0,
              }
              adv = sess.run(
                  fgsm.generate(x, **attack_params),
                  feed_dict={x: test_data},
              )
  
              # Test the DkNN on clean test data and FGSM test data
              for data_in, fname in zip(
                  [test_data, adv], ["test", "adv"]
              ):
                  dknn_preds = dknn.fprop_np(data_in)
                  print(dknn_preds.shape)
                  print(
                      np.mean(
                          np.argmax(dknn_preds, axis=1)
                          == np.argmax(y_test, axis=1)
                      )
                  )
                  plot_reliability_diagram(
                      dknn_preds,
                      np.argmax(y_test, axis=1),
                      "/tmp/dknn_" + fname + ".pdf",
                  )
  
      return True
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  import copy
  import os
  from bisect import bisect_left
  import matplotlib
  import matplotlib.pyplot
  import numpy
  from six.moves import xrange
  import enum
  import tensorflow
  from cleverhans.attacks import FastGradientMethod
  from cleverhans.loss import CrossEntropy
  from cleverhans.dataset import MNIST
  from cleverhans.model import Model
  from cleverhans.picklable_model import MLP, Conv2D, ReLU, Flatten, Linear, Softmax
  from cleverhans.train import train
  from cleverhans.utils_tf import batch_eval, model_eval
label_desc: Use image node_id 4 for calling a global function with example usage: dknn_tutorial() and returns: True, data_activations