code: def generate_graph_and_checkpoint(
      graph_output_path, checkpoint_output_path
  ):
      """
      Reimplementation of the TensorFlow official MNIST CNN tutorials and generate the graph and checkpoint for this model:
      - https://www.tensorflow.org/versions/r0.8/tutorials/mnist/pros/index.html
      - https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/mnist/convolutional.py
  
      - For simplified CNN layer see "Convolutional layer (Simplified)"
  
      Parameters
      -----------
      graph_output_path : string
          the path of the graph where you want to save.
      checkpoint_output_path : string
          the path of the checkpoint where you want to save.
  
      References
      -----------
      - `onnx-tf exporting tutorial <https://github.com/onnx/tutorials/blob/master/tutorials/OnnxTensorflowExport.ipynb>`__
  
      """
      (
          X_train,
          y_train,
          X_val,
          y_val,
          X_test,
          y_test,
      ) = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))
  
      sess = tf.InteractiveSession()
  
      batch_size = 128
      x = tf.placeholder(
          tf.float32, shape=[batch_size, 28, 28, 1]
      )  # [batch_size, height, width, channels]
      y_ = tf.placeholder(tf.int64, shape=[batch_size])
  
      net = tl.layers.InputLayer(x, name="input")
  
      # Simplified conv API (the same with the above layers)
      net = tl.layers.Conv2d(
          net,
          32,
          (5, 5),
          (1, 1),
          act=tf.nn.relu,
          padding="SAME",
          name="cnn1",
      )
      net = tl.layers.MaxPool2d(
          net, (2, 2), (2, 2), padding="SAME", name="pool1"
      )
      net = tl.layers.Conv2d(
          net,
          64,
          (5, 5),
          (1, 1),
          act=tf.nn.relu,
          padding="SAME",
          name="cnn2",
      )
      net = tl.layers.MaxPool2d(
          net, (2, 2), (2, 2), padding="SAME", name="pool2"
      )
      # end of conv
      net = tl.layers.FlattenLayer(net, name="flatten")
      net = tl.layers.DropoutLayer(net, keep=0.5, name="drop1")
      net = tl.layers.DenseLayer(net, 256, act=tf.nn.relu, name="relu1")
      net = tl.layers.DropoutLayer(net, keep=0.5, name="drop2")
      net = tl.layers.DenseLayer(net, 10, act=None, name="output")
  
      y = net.outputs
  
      print(
          [n.name for n in tf.get_default_graph().as_graph_def().node]
      )
  
      # To string Graph
      with open(graph_output_path, "wb") as file:
          graph = tf.get_default_graph().as_graph_def(add_shapes=True)
          file.write(graph.SerializeToString())
  
      cost = tl.cost.cross_entropy(y, y_, "cost")
  
      correct_prediction = tf.equal(tf.argmax(y, 1), y_)
      acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  
      # train
      n_epoch = 200
      learning_rate = 0.0001
      print_freq = 10
  
      train_params = net.all_params
      train_op = tf.train.AdamOptimizer(learning_rate).minimize(
          cost, var_list=train_params
      )
  
      tl.layers.initialize_global_variables(sess)
      net.print_params()
      net.print_layers()
  
      print("   learning_rate: %f" % learning_rate)
      print("   batch_size: %d" % batch_size)
  
      for epoch in range(n_epoch):
          start_time = time.time()
          for X_train_a, y_train_a in tl.iterate.minibatches(
              X_train, y_train, batch_size, shuffle=True
          ):
              feed_dict = {x: X_train_a, y_: y_train_a}
              feed_dict.update(net.all_drop)  # enable noise layers
              sess.run(train_op, feed_dict=feed_dict)
          # Save the checkpoint every 10 eopchs
          if epoch % 10 == 0:
              tl.files.save_ckpt(
                  sess,
                  mode_name="model.ckpt",
                  save_dir=checkpoint_output_path,
                  printable=True,
              )
          if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
              print(
                  "Epoch %d of %d took %fs"
                  % (epoch + 1, n_epoch, time.time() - start_time)
              )
              train_loss, train_acc, n_batch = 0, 0, 0
              for X_train_a, y_train_a in tl.iterate.minibatches(
                  X_train, y_train, batch_size, shuffle=True
              ):
                  dp_dict = tl.utils.dict_to_one(
                      net.all_drop
                  )  # disable noise layers
                  feed_dict = {x: X_train_a, y_: y_train_a}
                  feed_dict.update(dp_dict)
                  err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                  train_loss += err
                  train_acc += ac
                  n_batch += 1
              print("   train loss: %f" % (train_loss / n_batch))
              print("   train acc: %f" % (train_acc / n_batch))
              val_loss, val_acc, n_batch = 0, 0, 0
              for X_val_a, y_val_a in tl.iterate.minibatches(
                  X_val, y_val, batch_size, shuffle=True
              ):
                  dp_dict = tl.utils.dict_to_one(
                      net.all_drop
                  )  # disable noise layers
                  feed_dict = {x: X_val_a, y_: y_val_a}
                  feed_dict.update(dp_dict)
                  err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                  val_loss += err
                  val_acc += ac
                  n_batch += 1
              print("   val loss: %f" % (val_loss / n_batch))
              print("   val acc: %f" % (val_acc / n_batch))
  
      # Evaluation
      print("Evaluation")
      test_loss, test_acc, n_batch = 0, 0, 0
      for X_test_a, y_test_a in tl.iterate.minibatches(
          X_test, y_test, batch_size, shuffle=True
      ):
          dp_dict = tl.utils.dict_to_one(
              net.all_drop
          )  # disable noise layers
          feed_dict = {x: X_test_a, y_: y_test_a}
          feed_dict.update(dp_dict)
          err, ac = sess.run([cost, acc], feed_dict=feed_dict)
          test_loss += err
          test_acc += ac
          n_batch += 1
      print("   test loss: %f" % (test_loss / n_batch))
      print("   test acc: %f" % (test_acc / n_batch))
  
file_path: TensorLayer/examples/tutorial_work_with_onnx.py
imports: import time
  import numpy
  import tensorflow
  from tensorflow.python.tools.freeze_graph import freeze_graph
  import onnx
  import tensorlayer
  from onnx_tf.backend import prepare
  from onnx_tf.frontend import tensorflow_graph_to_onnx_model
label_desc: Use image node_id 1 for calling a global function with example usage: generate_graph_and_checkpoint(graph_output_path, checkpoint_output_path) without return types