Custom Vision: Chapter 4 - Image Classification with Vertex AI

Computer Vision Jul 28, 2021

In our previous posts we explored -

  1. What is Custom Computer Vision
  2. How to perform Data Annotation using Label Studio
  3. How to classify Images using Tensorflow

In today's article we are going to explore one of the most sought after tools in the Industry - Vertex AI

Now as we are performing the task of Image Classification, we will be using the folder structure in itself as a Label to setup our Data and upload it into GCP.

So before we begin, we need to answer the most obvious question

What is Vertex AI?

Vertex AI is the latest update from Google's artillery for Artificial Intelligence.

This year in 2021 Google Cloud Platform released their most advanced version of AutoML, that provides developer, business leaders, designers, practically anyone the capability to build Neural based models.

Building State of the Art AI Solutions has just been brought to our door steps.

Vertex AI has a plethora of features to leverage based on one's business needs or use case. Well honestly to name a few -

  1. Managing Datasets
  2. Feature Reviews
  3. Training for Edge specialised devices
  4. AutoML support
  5. Robust Annotator for Labelling
  6. Deployment right into Production systems

As we progress in our journey through various stages of AI development, we will unfold the rich features offered by Google Cloud Platform.

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.

Step 1: Upload Dataset to GCS

As we had already downloaded our intended Intel Dataset onto our local system for testing. Now we can just go ahead and upload it to our GCS bucket.

You can do this step by 2 approaches -

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

Step 2: 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.

What it basically implies is, based on your use case, choose the kind of configuration and Model architecture you may request the system to optimise for.

Now as we had already mentioned Vertex AI leverages AutoML in the background providing an engine with a capability to explore and get the best Neural Models to provide you with benchmarking results.

In this use case, as we are dealing with Image Classification -

  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 Dataset in Vertex AI

Step 3: Import necessary packages

Once you have created the dataset, before you can Import Dataset, Vertex AI expects the Data to be uploaded in a particular format.

A mandatory CSV file with all the annotation information needs to be uploaded to GCS from where it picks the reference and loads all the Data into the Vertex AI platform.

So where do we build the CSV and what does it look like.

Every use case has its own pre-defined template. As we are dealing with Image classification, let's take a look at what those CSV files look like.

Based on your Dataset and annotation status you can choose one of the below mentioned Styles of CSV generation -

For example:

  • Labeled: gs://my-storage-bucket-vcm/flowers/images/img100.jpg,daisy
  • Not labeled: gs://my-storage-bucket-vcm/flowers/images/img403.jpg
  • Multi-label: gs://my-storage-bucket-vcm/flowers/images/img384.jpg,dandelion,tulip,rose
  • Assigned to a set: TEST,gs://my-storage-bucket-vcm/flowers/images/img805.jpg,daisy

Now let's go ahead and import the packages that we will be using in our next steps.

from google.cloud import storage
import csv

Step 4: Transform Dataset

As we have already uploaded the files to GCS, we can just read the GCS URI and create a corresponding CSV, that needs to be uploaded into GCS

def list_blobs(bucket_name):
    """Lists all the blobs in the bucket."""
    # bucket_name = "your-bucket-name"

    storage_client = storage.Client()

    # Note: Client.list_blobs requires at least package version 1.17.0.
    blobs = storage_client.list_blobs(bucket_name)

    all_data = []
    for blob in blobs:
        gcs_path = "gs://" + bucket_name + "/" + blob.name
        label = str(blob.name).split("/")[0]
        all_data.append([gcs_path, label])

    with open("automl_vision.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 your Dataset

Step 7: 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 Image Classification 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.