更新时间:2024-01-29 GMT+08:00
分享

模型配置文件编写说明

模型开发者发布模型时需要编写配置文件config.json。模型配置文件描述模型用途、模型计算框架、模型精度、推理代码依赖包以及模型对外API接口。

配置文件格式说明

配置文件为JSON格式,参数说明如表1所示。

表1 参数说明

参数

是否必选

参数类型

描述

model_algorithm

String

模型算法,表示该模型的用途,由模型开发者填写,以便使用者理解该模型的用途。只能以英文字母开头,不能包含中文以及&!'\"<>=,不超过36个字符。常见的模型算法有image_classification(图像分类)、object_detection(物体检测)、predict_analysis(预测分析)等。

model_type

String

模型AI引擎,表明模型使用的计算框架,支持常用AI框架和“Image”

  • 可选的常用AI框架请参见推理支持的AI引擎
  • 当model_type设置为Image,表示以自定义镜像方式创建AI应用,此时swr_location为必填参数。Image镜像制作规范可参见模型镜像规范

runtime

String

模型运行时环境,系统默认使用python2.7。runtime可选值与model_type相关,当model_type设置为Image时,不需要设置runtime,当model_type设置为其他常用框架时,请选择您使用的引擎所对应的运行时环境。目前支持的运行时环境列表请参见推理支持的AI引擎

需要注意的是,如果您的模型需指定CPU或GPU上运行时,请根据runtime的后缀信息选择,当runtime中未包含cpu或gpu信息时,请仔细阅读“推理支持的AI引擎”中每个runtime的说明信息。

metrics

object数据结构

模型的精度信息,包括平均数、召回率、精确率、准确率,metrics object数据结构说明如表2所示。

结果会显示在AI应用详情页面的“模型精度”模块。

apis

api数据结构数组

表示模型接收和返回的请求样式,为结构体数据。

即模型可对外提供的Restful API数组,API数据结构如表3所示。示例代码请参见apis参数代码示例

  • “model_type”“Image”时,即自定义镜像的模型场景,“apis”可根据镜像实际对外暴露的请求路径在“apis”中声明不同路径的API。
  • “model_type”不为“Image”时,“apis”只能声明一个请求路径为“/”的API,因为系统预置的AI引擎仅暴露一个请求路径为“/”的推理接口。

dependencies

dependency结构数组

表示模型推理代码需要依赖的包,为结构体数据。

模型开发者需要提供包名、安装方式、版本约束。目前只支持pip安装方式。dependency结构数组说明如表6所示。

如果模型包内没有推理代码customize_service.py文件,则该字段可不填。自定义镜像模型不支持安装依赖包。

说明:

“dependencies”参数支持多个“dependency”结构数组,以list格式填入,默认安装包存在先后依赖关系(即写在前面的先安装,写在后面的后安装),且支持线下wheel包安装(wheel包必须与模型文件放在同一目录)。示例请参考导入模型时安装包依赖配置文件如何书写?

health

health数据结构

镜像健康接口配置信息,只有“model_type”“Image”时才需填写。

如果在滚动升级时要求不中断业务,那么必需提供健康检查的接口供ModelArts调用。health数据结构如表8所示。

表2 metrics object数据结构说明

参数

是否必选

参数类型

描述

f1

Number

平均数。精确到小数点后17位,超过17位时,取前17位数值。

recall

Number

召回率。精确到小数点后17位,超过17位时,取前17位数值。

precision

Number

精确率。精确到小数点后17位,超过17位时,取前17位数值。

accuracy

Number

准确率。精确到小数点后17位,超过17位时,取前17位数值。

表3 api数据结构说明

参数

是否必选

参数类型

描述

url

String

请求路径。默认值为“/”。自定义镜像的模型(即model_type为Image时)需要根据镜像内实际暴露的请求路径填写“url”。非自定义镜像模型(即model_type不为Image时)时,“url”只能为“/”

method

String

请求方法。默认值为“POST”

request

Object

请求体,request结构说明如表4所示。

response

Object

响应体,response结构说明如表5所示。

表4 request结构说明

参数

是否必选

参数类型

描述

Content-type

在线服务-非必选

批量服务-必选

String

data以指定内容类型发送。默认值为“application/json”

一般情况包括如下两种内容类型:

  • “application/json”,发送json数据。
  • “multipart/form-data”,上传文件。
说明:

针对机器学习类模型,仅支持“application/json”

data

在线服务-非必选

批量服务-必选

String

请求体以json schema描述。参数说明请参考官方指导

表5 response结构说明

参数

是否必选

参数类型

描述

Content-type

在线服务-非必选

批量服务-必选

String

data以指定内容类型发送。默认值为“application/json”

说明:

针对机器学习类模型,仅支持“application/json”

data

在线服务-非必选

批量服务-必选

String

响应体以json schema描述。参数说明请参考官方指导

表6 dependency结构数组说明

参数

是否必选

参数类型

描述

installer

String

安装方式,当前只支持“pip”

packages

package结构数组

依赖包集合,package结构数组说明如表7所示。

表7 package结构数组说明

参数

是否必选

参数类型

描述

package_name

String

依赖包名称。不能含有中文及特殊字符&!'"<>=。

package_version

String

依赖包版本,如果不强依赖于版本号,则该项不填。不能含有中文及特殊字符&!'"<>=。

restraint

String

版本限制条件,当且仅当“package_version”存在时必填,可选“EXACT/ATLEAST/ATMOST”

  • “EXACT”表示安装给定版本。
  • “ATLEAST”表示安装版本不小于给定版本。
  • “ATMOST”表示安装包版本不大于给定版本。
    说明:
    • 如果对版本有明确要求,优先使用“EXACT”;如果使用“EXACT”与系统安装包有冲突,可以选择“ATLEAST”
    • 如果对版本没有明确要求,推荐不填写“restraint”“package_version”,只保留“package_name”参数
表8 health数据结构说明

参数

是否必选

参数类型

描述

check_method

String

健康检查方式。可选“HTTP/EXEC”。

  • HTTP:HTTP请求检查
  • EXEC:执行命令检查。

command

String

健康检查命令。健康检查方式为EXEC时必选。

url

String

健康检查接口请求路径。健康检查方式为HTTP时必选。

protocol

String

健康检查接口请求协议,默认为http。健康检查方式为HTTP时必选。

initial_delay_seconds

String

健康检查初始化延迟时间。

timeout_seconds

String

健康检查超时时间。

period_seconds

String

健康检查周期。填写大于0且小于等于2147483647的整数,单位为秒。

failure_threshold

String

健康检查最大失败次数。填写大于0且小于等于2147483647的整数。

apis参数代码示例

[{
    "url": "/",
    "method": "post",
    "request": {
        "Content-type": "multipart/form-data",
        "data": {
            "type": "object",
            "properties": {
                "images": {
                    "type": "file"
                }
            }
        }
    },
    "response": {
        "Content-type": "applicaton/json",
        "data": {
            "type": "object",
            "properties": {
                "mnist_result": {
                    "type": "array",
                    "item": [
                        {
                            "type": "string"
                        }
                    ]
                }
            }
        }
    }
}]

目标检测模型配置文件示例

如下代码以TensorFlow引擎为例,您可以根据实际使用的引擎类型修改model_type参数后使用。

  • 模型输入

    key:images

    value:图片文件

  • 模型输出
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    {
        "detection_classes": [
            "face",
            "arm"
        ],
        "detection_boxes": [
            [
                33.6,
                42.6,
                104.5,
                203.4
            ],
            [
                103.1,
                92.8,
                765.6,
                945.7
            ]
        ],
        "detection_scores": [0.99, 0.73]
    }
    
  • 配置文件
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    {
        "model_type": "TensorFlow",
        "model_algorithm": "object_detection",
        "metrics": {
            "f1": 0.345294,
            "accuracy": 0.462963,
            "precision": 0.338977,
            "recall": 0.351852
        },
        "apis": [{
            "url": "/",
            "method": "post",
            "request": {
                "Content-type": "multipart/form-data",
                "data": {
                    "type": "object",
                    "properties": {
                        "images": {
                            "type": "file"
                        }
                    }
                }
            },
            "response": {
                "Content-type": "application/json",
                "data": {
                    "type": "object",
                    "properties": {
                        "detection_classes": {
                            "type": "array",
                            "items": [{
                                "type": "string"
                            }]
                        },
                        "detection_boxes": {
                            "type": "array",
                            "items": [{
                                "type": "array",
                                "minItems": 4,
                                "maxItems": 4,
                                "items": [{
                                    "type": "number"
                                }]
                            }]
                        },
                        "detection_scores": {
                            "type": "array",
                            "items": [{
                                "type": "number"
                            }]
                        }
                    }
                }
            }
        }],
        "dependencies": [{
            "installer": "pip",
            "packages": [{
                    "restraint": "EXACT",
                    "package_version": "1.15.0",
                    "package_name": "numpy"
                },
                {
                    "restraint": "EXACT",
                    "package_version": "5.2.0",
                    "package_name": "Pillow"
                }
            ]
        }]
    }
    

图像分类模型配置文件示例

如下代码以TensorFlow引擎为例,您可以根据实际使用的引擎类型修改model_type参数后使用。

  • 模型输入

    key:images

    value:图片文件

  • 模型输出
    1
    2
    3
    4
    5
    6
    7
    {
        "predicted_label": "flower",
        "scores": [
           ["rose", 0.99],
           ["begonia", 0.01]
        ]
    }
    
  • 配置文件
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    {
        "model_type": "TensorFlow",
        "model_algorithm": "image_classification",
        "metrics": {
            "f1": 0.345294,
            "accuracy": 0.462963,
            "precision": 0.338977,
            "recall": 0.351852
        },
        "apis": [{
            "url": "/",
            "method": "post",
            "request": {
                "Content-type": "multipart/form-data",
                "data": {
                    "type": "object",
                    "properties": {
                        "images": {
                            "type": "file"
                        }
                    }
                }
            },
            "response": {
                "Content-type": "application/json",
                "data": {
                    "type": "object",
                    "properties": {
                        "predicted_label": {
                            "type": "string"
                        },
                        "scores": {
                            "type": "array",
                            "items": [{
                                "type": "array",
                                "minItems": 2,
                                "maxItems": 2,
                                "items": [
                                    {
                                        "type": "string"
                                    },
                                    {
                                        "type": "number"
                                    }
                                ]
                            }]
                        }
                    }
                }
            }
        }],
        "dependencies": [{
            "installer": "pip",
            "packages": [{
                    "restraint": "ATLEAST",
                    "package_version": "1.15.0",
                    "package_name": "numpy"
                },
                {
                    "restraint": "",
                    "package_version": "",
                    "package_name": "Pillow"
                }
            ]
        }]
    }
    

如下代码以MindSpore引擎为例,您可以根据实际使用的引擎类型修改model_type参数后使用。

  • 模型输入

    key:images

    value:图片文件

  • 模型输出
    1
    "[[-2.404526   -3.0476532  -1.9888215   0.45013925 -1.7018927   0.40332815\n  -7.1861157  11.290332   -1.5861531   5.7887416 ]]"
    
  • 配置文件
    {
         "model_algorithm": "image_classification",
         "model_type": "MindSpore",
         "metrics": {
             "f1": 0.124555,
             "recall": 0.171875,
             "precision": 0.0023493892851938493,
             "accuracy": 0.00746268656716417
         },
         "apis": [{
                 "url": "/",
                 "method": "post",
                 "request": {
                     "Content-type": "multipart/form-data",
                     "data": {
                         "type": "object",
                         "properties": {
                             "images": {
                                 "type": "file"
                             }
                         }
                     }
                 },
                 "response": {
                     "Content-type": "applicaton/json",
                     "data": {
                         "type": "object",
                         "properties": {
                             "mnist_result": {
                                 "type": "array",
                                 "item": [{
                                     "type": "string"
                                 }]
                             }
                         }
                     }
                 }
             }
         ],
        "dependencies": []
     }

预测分析模型配置文件示例

如下代码以TensorFlow引擎为例,您可以根据实际使用的引擎类型修改model_type参数后使用。

  • 模型输入
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    {
        "data": {
            "req_data": [
                {
                    "buying_price": "high",
                    "maint_price": "high",
                    "doors": "2",
                    "persons": "2",
                    "lug_boot": "small",
                    "safety": "low",
                    "acceptability": "acc"
                },
                {
                    "buying_price": "high",
                    "maint_price": "high",
                    "doors": "2",
                    "persons": "2",
                    "lug_boot": "small",
                    "safety": "low",
                    "acceptability": "acc"
                }
            ]
        }
    }
    
  • 模型输出
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    {
        "data": {
            "resp_data": [
                {
                    "predict_result": "unacc"
                },
                {
                    "predict_result": "unacc"
                }
            ]
        }
    }
    
  • 配置文件

    代码中request结构和response结构中的data参数是json schema数据结构。data/properties里面的内容对应“模型输入”“模型输出”

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    {
        "model_type": "TensorFlow",
        "model_algorithm": "predict_analysis",
        "metrics": {
            "f1": 0.345294,
            "accuracy": 0.462963,
            "precision": 0.338977,
            "recall": 0.351852
        },
        "apis": [
            {
                "url": "/",
                "method": "post",
                "request": {
                    "Content-type": "application/json",
                    "data": {
                        "type": "object",
                        "properties": {
                            "data": {
                                "type": "object",
                                "properties": {
                                    "req_data": {
                                        "items": [
                                            {
                                                "type": "object",
                                                "properties": {}
                                            }
                                        ],
                                        "type": "array"
                                    }
                                }
                            }
                        }
                    }
                },
                "response": {
                    "Content-type": "application/json",
                    "data": {
                        "type": "object",
                        "properties": {
                            "data": {
                                "type": "object",
                                "properties": {
                                    "resp_data": {
                                        "type": "array",
                                        "items": [
                                            {
                                                "type": "object",
                                                "properties": {}
                                            }
                                        ]
                                    }
                                }
                           }
                        }
                    }
                }
            }
        ],
        "dependencies": [
            {
                "installer": "pip",
                "packages": [
                    {
                        "restraint": "EXACT",
                        "package_version": "1.15.0",
                        "package_name": "numpy"
                    },
                    {
                        "restraint": "EXACT",
                        "package_version": "5.2.0",
                        "package_name": "Pillow"
                    }
                ]
            }
        ]
    }
    

自定义镜像类型的模型配置文件示例

模型输入和输出与目标检测模型配置文件示例类似。

  • 模型预测输入为图片类型时,request请求示例如下:

    该实例表示模型预测接收一个参数名为images、参数类型为file的预测请求,在推理界面会显示文件上传按钮,以文件形式进行预测。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    {
        "Content-type": "multipart/form-data",
        "data": {
            "type": "object",
            "properties": {
                "images": {
                    "type": "file"
                }
            }
        }
    }
    
  • 模型预测输入为json数据类型时,request请求示例如下:

    该实例表示模型预测接收json请求体,只有一个参数名为input、参数类型为string的预测请求,在推理界面会显示文本输入框,用于填写预测请求。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    {
        "Content-type": "application/json",
        "data": {
            "type": "object",
            "properties": {
                "input": {
                    "type": "string"
                }
            }
        }
    }
    

完整请求示例如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
    "model_algorithm": "image_classification",
    "model_type": "Image",
    "metrics": {
        "f1": 0.345294,
        "accuracy": 0.462963,
        "precision": 0.338977,
        "recall": 0.351852
    },
    "apis": [{
        "url": "/",
        "method": "post",
        "request": {
            "Content-type": "multipart/form-data",
            "data": {
                "type": "object",
                "properties": {
                    "images": {
                        "type": "file"
                    }
                }
            }
        },
        "response": {
            "Content-type": "application/json",
            "data": {
                "type": "object",
                "required": [
                    "predicted_label",
                    "scores"
                ],
                "properties": {
                    "predicted_label": {
                        "type": "string"
                    },
                    "scores": {
                        "type": "array",
                        "items": [{
                            "type": "array",
                            "minItems": 2,
                            "maxItems": 2,
                            "items": [{
                                    "type": "string"
                                },
                                {
                                    "type": "number"
                                }
                            ]
                        }]
                    }
                }
            }
        }
    }]
}

机器学习类型的模型配置文件示例

以下代码以XGBoost为例。

  • 模型输入:
{
    "req_data": [
        {
            "sepal_length": 5,
            "sepal_width": 3.3,
            "petal_length": 1.4,
            "petal_width": 0.2
        },
        {
            "sepal_length": 5,
            "sepal_width": 2,
            "petal_length": 3.5,
            "petal_width": 1
        },
        {
            "sepal_length": 6,
            "sepal_width": 2.2,
            "petal_length": 5,
            "petal_width": 1.5
        }
    ]
}
  • 模型输出:
{
    "resp_data": [
        {
            "predict_result": "Iris-setosa"
        },
        {
            "predict_result": "Iris-versicolor"
        }
    ]
}
  • 配置文件:
{
    "model_type": "XGBoost",
    "model_algorithm": "xgboost_iris_test",
    "runtime": "python2.7",
    "metrics": {
        "f1": 0.345294,
        "accuracy": 0.462963,
        "precision": 0.338977,
        "recall": 0.351852
    },
    "apis": [
        {
            "url": "/",
            "method": "post",
            "request": {
                "Content-type": "application/json",
                "data": {
                    "type": "object",
                    "properties": {
                        "req_data": {
                            "items": [
                                {
                                    "type": "object",
                                    "properties": {}
                                }
                            ],
                            "type": "array"
                        }
                    }
                }
            },
            "response": {
                "Content-type": "applicaton/json",
                "data": {
                    "type": "object",
                    "properties": {
                        "resp_data": {
                            "type": "array",
                            "items": [
                                {
                                    "type": "object",
                                    "properties": {
                                        "predict_result": {}
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        }
    ]
}

使用自定义依赖包的模型配置文件示例

如下示例中,定义了1.16.4版本的numpy的依赖环境。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
    "model_algorithm": "image_classification",
    "model_type": "TensorFlow",
    "runtime": "python3.6",
    "apis": [
        {
            "url": "/",
            "method": "post",
            "request": {
                "Content-type": "multipart/form-data",
                "data": {
                    "type": "object",
                    "properties": {
                        "images": {
                            "type": "file"
                        }
                    }
                }
            },
            "response": {
                "Content-type": "applicaton/json",
                "data": {
                    "type": "object",
                    "properties": {
                        "mnist_result": {
                            "type": "array",
                            "item": [
                                {
                                    "type": "string"
                                }
                            ]
                        }
                    }
                }
            }
        }
    ],
    "metrics": {
        "f1": 0.124555,
        "recall": 0.171875,
        "precision": 0.00234938928519385,
        "accuracy": 0.00746268656716417
    },
    "dependencies": [
        {
            "installer": "pip",
            "packages": [
                {
                    "restraint": "EXACT",
                    "package_version": "1.16.4",
                    "package_name": "numpy"
                }
            ]
        }
    ]
}
分享:

    相关文档

    相关产品