
Before you begin
- Labs create a Google Cloud project and resources for a fixed time
- Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
- On the top left of your screen, click Start lab to begin
Launch Vertex AI Workbench instance
/ 100
In this lab, you learn how to use Vertex AI to train and serve a TensorFlow model using code in a custom container.
While you're using TensorFlow for the model code here, you could easily replace it with another framework.
This lab uses the newest AI product offering available on Google Cloud. Vertex AI integrates the ML offerings across Google Cloud into a seamless development experience. Previously, models trained with AutoML and custom models were accessible via separate services. The new offering combines both into a single API, along with other new products. You can also migrate existing projects to Vertex AI. If you have any feedback, please see the support page.
Vertex AI includes many different products to support end-to-end ML workflows. This lab will focus on the products highlighted below: Training, Prediction, and Notebooks.
For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.
Sign in to Qwiklabs using an incognito window.
Note the lab's access time (for example, 1:15:00
), and make sure you can finish within that time.
There is no pause feature. You can restart if needed, but you have to start at the beginning.
When ready, click Start lab.
Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.
Click Open Google Console.
Click Use another account and copy/paste credentials for this lab into the prompts.
If you use other credentials, you'll receive errors or incur charges.
Accept the terms and skip the recovery resource page.
Navigate to Compute Engine API and select Enable if it isn't already enabled. You'll need this to create your Workbench instance.
Navigate to the Container Registry and select Enable if it isn't already enabled. You'll use this to create a container for your custom training job.
In the Google Cloud console, from the Navigation menu (), select Vertex AI.
Click Enable All Recommended APIs.
In the Navigation menu, click Workbench.
At the top of the Workbench page, ensure you are in the Instances view.
Click Create New.
Configure the Instance:
This will take a few minutes to create the instance. A green checkmark will appear next to its name when it's ready.
Click Check my progress to verify the objective.
You are going to submit this training job to Vertex by putting your training code in a Docker container and pushing this container to Google Container Registry. Using this approach, you can train a model built with any framework.
To start, from the Launcher menu, open a terminal window in your Workbench instance.
Create a new directory called mpg
and cd into it:
Your first step in containerizing your code is to create a Dockerfile. In your Dockerfile you'll include all the commands needed to run your image. It'll install all the libraries you're using and set up the entry point for your training code. From your terminal, create an empty Dockerfile:
Open the Dockerfile by navigating to mpg > Dockerfile
and copy the following into it:
This Dockerfile uses the Deep Learning Container TensorFlow Enterprise 2.3 Docker image. The Deep Learning Containers on Google Cloud come with many common ML and data science frameworks pre-installed. The one you're using includes TF Enterprise 2.3, Pandas, Scikit-learn, and others. After downloading that image, this Dockerfile sets up the entrypoint for your training code. You haven't created these files yet--in the next step, you'll add the code for training and exporting your model.
In your training job, you'll export your trained TensorFlow model to a Cloud Storage Bucket. Vertex will use this to read your exported model assets and deploy the model. From your terminal, run the following to define an env variable for your project, making sure to replace your-cloud-project
with the ID of your project:
Next, run the following in your terminal to create a new bucket in your project. The -l
(location) flag is important since this needs to be in the same region where you'll deploy a model endpoint later in the tutorial:
From your terminal, run the following to create a directory for your training code and a Python file where you'll add the code:
You should now have the following in your mpg/ directory:
Next, open the train.py
file you just created by navigating to mpg > trainer > train.py
and copy the code below (this is adapted from the tutorial in the TensorFlow docs).
At the beginning of the file, update the BUCKET
variable with the name of the Storage Bucket you created in the previous step:
From your terminal, define a variable with the URI of your container image in Google Container Registry:
Then, build the container by running the following from the root of your mpg
directory:
Run the container within your Workbench instance to ensure it's working correctly:
The model should finish training in 1-2 minutes with a validation accuracy around 72% (exact accuracy may vary). When you've finished running the container locally, push it to Google Container Registry:
With our container pushed to Container Registry, you're now ready to kick off a custom model training job.
Vertex AI gives you two options for training models:
In this lab, you're using custom training via our own custom container on Google Container Registry. To start, navigate to the Model Registry section in the Vertex section of your Cloud console:
Click Create to enter the parameters for your training job and deployed model:
In the Container settings step, select Custom container:
In the first box (Container image on GCR), enter the value of your IMAGE_URI
variable above. It should be: gcr.io/your-cloud-project/mpg:v1
, with your own project name. Leave the rest of the fields blank and click Continue.
You won't use hyperparameter tuning in this tutorial, so leave the Enable hyperparameter tuning box unchecked and click Continue.
In Compute and pricing, select the Region e2-standard-4
and click Continue.
Because the model in this demo trains quickly, you're using a smaller machine type.
For the Prediction container step, select Pre-built container and select 2.11 as the Model framework version.
Leave the default settings for the pre-built container as is. For model directory, browse your GCS bucket with the mpg subdirectory. This is the path in your model training script where you export your trained model. It should look like this:
Vertex will look in this location when it deploys your model. Now you're ready for training! Click Start training to kick off the training job. In the Training section of your console, select Region
When you set up your training job, you specified where Vertex AI should look for your exported model assets. As part of our training pipeline, Vertex will create a model resource based on this asset path. The model resource itself isn't a deployed model, but once you have a model you're ready to deploy it to an endpoint. To learn more about models and endpoints in Vertex AI, check out the Get started documentation for Vertex AI.
In this step you'll create an endpoint for our trained model. You can use this to get predictions on our model via the Vertex AI API.
When your training job completes, you should see a model named mpg (or whatever you named it) in the Model Registry section of your console:
When your training job ran, Vertex created a model resource for you. In order to use this model, you need to deploy an endpoint. You can have many endpoints per model. Click on the model and select Deploy & Test tab and then click Deploy to endpoint.
Select Create new endpoint and give it a name, like v1
, and click Continue. Leave Traffic split at 100 and enter 1
for Minimum number of compute nodes. Under Machine type, select e2-standard-2 (or any machine type you'd like). Then click Done and then Deploy.
Deploying the endpoint will take 10-15 minutes. When the endpoint has finished deploying, you'll see the following, which shows one endpoint deployed under your model resource:
You'll get predictions on our trained model from a Python notebook, using the Vertex Python API. Go back to your Workbench instance, and create a Python 3 notebook from the Launcher:
In your notebook, run the following in a cell to install the Vertex AI SDK:
Then add a cell in your notebook to import the SDK and create a reference to the endpoint you just deployed:
You'll need to replace two values in the endpoint_name string above with your project number and endpoint. You can find your project number by navigating to your project dashboard and getting the Project Number value.
You can find your endpoint ID in the endpoints section of the console here:
Finally, make a prediction to your endpoint by copying and running the code below in a new cell:
This example already has normalized values, which is the format our model is expecting.
Run this cell, and you should see a prediction output around 16 miles per gallon.
Do one of the following:
To delete the endpoint you deployed, in the Online Prediction section of your Vertex AI console, select Region next to your endpoint and select Undeploy model from endpoint. Finally, click Undeploy.
To remove the endpoint, navigate back to Endpoints section and click the Actions icon , and then click Delete endpoint.
To delete the Cloud Storage bucket, on the Cloud Storage page, select your bucket, and then click Delete.
You've learned how to use Vertex AI to:
When you have completed your lab, click End Lab. Qwiklabs removes the resources you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.
The number of stars indicates the following:
You can close the dialog box if you don't want to provide feedback.
For feedback, suggestions, or corrections, please use the Support tab.
Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.
This content is not currently available
We will notify you via email when it becomes available
Great!
We will contact you via email if it becomes available
One lab at a time
Confirm to end all existing labs and start this one