Build a Custom Image Classification Model Using Google AutoML

Wanna build an image classifier with a fascinating level of accuracy in less than 15 minutes? Are you worried about not getting better accuracy for your custom models? Don’t know how to write Neural Nets for Computer Vision?

Prashanth Mangipudi
Analytics Vidhya
8 min readJul 2, 2020

--

Woo-hoo!! You are at the right place. I’ve been in your shoe a couple of times where you know the code wasn’t an important aspect whereas the result of the classification model was crucial. Then I realized that there is something great that already exists to solve our problem. That is Google Cloud’s Vision API with AutoML feature.

In this article, I’ll try to make things clear about the Google Cloud Platform with respect to the Vision API. I’ll walk you through all the steps right from the scratch to building a custom image classifier.

What is Google Vision API?
Google’s Vision API is a one-stop for computer vision that allows users to integrate vision detection features within applications, including image classification, object recognition, image labeling, Optical Character Recognition(OCR), face, and landmark detection.

Google Cloud Vision API. Imageby google on Google Images

What is AutoML?
Automated Machine Learning(AutoML) is the process of automating the process of applying machine learning to real-world problems. AutoML covers the complete pipeline from a raw dataset to a deployable model.
In short, just feed in the data and get the deployable model for predictions with just a few clicks.

AutoML High-level view. Image by Google on Google Images.

Before starting the implementation, please ensure that you have a google cloud account. If you don’t have one please do create here. Also, (do) avail the $300 credits by signing up for the free trial.

I hope that by now we are good to go and start building our custom classification model.

Step-0:

Once, you have your account ready, please crash on the console page, you should see something like this(below image) with the project name as My First Project.

Google Cloud Console. Image by author

The console page is the place where you can monitor every resource that is allocated to your project.

Step-1:
Click on the My First Project and select New Project

Step-1 — Image by author

Step-2:
You’ll get a window, please provide your project name or leave the default name. But please ensure that you cannot change the project name later.
Once, you are done with that you’ll observe the below console for your new project.

Step-2 (Image by author)

Step-3:
Now, that the project is created, let’s explore the Vision API by clicking the Navigation Menu and scroll down until the Artificial Intelligence menu with the Vision API option.

Image by author
Image by author

After Clicking on the Vision, you’ll land on the Cloud Vision Dashboard

Image by author

Step-4:
Click on the Datasets option to Enable the AutoML service for your project.

Image by author
Image by author

After the API is enabled you’ll see the above screen. At this point, there will a service account created automatically for the AutoML agent. It is always good to verify that.

Step-5:
Open your cloud console here in a new tab and select the project that you have created now if prompted.
From the console, Navigation Menu select the IAM(Identity Access Management) IAM& Admin → IAM

Image by author

You should see a service named AutoML-service Agent, this is basically for the authorization of your project with the AutoML services.

Image by author

Step-6:
Now, let’s get back to our Vision API page, and click on the GET STARTED

Image by author

Step-7:
You’ll be redirected to the Datasets page, Click on the New Data Set which is present on the top.

Image by author

Step-8:
Enter the dataset name of your choice or let the default name be there. Now, you have to select the type of dataset based on your use case.
Single label: Use this if your use-case involves only one class in an image.
Multi-label: Use this if your use-case involves more than one class in an image.
Object detection: Use this if you want to build an object detection model(This is beyond the scope of this article)

Image by author

Select your option and click on the CREATE DATASET.

Image by author

After the project is created, you’ll see the below screen

Image by author

Step-9:
Importing the dataset(Images). This has to ways either we can upload the CSV file that consists of the path the images and their corresponding labels which are stored in Google Cloud Storage Bucket or Upload the images directly from your local computer. In this article I’ll walk you through the easy approach, in essence, uploading from the local computer.
Please refer to the reference section for the tutorial of uploading the data using CSV file.

For Uploading the images, the best way would be to make a zip of the below directory structure.
— — data

— — Class-1

- — — image0

- — — image1

- — Class-2

- — — image0

- — — image1

  • — — …
The above folders are present in the data folder Image by author

Step-10:
Compress the data folder as data.zip . Ensure that you convert to .zip only.
Now, click on the select files (refer step-8) and select your .zip file and you’ll see similar to the below image.

Image by author

Now, whatever the images we upload will be stored in Google Cloud Storage Bucket.
What is Google Cloud Storage Bucket?
Cloud Storage is a flexible, scalable, and durable storage option for your virtual machine instances. You can read and write files to Cloud Storage buckets from almost anywhere, so you can use buckets as common storage between your instances, App Engine, your on-premises systems, and other cloud services.

Step-11:
Click on the browse

Image by author

Now, click on the + to create a new bucket

Image by author

Follow below images for bucket creation, I’d recommend you not change the default options as sometimes you might not be able to import the images due to location.

Image by author
Image by author
Image by author

After Clicking on Create, you’ll observe the below screen.
Click Select and then click on the continue button.

Image by author

Ah! With this we have almost finished our task, we are just one step behind training our model.
Now, click on the Images tab, you’ll see that the images are being imported, this process takes some time even if the images are less in number.

Image by author

Just to show, I’ve just uploaded 3 images for each class. Ignore the number of samples.

Image by author

Step-12
Here comes the final part, in essence, the training part. if you could see below, it is showing me errors because I’ve uploaded fewer data. So, please ensure that you upload at least 100 images per each class. Then the START TRAINING the option is highlighted.

Image by author

After you click on the START TRAINING you’ll see the below window

Image by author

You can change the model name if you wish to.
Cloud Hosted: Your model after training will be hosted on the google cloud server and you can get predictions via REST API calls.
Edge: You can export the model to run on your edge devices like mobile or web.

Remember that, both have varying cost scale, please find the related information about pricing here. After setting your node hour click on START TRAINING WHOA!!!! THAT’S IT !!!

Now, you can just sit back and relax!! Based on the selected node hours, your model training time will be affected. You do not have to keep checking it, you’ll be mailed once the training process is completed.

After the training process, you can export your model as per your requirement.

Image by author

References :
1.
For training-related information https://cloud.google.com/vision/automl/docs/train
2. For Edge model training information https://cloud.google.com/vision/automl/docs/train-edge
3. For the complete automl documentations
https://cloud.google.com/vision/automl/docs
4. For uploading data using a CSV file
https://youtu.be/kgxfdTh9lz0
5. Google Cloud Platform Overview
https://cloud.google.com/docs/overview

I hope that I did make the whole process of building easy and clear. Thank you for reading. Have a great day :-)

If you would like to make any sort of constructive comments, please feel free to reach out to me on Linkedin.

--

--

Prashanth Mangipudi
Analytics Vidhya

I’m a technology enthusiast. Curious enough to learn something new every day. I enjoy writing about whatever I learn.