Classify Images of Clouds in the Cloud with AutoML Vision

AutoML Vision helps developers with limited ML expertise train high quality image recognition models. Once you upload images to the AutoML UI, you can train a model that will be immediately available on GCP for generating predictions via an easy to use REST API.

In this lab you will upload images to Cloud Storage and use them to train a custom model to recognize different types of clouds (cumulus, cumulonimbus, etc.).

Set up AutoML Vision

AutoML Vision provides an interface for all the steps in training an image classification model and generating predictions on it. Start by enabling the Cloud AutoML API.

From the Navigation menu and select APIs & Services > Library.

In the search bar type in “Cloud AutoML”. Click on the Cloud AutoML API result and then click Enable.

This will take a minute to set up.

Now open this AutoML UI link in a new browser. Select your lab credentials, then click Allow to log in.

Note for Chrome users: If you have trouble seeing this page, log out of your current Chrome user profile and try to open it again.

You are prompted to specify a Google Cloud project. Click into the Project ID field and select your Qwiklabs Project ID from the dropdown menu, then click CONTINUE:

select-project.png

Click the Set up Now button. This will take a minute or two to complete.

autoML_setupnow.png

Activate Google Cloud Shell

Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Google Cloud Shell provides command-line access to your GCP resources.

In Cloud Shell use the following commands to create environment variables for you Project ID and Username, replacing <USERNAME> with the User Name you logged into the lab with:

export PROJECT_ID=$DEVSHELL_PROJECT_ID

export QWIKLABS_USERNAME=<USERNAME>

Run these commands to give AutoML permissions:

gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member="user:$QWIKLABS_USERNAME" \
    --role="roles/automl.admin"

gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member="serviceAccount:custom-vision@appspot.gserviceaccount.com" \
    --role="roles/ml.admin"

gcloud projects add-iam-policy-binding $PROJECT_ID \
    --member="serviceAccount:custom-vision@appspot.gserviceaccount.com" \
    --role="roles/storage.admin"

One of the resources created for you is a storage bucket. In the GCP console, open the Navigation menu and click on Storage to see it.

b91e6a77309137cf.png

Upload training images to Google Cloud Storage

In order to train a model to classify images of clouds, you need to provide labeled training data so the model can develop an understanding of the image features associated with different types of clouds. In this example your model will learn to classify three different types of clouds: cirrus, cumulus, and cumulonimbus. To use AutoML Vision you need to put your training images in Google Cloud Storage.

Before adding the cloud images, create an environment variable with the name of your bucket.

Run the following command in Cloud Shell:

export BUCKET=$PROJECT_ID-vcm

The training images are publicly available in a Cloud Storage bucket.

Use the gsutil command line utility for Cloud Storage to copy the training images into your bucket:

gsutil -m cp -r gs://automl-codelab-clouds/* gs://${BUCKET}

When the images finish copying, click the Refresh button at the top of the Storage browser, then click on your bucket name. You should see 3 folders of photos for each of the 3 different cloud types to be classified:

autoML_bucket_folders.png

If you click on the individual image files in each folder you can see the photos you’ll be using to train your model for each type of cloud.

Create a dataset

Now that your training data is in Cloud Storage, you need a way for AutoML Vision to access it. You’ll create a CSV file where each row contains a URL to a training image and the associated label for that image. This CSV file has been created for you; you just need to update it with your bucket name.

Run the following command to copy the file to your Cloud Shell instance:

gsutil cp gs://automl-codelab-metadata/data.csv .

Then update the CSV with the files in your project:

sed -i -e "s/placeholder/${BUCKET}/g" ./data.csv

Now upload this file to your Cloud Storage bucket:

gsutil cp ./data.csv gs://${BUCKET}

Once that comand completes, click the Refresh button on the on the Bucket details page. Click on your bucket name. Confirm that you see the data.csv file in your bucket.

Navigate back to the AutoML Vision tab. Your page should now resemble the following:

MLVision_nobeta.png

At the top of the console, click + NEW DATASET.

Type “clouds” for the Dataset name.

Select “Single-Label Classification”.

mlvision-new-dataset.png

In your own projects, you may want to use multi-class classification.

Click Create Dataset.

Choose Select a CSV file on Cloud Storage and add the file name to the URL for the file you just uploaded – gs://your-bucket-name/data.csv

An easy way to get this link is to go back to the GCP Console, click on the data.csv file. Click on the copy icon in the URI field.

mlvision-select-file.png

Click Continue.

It will take 2 – 5 minutes for your images to import. Once the import has completed, you’ll be brought to a page with all the images in your dataset.

Inspect images

After the import completes, click on the Images tab to see the images you uploaded.

VisionAutoML_images.png

Try filtering by different labels in the left menu (i.e. click cumulus) to review the training images:

Note: If you were building a production model, you’d want at least 100 images per label to ensure high accuracy. This is just a demo so only 20 images were used so the model could train quickly.

If any images are labeled incorrectly you can click on the image to switch the label:

mlvision-image-detail.png

To see a summary of how many images you have for each label, click on LABEL STATS at the top of the page. You should see the following show up on the right side of your browser.

mlvision-label-stats.png

Note: If you are working with a dataset that isn’t already labeled, AutoML Vision provides an in-house human labeling service .

Train your model

You’re ready to start training your model! AutoML Vision handles this for you automatically, without requiring you to write any of the model code.

To train your clouds model, go to the Train tab and click Start Training.

Enter a name for your model, or use the default auto-generated name.

Leave Cloud-hosted selected, then click Continue.

Set the node hours set to 8.

VisionAutoML_8nodehrs.png

Click Start Training.

Since this is a small dataset, it will only take around 25-30 minutes to complete.

While you’re waiting, you can watch this YouTube video on preparing an image data in AutoML – the images should look familiar!

Evaluate your model

In the Evaluate tab, you’ll see information about Precision and Recall of the model.

AutoML_precision_graph.png

You can also play around with Score threshold.

Finally, scroll down to take a look at the Confusion matrix.

AutoML_confusion.png

Generate predictions

Now it’s time for the most important part: generating predictions on your trained model using data it hasn’t seen before.

Navigate to the Test & Use tab in the AutoML UI:

Deploy model then Deploy.

This will take around 20 minutes to deploy.

Generate predictions

There are a few ways to generate predictions. In this lab you’ll use the UI to upload images. You’ll see how your model does classifying these two images (the first is a cirrus cloud, the second is a cumulonimbus).

Download these images to your local machine by right-clicking on each of them:

a4e6d50183e83703.png
1d4aaa17ec62e9ba.png

Return to the AutoML Vision UI, click Upload Images and upload the clouds to the online prediction UI. When the prediction request completes you should see something like the following:

AutoML_cumulo.png

congratulations on your skills

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s