Running MNIST on Cloud TPU

This tutorial contains a high-level description of the MNIST model, instructions on downloading the MNIST TensorFlow TPU code sample, and a guide to running the code on Cloud TPU.

Model description

The MNIST dataset contains a large number of images of hand-written digits in the range 0 to 9, as well as the labels identifying the digit in each image.

This tutorial trains a machine learning model to classify images based on the MNIST dataset. After training, the model classifies incoming images into 10 categories (0 to 9) based on what it's learned about handwritten images from the MNIST dataset. You can then send the model an image that it hasn't seen before, and the model identifies the digit in the image based on what the model has learned during training.

The MNIST dataset has been split into three parts:

  • 55,000 examples of training data
  • 10,000 examples of test data
  • 5,000 examples of validation data

You can find more information about the dataset at the MNIST database site.

The model has a mixture of seven layers:

  • 2 x convolution
  • 2 x max pooling
  • 2 x dense (fully connected)
  • 1 x dropout

Loss is computed via softmax.

This version of the MNIST model uses tf.estimator —a high-level TensorFlow API—which is the recommended way to build and run a machine learning model on a Cloud TPU.

The Tensorflow Estimator API simplifies the model development process by hiding most of the low-level implementation, which also makes it easy to switch between TPU and other test platforms such as GPUs or CPUs.

Objectives

  • Create a Cloud Storage bucket to hold your dataset and model output.
  • Run the training job.
  • Verify the output results.

Costs

This tutorial uses billable components of Google Cloud, including:

  • Compute Engine
  • Cloud TPU
  • Cloud Storage

Use the pricing calculator to generate a cost estimate based on your projected usage.

New Google Cloud users might be eligible for a free trial.

Before you begin

Before starting this tutorial, check that your Google Cloud project is correctly set up.

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Cloud Console, on the project selector page, select or create a Cloud project.

    Go to the project selector page

  3. Make sure that billing is enabled for your Google Cloud project. Learn how to confirm billing is enabled for your project.

  4. This walkthrough uses billable components of Google Cloud. Check the Cloud TPU pricing page to estimate your costs. Be sure to clean up resources you create when you've finished with them to avoid unnecessary charges.

Set up your resources

This section provides information on setting up Cloud Storage, VM, and Cloud TPU resources for tutorials.

  1. Open a Cloud Shell window.

    Open Cloud Shell

  2. Create a variable for your project's ID.

    export PROJECT_ID=project-id
    
  3. Configure gcloud command-line tool to use the project where you want to create Cloud TPU.

    gcloud config set project ${PROJECT_ID}
    
  4. Create a Cloud Storage bucket using the following command:

    gsutil mb -p ${PROJECT_ID} -c standard -l europe-west4 -b on gs://bucket-name
    

    This Cloud Storage bucket stores the data you use to train your model and the training results. The ctpu up tool used in this tutorial sets up default permissions for the Cloud TPU service account. If you want finer-grain permissions, review the access level permissions.

    The bucket location must be in the same region as your virtual machine (VM) and your TPU node. VMs and TPU nodes are located in specific zones, which are subdivisions within a region.

  5. Launch the Compute Engine resources required for this using the ctpu up command.

    ctpu up --zone=europe-west4-a \
    --machine-type=n1-standard-8 \
    --tf-version=1.15.3 \
    --name=mnist-tutorial
    

    For more information on the CTPU utility, see CTPU Reference.

  6. When prompted, press y to create your Cloud TPU resources.

When the ctpu up command has finished executing, verify that your shell prompt has changed from username@projectname to username@vm-name. This change shows that you are now logged into your Compute Engine VM. If you are not connected to the Compute Engine instance, you can do so by running the following command:

  gcloud compute ssh mnist-tutorial --zone=europe-west4-a

From this point on, a prefix of (vm)$ means you should run the command on the Compute Engine VM instance.

  1. Create environment variables for the storage bucket and model directory. Replace bucket-name with the name of your Cloud Storage bucket
    (vm)$ export STORAGE_BUCKET=gs://bucket-name
    
    (vm)$ export TPU_NAME=mnist-tutorial
    (vm)$ export MODEL_DIR=${STORAGE_BUCKET}/mnist
    (vm)$ export DATA_DIR=${STORAGE_BUCKET}/data
    (vm)$ export PYTHONPATH="${PYTHONPATH}:/usr/share/tpu/models"
    

Get the data

The MNIST dataset is hosted on the MNIST database site. Follow the instructions below to download and convert the data to the required format, and to upload the converted data to Cloud Storage.

Download and convert the MNIST data

The convert_to_records.py script downloads the data and converts it to the TFRecord format expected by the example MNIST model.

Use the following commands to run the script and decompress the files:

(vm)$ python3 /usr/share/tensorflow/tensorflow/examples/how_tos/reading_data/convert_to_records.py --directory=./data
(vm)$ gunzip ./data/*.gz

Upload the data to Cloud Storage

Upload the data to your Cloud Storage bucket so that the TPU server can access the data:

(vm)$ gsutil cp -r ./data ${DATA_DIR}

Run the MNIST TPU model

The MNIST TPU model is pre-installed on your Compute Engine VM in the following directory:

/usr/share/tpu/models/official/mnist/

The source code for the MNIST TPU model is also available on GitHub. You can run the model on a Cloud TPU. Alternatively, see how to run the model on a local machine.

Running the model on Cloud TPU

In the following steps, a prefix of (vm)$ means you should run the command on your Compute Engine VM.

  1. Change to the model directory:

      (vm)$ cd /usr/share/tpu/models/official/mnist/
    
  2. Run the MNIST model:

    (vm)$ python3 mnist_tpu.py \
      --tpu=${TPU_NAME} \
      --data_dir=${DATA_DIR} \
      --model_dir=${MODEL_DIR} \
      --use_tpu=True \
      --iterations=500 \
      --train_steps=2000
     
    Parameter Description
    tpu The name of the Cloud TPU. Note that ctpu passes this name to the Compute Engine VM as an environment variable (TPU_NAME). If you fail to connect to the VM, or lose your connection, you can connect by running ctpu up again. TPU_NAME is not set if you connect to the VM by running gcloud compute ssh.
    data_dir The directory that contains files used for training.
    model_dir The directory that contains the model files. This tutorial uses a folder within the Cloud Storage bucket. The script creates the folder if it does not already exist. The model_dir must be a Cloud Storage path (gs://...). You can reuse an existing folder to load current checkpoint data and to store additional checkpoints.
    iterations The number of training steps to run on the TPU before returning control to Python. If this number is too small (for example, less than 100) then this can result in excessive communication overhead, which negatively impacts performance.
    train_steps The total number of steps (batches) for training to run.

Running the model on a local (non-TPU) machine

To run the model on a non-TPU machine, omit --tpu, and set the following flag:

--use_tpu=False

This causes the computation to land on a GPU if one is present. If no GPU is present, the computation falls back to the CPU.

What to expect

By default, the tf.estimator model reports loss value and step time in the following format:

INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:TPU job name tpu_worker
INFO:tensorflow:Graph was finalized.
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Init TPU system
INFO:tensorflow:Start infeed thread controller
INFO:tensorflow:Starting infeed thread controller.
INFO:tensorflow:Start outfeed thread controller
INFO:tensorflow:Starting outfeed thread controller.
INFO:tensorflow:Enqueue next (500) batch(es) of data to infeed.
INFO:tensorflow:Dequeue next (500) batch(es) of data from outfeed.
INFO:tensorflow:Saving checkpoints for 500 into gs://ctpu-mnist-test/output/model.ckpt.
INFO:tensorflow:loss = 0.08896458, step = 0
INFO:tensorflow:loss = 0.08896458, step = 0
INFO:tensorflow:Enqueue next (500) batch(es) of data to infeed.
INFO:tensorflow:Dequeue next (500) batch(es) of data from outfeed.
INFO:tensorflow:Enqueue next (500) batch(es) of data to infeed.
INFO:tensorflow:Dequeue next (500) batch(es) of data from outfeed.
INFO:tensorflow:global_step/sec: 242.829
INFO:tensorflow:examples/sec: 248715
INFO:tensorflow:Enqueue next (500) batch(es) of data to infeed.
INFO:tensorflow:Dequeue next (500) batch(es) of data from outfeed.
INFO:tensorflow:Saving checkpoints for 2000 into gs://ctpu-mnist-test/output/model.ckpt.
INFO:tensorflow:Stop infeed thread controller
INFO:tensorflow:Shutting down InfeedController thread.
INFO:tensorflow:InfeedController received shutdown signal, stopping.
INFO:tensorflow:Infeed thread finished, shutting down.
INFO:tensorflow:Stop output thread controller
INFO:tensorflow:Shutting down OutfeedController thread.
INFO:tensorflow:OutfeedController received shutdown signal, stopping.
INFO:tensorflow:Outfeed thread finished, shutting down.
INFO:tensorflow:Shutdown TPU system.
INFO:tensorflow:Loss for final step: 0.044236258.

Cleaning up

To avoid incurring charges to your GCP account for the resources used in this topic:

  1. Disconnect from the Compute Engine VM:

    (vm)$ exit
    

    Your prompt should now be username@projectname, showing you are in the Cloud Shell.

  2. In your Cloud Shell, run ctpu delete with the --zone flag you used when you set up the Cloud TPU to delete your Compute Engine VM and your Cloud TPU:

    $ ctpu delete --zone=europe-west4-a \
      --name=mnist-tutorial
    
  3. Run ctpu status to make sure you have no instances allocated to avoid unnecessary charges for TPU usage. The deletion might take several minutes. A response like the one below indicates there are no more allocated instances:

    $ ctpu status --zone=europe-west4-a
    
    2018/04/28 16:16:23 WARNING: Setting zone to "europe-west4-a"
    No instances currently exist.
        Compute Engine VM:     --
        Cloud TPU:             --
    
  4. Run gsutil as shown, replacing bucket-name with the name of the Cloud Storage bucket you created for this tutorial:

    $ gsutil rm -r gs://bucket-name
    

What's next

In this tutorial you have trained the MNIST model using a sample dataset. The results of this training are (in most cases) not usable for inference. To use a model for inference you can train the data on a publicly available dataset or your own data set. Models trained on Cloud TPUs require datasets to be in TFRecord format.

You can use the dataset conversion tool sample to convert an image classification dataset into TFRecord format. If you are not using an image classification model, you will have to convert your dataset to TFRecord format yourself. For more information, see TFRecord and tf.Example

Hyperparameter tuning

To improve the model's performance with your dataset, you can tune the model's hyperparameters. You can find information about hyperparameters common to all TPU supported models on GitHub. Information about model-specific hyperparameters can be found in the source code for each model. For more information on hyperparameter tuning, see Overview of hyperparameter tuning, Using the Hyperparameter tuning service and Tune hyperparameters.

Inference

Once you have trained your model you can use it for inference (also called prediction). AI Platform is a cloud-based solution for developing, training, and deploying machine learning models. Once a model is deployed, you can use the AI Platform Prediction service.