Custom Vision: Chapter 8 - Object Detection with Vertex AI

Computer Vision Aug 9, 2021

In our previous posts we explored the concepts around Object Detection and How we can use Annotation Tools to perform Labelling.

We all are aware that Data preparation is a very critical step in the process of any AI product life cycle. Hence to make your life easier, the Open Source Community puts in a lot of effort to help you readers enjoy implementing AI with ease.

As a part of on going series we have covered some of the very significant tools which prove really beneficial for an AI Designer.

  1. Data Annotation - Label Studio
  2. End to End Data Science - Roboflow
  3. Datasets - Kaggle

But enough about Open source for now. What is today's article all about?

Most of the beginners face heavy obstacles while taking their step from performing simpler Computer Vision Tasks such as Image Classification to taking a bigger jump into Object Detection.

So today, we intend on making everyone's journey smoother by leveraging one of the most sought after tool of 2021 - Vertex AI.

What is Vertex AI?

Of the many Gems of Google, this is their current Diamond - Vertex AI

After the launch of AutoML in 2018, people were already spellbound and before they could even digest the capabilities of AutoML, Google yet successfully launched another breathtaking service in just 3 years.

Vertex AI acts as that unified AI platform, that not only lets us train models using pre-trained models offered by Google Research, but rather also lets us Manage, Build and Deploy models with 80% less lines of code.

As promised in our previous articles, as we are moving ahead in our journey of AI Product Development, today is a new Chapter, where we will explore a new set of features of Vertex AI.

Now without wasting much time, let's get right to it.

Step 0: Setup your GCP Account

This is a rather straight forward but a combination of elaborative steps. So if you haven't already done it, based on our previous posts, follow the link.

Feel free to skip this step, as we will be performing all the below mentioned tasks in our future posts using Python SDK. But as a part of this article, we will be dealing with leveraging Vertex AI using Google Cloud Console.

Step 1: Upload Dataset to GCS

In order to leverage Vertex AI, we first need to upload all the data into GCS bucket. You can find the necessary dataset right on this link

You can do this step by 2 approaches -

  1. Using Python SDK provided by Google
  2. Using the Console
Create GCS bucket

Once we have created the bucket, let's go ahead and upload all the files of our Dataset.

Upload Dataset to GCS

Step 3: Create Dataset in Vertex AI

Uploading files may take a while depending upon your internet speed. So how about we make the best of it and setup Vertex AI for our future steps.

In order to perform training using the platform, one needs to create a Dataset within the AI platform. What it typically implies is setting up the config and choosing the type of Use Case we want to tackle.

In this use case, as we are dealing with Object Detection -

  1. Open the GCP console
  2. Navigate to Vertex AI
  3. Provide your dataset with a Unique name
  4. Choose the type of problem you want to tackle - In our case (Image Classification - Single Label)
Create a Dataset

Step 3: Import necessary Packages

Once you have created the Dataset, we can now Import Dataset into Vertex AI. But before we can do that, we need to convert the Annotations in an expected format.

So what is that expected format? It is a CSV.

Now to keep things easy as before, the complete Dataset and its' annotations have been set in line with what is expected by Tensorflow Object Detection API. The advantage with using this Dataset is, you can even leverage it for training it on your local system on Custom Built Neural Models.

So what we are going to do is take that CSV and transform it into the expected CSV format of Vertex AI.

import csv
import pandas as pd

Step 4: Transform Dataset

As we already have every Image uploaded to GCS, we can just go ahead and read and transform the CSV

def read_from_csv(csv_path, bucket_name):
    df = pd.read_csv(csv_path)

    all_data = []
    for index, row in df.iterrows():
        filename = row.get("filename")
        label = row.get("class")
        width = row.get("width")
        height = row.get("height")
        xmin = row.get("xmin")
        ymin = row.get("ymin")
        xmax = row.get("xmax")
        ymax = row.get("ymax")
        new_xmin = round((xmin / width), 1)
        new_xmax = round((xmax / width), 1)
        new_ymin = round((ymin / height), 1)
        new_ymax = round((ymax / height), 1)
        gcs_uri = "gs://" + bucket_name + "/" + filename
        new_row = [
            "UNASSIGNED",
            gcs_uri,
            label,
            new_xmin,
            new_ymin,
            "",
            "",
            new_xmax,
            new_ymax,
            "",
            "",
        ]
        all_data.append(new_row)

    with open("automl_vision_obj.csv", "w") as f:
        write = csv.writer(f)
        write.writerows(all_data)

Step 5: Upload CSV to GCS

Once the CSV is generated and stored in your local system, you can upload the same using the below function.

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""

    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print("Uploaded to {}.".format(destination_blob_name))
    gcs_uri = "gs://" + bucket_name + "/" + destination_blob_name
    return gcs_uri

Step 6: Import Dataset

Now all you are left with is to Import the Dataset and direct it to the apt CSV file that you just generated.

Import Dataset into Vertex AI

Now sit back and Relax, as it may take a couple of minutes based on your Internet Speed.

Step 7: Review and Enjoy your Data

Once your Data is uploaded, it should resemble to the pictographic representation below.

Explore and Analyse your Dataset

Step 8: Patience

This is usually a long running process, depending on your internet speed. As we have done our bit for now, let's sit back and wait for the Upload to complete.

All the steps mentioned above can be Automated or accomplished via using Python SDK as well. If you have followed the steps above you should be ready to Train and Deploy your first Object Detection model on Vertex AI.

I hope this article finds you well. STAY TUNED for more content following in our journey of Custom Computer Vision. 😁

Tags

Vaibhav Satpathy

AI Enthusiast and Explorer

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.