[MUSIC PLAYING]
NIKITA NAMJOSHI: Hi, I'm Nikita.
I'm a Google Cloud developer advocate.
If you're anything like me, when you
start working on a new machine learning problem,
the first place you go is a notebook.
Maybe you like running Jupyter in a local environment
or using a Kaggle Kernel, or my personal favorite, Colab.
With tools like this, as well as repositories
for pre-trained models such as TensorFlow Hub,
creating and experimenting with machine learning
is becoming increasingly accessible.
But while experimentation in notebooks is great,
it's easy to hit a wall when it comes time
to elevate those experiments up to enterprise scale.
For example, what if you have a long running job,
want to do distributed training, host a model
for online predictions, or maybe your use case
requires more granular permissions
around security and data privacy?
Making applications or training large models
requires additional tooling to help you scale
beyond just code in a notebook.
But that process can feel a little bit daunting.
There are so many different cloud Services and products.
And honestly, it's a little hard to know where to start.
So today, I'm going to show you how
to become a cloud machine learning developer
in just five steps.
We're going to take some code in a Colab notebook
and then make it production ready by training and deploying
a model on Google Cloud.
If you're new to Colab, it's a hosted Jupyter Notebook service
that requires no setup to use, works entirely in the browser,
and provides free access to computing resources, including
GPUs.
To train and deploy our model, we'll
be using Vertex AI, which is Google Cloud's managed machine
learning platform.
There are a lot of different products.
But today, we'll be focusing on training, predictions,
and Workbench, which is the hosted notebook service.
So here are the five steps we'll take.
First, we'll create a managed notebook
instance on Google Cloud.
We'll use Vertex AI Workbench to create this notebook.
Next, we're going to download our Colab notebook as a .ipynb
file, and then upload this file to our Vertex AI Workbench
notebook.
Then we'll launch an execution of our notebook, which
means that the notebook will be run cell
by cell in the cloud on the Vertex AI managed training
service.
We won't need to manually execute the cells.
Next, we'll deploy the model to an endpoint.
And finally, we'll use this endpoint
to get some predictions.
Because why go to all this effort
if we can't use the model to actually do something, right?
So let's jump over to a demo.
And I'll show you how all of this works.
Here's a Colab notebook with some code.
We're going to train an image classification
model using TensorFlow on the deep weeds data set.
This data set contains images of various weed species
local to pastoral grasslands.
So first in this notebook, we are
going to import some libraries.
Then we'll download the data from TensorFlow data sets.
And we can print out all of the labels in our data set.
We'll do a little preprocessing, just to scale the data
and split it into training and validation sets.
And then we can take a look at some
of the examples in our data set.
Looks like we've got lantana here
in the top left, snake weed, a plant
called rubber vine, et cetera.
Now, the next step is to create our model.
And instead of writing a model totally from scratch,
I decided to use TensorFlow Hub, which
is a repository of trained machine learning models.
TensorFlow Hub provides feature vectors,
which are pretrained models without the top classification
layer.
We can use these feature vectors for transfer learning.
So in the code here, the first thing we do
is create a feature extractor from a resnet model.
Then we add a classification layer on top
with the number of classes in our data set,
which is what we'll train on our deep weeds data set.
Lastly, we compile and train the model.
If you're not very familiar with TensorFlow or Keras,
don't worry about it.
All you need to know is that we have some code here
that creates and trains a machine learning model.
And we could have just as easily been
using scikit-learn, or any other machine learning
framework you prefer.
Now, our goal is to take all of this code
and run it on Google Cloud, making use of more performant
hardware, and then deploy the resulting model.
The first thing we need to do is create our Vertex AI Workbench
notebook.
I'm in the Vertex AI section of the Google Cloud Console.
And from here, we'll select Workbench.
Then we'll click on Manage Notebooks in the top
and select New Notebook.
The first step is to give the notebook a name.
Then under the Advanced Settings,
you can customize your notebook by providing your own Docker
image, changing the hardware profile, and adding GPUs.
And you can see here that there are lots of different hardware
options to choose from.
But we'll just stick to all the defaults for now.
And I'll scroll down and click Create.
This will take about a minute or so.
But once this notebook is created,
we can open it up and follow the steps in the UI
to authenticate.
So that completes our first step.
We have now set up our notebook environment on Google Cloud.
Next, we're going to download the Colab notebook as a .ipynb
file and then upload it to our Vertex AI Workbench managed
notebook.
We can open this notebook and select the TensorFlow 2
kernel, since we're using TensorFlow for this example.
But note that there are multiple kernels to choose from,
if you're working with a different framework.
Now, we want to make sure that our trained
model is safe to Cloud Storage.
That way we can access it later for deployment and predictions.
When we launch our training job on Google Cloud,
it's going to run on a machine we won't have access
to after the job completes.
So we don't want to save our model to a local path.
Instead, we want it to be a path in Cloud Storage.
So I'll scroll down to the end of our code
here, where we have the model.save.
And we're going to go ahead and update this model.save path
to a Cloud Storage bucket.
Now while we could run the cells of our notebook
manually, for models that take a long time to train,
a notebook isn't always the most convenient option.
And if you're building an application
with machine learning, it's unlikely
that you'll only need to train your model once.
Over time, you'll want to retrain your model
to make sure it stays fresh and keeps
producing valuable results.
Manually executing the cells of your notebook
might be the right option when you're
getting started with a new machine learning problem.
But when you want to automate experimentation
at a large scale or retrain models
for a production application, a serverless ML training option
will make things much easier.
So instead, we'll use the notebook execution feature,
which will run our notebook cell by cell on the Vertex
AI managed training service.
We'll click on the Execution button up here.
And we'll give our execution a name and select a GPU.
We're using a more performant GPU,
so our model will train even faster than it did in Colab.
Next, we select the TensorFlow 2.7 GPU image.
This container comes pre-installed with TensorFlow
and many other data science libraries.
And we don't have to worry about installing GPU drivers.
Then we launch the execution.
Back in the console, we can track
the progress of our training job in the Executions tab.
And this will take some time to complete.
But once the training is finished,
we'll be able to see the saved model artifacts that we stored
in our cloud storage bucket.
Of course, machine learning is not just about training models.
Now that we have a trained model,
the next step is to deploy it so that we can use this model
to get predictions.
Back on our notebook, I'm going to paste in
this additional cell here, which will use the Vertex AI Python
SDK to deploy the model we just trained.
Deploying the model to an endpoint associates
the save model artifacts with physical resources
for low latency predictions.
This will also take a few minutes.
But when the deployment finishes,
we can see our model and our endpoint
over here in the console.
So our last step is to get predictions
with our newly deployed model.
I've gone ahead and uploaded some test images to this folder
here in our notebook instance.
And I have this notebook that I've also
uploaded that I wrote called Predict, which
will do a little pre-processing on these images,
and then make a prediction request to our endpoint.
So we can use the Vertex AI Python SDK again
to get predictions for these images.
So let's see what we've got here.
Here's the image.
And it's generating the prediction.
And we can execute a few more of these cells
and just see what happens.
Now, I thought it would be interesting to actually test
out an image of my own.
I've recently been working on starting a vegetable
garden in my backyard.
And in addition to dealing with squirrels and butterflies
and rabbits that keep trying to eat up my tomatoes
and blackberries, I have to figure out
how to get rid of the weeds that keep growing in my beds.
And there's a plant that keeps popping up.
And I wanted to know what it was.
So I went ahead and uploaded an image from my garden
as well, just to see if we could identify it.
And it looks like I might have a species called rubber
vine appearing in my garden.
The steps we took today were to create a Vertex AI Workbench
managed notebook, upload our .ipynb file,
launch a notebook execution, deploy the model, and lastly,
get predictions from that model.
With this framework in mind, I hope
you start thinking about how you can
build your own machine learning applications with Vertex AI.
There are tons of other features in Vertex AI to explore.
And if you want some more hands-on experience,
be sure to check out these code labs.
If you build something interesting with Vertex
AI and notebooks, definitely let me know.
Thanks for watching.
[MUSIC PLAYING]