def get_input_fn(filename):
  """Returns an `input_fn` for training and evaluation."""

  def input_fn(params):
    # Retrieves the batch size for the current shard. The number of shards is
    # computed according to the input pipeline deployment. See
    # https://www.tensorflow.org/api_docs/python/tf/contrib/tpu/RunConfig
    # for details.
    batch_size = params["batch_size"]

    def parse(serialized_example):
      """Parses a single tf.Example into image and label tensors."""
      features = tf.parse_single_example(
          serialized_example,
          features={
              "label": tf.FixedLenFeature([], tf.int64),
              "image": tf.FixedLenFeature([], tf.string),
          })
      image = tf.decode_raw(features["image"], tf.float32)
      image = tf.reshape(image, [FLAGS.image_width, FLAGS.image_height])
      label = tf.cast(features["label"], tf.int32)
      return image, label

    dataset = tf.data.TFRecordDataset(filename, buffer_size=500000)
    dataset = dataset.map(parse).cache().repeat()
    dataset = dataset.batch(batch_size, drop_remainder=True)
    images, labels = dataset.make_one_shot_iterator().get_next()
    return images, labels
  return input_fn