Qwiklabs ML APIs - 7. Classify Images of Clouds in the Cloud with AutoML Vision
Overview
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.).
What you’ll learn
-
Uploading a labeled dataset to Google Cloud Storage and connecting it to AutoML Vision with a CSV label file.
-
Training a model with AutoML Vision and evaluating its accuracy.
-
Generating predictions on your trained model.
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.
Open the navigation menu and and select APIs & Services > Library. In the search bar type in “Cloud AutoML API”. Click on the Cloud AutoML API result and then click Enable.
This may take a minute. You should now be on the following page (ensure that the activation status is set to Enabled):
Now open a new tab and navigate to the AutoML UI. You’ll need to select the lab credentials and click Allow to log in.
Note: Chrome users: if you have trouble seeing this page, log out of your current Chrome user profile and try to open it again.
You will then be prompted to specify a Google Cloud project — select your Qwiklabs Project ID from the dropdown menu and click CONTINUE:
Click the Set up Now button. This will take a little while.
Return to the GCP Console and in Cloud Shell copy these commands to create environment variables in Cloud Shell for you Project ID and Qwiklabs Username, replacing <QWIKLABS_USERNAME>
with the user name you logged into the lab with:
1
2
export PROJECT_ID=$DEVSHELL_PROJECT_ID
export QWIKLABS_USERNAME=<QWIKLABS_USERNAME>
Return to the GCP Console and in Cloud Shell copy these commands to give AutoML permissions:
1
2
3
4
5
6
7
8
9
10
11
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"
Now create a Storage bucket:
1
2
3
4
gsutil mb -p $PROJECT_ID \
-c regional \
-l us-central1 \
gs://$PROJECT_ID-vcm/
Upload training images to Google Cloud Storage
In order to train a model to classify images of clouds, you need to provide it labeled training data so it 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.
In the GCP console, open the navigation menu and select Storage > Browser:
Once there, you should see the bucket you created in the last step.
The training images are publicly available in another Cloud Storage bucket. Copy them to your bucket so you can take a look.
First, create an environment variable with the name of your bucket by running the following command in Cloud Shell, replacing YOUR_BUCKET_NAME
in the command below with the name of your bucket:
1
export BUCKET=YOUR_BUCKET_NAME
Next, using the gsutil
command line utility for Cloud Storage, copy the training images into your bucket:
1
gsutil -m cp -r gs://automl-codelab-clouds/* gs://${BUCKET}
When the images finish copying, click the Refresh button at the top of the Cloud Storage browser. Then click on your bucket name. You should see the following folders of photos of each of the 3 different cloud types we’ll be classifying:
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:
1
gsutil cp gs://automl-codelab-metadata/data.csv .
Then run the following command to update the CSV with the files in your project:
1
sed -i -e "s/placeholder/${BUCKET}/g" ./data.csv
Now you’re ready to upload this file to your Cloud Storage bucket:
1
gsutil cp ./data.csv gs://${BUCKET}
Once that comand goes through, click the Refresh bucket button. Confirm that you see the data.csv
file in your bucket.
Navigate back to the AutoML Vision tab. If you are still on the “Finish setting up your Google Cloud project”, click CHECK AGAIN.
Your page should now resemble the following:
At the top of the console, click + NEW DATASET
. Type “clouds” for the Dataset name.
Choose Select a CSV file on Cloud Storage and enter the URL of the file you just uploaded - gs://your-project-name-vcm/data.csv
.
For this example, leave “Enable multi-label classification” unchecked. In your own projects, you may want to check this box if you’re doing multi-class classification.
Select Create Dataset.
It will take around 2 minutes for your images to finish importing. 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, you are be brought to the Images tab.
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 them to switch the label or delete the image from your training set:
To see a summary of how many images you have for each label, click on LABEL STATS. You should see the following show up on the left side of your browser.
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, and click start training.
Since this is a small dataset, it will only take around 5 minutes to complete.
Evaluate your model
In the Evaluate tab, you’ll see information about Precision and Recall of the model. It should resemble the following:
You can also play around with Score threshold:
Finally, scroll down to take a look at the Confusion matrix.
All of this provides some common machine learning metrics to evaluate your model accuracy and see where you can improve your training data. Since the focus for this lab was not on accuracy, skip to the prediction section, but feel free to browse the accuracy metrics on your own.
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 Predict tab in the AutoML UI:
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:
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:
Pretty cool - the model classified each type of cloud correctly!
Note: In addition to generating predictions in the AutoML UI, you can also use the REST API or the Python client to make prediction requests on your trained model. Check out the tabs for each to see some sample code. You can try it out by copy/pasting these commands into Cloud Shell and providing an image URL.
Congratulations!
You’ve learned how to train your own custom machine learning model and generate predictions on it through the web UI. Now you’ve got what it takes to train a model on your own image dataset.
What we’ve covered
- Uploading training images to Cloud Storage and creating a CSV for AutoML Vision to find these images.
- Reviewing labels and training a model in the AutoML Vision UI.
- Generating predictions on new cloud images.
Next Steps
- Watch the intro video
- Learn more about how AutoML Vision works by listening to the GCP Podcast episode
- Read the announcement blog post
- Learn how to perform each step with the API