1. OpenVINO™ Model Server
1.1 介绍
降低AI部署门槛。一种常见的部署方式是,在docker中加入python web框架和openvino推理框架,为了做多线程、负载均衡和扩展,需要添加gunicorn和gevent。1)gunicorn使用pre-fork模式来管理多进程,简单来说是启动多个worker、进行负载均衡、进行失败重启等功能。2)gevent是基于greenlet的轻量级协程网络库,用来支持异步和高并发(不然请求量一上来很容易就超时)。3)此外,在部署到不同的机器上时(异构设备),需要对openvino调用的库文件进行修改;4)为了做横向扩展,需要用到k8s+docker;5)模型更新时,需要更新docker,新增模型时则需要新写一个函数。此外,热更新依赖于k8s。
上面这些可以用model server+k8s来简单应对。OpenVINO面对的AI算法工程师,需要一定的Coding来实现算法引擎,OpenVINO Model Server不用coding,也能完成AI推理部署。
1.2 上手
参考网址https://github.com/openvinotoolkit/model_server/blob/main/docs/ovms_quickstart.md
1) 拉镜像,起server:
docker pull openvino/model_server:latest
docker run -d -v $(pwd)/model:/models/face-detection -p 9000:9000 openvino/model_server:latest \
--model_path /models/face-detection --model_name face-detection --port 9000 --grpc_workers 8 --nireq 8
注意几个必备参数:
- -v和–model_path用来安放模型
- -p和–port用来定义端口
- –grpc_workers和–nireq用来做多进程和异步并发
2)准备模型和数据
下载xml和bin文件,这里用网上的公开模型:
curl --create-dirs https://download.01.org/opencv/2021/openvinotoolkit/2021.1/open_model_zoo/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.xml https://download.01.org/opencv/2021/openvinotoolkit/2021.1/open_model_zoo/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o model/1/face-detection-retail-0004.xml -o model/1/face-detection-retail-0004.bin
把它放在如下的结构下,模型放在版本号下main
models/
└── face-detection
└── 1
├── face-detection-retail-0004.bin
└── face-detection-retail-0004.xml
下载需要用到的推理图片:
curl --create-dirs https://raw.githubusercontent.com/openvinotoolkit/model_server/master/example_client/images/people/people1.jpeg -o images/people1.jpeg
创建一个results文件夹用于存放结果。
3) clinet方式调用
先下载必要文件:
curl https://raw.githubusercontent.com/openvinotoolkit/model_server/master/example_client/client_utils.py -o client_utils.py
curl https://raw.githubusercontent.com/openvinotoolkit/model_server/master/example_client/face_detection.py -o face_detection.py
curl https://raw.githubusercontent.com/openvinotoolkit/model_server/master/example_client/client_requirements.txt -o client_requirements.txt
pip install -r client_requirements.txt
安装必要的库
运行python face_detection.py --batch_size 1 --width 600 --height 400 --input_images_dir images --output_dir results
得到结果:
Running face-detection on files:['people1.jpeg']
Start processing 1 iterations with batch size 1
Request shape (1, 3, 400, 600)
Response shape (1, 1, 200, 7)
image in batch item 0 , output shape (3, 400, 600)
detection 0 [[[0. 1. 1. 0.55241716 0.3024692 0.59122956
0.39170963]]]
x_min 331
y_min 120
x_max 354
y_max 156
detection 1 [[[0. 1. 0.9999999 0.33650026 0.6238751 0.38452044
0.71090096]]]
x_min 201
y_min 249
x_max 230
y_max 284
detection 2 [[[0. 1. 0.99999917 0.22734313 0.34604052 0.2695081
0.44246814]]]
x_min 136
y_min 138
x_max 161
y_max 176
detection 3 [[[0. 1. 0.99999905 0.4421842 0.293691 0.4823516
0.37783983]]]
x_min 265
y_min 117
x_max 289
y_max 151
detection 4 [[[0. 1. 0.9999931 0.51710206 0.5603103 0.5672712
0.65032166]]]
x_min 310
y_min 224
x_max 340
y_max 260
detection 5 [[[0. 1. 0.99996173 0.7985404 0.35696298 0.8411698
0.42877442]]]
x_min 479
y_min 142
x_max 504
y_max 171
detection 6 [[[0. 1. 0.99993885 0.65063936 0.59173524 0.6996588
0.6923708 ]]]
x_min 390
y_min 236
x_max 419
y_max 276
detection 7 [[[0. 1. 0.99990237 0.67253107 0.3239646 0.7202968
0.41418064]]]
x_min 403
y_min 129
x_max 432
y_max 165
detection 8 [[[0. 1. 0.99935323 0.13880333 0.36219725 0.18785533
0.45266667]]]
x_min 83
y_min 144
x_max 112
y_max 181
detection 9 [[[0. 1. 0.9158126 0.3301806 0.37731093 0.3695439
0.44581354]]]
x_min 198
y_min 150
x_max 221
y_max 178
Iteration 1; Processing time: 500.69 ms; speed 2.00 fps
processing time for all iterations
average time: 500.00 ms; average speed: 2.00 fps
median time: 500.00 ms; median speed: 2.00 fps
max time: 500.00 ms; min speed: 2.00 fps
min time: 500.00 ms; max speed: 2.00 fps
time percentile 90: 500.00 ms; speed percentile 90: 2.00 fps
time percentile 50: 500.00 ms; speed percentile 50: 2.00 fps
time standard deviation: 0.00
time variance: 0.00
results中的图如下:
1.3 调用须知
遵循如下格式:
from tensorflow import make_tensor_proto, make_ndarray
import grpc
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2_grpc
channel = grpc.insecure_channel("localhost:9000")
stub = prediction_service_pb2_grpc.PredictionServiceStub(channel)
request = predict_pb2.PredictRequest()
request.model_spec.name = "combined-recognition"
request.inputs["image"].CopyFrom(make_tensor_proto(my_image, shape=(my_image.shape)))
result = stub.Predict(request, 10.0)
age_results = make_ndarray(result.outputs["age"])
使用config文件可以实现pipeline:
{
"model_config_list": [
{
"config": {
"name": "emotions-recognition",
"base_path": "/models/emotions-recognition-retail-0003"
}
},
{
"config": {
"name": "age-gender-recognition",
"base_path": "/models/age-gender-recognition-retail-0013"
}
}
],
"pipeline_config_list": [
{
"name": "combined-recognition",
"inputs": ["image"],
"nodes": [
{
"name": "emotions",
"model_name": "emotions-recognition",
"type": "DL model",
"inputs": [
{"data": {"node_name": "request",
"data_item": "image"}}
],
"outputs": [
{"data_item": "prob_emotion",
"alias": "emotion"}
]
},
{
"name": "age-gender",
"model_name": "age-gender-recognition",
"type": "DL model",
"inputs": [
{"data": {"node_name": "request",
"data_item": "image"}}
],
"outputs": [
{"data_item": "age_conv3",
"alias": "age"},
{"data_item": "prob",
"alias": "gender"}
]
}
],
"outputs": [
{"age": {"node_name": "age-gender",
"data_item": "age"}},
{"gender": {"node_name": "age-gender",
"data_item": "gender"}},
{"emotion": {"node_name": "emotions",
"data_item": "emotion"}}
]
}
]
}
调用时增加一个–config_path选项
docker run -d -p 9000:9000 -v ${PWD}/models:/models -v ${PWD}/config.json:/config.json openvino/model_server --config_path /config.json --port 9000
https://github.com/openvinotoolkit/model_server/tree/main/docs下有更多示例。
2. DL workbench
2.1 介绍
DL Workbench 是一个基于 Web 的图形环境,可让您轻松使用各种复杂的 OpenVINO™ 工具套件组件:
- 模型下载器用于从包含适用于各种不同任务的预训练模型的英特尔® Open Model Zoo 中下载模型
- 模型优化器将模型转换成中间表示文件 (IR) 格式
- 训练后优化工具套件 对模型进行微调,然后以 INT8 的精度执行
- 准确率检查程序用于确定模型的准确率
- 基准测试工具用于估算受支持设备上的推理性能
2.2 上手
用下面的脚本安装并启动
wget https://raw.githubusercontent.com/openvinotoolkit/workbench_aux/master/start_workbench.sh && bash start_workbench.sh
首页如下:
运行完基准测试后如下:
一些常用总结:
2.3 部署与使用
首先可以进行一下测试。注意整个项目是在docker里面的,除非设置的-v选项,否则是没有本地目录的。
可以使用jupyterlab打开运行,里面有个tutorials可以用
这个tab可以打包输出,里面包含了运行openvino的必要api
3. Analytics Zoo
3.1 介绍
可以简单理解为:超大规模的数据流水线。流水线架构可以是spark也可以是flink。
下图是美的的一个工业项目,他们希望通过计算机视觉将其生产线生产出来的产品自动识别瑕疵、没有贴标签等。
3.2 应用案例
见
https://blog.csdn.net/kittyzc/article/details/108270358