<jupyter_start><jupyter_text>&uarr;   Ensure Kernel is set to  &uarr;   conda_python3    PyTorch Estimator Bring your own ScriptIn this notebook we will go through and run a PyTorch model to classify the junctions as priority, signal and roundabout as seen in data prep.The outline of this notebook is 1. to prepare a training script (provided).2. use the AWS provided PyTorch container and provide our script to it.3. Run training.4. deploy model to end point.5. Test using an image in couple of possible ways  Upgrade Sagemaker so we can access the latest containers<jupyter_code>!pip install -U sagemaker<jupyter_output><empty_output><jupyter_text>Next we will import the libraries and set up the initial variables we will be using in this lab<jupyter_code>import os
import sagemaker
import numpy as np
from sagemaker.pytorch import PyTorch

ON_SAGEMAKER_NOTEBOOK = True

sagemaker_session = sagemaker.Session()
if ON_SAGEMAKER_NOTEBOOK:
    role = sagemaker.get_execution_role()
else:
    role = "[YOUR ROLE]"

import boto3
client = boto3.client('sagemaker-runtime')<jupyter_output><empty_output><jupyter_text>In the cell below, replace **"your-unique-bucket-name"** with the name of bucket you created in the data-prep notebook<jupyter_code>bucket = "your-unique-bucket-name"
# key = "data-folder"   (in case you structure your data as your-bucket/data-folder) 
training_data_uri="s3://{}".format(bucket)<jupyter_output><empty_output><jupyter_text>PyTorch EstimatorUse AWS provided open source containers, these containers can be extended by starting with the image provided by AWS and the add additional installs in dockerfileor you can use requirements.txt in source_dir to install additional libraries.Below code is for PyTorch<jupyter_code>estimator = PyTorch(entry_point='ptModelCode.py',
                    role=role,
                    framework_version='1.8',
                    instance_count=1,
                    instance_type='ml.m5.12xlarge',
                    py_version='py3',
                    # available hyperparameters: emsize, nhid, nlayers, lr, clip, epochs, batch_size,
                    #                            bptt, dropout, tied, seed, log_interval
                    )<jupyter_output><empty_output><jupyter_text>Now we call the estimators fit method with the URI location of the training data to start the training **Note:** This cell takes approximately **20 mins** to run<jupyter_code>%%time
estimator.fit(training_data_uri)<jupyter_output><empty_output><jupyter_text>We can call the model_data method on the estimator to find the location of the trained model artifacts<jupyter_code>estimator.model_data<jupyter_output><empty_output><jupyter_text>Deploying a modelOnce trained, deploying a model is a simple call.**Note:** Replace the **'your_model_uri'** with the URI from the cell above<jupyter_code>from sagemaker.pytorch import PyTorchModel
pytorch_model = PyTorchModel(model_data='your_model_uri', 
                             role=role, 
                             entry_point='ptInfCode.py', 
                             framework_version='1.7',
                             py_version='py3')
predictor = pytorch_model.deploy(instance_type='ml.m5.4xlarge', initial_instance_count=1)<jupyter_output><empty_output><jupyter_text>Now lets get the endpoint name from predictor<jupyter_code>print(predictor.endpoint_name)<jupyter_output><empty_output><jupyter_text>Now that our endpoint is up and running, lets test it with an image and see how well it doesIn the cell below, replace the **'your_endpoint_name'** with the your endpoint name you had printed out<jupyter_code>%%time

im_name="../data/test/Signal/S2.png"

response = client.invoke_endpoint(
    EndpointName='pytorch-inference-2022-05-11-21-38-35-889',
    ContentType='application/x-image',
    Body=open(im_name, 'rb').read())<jupyter_output><empty_output><jupyter_text>Now let us view the JSON response<jupyter_code>import json
json.loads(response['Body'].read().decode("utf-8"))<jupyter_output><empty_output><jupyter_text>Attach to a training job that has been left to run If your kernel becomes disconnected and your training has already started, you can reattach to the training job.Simply look up the training job name and replace the **your-training-job-name** and then run the cell below. Once the training job is finished, you can continue the cells after the training cell<jupyter_code>import sagemaker
import boto3

sess = sagemaker.Session()

training_job_name = 'your-training-job-name'

if training_job_name != 'your-training-job-name':
    estimator = sagemaker.estimator.Estimator.attach(training_job_name=training_job_name, sagemaker_session=sess)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08167161646160373, 'fraction_numerical': 0.009333899024183284, 'mean_word_length': 4.345804988662132, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '231354', 'license': 'Apache-2.0', 'n_tokens_mistral': 1442, 'n_tokens_neox': 1391, 'n_words': 430, 'path': 'pytorch/PyTorch-SM.ipynb', 'repo_name': 'GavinatorK/pssummitwkshp', 'size': '4714'}