How To Classify Images with TensorFlow - a Step-By-Step Tutorial

T-SNE embedding of CIFAR10. Image source:

Welcome to our Beginner's Demo!  We will walk you through the steps of building an image classification application with TensorFlow. We will also introduce you to a few building blocks for creating your own deep learning demos.

The network architecture used in this demo is ResNet32, and the dataset is CIFAR10. The reference performance is:

  • Training speed: 5,000 images/sec.
  • Testing accuracy: 92% Top-1 accuracy in 100 epochs (~15 mins).
  • Hardware: Lambda Quad i7-7820X CPU + 4x GeForce 1080 Ti
  • OS: Ubuntu 18.04 LTS with Lambda Stack

You can jump to the code and the instructions from here.


CIFAR10 is consists of  60,000 32 x 32 pixel color images. There are ten different classes: {airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck}. CIFAR10 is very popular among researchers because it is both small enough to offer a fast training turnaround time while challenging enough for conducting scientific studies and drawing meaningful conclusions.

You can download CIFAR10 in different formats (for Python, Matlab or C) from its official website. However, as an example of managing data for machine learning applications, we re-organized the raw data with a CSV file. Below is a snippet of the CSV file: the first column is the path to the image, the second column is the class id:

Application overview

Our TensorFlow demo is comprised of three main components:

  • Inputter: The data pipeline. It reads data from the disk, shuffles and preprocesses the data, creates batches, and does prefetching. An inputter is applicable to a specific problem. For example, we have image_classification_inputter, machine_translation_inputter, object_detection_inputter ... etc. An inputter can optionally own an augmenter for data augmentation.

  • Modeler: The model pipeline. The Modeler encapsulates the forward pass and the computation of loss, gradient, and accuracy. Like the inputter, a modeler is applicable to a specific problem such as image classification, object detection ... etc. A modeler must own a network member that implements the network architecture, for example, ResNet32, VGG19 or InceptionV4.

  • Runner: The executor. It owns an Inputter, a Modeler and a number of callbacks. A runner orchestrates the execution of an Inputter and a Modeler and distributes the workload across multiple hardware devices. It also uses callbacks to perform auxiliary tasks such as logging the statistics of the job and saving the trained model.

Architectural overview of our TensorFlow application.

Below is an example of creating an training application for ResNet32 and CIFAR10 using the aforementioned components:

    # (Optionally) Create a augmenter.
    augmenter = importlib.import_module("source.augmenter.cifar_augmenter")

    # Create a network object.
    net = getattr(importlib.import_module(""), "net")

    # Create callbacks
    callback_names = ["source.callback.train_basic",
    for name in callback_names:
      callback = importlib.import_module(name).build(args)

    # Create a Inputter.
    inputter_name = "source.inputter.image_classification_csv_inputter"
    inputter = importlib.import_module(inputter_name).build(args,

    # Create a Modeler
    modeler_name = "source.modeler.image_classification_modeler"
    modeler = importlib.import_module(modeler_name).build(args,

    # Create a Runner
    runner_name = "source.runner.parameter_server_runner"
    runner = importlib.import_module(runner_name).build(args,

    # Run application
    demo = app.APP(args, runner)


You can download the demo from this repo.

git clone

You'll need a machine with at least one, but preferably multiple GPUs and you'll also want to install Lambda Stack which installs GPU-enabled TensorFlow in one line.

Once you have TensorFlow with GPU support, simply run the commands in this guide to reproduce the results.

Additional Notes:

Below, we'll dive into the implementation details of each module:

Inputter: The first important component of our TensorFlow application is the Inputter. It encapsulates the entire data pipeline into the input_fn member function:  

def input_fn(self):
  batch_size = (self.args.batch_size_per_gpu * self.args.num_gpu)
  # Fetching meta data from the CSV file (images path and class labels).
  samples = self.get_samples_fn()

  # Create a TensorFlow dataset object.
  dataset =

  # Shuffle the data for training.
  if self.args.mode == "train":
    dataset = dataset.shuffle(self.args.shuffle_buffer_size)

  # Repeat the dataset for multi-epoch training.
  dataset = dataset.repeat(self.args.epochs)

  # Parse each sample (read images from path, preprocess and augmentation).
  dataset =
    lambda image, label: self.parse_fn(image, label),

  # Batch the data.
  dataset = dataset.apply(

  # Pre-fetch for efficiency
  dataset = dataset.prefetch(2)

  # Return data generator
  iterator = dataset.make_one_shot_iterator()
  return iterator.get_next()

Augmenter: An inputter can optionally have an augmenter. In this example, we use one to inflate the amount of training data by random image resizing and cropping.  

resized_image = tf.image.resize_image_with_crop_or_pad(image,

The output of the input_fn will be fed to the modeler, which we will explain next.

Modeler: The second key component of our TensorFlow application is the Modeler. It consumes the input data and encapsulates the entire model computation into the model_fn method:

  def model_fn(self, x):
    # x is the output generator from input_fn.
    images = x[0]
    labels = x[1]
    # Create the feed-forward computation.
    logits, predictions = self.create_graph_fn(images)

    if self.args.mode == "train":
      # Additonal training related computations.
      loss = self.create_loss_fn(logits, labels)
      grads = self.create_grad_fn(loss)
      accuracy = self.create_eval_metrics_fn(predictions, labels)
      return {"loss": loss,
              "grads": grads,
              "accuracy": accuracy,
              "learning_rate": self.learning_rate}
    elif self.args.mode == "eval":
      # Additonal evaluation related computations.
      loss = self.create_loss_fn(logits, labels)
      accuracy = self.create_eval_metrics_fn(predictions, labels)
      return {"loss": loss, "accuracy": accuracy}
    elif self.args.mode == "infer":
      # Directly returns feed-forward result for inference.
      return {"classes": predictions["classes"],
              "probabilities": predictions["probabilities"]}

The output of the model_fn is a dictionary of TensorFlow operators. The composition of this dictionary depends on the mode of the model. The models are {train, eval, infer}. The Runner is responsible for calling these operators.

Network: A Modeler must own a network, which implements the feed-forward computation. In this example, we directly call TensorFlow slim's ResNet implementation:

from import resnet_v2

# Feed-forward pass.
logits, _ = resnet_v2.resnet_v2_32(inputs,

Runner: The last piece of our TensorFlow application is a Runner. A Runner owns an Inputter, a Modeler, and a bunch of callbacks. It orchestrates the execution of the Inputter and the Modeler, distributes the workload across multiple hardware devices, and uses callbacks to perform auxiliary tasks such as logging execution, saving the model and displaying the results. The main logic lives inside of Runner's run method:

  def run(self):
    # Combine the computational graphs of the inputter and the modeler.
    # Also distribute the workload across multiple devices.

    # Create a Tensorflow session.
    with tf.Session(config=self.session_config) as self.sess:

      # Do auxiliary tasks before the run.

      # Get the current step and the maximum step.
      global_step = 0
      if self.args.mode == "train":
        global_step =

      max_step =

      # Run the application till reaching the maximum step.
      while global_step < max_step:
        # Do auxiliary tasks before each step.

        # Run the step.
        self.outputs =,

        # Do auxiliary tasks after each step.

        # Increase the current step by one.
        global_step = global_step + 1

      # Do auxiliary tasks after the job has finished.

We can, for example, use a parameter server Runner to utilize multiple GPUs. A parameter server distributes data and computation across multiple GPU devices, and averages the results on a parameter server device (a CPU in the example below):

  def replicate_graph(self):

    # Get the data generator from input_fn.
    batch = self.inputter.input_fn()

    output = {}
    for i in range(self.args.num_gpu):
      with tf.device(self.assign_to_device("/gpu:{}".format(i),
        # Split input data across multiple devices
        x = self.batch_split(batch, i)
        # Run graph on a single device.
        y = self.modeler.model_fn(x)

        # Gather the output.
        if i == 0:
          for key in y:
            output[key] = [y[key]]
          for key in y:

    # Averaging the output from multiple devices.
    reduced_ops = {}
    for key in output:
      reduced_ops[key] = self.reduce_op(output[key])
    return reduced_ops

Callback: A Runner uses callbacks to perform auxiliary tasks. Each callback needs to implement a few functions for different stages of the execution. For example, checkout this callback for logging the training speed.

!-- Intercom -->