Training MnasNet on Cloud TPU

This tutorial shows you how to train the Tensorflow MnasNet model using a Cloud TPU device or Cloud TPU Pod slice (multiple TPU devices). You can apply the same pattern to other TPU-optimised image classification models that use TensorFlow and the ImageNet dataset.

Model description

The model in this tutorial is based on MnasNet: Platform-Aware Neural Architecture Search for Mobile, which first introduces the AutoML mobile neural network (MnasNet) architecture. The tutorial uses the state-of-the-art variant, 'mnasnet-a1', and demonstrates training the model using TPUEstimator.

Special considerations when training on a Pod slice (v2-32/v3-32 and above)

If you plan to train on a TPU Pod slice, please make sure you read this document that explains the special considerations when training on a Pod slice.

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. Replace bucket-name with a name for your bucket.

    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 tutorial using the ctpu up command.

    ctpu up --zone=europe-west4-a \
    --vm-only \
    --disk-size-gb=300 \
    --machine-type=n1-standard-8 \
    --tf-version=1.15.3 \
    --name=mnasnet-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 mnasnet-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.

  7. Create an environment variable for the storage bucket. Replace bucket-name with the name of your Cloud Storage bucket.

    (vm)$ export STORAGE_BUCKET=gs://bucket-name
    
  8. Create an environment variable for the model directory.

    (vm)$ export MODEL_DIR=${STORAGE_BUCKET}/mnasnet
    
  9. Add the top-level /models folder to the Python path with the command

    (vm)$ export PYTHONPATH=${PYTHONPATH}:/usr/share/tpu/models:/usr/share/tpu/models/official/efficientnet
    

Locate the data

ImageNet is an image database. The images in the database are organized into a hierarchy, each node of the hierarchy contains hundreds and thousands of images.

This tutorial uses a demonstration version of the full ImageNet dataset, referred to as fake_imagenet. This demonstration version allows you to test the tutorial, while reducing the storage and time requirements typically associated with running a model against the full ImageNet database.

The fake_imagenet dataset is at this location on Cloud Storage:

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

  1. Create an environment variable for the data directory.

    (vm)$ export DATA_DIR=gs://cloud-tpu-test-datasets/fake_imagenet
    

The fake_imagenet 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.

For information on how to download and process the full ImageNet dataset, see Downloading, preprocessing, and uploading the ImageNet dataset.

Train the MnasNet model with fake_imagenet

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

/usr/share/tpu/models/official/mnasnet/

  1. Launch a Cloud TPU resource using the ctpu utility and set some environment variables used later on.

    (vm)$ ctpu up --tpu-only 
    --tf-version=1.15.3
    --name=mnasnet-tutorial

    (vm)$ export TPU_NAME=mnasnet-tutorial
    
  2. Navigate to the model directory.

    (vm)$ cd /usr/share/tpu/models/official/mnasnet/
    

  3. Run the training script.

    (vm)$ python3 mnasnet_main.py \
      --tpu=${TPU_NAME} \
      --data_dir=${DATA_DIR} \
      --model_dir=${MODEL_DIR} \
      --model_name="mnasnet-a1" \
      --skip_host_call=true \
      --train_steps=109474 \
      --train_batch_size=4096
    • --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 as long as the previous checkpoints were created using TPU of the same size and Tensorflow version.

      For a single Cloud TPU device, the procedure trains the MnasNet model ('mnasnet-a1' variant) for 350 epochs and evaluates every fixed number of steps. Using the specified flags, the model should train in about 23 hours. With real imagenet data, the settings will reproduce the state-of-the-art research result, while users should be able to tune up the training speed.

Scaling your model with Cloud TPU Pods

You can get results faster by scaling your model with Cloud TPU Pods. The fully supported Mnasnet model can work with the following Pod slices:

  • v2-32
  • v2-128
  • v2-256
  • v2-512
  • v3-32
  • v3-128
  • v3-256
  • v3-512
  • v3-1024
  • v3-2048

When working with Cloud TPU Pods, you first train the model using a Pod, then use a single Cloud TPU device to evaluate the model.

Training with Cloud TPU Pods

  1. Delete the Cloud TPU resource you created for training the model on a single device.

     (vm)$ ctpu delete --tpu-only --name=mnasnet-tutorial
    
  2. Run the ctpu up command, using the tpu-size parameter to specify the Pod slice you want to use. For example, the following command uses a v2-32 Pod slice.

      (vm)$ ctpu up --tpu-only --tpu-size=v2-32 --tf-version=1.15.3 --name=Cloud TPU
    
  3. Update the TPU_NAME environment variables.

    (vm)$ export TPU_NAME=Cloud TPU
    
  4. Update the MODEL_DIR directory to store the training data.

    (vm)$ export MODEL_DIR=${STORAGE_BUCKET}/Cloud TPU
    
  5. Train the model by running the following script.

    The script trains the model on the fake_imagnet dataset to 35 epochs. This takes approximately 90 minutes to run on a v3-128 Cloud TPU.

    (vm)$ python3 mnasnet_main.py \
      --tpu=${TPU_NAME} \
      --data_dir=${DATA_DIR} \
      --model_dir=${MODEL_DIR} \
      --model_name="mnasnet-a1" \
      --skip_host_call=true \
      --train_steps=109474 \
      --train_batch_size=4096
    
    • --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 as long as the previous checkpoints were created using TPU of the same size and Tensorflow version.

    The procedure trains MnasNet model ('mnasnet-a1' variant) on the fake_imagent data set to 350 epochs. It should finish in around 5 hours.

Evaluating the model

In this set of steps, you use Cloud TPU to evaluate the above trained model against the fake_imagenet validation data.

  1. Delete the Cloud TPU resource you created to train the model on a Pod.

    (vm)$ ctpu delete --tpu-only --name=${TPU_NAME}
  2. Start a v2-8 Cloud TPU. Use the same name that you used for the Compute Engine VM, which should still be running.

    (vm)$ ctpu up --tpu-only --tf-version=1.15.3 --name=mnasnet-eval
    
  3. Create an environment variable for your TPU name.

    (vm)$ export TPU_NAME=mnasnet-eval
    
  4. Create an environment variable for your accelerator type.

    (vm)$ export ACCELERATOR_TYPE=v3-8
    
  5. Run the model evaluation. This time, add the mode flag and set it to eval.

    (vm)$ python3 mnasnet_main.py \
      --tpu=${TPU_NAME} \
      --data_dir=${DATA_DIR} \
      --model_dir=${MODEL_DIR} \
      --mode=eval \
      --config_file=configs/cloud/${ACCELERATOR_TYPE}.yaml
    

    This generates output similar to the following:

       Eval results: {'loss': 7.532023,
       'top_1_accuracy': 0.0010172526,
       'global_step': 100,
       'top_5_accuracy': 0.005065918}.
       Elapsed seconds: 88
    

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 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 --zone=europe-west4-a --name=mnasnet-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 MNASNET 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.