{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# PyTorch CIFAR-10 local training \n", "\n", "## Prerequisites\n", "\n", "This notebook shows how to use the SageMaker Python SDK to run your code in a local container before deploying to SageMaker's managed training or hosting environments. This can speed up iterative testing and debugging while using the same familiar Python SDK interface. Just change your estimator's `instance_type` to `local` (or `local_gpu` if you're using an ml.p2 or ml.p3 notebook instance).\n", "\n", "In order to use this feature, you'll need to install docker-compose (and nvidia-docker if training with a GPU).\n", "\n", "**Note: you can only run a single local notebook at one time.**" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!/bin/bash ./setup.sh" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "The **SageMaker Python SDK** helps you deploy your models for training and hosting in optimized, productions ready containers in SageMaker. The SageMaker Python SDK is easy to use, modular, extensible and compatible with TensorFlow, MXNet, PyTorch. This tutorial focuses on how to create a convolutional neural network model to train the [CIFAR-10 dataset](https://www.cs.toronto.edu/~kriz/cifar.html) using **PyTorch in local mode**.\n", "\n", "### Set up the environment\n", "\n", "This notebook was created and tested on a single ml.p2.xlarge notebook instance.\n", "\n", "Let's start by specifying:\n", "\n", "- The S3 bucket and prefix that you want to use for training and model data. This should be within the same region as the Notebook Instance, training, and hosting.\n", "- The IAM role arn used to give training and hosting access to your data. See the documentation for how to create these. Note, if more than one role is required for notebook instances, training, and/or hosting, please replace the sagemaker.get_execution_role() with appropriate full IAM role arn string(s)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sagemaker\n", "\n", "sagemaker_session = sagemaker.Session()\n", "\n", "bucket = sagemaker_session.default_bucket()\n", "prefix = 'sagemaker/DEMO-pytorch-cnn-cifar10'\n", "\n", "role = sagemaker.get_execution_role()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "import subprocess\n", "\n", "instance_type = \"local\"\n", "\n", "try:\n", " if subprocess.call(\"nvidia-smi\") == 0:\n", " ## Set type to GPU if one is present\n", " instance_type = \"local_gpu\"\n", "except:\n", " pass\n", "\n", "print(\"Instance type = \" + instance_type)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download the CIFAR-10 dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from utils_cifar import get_train_data_loader, get_test_data_loader, imshow, classes\n", "\n", "trainloader = get_train_data_loader()\n", "testloader = get_test_data_loader()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data Preview" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import torchvision, torch\n", "\n", "# get some random training images\n", "dataiter = iter(trainloader)\n", "images, labels = dataiter.next()\n", "\n", "# show images\n", "imshow(torchvision.utils.make_grid(images))\n", "\n", "# print labels\n", "print(' '.join('%9s' % classes[labels[j]] for j in range(4)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Upload the data\n", "We use the ```sagemaker.Session.upload_data``` function to upload our datasets to an S3 location. The return value inputs identifies the location -- we will use this later when we start the training job." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "inputs = sagemaker_session.upload_data(path='data', bucket=bucket, key_prefix='data/cifar10')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Construct a script for training \n", "Here is the full code for the network model:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "!pygmentize source/cifar10.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Script Functions\n", "\n", "SageMaker invokes the main function defined within your training script for training. When deploying your trained model to an endpoint, the model_fn() is called to determine how to load your trained model. The model_fn() along with a few other functions list below are called to enable predictions on SageMaker.\n", "\n", "### [Predicting Functions](https://github.com/aws/sagemaker-pytorch-containers/blob/master/src/sagemaker_pytorch_container/serving.py)\n", "* model_fn(model_dir) - loads your model.\n", "* input_fn(serialized_input_data, content_type) - deserializes predictions to predict_fn.\n", "* output_fn(prediction_output, accept) - serializes predictions from predict_fn.\n", "* predict_fn(input_data, model) - calls a model on data deserialized in input_fn.\n", "\n", "The model_fn() is the only function that doesn't have a default implementation and is required by the user for using PyTorch on SageMaker. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a training job using the sagemaker.PyTorch estimator\n", "\n", "The `PyTorch` class allows us to run our training function on SageMaker. We need to configure it with our training script, an IAM role, the number of training instances, and the training instance type. For local training with GPU, we could set this to \"local_gpu\". In this case, `instance_type` was set above based on your whether you're running a GPU instance.\n", "\n", "After we've constructed our `PyTorch` object, we fit it using the data we uploaded to S3. Even though we're in local mode, using S3 as our data source makes sense because it maintains consistency with how SageMaker's distributed, managed training ingests data.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sagemaker.pytorch import PyTorch\n", "\n", "cifar10_estimator = PyTorch(py_version='py3',\n", " entry_point='source/cifar10.py',\n", " role=role,\n", " framework_version='1.7.1',\n", " instance_count=1,\n", " instance_type=instance_type)\n", "\n", "cifar10_estimator.fit(inputs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Deploy the trained model to prepare for predictions\n", "\n", "The deploy() method creates an endpoint (in this case locally) which serves prediction requests in real-time." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sagemaker.pytorch import PyTorchModel\n", "\n", "cifar10_predictor = cifar10_estimator.deploy(initial_instance_count=1,\n", " instance_type=instance_type)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Invoking the endpoint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# get some test images\n", "dataiter = iter(testloader)\n", "images, labels = dataiter.next()\n", "\n", "# print images\n", "imshow(torchvision.utils.make_grid(images))\n", "print('GroundTruth: ', ' '.join('%4s' % classes[labels[j]] for j in range(4)))\n", "\n", "outputs = cifar10_predictor.predict(images.numpy())\n", "\n", "_, predicted = torch.max(torch.from_numpy(np.array(outputs)), 1)\n", "\n", "print('Predicted: ', ' '.join('%4s' % classes[predicted[j]]\n", " for j in range(4)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Clean-up\n", "\n", "Deleting the local endpoint when you're finished is important, since you can only run one local endpoint at a time." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cifar10_predictor.delete_endpoint()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_pytorch_p36", "language": "python", "name": "conda_pytorch_p36" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.13" }, "notice": "Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the \"License\"). You may not use this file except in compliance with the License. A copy of the License is located at http://aws.amazon.com/apache2.0/ or in the \"license\" file accompanying this file. This file is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." }, "nbformat": 4, "nbformat_minor": 2 }