Model

class sagemaker.model.Model(model_data, image, role=None, predictor_cls=None, env=None, name=None, vpc_config=None, sagemaker_session=None, enable_network_isolation=False, model_kms_key=None)

Bases: object

A SageMaker Model that can be deployed to an Endpoint.

Initialize an SageMaker Model.

Parameters:
  • model_data (str) – The S3 location of a SageMaker model data .tar.gz file.
  • image (str) – A Docker image URI.
  • role (str) – An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role if it needs to access some AWS resources. It can be null if this is being used to create a Model to pass to a PipelineModel which has its own Role field. (default: None)
  • predictor_cls (callable[string, sagemaker.session.Session]) – A function to call to create a predictor (default: None). If not None, deploy will return the result of invoking this function on the created endpoint name.
  • env (dict[str, str]) – Environment variables to run with image when hosted in SageMaker (default: None).
  • name (str) – The model name. If None, a default model name will be selected on each deploy.
  • vpc_config (dict[str, list[str]]) – The VpcConfig set on the model (default: None) * ‘Subnets’ (list[str]): List of subnet ids. * ‘SecurityGroupIds’ (list[str]): List of security group ids.
  • sagemaker_session (sagemaker.session.Session) – A SageMaker Session object, used for SageMaker interactions (default: None). If not specified, one is created using the default AWS configuration chain.
  • enable_network_isolation (Boolean) – Default False. if True, enables network isolation in the endpoint, isolating the model container. No inbound or outbound network calls can be made to or from the model container.
  • model_kms_key (str) – KMS key ARN used to encrypt the repacked model archive file if the model is repacked
prepare_container_def(instance_type, accelerator_type=None)

Return a dict created by sagemaker.container_def() for deploying this model to a specified instance type.

Subclasses can override this to provide custom container definitions for deployment to a specific instance type. Called by deploy().

Parameters:
  • instance_type (str) – The EC2 instance type to deploy this Model to. For example, ‘ml.p2.xlarge’.
  • accelerator_type (str) – The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, ‘ml.eia1.medium’.
Returns:

A container definition object usable with the CreateModel API.

Return type:

dict

enable_network_isolation()

Whether to enable network isolation when creating this Model

Returns:If network isolation should be enabled or not.
Return type:bool
check_neo_region(region)

Check if this Model in the available region where neo support.

Parameters:region (str) – Specifies the region where want to execute compilation
Returns:boolean value whether if neo is available in the specified region
Return type:bool
compile(target_instance_family, input_shape, output_path, role, tags=None, job_name=None, compile_max_run=300, framework=None, framework_version=None)

Compile this Model with SageMaker Neo.

Parameters:
  • target_instance_family (str) – Identifies the device that you want to run your model after compilation, for example: ml_c5. For allowed strings see https://docs.aws.amazon.com/sagemaker/latest/dg/API_OutputConfig.html.
  • input_shape (dict) – Specifies the name and shape of the expected inputs for your trained model in json dictionary form, for example: {‘data’:[1,3,1024,1024]}, or {‘var1’: [1,1,28,28], ‘var2’:[1,1,28,28]}
  • output_path (str) – Specifies where to store the compiled model
  • role (str) – Execution role
  • tags (list[dict]) – List of tags for labeling a compilation job. For more, see https://docs.aws.amazon.com/sagemaker/latest/dg/API_Tag.html.
  • job_name (str) – The name of the compilation job
  • compile_max_run (int) – Timeout in seconds for compilation (default: 3 * 60). After this amount of time Amazon SageMaker Neo terminates the compilation job regardless of its current status.
  • framework (str) – The framework that is used to train the original model. Allowed values: ‘mxnet’, ‘tensorflow’, ‘keras’, ‘pytorch’, ‘onnx’, ‘xgboost’
  • framework_version (str) –
Returns:

A SageMaker Model object. See Model() for full details.

Return type:

sagemaker.model.Model

deploy(initial_instance_count, instance_type, accelerator_type=None, endpoint_name=None, update_endpoint=False, tags=None, kms_key=None, wait=True, data_capture_config=None)

Deploy this Model to an Endpoint and optionally return a Predictor.

Create a SageMaker Model and EndpointConfig, and deploy an Endpoint from this Model. If self.predictor_cls is not None, this method returns a the result of invoking self.predictor_cls on the created endpoint name.

The name of the created model is accessible in the name field of this Model after deploy returns

The name of the created endpoint is accessible in the endpoint_name field of this Model after deploy returns.

Parameters:
  • initial_instance_count (int) – The initial number of instances to run in the Endpoint created from this Model.
  • instance_type (str) – The EC2 instance type to deploy this Model to. For example, ‘ml.p2.xlarge’, or ‘local’ for local mode.
  • accelerator_type (str) – Type of Elastic Inference accelerator to deploy this model for model loading and inference, for example, ‘ml.eia1.medium’. If not specified, no Elastic Inference accelerator will be attached to the endpoint. For more information: https://docs.aws.amazon.com/sagemaker/latest/dg/ei.html
  • endpoint_name (str) – The name of the endpoint to create (default: None). If not specified, a unique endpoint name will be created.
  • update_endpoint (bool) – Flag to update the model in an existing Amazon SageMaker endpoint. If True, this will deploy a new EndpointConfig to an already existing endpoint and delete resources corresponding to the previous EndpointConfig. If False, a new endpoint will be created. Default: False
  • tags (List[dict[str, str]]) – The list of tags to attach to this specific endpoint.
  • kms_key (str) – The ARN of the KMS key that is used to encrypt the data on the storage volume attached to the instance hosting the endpoint.
  • wait (bool) – Whether the call should wait until the deployment of this model completes (default: True).
  • data_capture_config (sagemaker.model_monitor.DataCaptureConfig) – Specifies configuration related to Endpoint data capture for use with Amazon SageMaker Model Monitoring. Default: None.
Returns:

Invocation of

self.predictor_cls on the created endpoint name, if self.predictor_cls is not None. Otherwise, return None.

Return type:

callable[string, sagemaker.session.Session] or None

transformer(instance_count, instance_type, strategy=None, assemble_with=None, output_path=None, output_kms_key=None, accept=None, env=None, max_concurrent_transforms=None, max_payload=None, tags=None, volume_kms_key=None)

Return a Transformer that uses this Model.

Parameters:
  • instance_count (int) – Number of EC2 instances to use.
  • instance_type (str) – Type of EC2 instance to use, for example, ‘ml.c4.xlarge’.
  • strategy (str) – The strategy used to decide how to batch records in a single request (default: None). Valid values: ‘MULTI_RECORD’ and ‘SINGLE_RECORD’.
  • assemble_with (str) – How the output is assembled (default: None). Valid values: ‘Line’ or ‘None’.
  • output_path (str) – S3 location for saving the transform result. If not specified, results are stored to a default bucket.
  • output_kms_key (str) – Optional. KMS key ID for encrypting the transform output (default: None).
  • accept (str) – The accept header passed by the client to the inference endpoint. If it is supported by the endpoint, it will be the format of the batch transform output.
  • env (dict) – Environment variables to be set for use during the transform job (default: None).
  • max_concurrent_transforms (int) – The maximum number of HTTP requests to be made to each individual transform container at one time.
  • max_payload (int) – Maximum size of the payload in a single HTTP request to the container in MB.
  • tags (list[dict]) – List of tags for labeling a transform job. If none specified, then the tags used for the training job are used for the transform job.
  • volume_kms_key (str) – Optional. KMS key ID for encrypting the volume attached to the ML compute instance (default: None).
delete_model()

Delete an Amazon SageMaker Model.

Raises:ValueError – if the model is not created yet.
class sagemaker.model.FrameworkModel(model_data, image, role, entry_point, source_dir=None, predictor_cls=None, env=None, name=None, enable_cloudwatch_metrics=False, container_log_level=20, code_location=None, sagemaker_session=None, dependencies=None, git_config=None, **kwargs)

Bases: sagemaker.model.Model

A Model for working with an SageMaker Framework.

This class hosts user-defined code in S3 and sets code location and configuration in model environment variables.

Initialize a FrameworkModel.

Parameters:
  • model_data (str) – The S3 location of a SageMaker model data .tar.gz file.
  • image (str) – A Docker image URI.
  • role (str) – An IAM role name or ARN for SageMaker to access AWS resources on your behalf.
  • entry_point (str) –

    Path (absolute or relative) to the Python source file which should be executed as the entry point to model hosting. This should be compatible with either Python 2.7 or Python 3.5. If ‘git_config’ is provided, ‘entry_point’ should be a relative location to the Python source file in the Git repo. Example

    With the following GitHub repo directory structure:
    >>> |----- README.md
    >>> |----- src
    >>>         |----- inference.py
    >>>         |----- test.py
    

    You can assign entry_point=’src/inference.py’.

  • source_dir (str) –

    Path (absolute or relative) to a directory with any other training source code dependencies aside from the entry point file (default: None). Structure within this directory will be preserved when training on SageMaker. If ‘git_config’ is provided, ‘source_dir’ should be a relative location to a directory in the Git repo. If the directory points to S3, no code will be uploaded and the S3 location will be used instead. .. admonition:: Example

    With the following GitHub repo directory structure:
    >>> |----- README.md
    >>> |----- src
    >>>         |----- inference.py
    >>>         |----- test.py
    

    You can assign entry_point=’inference.py’, source_dir=’src’.

  • predictor_cls (callable[string, sagemaker.session.Session]) – A function to call to create a predictor (default: None). If not None, deploy will return the result of invoking this function on the created endpoint name.
  • env (dict[str, str]) – Environment variables to run with image when hosted in SageMaker (default: None).
  • name (str) – The model name. If None, a default model name will be selected on each deploy.
  • enable_cloudwatch_metrics (bool) – Whether training and hosting containers will generate CloudWatch metrics under the AWS/SageMakerContainer namespace (default: False).
  • container_log_level (int) – Log level to use within the container (default: logging.INFO). Valid values are defined in the Python logging module.
  • code_location (str) – Name of the S3 bucket where custom code is uploaded (default: None). If not specified, default bucket created by sagemaker.session.Session is used.
  • sagemaker_session (sagemaker.session.Session) – A SageMaker Session object, used for SageMaker interactions (default: None). If not specified, one is created using the default AWS configuration chain.
  • dependencies (list[str]) –

    A list of paths to directories (absolute or relative) with any additional libraries that will be exported to the container (default: []). The library folders will be copied to SageMaker in the same folder where the entrypoint is copied. If ‘git_config’ is provided, ‘dependencies’ should be a list of relative locations to directories with any additional libraries needed in the Git repo. If the `source_dir` points to S3, code will be uploaded and the S3 location will be used instead. .. admonition:: Example

    The following call >>> Estimator(entry_point=’inference.py’, dependencies=[‘my/libs/common’, ‘virtual-env’]) results in the following inside the container:
    >>> $ ls
    
    >>> opt/ml/code
    >>>     |------ inference.py
    >>>     |------ common
    >>>     |------ virtual-env
    
  • git_config (dict[str, str]) –

    Git configurations used for cloning files, including repo, branch, commit, 2FA_enabled, username, password and token. The repo field is required. All other fields are optional. repo specifies the Git repository where your training script is stored. If you don’t provide branch, the default value ‘master’ is used. If you don’t provide commit, the latest commit in the specified branch is used. .. admonition:: Example

    The following config:
    >>> git_config = {'repo': 'https://github.com/aws/sagemaker-python-sdk.git',
    >>>               'branch': 'test-branch-git-config',
    >>>               'commit': '329bfcf884482002c05ff7f44f62599ebc9f445a'}
    

    results in cloning the repo specified in ‘repo’, then checkout the ‘master’ branch, and checkout the specified commit.

    2FA_enabled, username, password and token are used for authentication. For GitHub (or other Git) accounts, set 2FA_enabled to ‘True’ if two-factor authentication is enabled for the account, otherwise set it to ‘False’. If you do not provide a value for 2FA_enabled, a default value of ‘False’ is used. CodeCommit does not support two-factor authentication, so do not provide “2FA_enabled” with CodeCommit repositories.

    For GitHub and other Git repos, when SSH URLs are provided, it doesn’t matter whether 2FA is enabled or disabled; you should either have no passphrase for the SSH key pairs, or have the ssh-agent configured so that you will not be prompted for SSH passphrase when you do ‘git clone’ command with SSH URLs. When HTTPS URLs are provided: if 2FA is disabled, then either token or username+password will be used for authentication if provided (token prioritized); if 2FA is enabled, only token will be used for authentication if provided. If required authentication info is not provided, python SDK will try to use local credentials storage to authenticate. If that fails either, an error message will be thrown.

    For CodeCommit repos, 2FA is not supported, so ‘2FA_enabled’ should not be provided. There is no token in CodeCommit, so ‘token’ should not be provided too. When ‘repo’ is an SSH URL, the requirements are the same as GitHub-like repos. When ‘repo’ is an HTTPS URL, username+password will be used for authentication if they are provided; otherwise, python SDK will try to use either CodeCommit credential helper or local credential storage for authentication.

  • **kwargs – Keyword arguments passed to the Model initializer.

Tip

You can find additional parameters for initializing this class at Model.

prepare_container_def(instance_type, accelerator_type=None)

Return a container definition with framework configuration set in model environment variables.

This also uploads user-supplied code to S3.

Parameters:
  • instance_type (str) – The EC2 instance type to deploy this Model to. For example, ‘ml.p2.xlarge’.
  • accelerator_type (str) – The Elastic Inference accelerator type to deploy to the instance for loading and making inferences to the model. For example, ‘ml.eia1.medium’.
Returns:

A container definition object usable with the CreateModel API.

Return type:

dict[str, str]

class sagemaker.model.ModelPackage(role, model_data=None, algorithm_arn=None, model_package_arn=None, **kwargs)

Bases: sagemaker.model.Model

A SageMaker Model that can be deployed to an Endpoint.

Initialize a SageMaker ModelPackage.

Parameters:
  • role (str) – An AWS IAM role (either name or full ARN). The Amazon SageMaker training jobs and APIs that create Amazon SageMaker endpoints use this role to access training data and model artifacts. After the endpoint is created, the inference code might use the IAM role, if it needs to access an AWS resource.
  • model_data (str) – The S3 location of a SageMaker model data .tar.gz file. Must be provided if algorithm_arn is provided.
  • algorithm_arn (str) – algorithm arn used to train the model, can be just the name if your account owns the algorithm. Must also provide model_data.
  • model_package_arn (str) – An existing SageMaker Model Package arn, can be just the name if your account owns the Model Package. model_data is not required.
  • **kwargs – Additional kwargs passed to the Model constructor.
enable_network_isolation()

Whether to enable network isolation when creating a model out of this ModelPackage

Returns:If network isolation should be enabled or not.
Return type:bool