当前位置:网站首页>通过 OpenVINO Model Server和 TensorFlow Serving简化部署
通过 OpenVINO Model Server和 TensorFlow Serving简化部署
2022-06-23 12:57:00 【英特尔边缘计算社区】
作者:Milosz Zeglarski
翻译:李翊玮
介绍
在这篇博客中,您将学习如何使用OpenVINO Model Server中的gRPC API对JPEG图像执行推理。Model servers在顺利地将模型从开发环境引入生产方面发挥着重要作用。它们通过网络终结点提供模型,并公开用于与之交互的 API。提供模型后,需要一组函数才能从我们的应用程序调用 API。
OpenVINO Model Server和TensorFlow Serving共享相同的前端API,这意味着我们可以使用相同的代码与两者进行交互。对于Python开发人员来说,典型的起点是使用tensorflow-serving-api pip包。不幸的是,由于这个软件包包含TensorFlow作为依赖项,因此它的占用空间相当大。

迈向轻量级客户之路
由于tensorflow-serving- api及其依赖项的大小约为1.3GB,因此我们决定创建一个轻量级客户端,仅具有执行API调用所需的功能。在最新版本的OpenVINO Model Server中,我们引入Model了Python客户端库的预览版 - ovmsclient。这个新软件包及其所有依赖项都不到100MB - 使其 比tensorflow-serving-api小13倍。

Python 环境比较。请注意与客户端一起安装的包数的差异。
除了具有更大的二进制大小之外,在应用程序中导入tensorflow-serving- api还会消耗更多内存。请参阅下面为执行相同操作的 Python 脚本运行 top 命令的结果 —
一个使用tensorflow-serve-api,另一个使用内存使用情况比较。
包 | VIRT [KB] | RES [KB] | SHR [KB] |
tensorflow-serving-api | 4543760 | 293516 | 155708 |
ovmsclient | 3500296 | 52008 | 23040 |
请注意 RES [KB] 列中的差异,该列指示任务使用的物理内存量。
导入占用空间较大的包也会增加初始化时间,这是使用轻量级客户端的另一个好处。新的Python客户端包也比tensorflow-serving- api更易于使用,因为它为与模型服务器的端到端交互提供了实用程序。使用ovmsclient时,应用程序开发人员不需要知道服务器 API 的详细信息。该软件包为交互的每个阶段提供了一组方便的功能 -从设置连接和进行API调用,到以标准格式解压缩结果。以前,开发人员需要知道哪个服务接受什么类型的请求,或者如何手动准备请求和处理响应。
让我们使用 ovmsclient
要使用 ResNet-50 图像分类模型运行预测,请使用该模型部署 OpenVINO Model Server。您可以使用以下命令执行此操作:
docker run -d --rm -p 9000:9000 openvino/model_server:latest \
--model_name resnet --model_path gs://ovms-public-eu/resnet50-binary \
--layout NHWC:NCHW --port 9000 此命令使用从 Google Cloud Storage 上的公共存储桶下载的 ResNet-50 模型启动服务器。 使用模型服务器侦听 端口 9000 上的 gRPC 调用,您可以开始与服务器交互。接下来,让我们使用 pip 安装 ovmsclient包:
pip3 install ovmsclient在运行客户端之前,下载图像进行分类和相应的ImageNet标签文件,以解释预测结果:
wget https://raw.githubusercontent.com/openvinotoolkit/model_server/main/demos/common/static/images/zebra... wget https://raw.githubusercontent.com/openvinotoolkit/model_server/main/demos/common/python/classes.py

用于预测的斑马的图片
步骤 1:创建与服务器的 gRPC 连接:
现在,您可以打开 Python interpreter并创建 与模型服务器的gRPC 连接。
$ python3
Python 3.6.9 (default, Jul 17 2020, 12:50:27)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>> from ovmsclient import make_grpc_client
>> client = make_grpc_client("localhost:9000")步骤 2:请求模型元数据:
客户端对象有三种方法: get_model_status、get_model_metadata和预测。要创建有效的推理请求,您需要知道模型输入。为此,让我们请求模型元数据:
>> client.get_model_metadata(model_name= "resnet")
{'model_version': 1, 'inputs': {'0': {'shape': [1, 224, 224, 3], 'dtype': 'DT_FLOAT'}}, 'outputs': {'1463': {'shape': [1, 1000], 'dtype': 'DT_FLOAT'}}}步骤 3:将 JPEG 图像发送到服务器:
从模型元数据中,我们了解到模型有一个输入和一个输出。输入名称为“0”,它需要形状为 (1,224,224,3)的数据,并带有浮点数据类型。现在,您拥有了运行推理所需的所有信息。让我们使用在上一步中下载的斑马的图像。在此示例中,我们将使用模型服务器二进制输入功能,该功能只需要加载 JPEG 并请求对编码的二进制文件进行预测 - 使用二进制输入时不需要预处理。
>> with open("zebra.jpeg", "rb") as f:
... img = f.read()
...
>> output = client.predict(inputs={ "0": img}, model_name= "resnet")
>> type(output)
<class 'numpy.ndarray'>
>> output.shape
(1, 1000) 步骤 4:将输出映射到 imagenet 类:
预测成功返回,输出形状为 numpy ndarray (1, 1000) - 与“输出”部分的模型元数据中描述的相同。下一步是解释模型输出并提取分类结果。
输出的形状为 (1, 1000),其中第一个维度表示批大小(处理的图像数),第二个维度表示图像属于每个 ImageNet 类的概率。若要获取分类结果,需要获取输出第二维度中最大值的索引。然后使用imagenet_classes 上一步中下载的 classes.py 字典执行索引号到类名的映射并查看结果。
>>> import numpy as np
>>> from classes import imagenet_classes
>>> result_index = np.argmax(output[0])
>>> imagenet_classes[result_index]
'zebra' 结论
新的 ovmsclient 包比 tensorflow-serving-api 更小,消耗更少的内存,并且更易于使用。在这篇博客中,我们学习了如何获取模型元数据,以及如何通过 OpenVINO 模型服务器中的 gRPC 接口对二进制编码的 JPEG 图像运行预测。
查看有关使用 NumPy 数组运行预测、检查模型状态以及在 GitHub 上使用 REST API 的更多详细信息示例:https://github.com/openvinotoolkit/model_server/tree/main/client/python/ovmsclient/samples
要了解有关 ovmsclient 功能的更多信息,请参阅 API 文档:
https://github.com/openvinotoolkit/model_server/blob/main/client/python/ovmsclient/lib/docs/README.m...
这是客户端库的第一个版本。它将 随着时间的推移而发展,但 已经能够使用 OpenVINO Model Server和TensorFlow Serving运行预测,只需一个最小的Python包。有问题或建议吗?请在 GitHub 上提出问题。
边栏推荐
- Windows install MySQL
- &lt; Sicily&gt; 1001. Rails
- 逆向调试入门-了解PE结构文件
- The way out after the development of Internet technology -- the birth of IVX
- R language uses the multinom function of NNET package to build a disordered multi classification logistic regression model, uses regression coefficients and their standard errors to calculate the valu
- Principle analysis of three methods for exchanging two numbers
- 2022软科大学专业排名出炉!西电AI专业排名超清北,南大蝉联全国第一 !
- .Net怎么使用日志框架NLog
- DBMS in Oracle_ output. put_ How to use line
- Esp32-c3 introductory tutorial problem ⑦ - fatal error: ESP_ Bt.h: no such file or directory ESP not found_ bt.h
猜你喜欢

栈和队列的基本使用

【深入理解TcaplusDB技术】单据受理之事务执行

Quickly understand the commonly used asymmetric encryption algorithm, and no longer have to worry about the interviewer's thorough inquiry

C语言的基本数据类型及其打印输出

Getting started with reverse debugging - learn about PE structure files

支持HomeKit、NFC:智汀智能门锁SL1仅需要149元

那些技术实战中的架构设计方法
![[Yunzhou said live room] - digital security special session will be officially launched tomorrow afternoon](/img/56/a6a9fbba0a9fc212883b469bb857c5.png)
[Yunzhou said live room] - digital security special session will be officially launched tomorrow afternoon

How did Tencent's technology bulls complete the overall cloud launch?

在线文本过滤小于指定长度工具
随机推荐
16 channel HD-SDI optical transceiver multi channel HD-SDI HD video optical transceiver 16 channel 3g-sdi HD audio video optical transceiver
In flinksql, the Kafka flow table and MySQL latitude flow table are left joined, and the association is made according to I'd. false
OS的常见用法(图片示例)
Hanyuan high tech USB2.0 optical transceiver USB2.0 optical fiber extender USB2.0 optical fiber transmitter USB2.0 interface to optical fiber
Restcloud ETL resolves shell script parameterization
RestCloud ETL解决shell脚本参数化
那些技术实战中的架构设计方法
CRMEB 二开短信功能教程
R语言dplyr包mutate_all函数将dataframe中的所有数值数值列(变量)乘以某一固定值并生成新的数据列,为新的数据列(变量)指定自定义后缀名称
How about stock online account opening and account opening process? Is it safe to open a mobile account?
What a talented company that can turn SAP system into a chicken rib!
Service stability governance
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.
服务稳定性治理
R语言使用nnet包的multinom函数构建无序多分类logistic回归模型、使用回归系数及其标准误计算每个系数对应的Z统计量的值、使用pnorm函数计算Z统计量对应的p值判断变量的显著性
Broadcast level E1 to aes-ebu audio codec E1 to stereo audio XLR codec
Homekit and NFC support: smart Ting smart door lock SL1 only costs 149 yuan
The way out after the development of Internet technology -- the birth of IVX
64 channel telephone +2-channel Gigabit Ethernet 64 channel PCM telephone optical transceiver voice telephone to optical fiber
20 years' Shanghai station D question Walker (two points, concise)