Updated on 2024-06-12 GMT+08:00

Importing a Model

Importing a model includes:

  • Initialize the existing model and create a model object based on the model ID.
  • Create a model. For details about the attributes of the created model, see Obtaining Details About a Model.

Sample Model File

The following uses PyTorch as an example to describe how to edit a model file. For details about the PyTorch model package structure, see "Model Inference > Inference Specifications > Model Package Specifications > Introduction to Model Package Specifications" in ModelArts User Guide.

For details about PyTorch custom scripts, see "Model Inference > Inference Specifications > PyTorch" in ModelArts User Guide.

OBS bucket or directory name
├── resnet
│   ├── model Mandatory: Fixed subdirectory name. The subdirectory is used to store model-related files.
│   │  ├──<<Custom Python package>> (Optional) Custom Python package, which can be directly referenced in model inference code
│   │  ├──mnist_mlp.pt (Mandatory) PyTorch model file, which contains variable and weight information and is saved as state_dict
│   │  ├──config.json Mandatory: Model configuration file. The file name is fixed to config.json. Only one model configuration file is allowed.
│   │  ├──customize_service.py Mandatory: Model inference code. The file name is fixed to customize_service.py. Only one model inference file is allowed. The files on which customize_service.py depends can be directly stored in the model directory.

Sample Code

In ModelArts notebook, you do not need to enter authentication parameters for session authentication. For details about session authentication of other development environments, see Session Authentication.

1
2
3
4
5
from modelarts.session import Session
from modelarts.model import Model
from modelarts.config.model_config import ServiceConfig, Params, Dependencies, Packages

session = Session()
  • Method 1: Initialize an existing model.
    1
    model_instance = Model(session, model_id="your_model_id")
    
  • Method 2: Create a model.
    • Use a preset image and specify an OBS path to create a model.
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      model_location = "/your_obs_bucket/model_path"            # Change to the OBS path to the model file
      execution_code = "/your_obs_bucket/model_path/customize_service.py"
      runtime = "python3.7"
      
      model_instance = Model(
                              session,
                              model_name="input_model_name",    #  (Optional) Model name
                              model_version="1.0.0",            # (Optional) Model version
                              source_location=model_location,   # OBS path to the model file, for example, /your_obs_bucket/model_path
                              model_type="PyTorch",             # Model type
                              execution_code=execution_code,    # (Optional) OBS path to the execution script, for example, /your_obs_bucket/model_path/customize_service.py
                              runtime = runtime                 # (Optional) Supported runtime environment
                             )
      

      dependencies will overwrite the data in config.json in the preceding example. You do not need to use dependencies. The following section describes the dependencies formats.

      • Format of the dependencies parameter group

        SDKs define the dependencies parameter group. dependencies is in list format, and those of the tuple objects in the list are Dependencies.

        The code is as follows:

        1
        2
        3
        4
        5
        6
        dependencies = []
        dependency1 = Dependencies(
                                    installer="pip",                     # Installation mode. pip is supported.
              packages=packages                   # Collection of dependency packages. For details, see packages.
        )
        dependencies.append(dependency1)
        
      • Format of the package parameter group

        SDKs define the packages parameter group. packages is in list format, and those of the tuple objects in the list are Packages.

        The code is as follows:

        1
        2
        3
        4
        5
        6
        7
        packages = []
        package1 = Packages(
                              package_name="package_name",               # Package name
                              package_version="version",                 # Package version
             restraint="EXACT"
        )  
        packages.append(package1)
        

        The following is an example of creating a dependencies parameter group:

        dependencies = []
        packages = [{
            "package_name": "numpy",
            "package_version": "1.15.0",
            "restraint": "EXACT"
            }, {
                "package_name": "h5py",
                "package_version": "2.8.0",
                "restraint": "EXACT"
            }]
        dependency = Dependencies(installer="pip", packages=packages)
        dependencies.append(dependency)
    • Use a custom image to create a model.

      This method applies if the script of the inference service has been built in the custom image and the service is automatically started when the image is started.

      from modelarts.session import Session
      from modelarts.model import Model
      
      session = Session()
      image_path = "custom_image_path"
      model_instance = Model(
                              session, 
                              model_name="your_model_name",               # Model name
                              model_version="0.1.0",                      # Model version
                              source_location=image_path,                 # Model file path
                              model_type="Image"                         # Model type
                            )

Parameters

Table 1 Parameters for initializing a model

Parameter

Mandatory

Type

Description

session

Yes

Object

Session object. For details about the initialization method, see Session Authentication.

model_id

Yes

String

Model ID

Table 2 Parameters for creating a model

Parameter

Mandatory

Type

Description

session

Yes

Object

Session object. For details about the initialization method, see Session Authentication.

model_name

No

String

Name of a model that consists of 1 to 64 characters. If this parameter is not specified, the system automatically generates a model name.

model_version

Yes

String

Model version in the format of Digit.Digit.Digit. The value range of the digits is [1, 99]. The version number cannot start with 0, for example, 01.01.01.

publish

No

Bool

Whether to publish a model. The options are as follows:

  • True: Publish the model. (Default value)
  • False: Do not publish the model.

source_location_type

No

String

Model location type. The options are as follows:

  • OBS_SOURCE: OBS path. (Default value)
  • LOCAL_SOURCE: local path.

source_location

Yes

String

Path (parent directory) of the model file

  • If source_location_type is set to OBS_SOURCE, the model file path is an OBS path in the format of /obs_bucketname/.../model_file_parent_dir/.
  • If source_location_type is set to LOCAL_SOURCE, the model file path is a local path in the format of /local_path/.../model_file_parent_dir/.

environment

No

Environment instance

Environment required for normal model running, such as the Python or TensorFlow version

source_job_id

No

String

ID of the source training job. If the model is generated from a training job, specify this parameter for source tracing. If the model is imported from a third-party meta model, leave this parameter blank. By default, this parameter is left blank.

source_job_version

No

String

Version of the source training job. If the model is generated from a training job, specify this parameter for source tracing. If the model is imported from a third-party meta model, leave this parameter blank. By default, this parameter is left blank.

source_type

No

String

Model source type. If the model is deployed via a training job, leave this parameter blank. By default, this parameter is left blank.

model_type

Yes

String

Model type. The value can be TensorFlow, MXNet, Spark_MLlib, Scikit_Learn, XGBoost, MindSpore, Image, or PyTorch.

model_algorithm

No

String

Model algorithm. If the algorithm has been configured in the model configuration file, this parameter can be left blank. For example, predict_analysis, object_detection, or image_classification.

description

No

String

Model description, which contains a maximum of 100 characters and cannot contain the following special characters: !<>=&'"

execution_code

No

String

OBS path to the script to be executed. If customize_service.py is not output by the model, configure this parameter to specify the path. The inference script must be stored in the model directory in the path where the model is located. For details, see the source_location parameter. The script name is fixed to customize_service.py.

runtime

No

String

Supported runtime environment. This parameter is mandatory if model_type is used. For details, see .

input_params

No

params array

List of input parameters for model inference. By default, this parameter is left blank. If the apis information has been configured in the model configuration file, you do not need to set this parameter. The backend automatically reads the input parameters from the apis field in the configuration file.

output_params

No

params array

List of output parameters for model inference. By default, this parameter is left blank. If the apis information has been configured in the model configuration file, you do not need to set this parameter. The backend automatically reads the output parameters from the apis field in the configuration file.

dependencies

No

dependency array

Dependency package required for running the code and model. By default, this parameter is left blank. If the dependencies information has been configured in the model configuration file, you do not need to set this parameter. The backend automatically reads the dependencies to be installed from the dependencies field in the configuration file.

apis

No

String

List of inference APIs provided by a model. By default, this parameter is left blank. If the apis information has been configured in the model configuration file, you do not need to set this parameter. The backend automatically reads the configured inference API information from the apis field in the configuration file.

Table 3 params parameters

Parameter

Mandatory

Type

Description

url

Yes

String

Request path of a model inference API

param_name

Yes

String

Parameter name, which contains a maximum of 64 characters

param_type

Yes

String

Basic parameter types of JSON schema, including string, object, array, boolean, number, and integer

min

No

Double

This parameter is optional when param_type is set to int or float. By default, this parameter is left blank.

max

No

Double

This parameter is optional when param_type is set to int or float. By default, this parameter is left blank.

param_desc

No

String

Parameter description, which contains a maximum of 100 characters. By default, this parameter is left blank.

Table 4 dependency parameters

Parameter

Mandatory

Type

Description

installer

Yes

String

Installation mode. Only pip is supported.

packages

Yes

package array

Collection of dependency packages

Table 5 package parameters

Parameter

Mandatory

Type

Description

package_name

Yes

String

Name of a dependency package

package_version

No

String

Version of a dependency package

restraint

No

String

Version filtering condition. This parameter is mandatory only when package_version exists. Possible values are as follows:

  • EXACT: the specified version
  • ATLEAST: not earlier than the specified version
  • ATMOST: not later than the specified version
Table 6 create_model response parameters

Parameter

Mandatory

Type

Description

model_instance

Yes

Model object

Model object, which can be any of the APIs described in this chapter

Example of creating a model in a handwritten digit recognition project using MXNet:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from modelarts.session import Session
from modelarts.model import Model

session = Session()
model_instance = Model(session,
                       model_name="digit_recognition",
                       model_version="1.0.0",
                       source_location=model_location,
                       model_type="MXNet",
                       model_algorithm="image_classification"
                       )