The model import function covers the following aspects:
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 | from modelarts.session import Session from modelarts.model import Model from modelarts.config.model_config import ServiceConfig,Params,Dependencies,Packages session = Session() |
1 | model_instance = Model(session, model_id="input your model id") |
1 2 3 4 5 6 7 8 9 10 11 12 | model_instance = Model( session, model_name="input model name", # Model name model_version="1.0.0", # Model version source_location=model_location, # OBS path to a model file, for example, obs://your_obs_bucket/mode_file_path model_type="MXNet", # Model type model_algorithm="image_classification", # Model algorithm execution_code="OBS_PATH", input_params=input_params, # For details, see the input_params format description. output_params=output_params, # For details, see the output_params format description. dependencies=dependencies, # For details, see the dependencies format description. apis=apis) |
The SDK provides the definition of input_params and output_params parameter groups. The types of input_params and output_params are list, and those of the tuple objects in the list are Params.
The following uses input_params as an example:
1 2 3 4 5 6 7 8 9 | input_params = [] # The type of input_params is list. Multiple objects of the Params type can be stored. input_params1 = Params( url='url', # URL param_name='param_name', # Parameter name param_type='param_type', # Parameter type min='min', max='max', param_desc='param_desc') input_params.append(input_params1) |
The SDK provides the definition of the dependencies parameter group. The type of dependencies is list, and those of the tuple objects in the list are Dependencies.
The code is as follows:
1 2 3 4 5 | dependencies = [] dependency1 = Dependencies( installer="pip", # Installation mode. pip is supported. packages=packages) # Collection of dependency packages. For details about the definition format, see the definition of packages. dependencies.append(dependency1) |
The SDK provides the definition of the packages parameter group. The type of packages is list, and those of the tuple objects in the list are Packages.
The code is as follows:
1 2 3 4 5 6 | packages = [] package1 = Packages( package_name="package_name", # Package name package_version="version", # Package version restraint="restraint") 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)
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
session |
Yes |
Object |
Session object. For details about the initialization method, see Session Authentication. |
model_id |
Yes |
String |
Model ID |
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, which contains 1 to 64 characters that consist of only letters, digits, underscores (_), and hyphens (-). It must start with a letter. 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 [0, 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:
|
source_location_type |
No |
String |
Model location type. The options are as follows:
|
source_location |
Yes |
String |
Path (parent directory) of the model file
|
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. Currently, the value can only be auto, which indicates an ExeML model (model download is not allowed). If the model is deployed by 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 execution script. 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. |
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. |
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. |
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
installer |
Yes |
String |
Installation mode. Only pip is supported. |
packages |
Yes |
package array |
Collection of dependency packages |
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:
|
Parameter |
Mandatory |
Type |
Description |
---|---|---|---|
model_instance |
Yes |
Model object |
Model object, which can be any of the APIs described in this chapter |
1 2 3 4 5 6 7 8 9 | 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") |