Amazon SageMaker Studio Lab for Beginners | by Karthik Bharathy | Towards Data Science

Machine Learning is pervasive. Pick any article or industrial application today, and you will read or use ML. The easiest way to learn and experiment with ML is by using a Jupyter notebook. We use a notebook to write code in the programming language of our choice, annotate with text, and play with data. Whenever I learn a new topic, I find myself jotting down rough notes in Evernote, Quip, or Notes. I then switch to a Word or a PDF document to draft the final version. Dabbling with ML should be no different. You want to start quick and experiment with ML.
Amazon SageMaker Studio Lab is a no-charge, no-setup notebook environment designed for learning and experimenting with ML. If you are familiar with Jupyter notebooks, you will feel right at home using SageMaker Studio Lab. The best part is you do not need to set up an AWS account or use a credit card. To get started, just sign-up for an account using your email.
I use Amazon SageMaker Studio Lab to experiment with ML quickly and easily. Whether you are a student learning ML courses or an expert data scientist running many TensorFlow models, you need a tool to experiment with ML.
(Note: I am a product manager in the Amazon SageMaker team and not a Data Scientist or an ML engineer by profession. I learn, breathe, and build all things ML just like you every day. However, you should take my opinions with a grain of salt 😊)
It is simplicity and peace of mind. SageMaker Studio Lab lets you spend more time learning ML rather than setting up the infrastructure for data science. If you wonder how this is different from setting up your own Jupyter notebook on your laptop or using other free notebook solutions, read on.
Building an environment from the ground up and managing it is painful. You do not want to spend time setting up a data science environment.
You do not want to sign up for a cloud provider and spend money on compute to experiment with ML.
You want an environment that saves your results even if you log off your account and does not force you to start from the beginning each time you log in.
You want to connect to services like GitHub and Kaggle from the environment quickly to prototype.
First, you need to request a new account from the SageMaker Studio Lab website. You are added to a waitlist, and once your account is approved, you can sign in.
Once you sign in, you will see the Project interface with options to start the runtime, Open project, and run samples. Choose Open Project. You will be taken to your project where you can create notebooks, terminals, source code, Markdown files and many more.
Let us open a Terminal (File > New > Terminal or choose the Terminal button) to check the configuration.
Let us check the CPU information. If you choose the compute type as CPU for your runtime, run the following command.
From the project landing page, you can switch from CPU to GPU (or vice-versa) by toggling the compute type, and the runtime will restart with the new hardware. After you choose compute type as GPU come back to the terminal window and run the following command.
Next, let us check the memory information.
You can also check the JupyterLab version with the following command.
We are running JupyterLab 3.2.4, which as of this writing, is the latest version of JupyterLab open-source package. Now that we have seen the basic hardware and software configuration, it is time to create and run an ML model.
We will build an image classification ML model using SageMaker Studio Lab running PyTorch. There are three steps to create a model:
1. Open a notebook from GitHub
2. Connect to Kaggle dataset
3. Train the image classification model using PyTorch
The quickest way to experiment is to start with a notebook from GitHub. You can use the weather data image classification notebook and tweak it to our needs. Alternatively, I have posted the notebook in GitHub with the natural images dataset we will be using in this experiment.
If you are logged into SageMaker Studio Lab, open the above URL. Choose Copy to project, and the sample will load in your runtime. Make sure your runtime is running with compute type as CPU.
We will use the natural images data set from Kaggle with 6.8K images across eight different classes. To download and use the data set, you will need a Kaggle account.
From your account page in Kaggle, create a new API token as shown below. Your browser will download the kaggle.json file, which contains the credentials to connect to Kaggle via the command line.
Upload this JSON file into your notebook directory. Then run the following commands in the notebook to install the Kaggle library and use your credentials.
Navigate to the Natural Images data set page and copy the API command to download the images.
Add a new cell in the notebook and paste the API command. Once the download completes, you can unzip the file and rename it to use with the rest of the sample.
We will update the sample to refer to the correct image classes in the data set.
Under dataset normalization, use the following dataset classes
dataset_classes = ['airplane', 'car', 'cat', 'dog', 'flower', 'fruit', 'motorbike', 'person']
Next, run through the cells which will normalize the data, load a DenseNet model to classify images, and fine-tune the model with the sample data. Training the model for me took less than 10 minutes.
Once the model training is complete, you can visualize the classification results for a subset of the images.
You can also view the confusion matrix for the model. Remember to update the number of classes to eight for this sample.
You can now publish your notebook on GitHub for others to use with SageMaker Studio Lab. You can also add the 1-click launch button to load your notebook by adding a code snippet in Markdown or HTML.
Replace the notebook URL above with the link to your notebook on GitHub.
We looked at the capabilities of SageMaker Studio Lab available in preview today. Specifically we looked at how you open notebooks in GitHub and connect to Kaggle datasets. If you want to dive into deep learning, you can use several Jupyter notebook examples here. If you have questions on SageMaker Studio Lab or feedback on the above experiment, feel free to drop a comment or reach #amazon-sagemaker on Stack Overflow.
[1] Andrew Ng, Stanford Machine Learning course, Coursera
[2] Andrew Ng, Deep Learning specialization, DeepLearning.AI
[3] Zhang, Aston and Lipton, Zachary C. and Li, Mu and Smola, Alexander J., Dive into Deep Learning (2021)
[4] AWS Machine Learning University (2021)
[5] Daniel Bourke, ‘I want to learn Artificial Intelligence and Machine Learning. Where can I start?’ (2018), Towards Data Science
Thanks for reading!