Training AmoebaNet-D on Cloud TPU

This tutorial shows you how to train AmoebaNet-D on Cloud TPU. You can apply the same pattern to other TPU-optimized image classification models that use TensorFlow and the ImageNet dataset.

Disclaimer

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

Model description

The AmoebaNet-D model is one of the image classifier architectures discovered using Evolutionary AutoML. The model is based on results from the AmoebaNet paper: Real, E., Aggarwal, A., Huang, Y. and Le, Q.V., 2018, Regularized Evolution for Image Classifier Architecture Search, arXiv preprint arXiv:1802.01548.

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

The API simplifies the model development process by hiding most of the low-level implementation, making it easier to switch between TPU and other platforms such as GPU or CPU.

Before you begin

Before starting this tutorial, check that your Google Cloud Platform 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. Select or create a GCP project.

    Go to the Manage resources page

  3. Make sure that billing is enabled for your project.

    Learn how to enable billing

  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 unecessary 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.

Run ctpu up to create resources

  1. Open a Cloud Shell window.

    Open Cloud Shell

  2. Run ctpu up and specify options for either a Cloud TPU device or Pod slice:

    You can use flags to change the following options:

    • --name - name of your Cloud TPU resource and your VM.
    • --zone - region and zone of the physical assets. The zone must be the same for the VM and Cloud TPU. The bucket must be in the same region.
    • --project name - name of an existing project.
    • --tpu_size - version and size of the Cloud TPU. The default is one device with 8 cores.
    • --disk-size-gb - disk size. Use only if your dataset requires more than the default 250GB.
    • --machine-type - virtual machine (VM) memory per CPU.
    • --preemptible - interruptable, but lower cost Cloud TPU.
  3. Set up either a Cloud TPU device or a Pod slice:

TPU Device

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.12
VM:
 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.

TPU Pod

Set up a Cloud TPU slice with 32 Cloud TPU cores, 8 CPUs, a 500GB disk on the VM and the zone you are working in:

$ ctpu up --zone=us-central1-a --tpu-size=v2-32 --disk-size-gb=500 --machine-type n1-standard-8 --preemptible

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.12
VM:
 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

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

(vm)$ export STORAGE_BUCKET=gs://YOUR-BUCKET-NAME

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.

Below are the instructions for using a randomly generated fake dataset to test the model. Alternatively, you can use the full ImageNet dataset.

The fake dataset is at this location on Cloud Storage:

gs://cloud-tpu-test-datasets/fake_imagenet

Note that the fake dataset is only useful for understanding how to use a Cloud TPU and validating end-to-end performance. The accuracy numbers and saved model will not be meaningful.

(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 AmoebaNet-D model

You are now ready to train and evaluate the AmoebaNet-D model on your Cloud TPU:

  1. Set up a DATA_DIR environment variable containing one of the following values:

    • If you are using the fake dataset:

      (vm)$ export DATA_DIR=gs://cloud-tpu-test-datasets/fake_imagenet
      
    • If you have uploaded a set of training data to your Cloud Storage bucket:

      (vm)$ export DATA_DIR=${STORAGE_BUCKET}/data
      
  2. The AmoebaNet-D model is pre-installed on your Compute Engine VM. Navigate to the directory:

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

    (vm)$ python amoeba_net.py \
        --tpu=$TPU_NAME \
        --data_dir=$DATA_DIR \
        --model_dir=${STORAGE_BUCKET}/amoeba_net
    
    • --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.

What to expect

The above procedure trains the AmoebaNet-D model on ImageNet with 256 batch size on a single Cloud TPU. With the default flags, the model should train to above 80% top-1 accuracy in under 48 hours (including evaluation time every few epochs).

You can also train the AmoebaNet-D model to the conditions of the Stanford DAWNBench competition, 93% top-5 accuracy, in under 7.5 hours, using the following command:

    (vm)$ python amoeba_net.py \
      --tpu=$TPU_NAME \
      --data_dir=$DATA_DIR \
      --model_dir=${STORAGE_BUCKET}/amoeba_net \
      --num_cells=6 \
      --image_size=224 \
      --num_epochs=35 \
      --train_batch_size=1024 \
      --eval_batch_size=1024 \
      --lr=2.56 \
      --lr_decay_value=0.88 \
      --lr_warmup_epochs=0.35 \
      --mode=train \
      --iterations_per_loop=1251
    

Clean up

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

  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]
    

    The operation may take a few moments. A message 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:             --
    
  3. Run ctpu status with the --zone flag you used when you set up the Cloud TPU. This checks that your instance was deleted so you can avoid unnecessary charges for TPU usage.

  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

Was this page helpful? Let us know how we did:

Send feedback about...