ResNet-50 benchmark on Cloud TPU

The ResNet-50 benchmark is an optimized version of the ResNet-50 model for Cloud TPU. This optimized version was created and used by Google in the DAWNBench competition. This document contains the setup and instructions needed to replicate the DAWNBench benchmark results for ResNet-50 on a Cloud TPU pod.

The technique used for this optimization is very simple: train on smaller images at the start of training, and gradually increase image size as you train further. It makes intuitive sense that you don’t need large images to learn the general sense of what cats and dogs look like (for instance), but later on when you’re trying to learn the difference between every breed of dog, you’ll often need larger images. For this, there is an extra step in the dataset preparation as the benchmark uses a small image dataset.

Networks trained on one size image can be used for other sizes. To do this, you use a global/adaptive pooling layer rather than a fixed-size pooling layer.

By using progressive resizing you are both able to make the initial epochs much faster than usual (using 128x128 images instead of the usual 224x224 for the first 17 epochs), and also make the final epochs more accurate (using 288x288 images at epoch 42 for even higher accuracy). But performance is only half of the reason for this success the other impact is better generalization performance. By showing the network a wider variety of image sizes, it helps it to avoid over-fitting.


This benchmark uses a third-party dataset. Google provides no representation, warranty, or other guarantees about the validity, or any other aspects of, this dataset.

Benchmark description

This benchmark is based on Deep Residual Learning for Image Recognition, which first introduces the residual network (ResNet) architecture. This benchmark uses the 50-layer variant, known as ResNet-50.

The model used here is nearly identical to the ResNet-50 tutorial. The main differences are that the training uses instead of and generates checkpoints at every epoch and evaluates in a separate job.

Before you begin

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

  1. Google アカウントにログインします。

    Google アカウントをまだお持ちでない場合は、新しいアカウントを登録します。

  2. GCP プロジェクトを選択または作成します。

    [リソースの管理] ページに移動

  3. プロジェクトに対して課金が有効になっていることを確認します。


  4. This walkthrough uses billable components of Google Cloud Platform. 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 storage, VM, and Cloud TPU resources for tutorials.

Create a Cloud Storage bucket

You need a Cloud Storage bucket to store 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 you create must reside in the same region as your virtual machine (VM) and your Cloud TPU device or Cloud TPU slice (multiple TPU devices) do.

  1. Go to the Cloud Storage page on the GCP Console.

    Go to the Cloud Storage page

  2. Create a new bucket, specifying the following options:

    • A unique name of your choosing.
    • Default storage class: Regional
    • Location: If you want to use a Cloud TPU device, accept the default presented. If you want to use a Cloud TPU Pod slice, you must specify a region where Cloud TPU Pods are available.

Use the ctpu tool

This section demonstrates using the Cloud TPU provisioning tool (ctpu) for creating and managing Cloud TPU project resources. The resources are comprised of a virtual machine (VM) and a Cloud TPU resource that have the same name. These resources must reside in the same region/zone as the bucket you just created.

You can also set up your VM and TPU resources using gcloud commands or through the Cloud Console. See the managing VM and TPU resources page to learn all the ways you can set up and manage your Compute Engine VM and Cloud TPU resources.

Run ctpu up to create resources

  1. Open a Cloud Shell window.

    Open Cloud Shell

  2. Run ctpu up specifying the flags shown for either a Cloud TPU device or Pod slice. Refer to CTPU Reference for flag options and descriptions.

  3. Set up a Cloud TPU device:

    $ ctpu up 

    The following configuration message appears:

    ctpu will use the following configuration:
    Name: [your TPU's name]
    Zone: [your project's zone]
    GCP Project: [your project's name]
    TensorFlow Version: 1.13
     Machine Type: [your machine type]
     Disk Size: [your disk size]
     Preemptible: [true or false]
    Cloud TPU:
     Size: [your TPU size]
     Preemptible: [true or false]
    OK to create your Cloud TPU resources with the above configuration? [Yn]:

    Press y to create your Cloud TPU resources.

The ctpu up command creates a virtual machine (VM) and Cloud TPU services.

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

Verify your Compute Engine VM

When the ctpu up command has finished executing, verify that your shell prompt has changed from username@project to username@tpuname. This change shows that you are now logged into your Compute Engine VM.

Prepare the data

This section describes the steps needed to prepare the benchmark data.

Set up the following environment variable, replacing YOUR-BUCKET-NAME with the name of your Cloud Storage bucket:


The training application expects your training data to be accessible in Cloud Storage. The training application also uses your Cloud Storage bucket to store checkpoints during training.

Using ImageNet dataset

You need about 500GB of space available on your local machine or Compute Engine VM to run the script used in this section.

If you decide to process the data on your Compute Engine VM, follow these steps to add disk space to the VM:

  • Follow the Compute Engine guide to add a disk to your VM.
  • Set the disk size to 500GB or more.
  • Set When deleting instance to Delete disk to ensure that the disk is removed when you remove the VM.
  • Make a note of the path to your new disk. For example: /mnt/disks/mnt-dir.

Download and convert the ImageNet data:

  1. Sign up for an ImageNet account. Remember the username and password you used to create the account.

  2. Set up DATA_DIR and DATA_DIR_SMALL environment variables pointing to paths on your Cloud Storage bucket:

    (vm)$ export DATA_DIR=${STORAGE_BUCKET}/resnet_data
    (vm)$ export DATA_DIR_SMALL=${STORAGE_BUCKET}/resnet_data_small
  3. Download the script from GitHub:

    $ wget
  4. Set a SCRATCH_DIR variable to contain the script's working files. The variable must specify a location on your local machine or on your Compute Engine VM. For example, on your local machine:

    $ SCRATCH_DIR=./imagenet_tmp_files

    Or if you're processing the data on the VM:

    (vm)$ SCRATCH_DIR=/mnt/disks/mnt-dir/imagenet_tmp_files
  5. Run the script to download, format, and upload the ImageNet data to the bucket. Replace YOUR-USERNAME and YOUR-PASSWORD with the username and password you used to create your ImageNet account. Since you will be using progressive resizing, you need a data directory for the initial small dataset, DATA_DIR_SMALL and one for the larger dataset, DATA_DIR.

    $ pip install google-cloud-storage
    $ python \
      --project=$PROJECT \
      --gcs_output_path=$DATA_DIR \
      --gcs_output_path_small=$DATA_DIR_SMALL \
      --local_scratch_dir=$SCRATCH_DIR \
      --imagenet_username=YOUR-USERNAME \

Note: Downloading and preprocessing the data can take more than a day, depending on your network and computer speed. Do not interrupt the script.

When the script finishes processing, a message like the following appears:

2018-02-17 14:30:17.287989: Finished writing all 1281167 images in data set.

The script produces a series of directories (for both training and validation) of the form:




(Optional) Set up TensorBoard

TensorBoard offers a suite of tools designed to present TensorFlow data visually. When used for monitoring, TensorBoard can help identify bottlenecks in processing and suggest ways to improve performance.

If you don't need to monitor the model's output at this time, you can skip the TensorBoard setup steps.

If you want to monitor the model's output and performance, follow the guide to setting up TensorBoard.

Run the ResNet-50 model

You are now ready to train and evaluate the ResNet-50 model on your Cloud TPU. In the following steps, a prefix of (vm)$ means you should run the command on your Compute Engine VM:

  1. Add the top-level /models folder to the Python path with the command

    (vm)$ export PYTHONPATH="$PYTHONPATH:/usr/share/tpu/models"
  2. The ResNet-50 model benchmark is pre-installed on your Compute Engine VM. Navigate to the directory:

    (vm)$ cd /usr/share/tpu/models/official/resnet/benchmark
  3. Run the training script:

    (vm)$ python \
      --tpu=$TPU_NAME \
      --data_dir=$DATA_DIR \
      --data_dir_small=$DATA_DIR_SMALL \
      --mode=train \
    • --tpu specifies the name of the Cloud TPU. Note that ctpu passes this name to the Compute Engine VM as an environment variable (TPU_NAME).
    • --data_dir specifies the Cloud Storage path for training input.
    • --model_dir specifies the directory where checkpoints and summaries are stored during model training. If the folder is missing, the program creates one. When using a Cloud TPU, 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.
  4. Evaluate the model (run after train completes):

    (vm)$ python \
      --tpu=$TPU_NAME \
      --data_dir=$DATA_DIR \
      --data_dir_small=gs:$DATA_DIR_SMALL \
      --model_dir=${STORAGE_BUCKET}/resnet_bench \
      --use_fast_lr=True \

What to expect

The above procedure was able to train the ResNet-50 model for 42 epochs in 3 hours 30 mins. With the flags shown in the training code in the previous section, the model should train to above 76% TOP1 accuracy and 93% TOP5 accuracy.

Clean 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 user@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 [optional: --zone]
  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:

    2018/04/28 16:16:23 WARNING: Setting zone to "us-central1-b"
    No instances currently exist.
            Compute Engine VM:     --
            Cloud TPU:             --
  4. Run gsutil as shown, replacing YOUR-BUCKET-NAME with the name of the Cloud Storage bucket you created for this tutorial:

    $ gsutil rm -r gs://YOUR-BUCKET-NAME

What's next