code: def main(use_cuda):
      """
      Train the cnn model on mnist datasets
      """
      img = fluid.layers.data(
          name="img", shape=[1, 28, 28], dtype="float32"
      )
      label = fluid.layers.data(name="label", shape=[1], dtype="int64")
      logits = mnist_cnn_model(img)
      cost = fluid.layers.cross_entropy(input=logits, label=label)
      avg_cost = fluid.layers.mean(x=cost)
      optimizer = fluid.optimizer.Adam(learning_rate=0.01)
      optimizer.minimize(avg_cost)
  
      batch_size = fluid.layers.create_tensor(dtype="int64")
      batch_acc = fluid.layers.accuracy(
          input=logits, label=label, total=batch_size
      )
  
      BATCH_SIZE = 50
      PASS_NUM = 3
      ACC_THRESHOLD = 0.98
      LOSS_THRESHOLD = 10.0
      train_reader = paddle.batch(
          paddle.reader.shuffle(
              paddle.dataset.mnist.train(), buf_size=500
          ),
          batch_size=BATCH_SIZE,
      )
  
      # 根据配置选择使用CPU资源还是GPU资源
      place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
  
      exe = fluid.Executor(place)
      feeder = fluid.DataFeeder(feed_list=[img, label], place=place)
      exe.run(fluid.default_startup_program())
  
      pass_acc = fluid.average.WeightedAverage()
      for pass_id in range(PASS_NUM):
          pass_acc.reset()
          for datas in train_reader():
              # 使用GaussianAugmentationDefence加强训练
              x = []
              y = []
  
              for data in datas:
                  x.append(data[0])
                  y.append(data[1])
  
              x_gad, y_gad = GaussianAugmentationDefence(
                  np.array(x), np.array(y), std=0.3, r=1.0
              )
  
              x_gad = x_gad.tolist()
              y_gad = y_gad.tolist()
  
              new_datas = []
  
              for i, a in enumerate(x_gad):
                  new_datas.append([a, y_gad[i]])
  
              loss, acc, b_size = exe.run(
                  fluid.default_main_program(),
                  feed=feeder.feed(new_datas),
                  fetch_list=[avg_cost, batch_acc, batch_size],
              )
              pass_acc.add(value=acc, weight=b_size)
              pass_acc_val = pass_acc.eval()[0]
              print(
                  "pass_id="
                  + str(pass_id)
                  + " acc="
                  + str(acc[0])
                  + " pass_acc="
                  + str(pass_acc_val)
              )
              if loss < LOSS_THRESHOLD and pass_acc_val > ACC_THRESHOLD:
                  # early stop
                  break
  
          print(
              "pass_id="
              + str(pass_id)
              + " pass_acc="
              + str(pass_acc.eval()[0])
          )
      fluid.io.save_params(
          exe,
          dirname="./mnist-gad",
          main_program=fluid.default_main_program(),
      )
      print("train mnist done")
  
file_path: AdvBox/tutorials/mnist_model_gaussian_augmentation_defence.py
imports: from __future__ import print_function
  from builtins import str
  from builtins import range
  import sys
  import os
  import logging
  import paddle.v2
  import paddle.fluid
  import numpy
  from adversarialbox.defences.gaussian_augmentation import GaussianAugmentationDefence
label_desc: Use image node_id 2 for calling a global function with example usage: main(use_cuda) without return types