code: """Converts MNIST data to TFRecords file format with Example protos."""
  import os
  import tensorflow as tf
  from PIL import Image
  import numpy as np
  from matplotlib.pyplot import imshow
  
  def gen_image(arr):
      try:
          two_d = (np.reshape(arr, (28, 28)) * 255).astype(np.uint8)
          img = Image.fromarray(two_d, 'L')
          imshow(np.asarray(two_d))
          return True
      except Exception as e:
          print('[gen_image] error {}'.format(e))
          return False
  
  
  def _int64_feature(value):
      return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
  
  
  def _bytes_feature(value):
      return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
  
  
  def convert_to(data_set, name, directory):
      """Converts a dataset to tfrecords."""
      images = data_set.images
      labels = data_set.labels
      num_examples = data_set.num_examples
  
      if images.shape[0] != num_examples:
          raise ValueError('Images size %d does not match label size %d.' %
                           (images.shape[0], num_examples))
      rows = images.shape[1]
      cols = images.shape[2]
      depth = images.shape[3]
  
      filename = os.path.join(directory, name + '.tfrecords')
      print('Writing', filename)
      writer = tf.python_io.TFRecordWriter(filename)
      for index in range(num_examples):
          image_raw = images[index].tostring()
          example = tf.train.Example(features=tf.train.Features(feature={
              'height': _int64_feature(rows),
              'width': _int64_feature(cols),
              'depth': _int64_feature(depth),
              'label': _int64_feature(int(labels[index])),
              'image_raw': _bytes_feature(image_raw)}))
          writer.write(example.SerializeToString())
      writer.close()
apis: numpy.reshape numpy.asarray tensorflow.train.Int64List tensorflow.python_io.TFRecordWriter tensorflow.train.BytesList