wao.ai logowao.ai blog - data labeling and applied machine learning
Blog HomeCan AI Guess Our Sex?Dataset vs Ground-Truth DatasetIntroducing the Universal Data ToolLabel Bounding Boxes with the Universal Data ToolSingle-Label Image Classification with Google AutoMLGetting the LabelsSetting Up Our ProjectCreating the ModelResultsDeploying our ModelSingle-Label Image Classification with Keras

Single-Label Image Classification Model with Google AutoML

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.

Directory of 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, as shown in the video below.

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.

Creating a 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.

Select Google AutoML Vision

Next, we can hit Get Started with Image Classification.

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.

Create New Dataset Create New Dataset Name

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).

Copy Images to Google Bucket

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:

customIdmale_or_female.value
faces/010260.jpgfemale
faces/010265.jpgfemale
faces/011094.jpgfemale
faces/010201.jpgmale

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

image_pathlabel
gs://YOUR_BUCKET/faces/010260.jpgfemale
gs://YOUR_BUCKET/faces/010265.jpgfemale
gs://YOUR_BUCKET/faces/011094.jpgfemale
gs://YOUR_BUCKET/faces/010201.jpgmale

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

Starting Format from wao.ai Final Format

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.

Import Into Dataset

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

Data Successfully Imported

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.

Cloud Model

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

Start Training Create Cloud Model Node Hour Budget

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

Model Complete

Edge Model

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.

Create Edge Model Goal Hour Budget

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.

Overview

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

Cloud Results

Edge Model Performance

Edge Results

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.

Cloud Edge Cases Correct Male Cloud Edge Cases False Positive Male Cloud Edge Cases False Negative

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.

Cloud Deploy

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.

Cloud Predict API

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!

Example Face Predictions 1 Example Face Predictions 2

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)

Edge Deploy

I downloaded the Tensorflow.js model and built a demo that uses the Edge Model and your webcam, try it out below!

Sex Classifier Demo
Our AI can use your webcam to guess your sex based on your face. None of your webcam data is sent to the cloud or saved. We'll ask for permission from your browser after you click the button below.

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!

Learn about applied machine learning with a new article every two weeks. Unsubscribe anytime.
You can also follow @waoai_ on twitter.