code: def main(argv):
      tf.logging.set_verbosity(tf.logging.INFO)
      trainer_lib.set_random_seed(FLAGS.random_seed)
      usr_dir.import_usr_dir(FLAGS.t2t_usr_dir)
      t2t_trainer.maybe_log_registry_and_exit()
  
      if FLAGS.cloud_mlengine:
          cloud_mlengine.launch()
          return
  
      if FLAGS.generate_data:
          t2t_trainer.generate_data()
  
      if cloud_mlengine.job_dir():
          FLAGS.output_dir = cloud_mlengine.job_dir()
  
      if argv:
          t2t_trainer.set_hparams_from_args(argv[1:])
  
      if FLAGS.surrogate_attack:
          tf.logging.warn("Performing surrogate model attack.")
          sur_hparams = create_surrogate_hparams()
          trainer_lib.add_problem_hparams(sur_hparams, FLAGS.problem)
  
      hparams = t2t_trainer.create_hparams()
      trainer_lib.add_problem_hparams(hparams, FLAGS.problem)
  
      attack_params = create_attack_params()
      attack_params.add_hparam(attack_params.epsilon_name, 0.0)
  
      if FLAGS.surrogate_attack:
          sur_config = create_surrogate_run_config(sur_hparams)
      config = t2t_trainer.create_run_config(hparams)
      params = {
          "batch_size": hparams.batch_size,
          "use_tpu": FLAGS.use_tpu,
      }
  
      # add "_rev" as a hack to avoid image standardization
      problem = registry.problem(FLAGS.problem + "_rev")
  
      inputs, labels, features = prepare_data(
          problem, hparams, params, config
      )
  
      sess = tf.Session()
  
      if FLAGS.surrogate_attack:
          sur_model_fn = t2t_model.T2TModel.make_estimator_model_fn(
              FLAGS.surrogate_model, sur_hparams, use_tpu=FLAGS.use_tpu
          )
          sur_ch_model = adv_attack_utils.T2TAttackModel(
              sur_model_fn,
              features,
              params,
              sur_config,
              scope="surrogate",
          )
          # Dummy call to construct graph
          sur_ch_model.get_probs(inputs)
  
          checkpoint_path = os.path.expanduser(
              FLAGS.surrogate_output_dir
          )
          tf.train.init_from_checkpoint(
              tf.train.latest_checkpoint(checkpoint_path),
              {"/": "surrogate/"},
          )
          sess.run(tf.global_variables_initializer())
  
      other_vars = set(tf.global_variables())
  
      model_fn = t2t_model.T2TModel.make_estimator_model_fn(
          FLAGS.model, hparams
      )
      ch_model = adv_attack_utils.T2TAttackModel(
          model_fn, features, params, config
      )
  
      acc_mask = None
      probs = ch_model.get_probs(inputs)
      if FLAGS.ignore_incorrect:
          preds = tf.argmax(probs, -1, output_type=labels.dtype)
          preds = tf.reshape(preds, labels.shape)
          acc_mask = tf.to_float(tf.equal(labels, preds))
      one_hot_labels = tf.one_hot(labels, probs.shape[-1])
  
      if FLAGS.surrogate_attack:
          attack = create_attack(attack_params.attack)(
              sur_ch_model, sess=sess
          )
      else:
          attack = create_attack(attack_params.attack)(
              ch_model, sess=sess
          )
  
      new_vars = set(tf.global_variables()) - other_vars
  
      # Restore weights
      saver = tf.train.Saver(new_vars)
      checkpoint_path = os.path.expanduser(FLAGS.output_dir)
      saver.restore(sess, tf.train.latest_checkpoint(checkpoint_path))
  
      # reuse variables
      tf.get_variable_scope().reuse_variables()
  
      def compute_accuracy(x, l, mask):
          """Compute model accuracy."""
          preds = ch_model.get_probs(x)
          preds = tf.squeeze(preds)
          preds = tf.argmax(preds, -1, output_type=l.dtype)
  
          _, acc_update_op = tf.metrics.accuracy(l, preds, weights=mask)
  
          if FLAGS.surrogate_attack:
              preds = sur_ch_model.get_probs(x)
              preds = tf.squeeze(preds)
              preds = tf.argmax(preds, -1, output_type=l.dtype)
              acc_update_op = tf.tuple(
                  (
                      acc_update_op,
                      tf.metrics.accuracy(l, preds, weights=mask)[1],
                  )
              )
  
          sess.run(tf.initialize_local_variables())
          for i in range(FLAGS.eval_steps):
              tf.logging.info(
                  "\tEvaluating batch [%d / %d]"
                  % (i + 1, FLAGS.eval_steps)
              )
              acc = sess.run(acc_update_op)
          if FLAGS.surrogate_attack:
              tf.logging.info(
                  "\tFinal acc: (%.4f, %.4f)" % (acc[0], acc[1])
              )
          else:
              tf.logging.info("\tFinal acc: %.4f" % acc)
          return acc
  
      epsilon_acc_pairs = []
      for epsilon in attack_params.attack_epsilons:
          tf.logging.info("Attacking @ eps=%.4f" % epsilon)
          attack_params.set_hparam(attack_params.epsilon_name, epsilon)
          adv_x = attack.generate(
              inputs, y=one_hot_labels, **attack_params.values()
          )
          acc = compute_accuracy(adv_x, labels, acc_mask)
          epsilon_acc_pairs.append((epsilon, acc))
  
      for epsilon, acc in epsilon_acc_pairs:
          if FLAGS.surrogate_attack:
              tf.logging.info(
                  "Accuracy @ eps=%.4f: (%.4f, %.4f)"
                  % (epsilon, acc[0], acc[1])
              )
          else:
              tf.logging.info(
                  "Accuracy @ eps=%.4f: %.4f" % (epsilon, acc)
              )
  
file_path: tensor2tensor/tensor2tensor/bin/t2t_attack.py
imports: import os
  from tensor2tensor.bin import t2t_trainer
  from tensor2tensor.data_generators import problem
  from tensor2tensor.utils import adv_attack_utils
  from tensor2tensor.utils import cloud_mlengine
  from tensor2tensor.utils import registry
  from tensor2tensor.utils import t2t_model
  from tensor2tensor.utils import trainer_lib
  from tensor2tensor.utils import usr_dir
  import tensorflow.compat.v1
  from tensorflow.compat.v1 import estimator
label_desc: Use image node_id 6 for calling a global function with example usage: main(argv) and returns: acc