paint-brush
Image Classification Model with Google AutoML [A How To Guide]by@seveibar
1,695 reads
1,695 reads

Image Classification Model with Google AutoML [A How To Guide]

by SeveFebruary 21st, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this tutorial, I'll show you how to create a single label classification model in Google AutoML. We'll be using a dataset of AI-generated faces from generated photos. After that, we'll deploy our model to the cloud AND create the web browser version of the algorithm. In this section we'll create a Cloud Model that runs on GCP with an easy-to-use API as well as an Edge Model that can be exported to Tensorflow and run on mobile devices, browsers, locally or hosted on premise.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Image Classification Model with Google AutoML [A How To Guide]
Seve HackerNoon profile picture

In this tutorial, I'll show you how to create a single label classification model in Google AutoML. We'll be using a dataset of AI-generated faces from generated.photos. We'll be training our algorithm to determine whether a face is male or female. After that, we'll deploy our model to the cloud AND create the web browser version of the algorithm.

Getting The Labels

Okay let's get started! First let's take a look at the data we'll be classifying (you can download it here). In total there are about 2,000 faces.

It's hard to believe these faces aren't real! Okay now let's get the ground truth labels to train our algorithm on. We can do this pretty quickly with wao.ai by creating a job for image classification, you can see this process in the video here.

After our job completes, we'll have a CSV containing our ground-truth labels (download here).

Setting Up Our Project

Time to jump into Google AutoML. In this section we'll create a Dataset on Google AutoML and begin training our model.

If you haven't already, create an account on Google Cloud Platform. After that, we need to create a new project.

After our project is created, we can use the sidebar or search bar at the top to navigate to Google AutoML. You may need to enable some APIs and set up billing for this to work, GCP will walk you through the process.

Next, we can hit Get Started with Image Classification.

This took me to the Datasets view. From the Datasets view, we can click Create New Dataset and fill out some details for our dataset to train on.

Formatting the Input Data

Now to actually get our data into the Google Cloud Platform. All the data needs to be in a GCP Storage Bucket. The browser interface won't work because our dataset is too big.

The GCP command line tool, however, should work perfectly. You can install the Google Cloud SDK (which includes the command line tool) here.

Now we just need to execute gsutil cp -r path/to/faces gs://YOUR_BUCKET/faces to move our files up to the bucket. Make sure to change YOUR_BUCKET to the name of the bucket that was created for you (my bucket name in the screenshot below is woven-icon-263815-vcm).

Next, we'll want to convert the ground-truth labels CSV from wao.ai to the CSV output expected by AutoML.

Our original CSV looks like this:

For Google AutoML to use it, we need to convert it this:

I did this using a pandas DataFrame in an ipython terminal (as shown below)

Finish Creating the Dataset

Now that we have a CSV in the format Google AutoML needs, we're ready to finish creating our dataset.

Upload the new CSV we created to your bucket and select it within the Import Dataset screen.

After the data is imported, you can view all our images and labels from the browser.

Creating the Model

In this section, we'll create a Cloud Model that runs on GCP with an easy-to-use API as well as an Edge Model that can be exported to Tensorflow and run on mobile devices, browsers, locally or hosted on premise.

1. Training the Cloud Model

Navigate to the "TRAIN" tab and click "START TRAINING". I used all the default options.

A couple hours later the model completed with an overview of the model's performance and the budget used (all 16 hours I allocated :)

2. Training the "Edge" Model (the one that runs anywhere)

Creating the edge model can be done in basically the same way, just click "Edge" instead of "Cloud". When you create an Edge Model you can optimize for speed or accuracy. I decided to optimize for accuracy because I wanted to compare the Edge Model to the Cloud Model.

Results

In the "EVALUATE" tab, we can see how our model performed. The Cloud Model was able to get 94.5% accuracy. The Edge Model was able to get 95.5% accuracy. It surprised me that the Cloud Model performed slightly worse, especially because it had a greater training budget!

Overall, I'm happy with the performance of both of the models. The confusion matrix reveals that the Cloud Model made more mistakes when predicting males than females, whereas the Edge Model was more even.

Cloud Model Performance

In the screenshot below, you can see the confusion matrix of the cloud model, as well as some statistics reported by AutoML. It was slightly better at predicting females than males.

Edge Model Performance

In the screenshot below, you can see the confusion matrix of the edge model, as well as some statistics reported by AutoML. The edge model was slightly better at predicting males!

Edge Cases

Google AutoML gives you a breakdown of where your model performed well and where it made mistakes. As with my keras model, children and unusual face angles are a problem. The screenshots below show some examples of false positives and false negatives.

Deploying our Model

Now that we've gotten models we're happy with, we should put them to use! Our Cloud Model can be deployed on GCP and our Edge model can be downloaded and run with Tensorflow. Let's explore the deployment of both Cloud and Edge Models.

Cloud Deployment

Navigate to the "TEST & USE" tab and hit the "DEPLOY MODEL" button. For testing, I decided to only deploy to one node. It took about an hour to deploy the model.

The Cloud Model exposes an easy-to-use API where you upload a simple JSON object and receive a set of predictions with probabilities back. For me, this is the perfect API for integrations, nice and simple.

We can also use the API directly in the browser and inspect the results. I uploaded some face photos from the training set, and things appearing to be working great! Overall I'd say it's a pretty easy to use API if you can afford to run a cloud instances in the background.

Edge Deployment

For the Edge Deployment, we have a variety of ways to download the model. Each option is really powerful:

  • TF Lite: Allows you to run your model on mobile devices
  • TensorFlow.js: Allows you to run your model in a web browser
  • Core ML: Allows you to run your model on Apple devices
  • Container Allows you to run your model in a docker container (perfect for web servers)

I downloaded the Tensorflow.js model and built a demo that uses the Edge Model and your webcam, try it out here! Note: This model does NOT upload your images to a server, everything runs locally!

Final Thoughts

Overall, Google AutoML was easy-to-use and super effective at this task. I'm looking forward to trying other cloud providers to see how they compare!

(Originally published here)