通过配置模型提供Servable

通过配置模型提供Servable

MindSpore Serving当前仅支持Ascend 310、Ascend 910和Nvidia GPU环境。

MindSpore Serving的Servable提供推理服务,包含两种类型。一种是推理服务来源于单模型,一种是推理服务来源于多模型组合,多模型组合正在开发中。模型需要进行配置以提供Serving推理服务。

本文将说明如何对单模型进行配置以提供Servable,以下所有Servable配置说明针对的是单模型Servable,Serving客户端简称客户端。

以ResNet-50作为样例介绍如何配置模型提供Servable。

相关概念

预处理和后处理

模型提供推理能力,模型的每个输入和输出的数据类型、数据长度、Shape是固定的。

如果客户端发送的数据不能直接满足模型的输入要求,则需要通过预处理将其转换为满足模型输入的数据。如果模型的输出不是直接提供给客户端,则需要通过后处理将其转换为所需的输出数据。

[En]

If the data sent by the client can not directly meet the input requirements of the model, it needs to be transformed into data that meets the input of the model through preprocessing. If the output of the model is not provided directly to the client, it needs to be converted into the desired output data through post-processing.

以下图是resnet50 Servable数据流程图,描述了图像数据从Serving客户端通过网络传输到Serving,Serving进行预处理、推理和后处理,最后向Serving客户端返回结果:

针对Resnet50推理模型,客户端发来的数据为jpg、png等格式的图片,预期返回图片的分类。Resnet模型输入为经过图片Decode、Resize、Normalize等操作产生的Tensor,输出为每个类别的得分Tensor。需要通过预处理将图片转化为满足模型输入的Tensor,通过后处理返回得分最大的类别名称或者前5类别名称及其得分。

在不同的场景下,如果来自客户端的数据输入组成、结构或类型不同,可以提供不同的预处理。如果对模型的输出也有不同的要求,可以提供不同的后处理。比如上述resnet50 Servable,针对返回得分最大的类别名称还是前5类别名称及其得分这两种场景提供了两个后处理。

上述的resnet Servable提供了classify_top5和classify_top1两个方法(Method)。classify_top5输入为image,输出为label和score,返回前5的分类名称和得分。classify_top1预处理和classify_top5一致,而后处理不同,输入为image,输出为label,返回最大得分的分类名称。

一个Servable可提供一个或多个方法,Servable的名称和方法的名称标记了Serving提供的一个服务,每个方法对客户端提供的数据进行可选的预处理,接着进行模型推理,对模型的推理结果进行可选的后处理,最后将需要的结果返回给客户端。

Servable包含如下内容:

指定可选的预处理和可选的后处理

[En]

Specify optional preprocessing and optional post-processing

定义方法输入、前处理、模型、后处理和方法输出之间的数据流,前者可以作为后者的输入。例如,方法输出的值可以来自方法输入、预处理、模型或后处理。

[En]

Define the data flow between method input, preprocessing, model, post-processing, and method output, the former can be used as the input of the latter. For example, the value of method output can come from method input, preprocessing, model, or post-processing.

指定方法名称,以便客户端可以通过方法名称指定要使用的方法

[En]

Specify the method name so that the client can specify the method to use through the method name

指定方法的输入和输出名称,以便客户端可以指定输入并按名称获取输出。

[En]

Specify the input and output names of the method, so that the client can specify the input and get the output by name.

每个请求可以包括一个或多个实例,每个实例彼此独立,并且结果不会相互影响。例如,一张图片返回一个类别,三张独立的图片分别返回三个类别。

[En]

Each request can include one or more instances, each of which is independent of each other and the results do not affect each other. For example, one picture returns one category, and three independent pictures return three categories independently.

模型配置

以Resnet50模型为例,模型配置文件目录结果如下图所示:

目录resnet50指示Servable的名称。

通过servable_config.py配置Servable,其中包括预处理和后处理定义、模型声明、方法定义。

目录1和2表示版本1和版本2的模型,模型版本为正整数,从1开始,数字越大表示版本越新。

resnet50_1b_cifar10.mindir为模型文件,Servable启动会加载对应版本的模型文件。

预处理和后处理定义

如何定义前处理和后处理的示例如下:

[En]

Examples of how preprocessing and post-processing are defined are as follows:

代码如下:

import numpy as np

import mindspore.dataset.vision.c_transforms as VC

idx_2_label = [‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’]

def preprocess_eager(image):

“””

Define preprocess, input is image numpy, return preprocess result.

Return type can be numpy, str, bytes, int, float, or bool.

Use MindData Eager, this image processing can also use other image processing library,

likes numpy, PIL or cv2 etc.

“””

image_size = 224

mean = [0.4914 * 255, 0.4822 * 255, 0.4465 * 255]

std = [0.2023 * 255, 0.1994 * 255, 0.2010 * 255]

decode = VC.Decode()

resize = VC.Resize([image_size, image_size])

normalize = VC.Normalize(mean=mean, std=std)

hwc2chw = VC.HWC2CHW()

image = decode(image)

image = resize(image)

image = normalize(image)

image = hwc2chw(image)

return image

def postprocess_top1(score):

“””

Define postprocess. This example has one input and one output.

The input is the numpy tensor of the score, and the output is the label str of top one.

“””

max_idx = np.argmax(score)

return idx_2_label[max_idx]

def postprocess_top5(score):

“””

Define postprocess. This example has one input and two outputs.

The input is the numpy tensor of the score. The first output is the str joined by labels of top five,

and the second output is the score tensor of the top five.

“””

idx = np.argsort(score)[::-1][:5] # top 5

ret_label = [idx_2_label[i] for i in idx]

ret_score = score[idx]

return “;”.join(ret_label), ret_score

预处理和后处理定义格式相同,入参为每个实例的输入数据。输入数据为文本时,入参为str对象;输入数据为其他数据类型,包括Tensor、Scalar number、Bool、Bytes时,入参为numpy对象。通过return返回实例的处理结果,return返回的每项数据可为numpy、Python的bool、int、float、str、或bytes数据对象。

模型声明

resnet50 Servabale模型声明示例代码如下所示:

代码如下:

from mindspore_serving.server import register

resnet_model = register.declare_model(model_file=”resnet50_1b_cifar10.mindir”, model_format=”MindIR”, with_batch_dim=True)

其中declare_model入参model_file指示模型的文件名称;model_format指示模型的模型类别,当前Ascend310环境支持OM和MindIR两种模型类型,Ascend910和GPU环境仅支持MindIR模型类型。

如果模型输入和输出第1维度不是batch维度,需要设置参数with_batch_dim=False,with_batch_dim默认为True。

设置with_batch_dim为True,主要针对处理图片、文本等包含batch维度的模型。假设batch_size=2,当前请求有3个实例,共3张图片,会拆分为2次模型推理,第1次处理2张图片返回2个结果,第2次对剩余的1张图片进行拷贝做一次推理并返回1个结果,最终返回3个结果。

另外,对于一个模型,假设其中一个输入是数据输入,包括batch维度信息,另一个输入为模型配置信息,没有包括batch维度信息,此时在设置with_batch_dim为True基础上,设置额外参数without_batch_dim_inputs指定没有包括batch维度信息的输入信息。 例如:

代码如下

from mindspore_serving.server import register

yolov_model = register.declare_model(model_file=”yolov3_darknet53.mindir”, model_format=”MindIR”,

with_batch_dim=True, without_batch_dim_inputs=1)

对于分布式模型,与非分布式单模型配置相比仅声明方法不同,需要使用mindspore_serving.server.distributed.declare_servable,其中入参rank_size表示模型推理使用的device个数,stage_size表示流水线的段数

代码如下:

from mindspore_serving.server import distributed

model = distributed.declare_servable(rank_size=8, stage_size=1, with_batch_dim=False)

方法定义

方法定义的例子如下:

代码如下:

from mindspore_serving.server import register

@register.register_method(output_names=[“label”])

def classify_top1(image): # pipeline: preprocess_eager/postprocess_top1, model

“””Define method classify_top1 for servable resnet50.

The input is image and the output is label.”””

x = register.add_stage(preprocess_eager, image, outputs_count=1)

x = register.add_stage(resnet_model, x, outputs_count=1)

x = register.add_stage(postprocess_top1, x, outputs_count=1)

return x

@register.register_method(output_names=[“label”, “score”])

def classify_top5(image):

“””Define method classify_top5 for servable resnet50.

The input is image and the output is label and score. “””

x = register.add_stage(preprocess_eager, image, outputs_count=1)

x = register.add_stage(resnet_model, x, outputs_count=1)

label, score = register.add_stage(postprocess_top5, x, outputs_count=2)

return label, score

上述代码在Servable resnet50定义了classify_top1和classify_top5方法,其中方法classify_top1入参为image,出参为label,方法classify_top5入参为image,出参为label和score。即,Servable方法的入参由Python方法的入参指定,Servable方法的出参由register_method的output_names指定。

另外方法定义中:

add_stage指示了使用的预处理、模型和后处理,以及它们的输入。

return指示了方法的返回数据,可以是方法的输入或者add_stage的输出,和register_method的output_names参数对应。

用户在客户端使用Servable某个方法提供的服务时,需要通过入参名称指定对应输入的值,通过出参名称识别各个输出的值。比如客户端访问方法classify_top5:

代码如下:

import os

from mindspore_serving.client import Client

def read_images():

“””Read images for directory test_image”””

image_files = []

images_buffer = []

for path, _, file_list in os.walk(“./test_image/”):

for file_name in file_list:

image_file = os.path.join(path, file_name)

image_files.append(image_file)

for image_file in image_files:

with open(image_file, “rb”) as fp:

images_buffer.append(fp.read())

return image_files, images_buffer

def run_classify_top5():

“””Client for servable resnet50 and method classify_top5″””

client = Client(“localhost:5500”, “resnet50”, “classify_top5”)

instances = []

image_files, images_buffer = read_images()

for image in images_buffer:

instances.append({“image”: image}) # input image

result = client.infer(instances)

for file, result_item in zip(image_files, result): # result for every image

label = result_item[“label”] # result label

score = result_item[“score”] # result score

print(“file:”, file)

print(“label result:”, label)

print(“score result:”, score)

if name == ‘ main‘:

run_classify_top5()

Original: https://blog.csdn.net/qq_36893844/article/details/121920187
Author: qq_36893844
Title: 通过配置模型提供Servable

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/514070/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • pandas期末复习

    Pandas(Python Data Analysis Library)是基于NumPy的数据分析模块,它提供了大量标准数据模型和高效操作大型数据集所需的工具,可以说Pandas是…

    人工智能 2023年7月18日
    061
  • [YOLO专题-27]:YOLO V5 小目标检测遇到的问题与常见解决办法

    抵扣说明: 1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。 Original: https:…

    人工智能 2023年5月28日
    080
  • 【OpenCV小练手】-仿造验证码去除干扰因子

    🤖🤖🤖🤖 欢迎浏览本博客 🤖🤖🤖🤖😆😆😆😆😆😆😆我是:我菜就爱学😆😆😆😆😆😆😆一名刚刚入行OpenCV的小白👻👻👻👻🔔🔔我菜就爱学,分享有误,欢迎大佬指出🔔🔔 最近这几天一直在回顾…

    人工智能 2023年6月20日
    070
  • 微软认知服务-语音识别相关

    微软认知服务 在csdn上面看到有微软认知服务的试用,之前正好因为一些需求,接触到了这块的一些东西,正好总结一下,之前使用的是国外的账号,这个登录以后看到是国内世纪互联运营的国内的…

    人工智能 2023年5月25日
    059
  • 数组和链表

    ; 一、计算机内存的工作原理 为了更好的理解数组和链表,先来简单介绍一下计算机内存的工作原理。简单来说:计算机像是很多抽屉的集合体,每个抽屉都有地址。如下图: fe0ffeeb是一…

    人工智能 2023年6月27日
    066
  • DEFORMABLE DETR 论文精度,并解析网络模型结构

    DEFORMABLE DETR: DEFORMABLE TRANSFORMERS FOR END-TO-END OBJECT DETECTION Deformable Detr:用…

    人工智能 2023年7月9日
    066
  • 使用pandas对数据做快速傅里叶变换(FFT)

    最近做实验,要对实验数据做统计分析,所有的实验数据都在一个excel文件里的几个sheet里,需要对每个表里的数据进行傅里叶变换并获得其对应的共轭复数,然后求出频率强度和频率,计算…

    人工智能 2023年6月11日
    075
  • 变分贝叶斯深度学习综述

    ©PaperWeekly 原创 · 作者 |薛博阳 单位 |香港中文大学 研究方向 |语言模型 引言 近年来,贝叶斯深度学习(Bayesian Deep Learn-ing)在诸多…

    人工智能 2023年7月13日
    067
  • 性能测试场景设计之 阶梯性能场景(负载测试场景)

    「负载测试:」 逐步增加并发用户数。看服务器的最大拐点区间在哪里。再缩小拐点区间,找出最大并发用户数。 使用方式: 安装 jpgc插件 每次递增10个并发 This group w…

    人工智能 2023年6月30日
    087
  • 像素格式RGB与YUV

    1.RGB像素格式 RGB彩色模式是一种颜色标准,是通过对红(R)、绿(G)、蓝(B)三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的,这个标准几乎包括了人类视力所能…

    人工智能 2023年6月22日
    0106
  • 单目标追踪——【Transformer】Transformer Tracking

    目录 文章侧重点 网络结构 * 上下文增强模块 交叉特征增强 TransT网络结构 可视化结果分析 * – n=1 n=2 n=3 n=4 Transformer这个香…

    人工智能 2023年6月24日
    076
  • 备战数学建模34-BP神经网络预测2

    目录 一、辛烷值的预测 1-题目分析与原理介绍 2-神经网络建立过程 3-预测结果分析 BP神经网络模型,包含输入层,隐含层和输出层,正向传播过程是通过输入样本到输入层,通过输入层…

    人工智能 2023年6月16日
    065
  • Anaconda虚拟环境安装

    最近刚开始接触深度学习,小白一枚,经老师同学推荐,才知道有优秀的免费教材《动手学深度学习》的中文版,还可以跟着李沐大神的B站视频学习。 刚刚开始第一步:环境安装以及配置。在网上搜索…

    人工智能 2023年6月17日
    0110
  • conda安装GPU版pytorch,结果却是cpu版本[找到问题根源,从容解决]

    conda安装GPU版pytorch,结果却是cpu版本[找到问题根源,从容解决] * – 一、问题描述 – 二、网上解决方案罗列【此节为反面方案罗列!!!…

    人工智能 2023年7月3日
    0117
  • 基于AI的图像处理软件——项目总结

    第三周——配置项目环境、熟悉项目框架第四周——人像抠图功能第五周——图像无损放大、去除遮挡物功能第六周——图像去雾、清晰度增强功能第七周——黑白图像上色、动漫化风格化功能第九周——…

    人工智能 2023年6月22日
    068
  • 浅谈:多光谱和高光谱图像有什么区别?

    多光谱、高光谱甚至是超光谱首先是应用于卫星的遥感,遥感技术已经成为人类获得地球以及其他星球信息重要的手段之一。利用遥感成像系统得到的地球资源信息已成为人类开发、合理的利用、管理和监…

    人工智能 2023年6月20日
    0106
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球