Chapter 1: Introducing Amazon SageMaker
Machine learning (ML) practitioners use a large collection of tools in the course of their projects: open source libraries, deep learning frameworks, and more. In addition, they often have to write their own tools for automation and orchestration. Managing these tools and their underlying infrastructure is time-consuming and error-prone.
This is the very problem that Amazon SageMaker was designed to address (https://aws.amazon.com/sagemaker/). Amazon SageMaker is a fully managed service that helps you quickly build and deploy machine learning models. Whether you're just beginning with machine learning or you're an experienced practitioner, you'll find SageMaker features to improve the agility of your workflows, as well as the performance of your models. You'll be able to focus 100% on the machine learning problem at hand, without spending any time installing, managing, and scaling machine learning tools and infrastructure.
In this first chapter, we're going to learn what the main capabilities of SageMaker are, how they help solve pain points faced by machine learning practitioners, and how to set up SageMaker. This chapter will comprise the following topics:
- Exploring the capabilities of Amazon SageMaker
- Setting up Amazon SageMaker on your local machine
- Setting up Amazon SageMaker Studio
- Deploying one-click solutions and models with Amazon SageMaker JumpStart
Technical requirements
You will need an AWS account to run the examples included in this chapter. If you haven't got one already, please point your browser to https://aws.amazon.com/getting-started/ to learn about AWS and its core concepts, and to create an AWS account. You should also familiarize yourself with the AWS Free Tier (https://aws.amazon.com/free/), which lets you use many AWS services for free within certain usage limits.
You will need to install and configure the AWS CLI for your account (https://aws.amazon.com/cli/).
You will need a working Python 3.x environment. Installing the Anaconda distribution (https://www.anaconda.com/) is not mandatory but is strongly encouraged as it includes many projects that we will need (Jupyter, pandas
, numpy
, and more).
Code examples included in the book are available on GitHub at https://github.com/PacktPublishing/Learn-Amazon-SageMaker-second-edition. You will need to install a Git client to access them (https://git-scm.com/).
Exploring the capabilities of Amazon SageMaker
Amazon SageMaker was launched at AWS re:Invent 2017. Since then, a lot of new features have been added: you can see the full (and ever-growing) list at https://aws.amazon.com/about-aws/whats-new/machine-learning.
In this section, you'll learn about the main capabilities of Amazon SageMaker and its purpose. Don't worry, we'll dive deep into each of them in later chapters. We will also talk about the SageMaker Application Programming Interfaces (APIs), and the Software Development Kits (SDKs) that implement them.
The main capabilities of Amazon SageMaker
At the core of Amazon SageMaker is the ability to prepare, build, train, optimize, and deploy models on fully managed infrastructure at any scale. This lets you focus on studying and solving the machine learning problem at hand, instead of spending time and resources on building and managing infrastructure. Simply put, you can go from building to training to deploying more quickly. Let's zoom in on each step and highlight relevant SageMaker capabilities.
Preparing
Amazon SageMaker includes powerful tools to label and prepare datasets:
- Amazon SageMaker Ground Truth: Annotate datasets at any scale. Workflows for popular use cases are built in (image detection, entity extraction, and more), and you can implement your own. Annotation jobs can be distributed to workers that belong to private, third-party, or public workforces.
- Amazon SageMaker Processing: Run batch jobs for data processing (and other tasks such as model evaluation) using your own code written with scikit-learn or Spark.
- Amazon SageMaker Data Wrangler: Using a graphical interface, apply hundreds of built-in transforms (or your own) to tabular datasets, and export them in one click to a Jupyter notebook.
- Amazon SageMaker Feature Store: Store your engineered features offline in Amazon S3 to build datasets, or online to use them at prediction time.
- Amazon SageMaker Clarify: Using a variety of statistical metrics, analyze potential bias present in your datasets and models, and explain how your models predict.
Building
Amazon SageMaker provides you with two development environments:
- Notebook instances: Fully managed Amazon EC2 instances that come preinstalled with the most popular tools and libraries: Jupyter, Anaconda, and so on.
- Amazon SageMaker Studio: An end-to-end integrated development environment for machine learning projects, providing an intuitive graphical interface for many SageMaker capabilities. Studio is now the preferred way to run notebooks, and we recommend that you use it instead of notebook instances.
When it comes to experimenting with algorithms, you can choose from the following:
- A collection of 17 built-in algorithms for machine learning and deep learning, already implemented and optimized to run efficiently on AWS. No Machine learning code to write!
- A collection of built-in, open source frameworks (TensorFlow, PyTorch, Apache MXNet, scikit-learn, and more), where you simply bring your own code.
- Your own code running in your own container: custom Python, R, C++, Java, and so on.
- Algorithms and pre-trained models from AWS Marketplace for machine learning (https://aws.amazon.com/marketplace/solutions/machine-learning).
- Machine learning solutions and state-of-the-art models available in one click in Amazon SageMaker JumpStart.
In addition, Amazon SageMaker Autopilot uses AutoMachine learning to automatically build, train, and optimize models without the need to write a single line of Machine learning code.
Training
As mentioned earlier, Amazon SageMaker takes care of provisioning and managing your training infrastructure. You'll never spend any time managing servers, and you'll be able to focus on machine learning instead. On top of this, SageMaker brings advanced capabilities such as the following:
- Managed storage using either Amazon S3, Amazon EFS, or Amazon FSx for Lustre depending on your performance requirements.
- Managed spot training, using Amazon EC2 Spot instances for training in order to reduce costs by up to 80%.
- Distributed training automatically distributes large-scale training jobs on a cluster of managed instances, using advanced techniques such as data parallelism and model parallelism.
- Pipe mode streams infinitely large datasets from Amazon S3 to the training instances, saving the need to copy data around.
- Automatic model tuning runs hyperparameter optimization to deliver high-accuracy models more quickly.
- Amazon SageMaker Experiments easily tracks, organizes, and compares all your SageMaker jobs.
- Amazon SageMaker Debugger captures the internal model state during training, inspects it to observe how the model learns, detects unwanted conditions that hurt accuracy, and profiles the performance of your training job.
Deploying
Just as with training, Amazon SageMaker takes care of all your deployment infrastructure, and brings a slew of additional features:
- Real-time endpoints create an HTTPS API that serves predictions from your model. As you would expect, autoscaling is available.
- Batch transform uses a model to predict data in batch mode.
- Amazon Elastic Inference adds fractional GPU acceleration to CPU-based endpoints to find the best cost/performance ratio for your prediction infrastructure.
- Amazon SageMaker Model Monitor captures data sent to an endpoint and compares it with a baseline to identify and alert on data quality issues (missing features, data drift, and more).
- Amazon SageMaker Neo compiles models for a specific hardware architecture, including embedded platforms, and deploys an optimized version using a lightweight runtime.
- Amazon SageMaker Edge Manager helps you deploy and manage your models on edge devices.
- Last but not least, Amazon SageMaker Pipelines lets you build end-to-end automated pipelines to run and manage your data preparation, training, and deployment workloads.
The Amazon SageMaker API
Just like all other AWS services, Amazon SageMaker is driven by APIs that are implemented in the language SDKs supported by AWS (https://aws.amazon.com/tools/). In addition, a dedicated Python SDK, aka the SageMaker SDK is also available. Let's look at both, and discuss their respective benefits.
The AWS language SDKs
Language SDKs implement service-specific APIs for all AWS services: S3, EC2, and so on. Of course, they also include SageMaker APIs, which are documented here: https://docs.aws.amazon.com/sagemaker/latest/dg/api-and-sdk-reference.htmachine learning.
When it comes to data science and machine learning, Python is the most popular language, so let's take a look at the SageMaker APIs available in boto3
, the AWS SDK for the Python language (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.htmachine learning). These APIs are quite low-level and verbose: for example, create_training_job()
has a lot of JSON parameters that don't look very obvious. You can see some of them in the next screenshot. You may think that this doesn't look very appealing for everyday Machine learning experimentation… and I would totally agree!

Figure 1.1 – A (partial) view of the create_training_job() API in boto3
Indeed, these service-level APIs are not meant to be used for experimentation in notebooks. Their purpose is automation, through either bespoke scripts or Infrastructure as Code tools such as AWS CloudFormation (https://aws.amazon.com/cloudformation) and Terraform (https://terraform.io). Your DevOps team will use them to manage production, where they do need full control over each possible parameter.
So, what should you use for experimentation? You should use the Amazon SageMaker SDK.
The Amazon SageMaker SDK
The Amazon SageMaker SDK (https://github.com/aws/sagemaker-python-sdk) is a Python SDK specific to Amazon SageMaker. You can find its documentation at https://sagemaker.readthedocs.io/en/stable/.
Note
Every effort has been made to check the code examples in this book with the latest SageMaker SDK (v2.58.0 at the time of writing).
Here, the abstraction level is much higher: the SDK contains objects for models, estimators, models, predictors, and so on. We're definitely back in Machine learning territory.
For instance, this SDK makes it extremely easy and comfortable to fire up a training job (one line of code) and to deploy a model (one line of code). Infrastructure concerns are abstracted away, and we can focus on Machine learning instead. Here's an example. Don't worry about the details for now:
# Configure the training job my_estimator = TensorFlow( entry_point='my_script.py', role=my_sagemaker_role, train_instance_type='machine learning.p3.2xlarge', instance_count=1, framework_version='2.1.0') # Train the model my_estimator.fit('s3://my_bucket/my_training_data/') # Deploy the model to an HTTPS endpoint my_predictor = my_estimator.deploy( initial_instance_count=1, instance_type='machine learning.c5.2xlarge')
Now that we know a little more about Amazon SageMaker, let's see how we can set it up.
Setting up Amazon SageMaker on your local machine
A common misconception is that you can't use SageMaker outside of the AWS cloud. Obviously, it is a cloud-based service, and its most appealing capabilities require cloud infrastructure to run. However, many developers like to set up their development environment their own way, and SageMaker lets them do that: in this section, you will learn how to install the SageMaker SDK on your local machine or on a local server. In later chapters, you'll learn how to train and deploy models locally.
It's good practice to isolate Python environments in order to avoid dependency hell. Let's see how we can achieve this using two popular projects: virtualenv
(https://virtualenv.pypa.io) and Anaconda (https://www.anaconda.com/).
Installing the SageMaker SDK with virtualenv
If you've never worked with virtualenv
before, please read this tutorial before proceeding: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/:
- First, let's create a new environment named
sagemaker
and activate it:$ mkdir workdir $ cd workdir $ python3 -m venv sagemaker $ source sagemaker/bin/activate
- Now, let's install
boto3
, the SageMaker SDK, and thepandas
library (https://pandas.pydata.org/), which is also required:$ pip3 install boto3 sagemaker pandas
- Now, let's quickly check that we can import these SDKs into Python:
$ python3 Python 3.9.5 (default, May 4 2021, 03:29:30) >>> import boto3 >>> import sagemaker >>> print(boto3.__version__) 1.17.70 >>> print(sagemaker.__version__) 2.39.1 >>> exit()
The installation looks fine. Your own versions will certainly be newer and that's fine. Now, let's run a quick test with a local Jupyter server (https://jupyter.org/). If Jupyter isn't installed on your machine, you can find instructions at https://jupyter.org/install:
- First, let's create a Jupyter kernel based on our virtual environment:
$ pip3 install jupyter ipykernel $ python3 -m ipykernel install --user --name=sagemaker
- Then, we can launch Jupyter:
$ jupyter notebook
- Creating a new notebook, we can see that the
sagemaker
kernel is available, so let's select it in the New menu, as seen in the following screenshot:Figure 1.2 – Creating a new notebook
- Finally, we can check that the SDKs are available by importing them and printing their version, as shown in the following screenshot:

Figure 1.3 – Checking the SDK version
This completes the installation with virtualenv
. Don't forget to terminate Jupyter, and to deactivate your virtualenv
:
$ deactivate
You can also install the SDK using Anaconda.
Installing the SageMaker SDK with Anaconda
Anaconda includes a package manager named conda
that lets you create and manage isolated environments. If you've never worked with conda
before, you should do the following:
- Install Anaconda: https://docs.anaconda.com/anaconda/install/.
- Read this tutorial: https://docs.conda.io/projects/conda/en/latest/user-guide/getting-started.htmachine learning.
We will get started using the following steps:
- Let's create and activate a new
conda
environment namedconda-sagemaker
:$ conda create -y -n conda-sagemaker $ conda activate conda-sagemaker
- Then, we install
pandas
,boto3
, and the SageMaker SDK. The latter has to be installed withpip
as it's not available as aconda
package:$ conda install -y boto3 pandas $ pip3 install sagemaker
- Now, let's add Jupyter and its dependencies to the environment, and create a new kernel:
$ conda install -y jupyter ipykernel $ python3 -m ipykernel install --user --name conda-sagemaker
- Then, we can launch Jupyter:
$ jupyter notebook
Check that the
conda-sagemaker
kernel is present in the New menu, as is visible in the following screenshot:Figure 1.4 – Creating a new conda environment
- Just like in the previous section, we can create a notebook using this kernel and check that the SDKs are imported correctly.
This completes the installation with conda
. Whether you'd rather use it instead of virtualenv
is largely a matter of personal preference. You can definitely run all notebooks in this book and build your own projects with one or the other.
A word about AWS permissions
Amazon Identity and Access Management (IAM) enables you to manage access to AWS services and resources securely (https://aws.amazon.com/iam). Of course, this applies to Amazon SageMaker as well, and you need to make sure that your AWS user has sufficient permissions to invoke the SageMaker API.
IAM permissions
If you're not familiar with IAM at all, please read the following documentation:
https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.htmachine learning
You can run a quick test by using the AWS CLI on one of the SageMaker APIs, for example, list-endpoints
. I'm using the eu-west-1
region here, but feel free to use the region that is nearest to you:
$ aws sagemaker list-endpoints --region eu-west-1 { "Endpoints": [] }
If you get an error message complaining about insufficient permissions, you need to update the IAM role attached to your AWS user.
If you own the AWS account in question, you can easily do this yourself in the IAM console by adding the AmazonSageMakerFullAccess
managed policy to your role. Note that this policy is extremely permissive: this is fine for a development account, but certainly not for a production account.
If you work with an account where you don't have administrative rights (such as a company-provided account), please contact your IT administrator to add SageMaker permissions to your AWS user.
For more information on SageMaker permissions, please refer to the documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/security-iam.htmachine learning.
Setting up Amazon SageMaker Studio
Experimentation is a key part of the Machine learning process. Developers and data scientists use a collection of open source tools and libraries for data exploration, data processing, and, of course, to evaluate candidate algorithms. Installing and maintaining these tools takes a fair amount of time, which would probably be better spent on studying the Machine learning problem itself!
Amazon SageMaker Studio brings you the machine learning tools you need from experimentation to production. At its core is an integrated development environment based on Jupyter that makes it instantly familiar.
In addition, SageMaker Studio is integrated with other SageMaker capabilities, such as SageMaker Experiments to track and compare all jobs, SageMaker Autopilot to automatically create machine learning models, and more. A lot of operations can be achieved in just a few clicks, without having to write any code.
SageMaker Studio also further simplifies infrastructure management. You won't have to create notebook instances: SageMaker Studio provides you with compute environments that are readily available to run your notebooks.
Note
This section requires basic knowledge of Amazon S3, Amazon VPC, and Amazon IAM. If you're not familiar with them at all, please read the following documentation:
https://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.htmachine learning
https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.htmachine learning
https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.htmachine learning
Now would also probably be a good time to take a look at (and bookmark) the SageMaker pricing page: https://aws.amazon.com/sagemaker/pricing/.
Onboarding to Amazon SageMaker Studio
You can access SageMaker Studio using any of these three options:
- Use the quick start procedure: This is the easiest option for individual accounts, and we'll walk through it in the following paragraphs.
- Use AWS Single Sign-On (SSO): If your company has an SSO application set up, this is probably the best option. You can learn more about SSO onboarding at https://docs.aws.amazon.com/sagemaker/latest/dg/onboard-sso-users.htmachine learning. Please contact your IT administrator for details.
- Use Amazon IAM: If your company doesn't use SSO, this is probably the best option. You can learn more about SSO onboarding at https://docs.aws.amazon.com/sagemaker/latest/dg/onboard-iam.htmachine learning. Again, please contact your IT administrator for details.
Onboarding with the quick start procedure
There are several steps to the quick start procedure:
- First, open the AWS Console in one of the regions where Amazon SageMaker Studio is available, for example, https://us-east-2.console.aws.amazon.com/sagemaker/.
- As shown in the following screenshot, the left-hand vertical panel has a link to SageMaker Studio:
Figure 1.5 – Opening SageMaker Studio
- Clicking on this link opens the onboarding screen, and you can see its first section in the next screenshot:
Figure 1.6 – Running Quick start
- Let's select Quick start. Then, we enter the username we'd like to use to log in to SageMaker Studio, and we create a new IAM role as shown in the preceding screenshot. This opens the following screen:
Figure 1.7 – Creating an IAM role
The only decision we have to make here is whether we want to allow our notebook instance to access specific Amazon S3 buckets. Let's select Any S3 bucket and click on Create role. This is the most flexible setting for development and testing, but we'd want to apply much stricter settings for production. Of course, we can edit this role later on in the IAM console, or create a new one.
- Once we've clicked on Create role, we're back to the previous screen. Please make sure that project templates and JumpStart are enabled for this account. (this should be the default setting).
- We just have to click on Submit to launch the onboarding procedure. Depending on your account setup, you may get an extra screen asking you to select a VPC and a subnet. I'd recommend selecting any subnet in your default VPC.
- A few minutes later, SageMaker Studio is in service, as shown in the following screenshot. We could add extra users if we needed to, but for now, let's just click on Open Studio:
Figure 1.8 – Launching SageMaker Studio
Don't worry if this takes a few more minutes, as SageMaker Studio needs to complete the first-run setup of your environment. As shown in the following screenshot, once we open SageMaker Studio, we see the familiar JupyterLab layout:
Note
SageMaker Studio is a living thing. By the time you're reading this, some screens may have been updated. Also, you may notice small differences from one region to the next, as some features or instance types are not available there.
Figure 1.9 – SageMaker Studio welcome screen
- We can immediately create our first notebook. In the Launcher tab, in the Notebooks and compute resources section, let's select Data Science, and click on Notebook – Python 3.
- This opens a notebook, as is visible in the following screenshot. We first check that SDKs are readily available. As this is the first time we are launching the Data Science kernel, we need to wait for a couple of minutes.
Figure 1.10 – Checking the SDK version
- As is visible in the following screenshot, we can easily list resources that are currently running in our Studio instance: an machine learning.t3.medium instance, the data science image supporting the kernel used in our notebook, and the notebook itself:
Figure 1.11 – Viewing Studio resources
- To avoid unnecessary costs, we should shut these resources down when we're done working with them. For example, we can shut down the instance and all resources running on it, as you can see in the following screenshot. Don't do it now, we'll need the instance to run the next examples!
Figure 1.12 – Shutting down an instance
- Machine learning.t3.medium is the default instance size that Studio uses. You can switch to other instance types by clicking on 2 vCPU + 4 GiB at the top of your notebook. This lets you select a new instance size and launch it in Studio. After a few minutes, the instance is up and your notebook code has been migrated automatically. Don't forget to shut down the previous instance, as explained earlier.
- When we're done working with SageMaker Studio, all we have to do is close the browser tab. If we want to resume working, we just have to go back to the SageMaker console and click on Open Studio.
- If we wanted to shut down the Studio instance itself, we'd simply select Shut Down in the File menu. All files would still be preserved until we deleted Studio completely in the SageMaker console.
Now that we've completed the setup, I'm sure you're impatient to get started with machine learning. Let's start deploying some models!
Deploying one-click solutions and models with Amazon SageMaker JumpStart
If you're new to machine learning, you may find it difficult to get started with real-life projects. You've run all the toy examples, and you've read several blog posts on the state of the models for COMPUTER VISION OR NATURAL LANGUAGE PROCESSING. Now what? How can you start using these complex models on your own data to solve your own business problems?
Even if you're an experienced practitioner, building end-to-end machine learning solutions is not an easy task. Training and deploying models is just part of the equation: what about data preparation, automation, and so on?
Amazon SageMaker JumpStart was specifically built to help everyone get started more quickly with their machine learning projects. In literally one click, you can deploy the following:
- 16 end-to-end solutions for real-life business problems such as fraud detection in financial transactions, explaining credit decisions, predictive maintenance, and more
- Over 180 TensorFlow and PyTorch models pre-trained on a variety of computer vision and natural language processing tasks
- Additional learning resources, such as sample notebooks, blog posts, and video tutorials
Time to deploy a solution.
Deploying a solution
Let's begin:
- Starting from the icon bar on the left, we open JumpStart. The following screenshot shows the opening screen:
Figure 1.13 – Viewing solutions in JumpStart
- Select Fraud Detection in Financial Transactions. As can be seen in the following screenshot, this is a fascinating example that uses graph data and graph neural networks to predict fraudulent activities based on interactions:
Figure 1.14 – Viewing solution details
- Once we've read the solution details, all we have to do is click on the Launch button. This will run an AWS CloudFormation template in charge of building all the AWS resources required by the solution.
CloudFormation
If you're curious about CloudFormation, you may find this introduction useful: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.htmachine learning.
- A few minutes later, the solution is ready, as can be seen in the following screenshot. We click on Open Notebook to open the first notebook.
Figure 1.15 – Opening a solution
- As you can see in the following screenshot, we can browse solution files in the left-hand pane: notebooks, training code, and so on:
Figure 1.16 – Viewing solution files
- From then on, you can start running and tweaking the notebook. If you're not familiar with the SageMaker SDK yet, don't worry about the details.
- Once you're done, please go back to the solution page and click on Delete all resources to clean up and avoid unnecessary costs, as shown in the following screenshot:

Figure 1.17 – Deleting a solution
As you can see, JumpStart solutions are a great way to explore how to solve business problems with machine learning and to start thinking about how you could do the same in your own business environment.
Now, let's see how we can deploy pre-trained models.
Deploying a model
JumpStart includes over 180 TensorFlow and PyTorch models pre-trained on a variety of computer vision and natural language processing tasks. Let's take a look at computer vision models:
- Starting from the JumpStart main screen, we open Vision models, as can be seen in the following screenshot:
Figure 1.18 – Viewing computer vision models
- Let's say that we're interested in trying out object detection models based on the Single Shot Detector (SSD) architecture. We click on the SSD model from the PyTorch Hub (the fourth one from the left).
- This opens the model details page, telling us where the model comes from, what dataset it has been trained on, and which labels it can predict. We can also select which instance type to deploy the model. Sticking to the default, we click on Deploy to deploy the model on a real-time endpoint, as shown in the following screenshot:
Figure 1.19 – Deploying a JumpStart model
- A few minutes later, the model has been deployed. As can be seen in the following screenshot, we can see the endpoint status in the left-hand panel, and we simply click on Open Notebook to test it.
Figure 1.20 – Opening a JumpStart notebook
- Clicking through the notebook cells, we download a test image and we predict which objects it contains. Bounding boxes, classes, and probabilities are visible in the following screenshot:
Figure 1.21 – Detecting objects in a picture
- When you're done, please make sure to delete the endpoint to avoid unnecessary charges: simply click on Delete in the endpoint details screen visible in Figure 1.20.
Not only does JumpStart make it extremely easy to experiment with state-of-the-art models, but it also provides you with code that you can readily use in your own projects: loading an image for prediction, predicting with an endpoint, plotting results, and so on.
As useful as pre-trained models are, we often need to fine-tune them on our own datasets. Let's see how we can do that with JumpStart.
Fine-tuning a model
Let's use an image classification model this time:
Note
A word of warning about fine-tuning text models: complex models such as BERT can take a very long time to fine-tune, sometimes several hours per epoch on a single GPU. In addition to the long waiting time, the cost won't be negligible, so I'd recommend avoiding these examples unless you have a real-life business project to work on.
- We select the Resnet 18 model (the second from the left in Figure 1.18).
- On the model details page, we see that this model can be fine-tuned either on a default dataset available for testing (a TensorFlow dataset with five flower classes) or on our own dataset stored in S3. Scrolling down, we learn about the format that our dataset should have.
- As visible in the following figure we stick to the default dataset. We also leave the deployment configuration and training parameters unchanged. Then, we click on Train to launch the fine-tuning job.
Figure 1.22 – Fine-tuning a model
- After just a few minutes, fine-tuning is complete (which is why I picked this example!). We can see the output path in S3 where the fine-tuned model has been stored. Let's write down that path; we're going to need it in a minute.
Figure 1.23 – Viewing fine-tuning results
- Then, we click on Deploy just like in the previous example. Once the model has been deployed, we open the sample notebook showing us how to predict with the initial pre-trained model.
- This notebook uses images from the original dataset that the model was pre-trained on. No problem, let's adapt it! Even if we're not yet familiar with the SageMaker SDK, the notebook is simple enough that we can understand what's going on, and add a few cells to predict a flower image with our fine-tuned model.
- First, we add a cell to copy the fine-tuned model artifact from S3, and we extract the list of classes and class indexes that JumpStart added:
%%sh aws s3 cp s3://sagemaker-REGION_NAME-123456789012/smjs-d-pt-ic-resnet18-20210511-142657/output/model.tar.gz . tar xfz model.tar.gz cat class_label_to_prediction_index.json {"daisy": 0, "dandelion": 1, "roses": 2, "sunflowers": 3, "tulips": 4}
- As expected, the fine-tuned model can predict five classes. Let's add a cell to download a sunflower image from Wikipedia:
%%sh wget https://upload.wikimedia.org/wikipedia/commons/a/a9/A_sunflower.jpg
- Now, we load the image and invoke the endpoint:
import boto3 endpoint_name = 'jumpstart-ftd-pt-ic-resnet18' client = boto3.client('runtime.sagemaker') with open('A_sunflower.jpg', 'rb') as file: image = file.read() response = client.invoke_endpoint( EndpointName=endpoint_name, ContentType='application/x-image', Body=image)
- Finally, we print out the predictions. The highest probability is class #3 at 60.67%, confirming that our image contains a sunflower!
import json model_predictions = json.loads(response['Body'].read()) print(model_predictions) [0.30362239480018616, 0.06462913751602173, 0.007234351709485054, 0.6067869663238525, 0.017727158963680267]
- When you're done testing, please make sure to delete the endpoint to avoid unnecessary charges.
This example illustrates how easy it is to fine-tune pre-trained models on your own datasets with SageMaker JumpStart and to use them to predict your own data. This is a great way to experiment with different models and find out which one could work best on the particular problem you're trying to solve.
This is the end of the first chapter, and it was already quite action-packed, wasn't it? It's now time to review what we've learned.
Summary
In this chapter, you discovered the main capabilities of Amazon SageMaker, and how they can help solve your machine learning pain points. By providing you with managed infrastructure and pre-installed tools, SageMaker lets you focus on the machine learning problem itself. Thus, you can go more quickly from experimenting with models to deploying them in production.
Then, you learned how to set up Amazon SageMaker on your local machine and in Amazon SageMaker Studio. The latter is a managed machine learning IDE where many other SageMaker capabilities are just a few clicks away.
Finally, you learned about Amazon SageMaker JumpStart, a collection of machine learning solutions and state-of-the-art models that you can deploy in one click, and start testing in minutes.
In the next chapter, we'll see how you can use Amazon SageMaker and other AWS services to prepare your datasets for training.