Updated on 2024-03-21 GMT+08:00

Debugging a Model

After the training is complete, create a local model, debug the model locally, and deploy the model on ModelArts.

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. Save the custom inference file and model configuration file to the directory storing the model file generated during training. If the model generated during training is stored in /home/ma-user/work/tensorflow_mlp_mnist_local_mode/train/model/, the inference file customize_service.py and model configuration file config.json are also stored in this directory.
  2. Create a Conda virtual environment for running models.

    1
    2
    3
    4
    5
    6
    7
    from modelarts.environment import Environment
    from modelarts.environment.conda_env import CondaDependencies
    
    env = Environment("tensorflow_mlp_mnist")
    cd = CondaDependencies.create(pip_packages=["tensorflow==1.13.1", "Pillow>=8.0.1"],
                                  conda_packages=["python=3.6.2"])
    env.conda = cd
    

  3. Create a local model.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    from modelarts.session import Session
    from modelarts.model import Model
    
    session = Session()
    src_local_path = "/home/ma-user/work/tensorflow_mlp_mnist_local_mode/train/"
    model = Model(session,
                  publish=False,
                  source_location_type="LOCAL_SOURCE",         # Type of the model file location
                  source_location=src_local_path + 'model',    # Location of the model file
                  environment=env,
                  model_version='1.0.1',
                  model_type='TensorFlow',                     # AI framework used by the model
                  model_algorithm="image_classification",
                  model_name="tensorflow_mlp_mnist_local_infer")
    

    After a local model is created, you can deploy it as a local service.

  4. Call the API to publish the model.

    1
    model.publish_model(obs_location=obs_location)
    

    After the obs_location parameter is specified, the local model file is uploaded to this directory. This parameter can be omitted. See the following example:

    1
    model.publish_model()
    

    The model file is uploaded to the directory whose name ends with the current timestamp in the default OBS bucket. The directory is displayed after the command is executed:

    1
    Successfully upload file /home/ma-user/work/tensorflow_mlp_mnist_local_mode/train/model to OBS modelarts-cn-north-4-08aae033/model-0107-224502
    

Parameters

Table 1 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 and must start with a letter. Only letters, digits, underscores (_), and hyphens (-) are allowed. 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. Create a local model, which can be used to debug related code.

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 For details, see Table 2.

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. The value can only be auto, which indicates an ExeML model (model download is not allowed). 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. Possible options are predict_analysis, object_detection, and 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.

Table 2 Environment parameters

Parameter

Mandatory

Type

Description

name

Yes

String

Environment name

conda

No

CondaDependencies

Conda environment. For details, see Table 3.

Table 3 CondaDependencies parameters

Parameter

Mandatory

Type

Description

channels

No

List

Source for downloading the Python package

pip_packages

No

List

Python package required by the Conda virtual environment, such as TensorFlow and Pillow

conda_packages

No

List

Conda package required by the Conda virtual environment, for example, a specified Python version

Table 4 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 5 dependency parameters

Parameter

Mandatory

Type

Description

installer

Yes

String

Installation mode, which can only be pip

packages

Yes

package array

Collection of dependency packages

Table 6 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 is available. Options:

  • EXACT: a specified version
  • ATLEAST: not earlier than the specified version
  • ATMOST: not later than the specified version
Table 7 Parameters for creating a model

Parameter

Mandatory

Type

Description

model

Yes

Model object

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