# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: conda_mxnet_p27
#     language: python
#     name: conda_mxnet_p27
# ---

# # Gluon CIFAR-10 Trained in Local Mode
# _**ResNet model in Gluon trained locally in a notebook instance**_
#
# ---
#
# ---
#
# _This notebook was created and tested on an ml.p3.8xlarge notebook instance._
#
# ## Setup
#
# Import libraries and set IAM role ARN.

# +
import sagemaker
from sagemaker.mxnet import MXNet

sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()
# -

# Install pre-requisites for local training.

# !/bin/bash setup.sh

# ---
#
# ## Data
#
# We use the helper scripts to download CIFAR-10 training data and sample images.

from cifar10_utils import download_training_data
download_training_data()

# 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.
#
# Even though we are training within our notebook instance, we'll continue to use the S3 data location since it will allow us to easily transition to training in SageMaker's managed environment.

inputs = sagemaker_session.upload_data(path='data', key_prefix='data/DEMO-gluon-cifar10')
print('input spec (in this case, just an S3 path): {}'.format(inputs))

# ---
#
# ## Script
#
# We need to provide a training script that can run on the SageMaker platform. When SageMaker calls your function, it will pass in arguments that describe the training environment. Check the script below to see how this works.
#
# The network itself is a pre-built version contained in the [Gluon Model Zoo](https://mxnet.incubator.apache.org/versions/master/api/python/gluon/model_zoo.html).

# !cat 'cifar10.py'

# ---
#
# ## Train (Local Mode)
#
# The ```MXNet``` estimator will create our training job. To switch from training in SageMaker's managed environment to training within a notebook instance, just set `train_instance_type` to `local_gpu`.

m = MXNet('cifar10.py', 
          role=role, 
          train_instance_count=1, 
          train_instance_type='local_gpu',
          hyperparameters={'batch_size': 1024, 
                           'epochs': 50, 
                           'learning_rate': 0.1, 
                           'momentum': 0.9})

# After we've constructed our `MXNet` object, we can fit it using the data we uploaded to S3. SageMaker makes sure our data is available in the local filesystem, so our training script can simply read the data from disk.

m.fit(inputs)

# ---
#
# ## Host
#
# After training, we use the MXNet estimator object to deploy an endpoint. Because we trained locally, we'll also deploy the endpoint locally.  The predictor object returned by `deploy` lets us call the endpoint and perform inference on our sample images.

predictor = m.deploy(initial_instance_count=1, instance_type='local_gpu')

# ### Evaluate
#
# We'll use these CIFAR-10 sample images to test the service:
#
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/airplane1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/automobile1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/bird1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/cat1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/deer1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/dog1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/frog1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/horse1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/ship1.png" />
# <img style="display: inline; height: 32px; margin: 0.25em" src="images/truck1.png" />
#
#

# +
# load the CIFAR-10 samples, and convert them into format we can use with the prediction endpoint
from cifar10_utils import read_images

filenames = ['images/airplane1.png',
             'images/automobile1.png',
             'images/bird1.png',
             'images/cat1.png',
             'images/deer1.png',
             'images/dog1.png',
             'images/frog1.png',
             'images/horse1.png',
             'images/ship1.png',
             'images/truck1.png']

image_data = read_images(filenames)
# -

# The predictor runs inference on our input data and returns the predicted class label (as a float value, so we convert to int for display).

for i, img in enumerate(image_data):
    response = predictor.predict(img)
    print('image {}: class: {}'.format(i, int(response)))

# ---
#
# ## Cleanup
#
# After you have finished with this example, remember to delete the prediction endpoint.  Only one local endpoint can be running at a time.

m.delete_endpoint()

StarcoderdataJupyterScriptsDedupFiltered