Downloading, preprocessing, and uploading the COCO dataset

COCO is a large-scale object detection, segmentation, and captioning dataset. Machine learning models that use the COCO dataset include:

  • Mask-RCNN
  • Retinanet
  • ShapeMask

Before you can train a model on a Cloud TPU, you must prepare the training data.

This document describes how to prepare the COCO dataset for models that run on Cloud TPU. The COCO dataset can only be prepared after you have created a Compute Engine VM. The script used to prepare the data, download_and_preprocess_coco.sh, is installed on the VM and must be run on the VM.

After preparing the data by running the download_and_preprocess_coco.sh script, you can bring up the Cloud TPU and run the training.

To fully download and preprocess and upload the COCO dataset to a Cloud Storage bucket takes approximately 2 hours.

  1. In your Cloud Shell, configure gcloud with your project ID.

    export PROJECT_ID=project-id
    gcloud config set project ${PROJECT_ID}
    
  2. In your Cloud Shell, create a Cloud Storage bucket using the following command:

    gcloud storage buckets create gs://bucket-name --project=${PROJECT_ID} --location=us-central2
    
  3. Create a Compute Engine VM to download and preprocess the dataset. For more information, see Create and start a Compute Engine instance.

    $ gcloud compute instances create vm-name \
        --zone=us-central2-b \
        --image-family=ubuntu-2204-lts \
        --image-project=ubuntu-os-cloud \
        --machine-type=n1-standard-16 \
        --boot-disk-size=300GB
    
  4. Connect to the Compute Engine VM using SSH:

    $ gcloud compute ssh vm-name --zone=us-central2-b
    

    When you connect to the VM, your shell prompt changes from username@projectname to username@vm-name.

  5. Set up two variables, one for the storage bucket you created earlier and one for the directory that holds the training data (DATA_DIR) on the storage bucket.

    (vm)$ export STORAGE_BUCKET=gs://bucket-name
    
    (vm)$ export DATA_DIR=${STORAGE_BUCKET}/coco
  6. Install the packages needed to pre-process the data.

    (vm)$ sudo apt-get update && \
      sudo apt-get install python3-pip && \
      sudo apt-get install -y python3-tk && \
      pip3 install --user Cython matplotlib opencv-python-headless pyyaml Pillow numpy absl-py tensorflow && \
      pip3 install --user "git+https://github.com/cocodataset/cocoapi#egg=pycocotools&subdirectory=PythonAPI"
    
  7. Run the download_and_preprocess_coco.sh script to convert the COCO dataset into a set of TFRecord files (*.tfrecord) that the training application expects.

    (vm)$ git clone https://github.com/tensorflow/tpu.git
    (vm)$ sudo bash tpu/tools/datasets/download_and_preprocess_coco.sh ./data/dir/coco
    

    This installs the required libraries and then runs the preprocessing script. It outputs *.tfrecord files in your local data directory. The COCO download and conversion script takes approximately one hour to complete.

  8. Copy the data to your Cloud Storage bucket.

    After you convert the data into the TFRecord format, copy the data from local storage to your Cloud Storage bucket using the gcloud CLI. You must also copy the annotation files. These files help validate the model's performance.

    (vm)$ gcloud storage cp ./data/dir/coco/*.tfrecord ${DATA_DIR}
    (vm)$ gcloud storage cp ./data/dir/coco/raw-data/annotations/*.json ${DATA_DIR}
    
  9. Disconnect from the Compute Engine VM:

    (vm)$ exit
    

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

  10. Delete your Compute Engine VM:

    $ gcloud compute instances delete vm-name \
    --zone=us-central2-b