当前位置:网站首页>tensorflow/serving deploys keras's h5 model service
tensorflow/serving deploys keras's h5 model service
2022-08-07 21:23:00 【Deep Learning with Keras】
当我们使用keras训练好模型之后,The next step is to deploy services,采用flask直接加载keras的h5模型,Service of concurrent performance is low.If the pursuit of high concurrency performance,就可以采用Nginx+gunicorn+gevent的方式来启动服务,Service though this way can achieve very high concurrency performance,但却存在一个问题,也就是gunicornWay is initiated the process of multiple,Each process will load a model,Cause the service starts the memory a lot,The other direct loadingh5模型,The whole process as memory in use process will also gradually increase,这就是pythonThe memory leak problem.I a project before deployment to a production environment,使用了两天,Memory has reached a dozenG,Therefore have to use every time to restart services this strategy.因此采用tensorflow/serving的方式来部署服务.
This article USES the environment:
python 3.8.13
TensorFlow 2.50
1 keras的h5模型转换为pb格式
Here is a verification code recognition model transformation,Verification code identification usingcrnn+ctx的形式训练,The model file ash5格式.
使用如下代码将h5模型转换为tensorflow/serving需要的pb格式文件:
from keras import models
import keras.backend as K
from tensorflow.python.framework import graph_util
import tensorflow._api.v2.compat.v1 as tf
tf.disable_eager_execution()
''' tf.saved_model.builder.SavedModelBuilder 这种模型转换的模型可以提供给TensorFlow Serving使用 多一个Variable文件 '''
def keras_model_to_tfs(model, export_path):
signature = tf.saved_model.signature_def_utils.predict_signature_def(
inputs={
'input_x': model.input},
outputs={
'output_y': model.output}
)
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
builder.add_meta_graph_and_variables(
sess=K.get_session(),
tags=[tf.saved_model.tag_constants.SERVING],
signature_def_map={
tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: signature,
},
legacy_init_op=legacy_init_op)
builder.save()
print('转换完成!')
if __name__=='__main__':
network=models.load_model('./model/hebei.h5')
export_path='pb2'
keras_model_to_tfs(network,export_path)
运行如上代码,得到转换好的pb文件.
重复以上过程,To convert a model,新建如下目录yzmModels,The directory tree structure for:
hebei目录下的100001Directory of the files for the transformation of the first model;
chongqing目录下的100001Directory of the files for the transformation of the second model;
models.config的文件内容如下:
model_config_list:{
config:{
name:"hebei",
base_path:"/models/multiModel/hebei",
model_platform:"tensorflow"
},
config:{
name:"chongqing",
base_path:"/models/multiModel/chongqing",
model_platform:"tensorflow"
}
}
2 使用docker启动tensorflow/serving服务
将yzmModelsThis directory is uploaded to the server,如下所示:

拉取一个tensorflow/serving的docker镜像
docker pull tensorflow/serving:2.5.1
接着启动docker服务:
docker run -p 9501:8501 --mount type=bind,source=/root/hhc_etax_login/yzm/yzmModels,target=/models/multiModel -t tensorflow/serving:2.5.1 --model_config_file=/models/multiModel/models.config &
9501Mainly on the port,8501For in a container port,容器中的端口8501This cannot be changed.source=/root/hhc_etax_login/yzm/yzmModelsThis directory is given priority to on the model file directory.启动成功后,如下所示:
Service after open is good,We have to test theTensorFlow/servingWhether the service operation success,Container service corresponding to the host port for9501,So using the following commands to test:
curl http://localhost:9501/v1/models/hebei
返回结果如下:
{
"model_version_status": [
{
"version": "100001",
"state": "AVAILABLE",
"status": {
"error_code": "OK",
"error_message": ""
}
}
]
}
使用python代码来测试hebei这个模型,TensorFlow/servingTransmit the data service is in the form of matrix,代码如下:
import numpy as np
import requests
import json
if __name__=='__main__':
#(1, 32, 77, 3)
#img_array代表图像数据,Is introduced to model the input data
img_array=np.random.randn(1,32,77,3)
#An array data into list format
img_list=img_array.tolist()
json_data={
'instances':img_list
}
url='http://localhost:9501/v1/models/hebei:predict'
res=requests.post(url=url,json=json_data)
if res.status_code==200:
#To deal with the predictions of a model
predictions=json.loads(res.text).get('predictions')
if predictions:
y_pred=np.array(predictions)
print(y_pred.shape)
如上所示,因为对于hebeiModel of the shape of the input image data format as(1,32,77,3),In the code constructs a this data,In the use of models to predict captcha images,This place was introduced into matrix of the real image data can be.运行结果如下:
边栏推荐
- 连续出现的数字[如何完美处理连续特定记录问题?]
- 在阿里云进行磁盘扩容操作后,ECS实际使用磁盘并没有扩容?阿里云ECS磁盘扩容操作
- 大智慧开户安全吗?
- Unity hot update Addressables to modify the resource cache path (download path)
- 无法访问打印机怎么办 共享打印机无法访问权限怎么解决
- 现在网上开户安全?手机开户哪个证券公司佣金最低?
- leetcode 232. Implement Queue using Stacks 用栈实现队列(简单)
- 微服务重启脚本
- 【每日一题】636. 函数的独占时间
- 《MySQL核心知识》第7章:插入、更新、删除
猜你喜欢

webstrom 插件开发(二)

怎么打开win7任务管理器的六种最详细方法

Unity's latest UnityWebRequest download, while displaying the download progress, and displaying the network speed, posted it today to share with you

【Day10】进程管理命令

电脑上怎么登陆两个微信_电脑上怎么登录两个微信

How to change the default opening method How to change the opening method of a file

Unity 最新UnityWebRequest下载,同时显示下载进度,和 显示网速,今天贴出来和大家分享

Use phpstudy (small skin panel) to configure the record of the local PHP development environment

一图了解原码、反码、补码的演进历史

无法访问打印机怎么办 共享打印机无法访问权限怎么解决
随机推荐
数据库连接池
一图了解原码、反码、补码的演进历史
苹果怎么装双系统 苹果电脑双系统安装教程
2022win7旗舰版永久激活密钥 win7旗舰版最新免费激活码分享
偶像工场,开启虚拟人数字藏品新时代
一次对BC网站的渗透测试实战
《微信小程序-进阶篇》Lin-ui组件库源码分析-动画组件Transition(二)
leetcode 232. Implement Queue using Stacks 用栈实现队列(简单)
怎么打开win7任务管理器的六种最详细方法
Unity hot update Addressables to modify the resource cache path (download path)
7zip自带hash校验功能
redis-集群-2-哨兵模式
打印机怎么停止打印以前的任务 怎么取消打印机正在打印的文件
City Safety Series Science | Alarm bells for electric shock accidents, remember to cut off the power first to save people
大智慧开户安全吗?
golang方法的使用细节:参数默认是值拷贝,不仅仅是struct自定义数据类型也可以绑定方法、方法名称首字母大写为public权限、String()方法的使用
Neo4j 基本使用创建并启动数据库
win7截图快捷键ctrl加什么 电脑任意截图的按键组合有哪些
【目标检测】YOLOv5:添加漏检率和虚检率输出
[The Beauty of Software Engineering - Column Notes] "One Question and One Answer" Issue 4 | 14 Common Problem Solving Strategies for Software Development