Training EfficientNet on Cloud TPU

This tutorial shows you how to train the Tensorflow EfficientNet model using a Cloud TPU device or Cloud TPU Pod slice (multiple TPU devices). The EfficientNet models are a family of image classification models, which achieve state-of-the-art accuracy, while also being smaller and faster than other models.

Model description

The model in this tutorial is based on EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks. In particular, it uses two variants: efficientnet-b0 and efficientnet-b3. The tutorial demonstrates training the model using TPUEstimator.

Set up your project

Before you start the tutorial, check that your Google Cloud Platform project is set up correctly or set up a new project.

  1. Faça login na sua Conta do Google.

    Se você ainda não tiver uma, inscreva-se.

  2. Selecione ou crie um projeto do GCP.

    Acessar a página Gerenciar recursos

  3. Verifique se o faturamento foi ativado para o projeto.

    Saiba como ativar o faturamento

  4. Verify that you have sufficient quota to use either TPU devices or Pods.

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

  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. For more information, see Managing VM and TPU Resource.

Run ctpu up to create resources

  1. Open a Cloud Shell window.

    Open Cloud Shell

  2. Run gcloud config set project <Your-Project> to use the project where you want to create Cloud TPU.

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

  4. 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.14
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 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.14
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 is username@tpuname, which shows you are logged into your Compute Engine VM.

Export the storage bucket

To specify a storage bucket to use for storing checkpoints during training and for writing training logs, set up the STORAGE_BUCKET 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.

(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 EfficientNet model with fake_imagenet

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. Navigate to the directory:

    (vm)$ cd /usr/share/tpu/models/official/efficientnet/
    
  3. Run the training script for either a single Compute Engine device or Pod as follows:

TPU Device

Using model efficientnet-b0 as an example, we run the training on a TPU v2 instance with 8 cores.

(vm)$ python main.py \
  --tpu=${TPU_NAME} \
  --data_dir=gs://cloud-tpu-test-datasets/fake_imagenet \
  --model_dir=${STORAGE_BUCKET}/efficientnet-b0-v2-8 \
  --model_name='efficientnet-b0' \
  --skip_host_call=true \
  --train_batch_size=2048 \
  --train_steps=218948
  • --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.

For a single Cloud TPU device, the procedure trains the EfficientNet model (efficientnet-b0 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 should obtain ~76.5% top-1 accuracy on ImageNet validation dataset. The best model checkpoint and corresponding evaluation result is inside the archive folder in the model directory: ${STORAGE_BUCKET}/efficientnet-b0-v2-8/archive.

TPU Pod

Using model efficientnet-b3 as an example, we run the training on a slice of TPU v3 Pod with 32 cores.

(vm)$ python main.py \
  --tpu=${TPU_NAME} \
  --data_dir=gs://cloud-tpu-test-datasets/fake_imagenet \
  --model_dir=${STORAGE_BUCKET}/efficientnet-b3-v3-32 \
  --model_name='efficientnet-b3' \
  --skip_host_call=true \
  --mode=train \
  --train_steps=109474 \
  --train_batch_size=4096 \
  --iterations_per_loop=100
  • --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.

The procedure trains the EfficientNet model (efficientnet-b3 variant) for 350 epochs. The model should get 81.1% accuracy on ImageNet dev set, which should finish in around 20 hours. The best model checkpoint and corresponding evaluation result is inside the archive folder in the model directory: ${STORAGE_BUCKET}/efficientnet-b3-v3-32/archive.

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

Esta página foi útil? Conte sua opinião sobre:

Enviar comentários sobre…