Amazon SageMaker

Import your model directly from Amazon SageMaker

This guide walks through the process of importing your SageMaker model directly into your Modzy instance.

👍

Supported SageMaker Model Typessage

  • Factorization machines classification
  • Factorization machines regression
  • Image classification
  • Object detection
  • Semantic segmentation
  • XGBoost classification
  • XGBoost regression

:file-folder: Saving Model Artifacts

After training your model, save your model weights and any other inference-required dependencies along with a metadata file to the cloud storage provider of your choice.

Resources

The following metadata file (i.e., model.yaml file) must be included to import your SageMaker model.

specification: "0.3"
type: "file"
source: "Custom Model"
version: "0.0.1"
name: "Sample Model Name"
author: "Sample Model Author"
description:
  summary: "Short description of model."
  details: "Longer description of what the model does, what inputs it expects, and what outputs it returns."
  technical: |-
    #OVERVIEW:
    A continuation of "details" section that may include any information about model architecture, use cases, and any other general information.
    #TRAINING:
    Training-specific information, including links to the training dataset, a list of specific training techniques used, and any other development-specific information.
    #VALIDATION:
    Information about how the model was validated and any relevant dataset-specific information.
    #INPUT SPECIFICATION:
    Detailed description of expected inputs the model expects.
    #OUTPUT DETAILS:
    Detailed description of the output the model returns.
  performance: "Description about the model's performance on validation dataset. Include common machine learning evaluation metrics."
releaseNotes: "Short phrase describing new model version."

tags:[]
filters:
- type: "Task"
  label: "<task>"
- type: "Input Type"
  label: "<input data type>"
- type: "Subject"
  label: "<subject>"

metrics:
- label: "Probability"
  type: "percentage"
  value: 1.0
  description: |-
    Use case dependent
inputs:
  image:
    acceptedMediaTypes:
      - "image/jpeg"
      - "image/png"
      - "image/tiff"
    maxSize: 5M
    description: "Any jpg or png image for object detection"

outputs:
  results.json:
    mediaType: "application/json"
    maxSize: 128K
    description: "Classify objects in an image and give probabilities for each"

# replace size, cpu-count, and gpu-count values to reflect the resources your model needs to run
resources:
  memory:
    size: 6G
  cpu:
    count: 1
  gpu:
    count: 1

timeout:
  status: 20s # amount of time it will take your model to load and spin up
  run: 20s # amount of time it will take your model to complete inference 

# keep these values as are
internal:
  recommended: false
  experimental: false
  available: true
  active: true
  features:
    explainable: false
    adversarialDefense: false

After filling in this file with your model information, you have two choices:

  1. If you choose to import your model directly within Modzy's user interface, zip up this file as a tar archive named resources.tar.gz
  2. If you plan to use the SDK, there is a helper function that takes care of this for you.

Note: For all model types except Factorization machines and XGBoost regression, your resources.tar.gz archive must also include a labels.json file that contains a mapping between numerical classes and human readable labels. For example, if you trained a simple model that classifies dogs and cats, where the numerical class '0' maps to 'dog' and '1' maps to 'cat', your labels.json file would look like this:

["dog", "cat"]

Model Artifacts

After saving your model resources as resources.tar.gz, save the raw model weights output of your SageMaker experiment (model.tar.gz) in a tar archive named weights.tar.gz. This model.tar.gz file should be located in the s3 bucket named after the job name of your SageMaker training experiment. Do not modify this file - instead, simply rename it and identify a new (or existing) s3 bucket to save both the resources tar archive and weights tar archive.



:construction: Executing Importer Job

When your model artifacts are saved in the appropriate s3 bucket or other cloud storage blob, it is time to import your model to your Modzy instance. There are two methods for importing your model:

  1. Modzy's user interface
  2. Python SDK

User Interface

Navigate to the Models page within your Modzy account and select Add New Model at the top of the page.

Figure 1. Models PageFigure 1. Models Page

Figure 1. Models Page


On the Model Deployment landing page, select SageMaker in the No-Code Model Importer section.

Figure 2. Deployment Home PageFigure 2. Deployment Home Page

Figure 2. Deployment Home Page


Select a model type, fill in your AWS credentials to access blob storage (if private), the bucket name (or blob storage container), and paths to your model weights and resources archives.

Figure 3. Import SageMaker ModelFigure 3. Import SageMaker Model

Figure 3. Import SageMaker Model


Wait until the converter job completes, and view your newly deployed model! Begin submitting jobs to this model using the Jobs API.

Figure 4. Converter Job Progress BarFigure 4. Converter Job Progress Bar

Figure 4. Converter Job Progress Bar


Figure 5. Converter Job CompletionFigure 5. Converter Job Completion

Figure 5. Converter Job Completion


Programmatic (SDK)

Import packages and set environment variables (MODZY_API_KEY, SP_ACCESS_KEY_ID and SP_SECRET_ACCESS_KEY) with your credentials.

import os
import json
import time

os.environ["MODZY_API_KEY"] = "modzy-api-key"
os.environ["SP_ACCESS_KEY"] = "cloud-storage-access-key"
os.environ["SP_SECRET_ACCESS_KEY"] = "cloud-storage-secret-key"

Use the Python SDK to set up an API Client to the Model Converter Service, which is the Modzy service that allows you to import your SageMaker model directly.

from modzy.converter.model_converter import ModelConverter
from modzy.client import ApiClient

# Create a Modzy API client to interact with your Modzy envrionment
modzy_api_key = os.getenv("MODZY_API_KEY")
modzy_instance_base_url = "https://<your-modzy-instance-url>/api"
modzy_api_client = ApiClient(api_key=modzy_api_key, base_url=modzy_instance_base_url)

# Instantiate a Model Converter client with access to your Modzy environment
model_converter = ModelConverter(modzy_api_client)

Define blob storage provider and container. This container inside your preferred cloud storage provider is where you will upload your model artifacts.

blob_storage_provider = "S3"
blob_storage_container = "<path-to-your-s3-bucket>"

Define local paths to model.yaml, your model weights file(s), and any auxiliary files required for inference (e.g., labels.json for an image classification model).

from modzy.converter.utils import upload_resources

current_working_directory = os.getcwd()

# Define source platform and model type
source_platform = "sagemaker"
model_type = "image-classification"

# Paths to your local files
model_yaml_path = os.path.join(current_working_directory, "local-file-path/model.yaml")
labels_json_path = os.path.join(current_working_directory, "local-file-path/labels.json")
auxiliary_files = [labels_json_path]

# Your local files will be archieved and stored in the following location:
weights_key = "cloud-storage-path-to-weights/weights.tar.gz"
resources_key = "cloud-storage-path-to-resources/resources.tar.gz"

# Utility function to wrap resources in tar archive and upload to specified cloud storage filepath
upload_resources(
    model_yaml_path, blob_storage_container, resources_key,
    os.getenv("SP_ACCESS_KEY_ID"), os.getenv("SP_SECRET_ACCESS_KEY"), blob_storage_provider, auxiliary_files
)
from modzy.converter.utils import upload_resources

current_working_directory = os.getcwd()

# Define source platform and model type
source_platform = "sagemaker"
model_type = "xgboost-regression"

# Paths to your local files
model_yaml_path = os.path.join(current_working_directory, "local-file-path/model.yaml")

# Your local files will be archieved and stored in the following location:
weights_key = "cloud-storage-path-to-weights/weights.tar.gz"
resources_key = "cloud-storage-path-to-resources/resources.tar.gz"

# Utility function to wrap resources in tar archive and upload to specified cloud storage filepath
upload_resources(
    model_yaml_path, blob_storage_container, resources_key,
    os.getenv("SP_ACCESS_KEY_ID"), os.getenv("SP_SECRET_ACCESS_KEY"), blob_storage_provider, auxiliary_files
)

Kick off importer job.

# Now, provide the Model converter with information about your stored model assets and the credentials required
# to access them. The Model converter will do the rest of the work.

converter_output = model_converter.start(
    sp_access_key_id=os.getenv("SP_ACCESS_KEY_ID"),
    sp_secret_access_key=os.getenv("SP_SECRET_ACCESS_KEY"),
    blobstore_provider=blob_storage_provider,
    blobstore_container=blob_storage_container,
    weights_path=weights_key,
    resources_path=resources_key,
    platform=source_platform,
    model_type=model_type,
)

job_id = converter_output["jobId"]

Check initial status of converter job.

print("Model Converter Status: {} - {}".format(converter_output["status"], converter_output["message"]) + '\n' + "Model Converter Job ID: {}".format(job_id))

Wait until converter job completes. This process usually takes a few minutes.

# Block Model Converter Job until complete
start_time = time.time()
converter_result = model_converter.block_until_complete(job_id, timeout=600)
end_time = time.time()
model_id = converter_result["modelId"]
model_version = converter_result["modelVersion"]
print("Model Converter Job Status: {}".format(converter_result["message"]) + '\n' + "Completed in {} minutes".format((end_time - start_time)/60) + '\n' + "Find your newly deployed model here: {}/models/{}/{}".format(modzy_instance_base_url[:-4], model_id, model_version))

Click on the output link which will take you to your newly deployed model on Modzy. Start submitting jobs right away using the Jobs API.


What’s Next
Did this page help you?