一、各种部署方式特点及注意事项

简称

  • hubserving=PaddleHub Serving

  • pdserving=PaddleHub Serving

  • hub install =指通过paddlehub库直接安装部署服务

部署方式

  • 都是基于Docker进行部署,Docker环境搭建参照该博文

  • 如果你对Docker比较熟悉,可以直接拉取该项目,对应的方式及版本都做了归类

    git pull https://github.com/steinvenic/PaddleOCR-Docker.git

    如果被墙可以使用镜像站git pull https://github.com.cnpmjs.org/steinvenic/PaddleOCR-Docker.git

各服务特点

  • pdserving更适合企业级部署,性能更高,摘取官方的介绍:

支持客户端和服务端之间高并发和高效通信

支持 工业级的服务能力 例如模型管理,在线加载,在线A/B测试等

支持 多种编程语言 开发客户端,例如C++, Python和Java

  • hubserving可以理解为源码方式安装某个服务,配置型强

  • hub install 其实和hubserving方式一样,都使用paddlehub库进行部署的,其首次进行识别的的时候,会自动下载模型文件,真正一条命令就可以运行起来一个服务,部署起来超简单。

    不足之处在于我现在还没找到对于相关模块的配置。只有paddlehub自己的一些启动参数可配置。如果你的显卡比较好,又想快点部署,优先考虑采用本方式。低端显卡要是使用此种方式,会导致显存迅速拉满,然后不可用,我现在也没找到解决办法。

    相同配置的硬件,使用该种方式,CPU版相较于hubserving明显速度变慢很多,估计是某些参数没设置好,请酌情使用,GPU版本未测试

    除了本文的OCR,还有很多有趣的服务通过此种方式可快速搭建,详见

注意事项

  • 相同价格的硬件,识别速度上还是GPU速度更快,优先选择GPU
  • 部署方式上优先选择pdserving方式
  • 如果你没有显卡,只能用CPU的话,一定要确认你的CPU要支持AVX指令集,验证方法:lscpu | grep avx

    没有AVX指令的话,部署起来比较困难,而且识别速度应该会很慢。

    如果你真想部署的话,要安装对应的noavx版本的paddlepaddlewhl在这并且只能使用Python3.8,这个我暂时没时间验证是否能安装成功。等后面有时间了再研究一下...
  • 当你使用wget获取资源的时候,如果发现速度很慢,只有几十KB,你可以尝试一下Ctrl+C取消后再重新获取,这个问题是什么导致的我也不清楚
  • 如果你使用的是阿里云或者其他(非百度)的云平台获取资源的时候,速度奇慢,我想是被百度限制了,这时候你可能需要在本地下载好再传到你机器上,当然你也可以使用代理的方式
  • 当你真想部署成一个可靠的服务时,GPU显存我感觉最少要16 GB
  • 当你在阿里云上使用pdserving方式部署,有可能遇到显存被瞬间填满,机器卡死的情况。我本以为是PaddleServing造成的现存泄露,我也一直在纠结这个问题。但在百度的aistudio上,同样16G显存,aistudio是正常的

二、pdserving方式部署

GPU

Dockerfile:

FROM paddlepaddle/paddle:2.1.0-gpu-cuda10.2-cudnn7
LABEL maintainer="steinven@qq.com"
LABEL version="1.0"
LABEL description="PaddleOCR pdserving GPU version" #github网速太慢或被墙,现用的cnpmjs加速,也可以更换为码云
ENV REPO_LINK=https://github.com.cnpmjs.org/PaddlePaddle/PaddleOCR.git #模型数据
ENV orc_detect_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar
ENV orc_recognition_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar #whl包,开发测试阶段,未上传到pypi
ENV paddle_serving_client_test=https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_client-0.0.0-cp37-none-any.whl #安装所需的库文件
RUN pip3.7 install --upgrade pip paddle-serving-server-gpu==0.6.1.post101 paddle-serving-app==0.6.1 -i https://mirror.baidu.com/pypi/simple \
&& git clone $REPO_LINK /PaddleOCR \
&& pip3.7 install -r /PaddleOCR/requirements.txt -i https://mirror.baidu.com/pypi/simple #下载模型数据并解压
WORKDIR /PaddleOCR/deploy/pdserving
ADD $orc_detect_model .
ADD $orc_recognition_model .
ADD $paddle_serving_client_test .
RUN for f in *.tar; do tar xf "$f"; done;rm -fr *.tar \
&& pip3.7 install paddle_serving_client-0.0.0-cp37-none-any.whl \
&& python3.7 -m paddle_serving_client.convert --dirname ./ch_ppocr_mobile_v2.0_det_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_det_mobile_2.0_serving/ \
--serving_client ./ppocr_det_mobile_2.0_client/ \
&& python3.7 -m paddle_serving_client.convert --dirname ./ch_ppocr_mobile_v2.0_rec_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_rec_mobile_2.0_serving/ \
--serving_client ./ppocr_rec_mobile_2.0_client/ \
&& rm -fr *.tar EXPOSE 9998 ENTRYPOINT ["/bin/bash","-c","python3.7 web_service.py"]

构建镜像

docker build -t pdserving_gpu:v1 .

运行

docker run -itd --network=host --gpus all --name pdserving_gpu pdserving_gpu:v1

修改配置

查看运行日志发现没有错误后,低端显卡的话,别先进行测试。我们需要修改一下配置文件。

默认的配置对显卡要求较高,需要修改一下QPS,默认的两个并发参数分别为8、4,现在拿我的GeForce 750 2GB显存,我需要把它改成2、1

  • 进入容器:docker exec -it pdserving_gpu /bin/bash
  • 打开配置文件vim /PaddleOCR/deploy/pdserving/config.yml,找到下图对应的两个参数,进行修改

  • 重启docker容器:docker restart pdserving_gpu
  • 测试:
# coding:utf-8
import base64
import json
import os import requests def cv2_to_base64(image):
return base64.b64encode(image).decode('utf8') url = "http://172.16.71.33:9998/ocr/prediction"
test_img_dir = r"C:\Users\eric\Desktop\pre_ocr_images"
for idx, img_file in enumerate(os.listdir(test_img_dir)):
with open(os.path.join(test_img_dir, img_file), 'rb') as file:
image_data1 = file.read() image = cv2_to_base64(image_data1) for i in range(1):
data = {"key": ["image"], "value": [image]}
r = requests.post(url=url, data=json.dumps(data))
print(r.json()) print("==> total number of test imgs: ", len(os.listdir(test_img_dir)))

CPU

Dockerfile:

FROM paddlepaddle/paddle:2.1.0
LABEL maintainer="steinven@qq.com"
LABEL version="1.0"
LABEL description="PaddleOCR pdserving CPU version" #github网速太慢或被墙,现用的cnpmjs加速,也可以更换为码云
ENV REPO_LINK=https://github.com.cnpmjs.org/PaddlePaddle/PaddleOCR.git #模型数据
ENV orc_detect_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar
ENV orc_recognition_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar #whl包,开发测试阶段,未上传到pypi
ENV paddle_serving_client_test=https://paddle-serving.bj.bcebos.com/test-dev/whl/paddle_serving_client-0.0.0-cp37-none-any.whl #安装所需的库文件
RUN pip3.7 install --upgrade pip paddle-serving-server==0.6.1 paddle-serving-app==0.6.1 -i https://mirror.baidu.com/pypi/simple \
&& git clone $REPO_LINK /PaddleOCR \
&& pip3.7 install -r /PaddleOCR/requirements.txt -i https://mirror.baidu.com/pypi/simple #下载模型数据并解压
WORKDIR /PaddleOCR/deploy/pdserving
ADD $orc_detect_model .
ADD $orc_recognition_model .
ADD $paddle_serving_client_test .
RUN for f in *.tar; do tar xf "$f"; done;rm -fr *.tar \
&& pip3.7 install paddle_serving_client-0.0.0-cp37-none-any.whl \
&& python3.7 -m paddle_serving_client.convert --dirname ./ch_ppocr_mobile_v2.0_det_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_det_mobile_2.0_serving/ \
--serving_client ./ppocr_det_mobile_2.0_client/ \
&& python3.7 -m paddle_serving_client.convert --dirname ./ch_ppocr_mobile_v2.0_rec_infer/ \
--model_filename inference.pdmodel \
--params_filename inference.pdiparams \
--serving_server ./ppocr_rec_mobile_2.0_serving/ \
--serving_client ./ppocr_rec_mobile_2.0_client/ \
&& rm -fr *.tar EXPOSE 9998 ENTRYPOINT ["/bin/bash","-c","python3.7 web_service.py"]

构建镜像

docker build -t pdserving_cpu:v1 .

运行

docker run -itd --network=host --name pdserving_cpu pdserving_cpu:v1

后续测试请参见上方的GPU版本,不再赘述

三、hubserving方式部署

GPU版

Dockerfile

FROM paddlepaddle/paddle:2.1.0-gpu-cuda10.2-cudnn7
LABEL maintainer="steinven@qq.com"
LABEL version="1.0"
LABEL description="PaddleOCR hubserving GPU version" #github网速太慢或被墙,现用的cnpmjs加速,也可以更换为码云
ENV REPO_LINK=https://github.com.cnpmjs.org/PaddlePaddle/PaddleOCR.git #模型数据,现用的ch_ppocr_mobile_v2.0_xx,为中英文超轻量OCR模型,因为源码参数中配置的就为该模型,
#所以不用修改源码。如果切换为服务端模型,记得修改deploy/hubserving/ocr_system/params.py下对应的模型位置
ENV orc_detect_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar
ENV orc_direction_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
ENV orc_recognition_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar #安装所需的库文件
RUN pip3.7 install --upgrade pip paddlehub -i https://mirror.baidu.com/pypi/simple \
&& git clone $REPO_LINK /PaddleOCR \
&& pip3.7 install -r /PaddleOCR/requirements.txt -i https://mirror.baidu.com/pypi/simple \
&& mkdir -p /PaddleOCR/inference #下载模型数据并解压
WORKDIR /PaddleOCR/inference/
ADD $orc_detect_model .
ADD $orc_direction_model .
ADD $orc_recognition_model .
RUN for f in *.tar; do tar xf "$f"; done;rm -fr *.tar
WORKDIR /PaddleOCR
EXPOSE 8868 ENTRYPOINT ["/bin/bash","-c","export CUDA_VISIBLE_DEVICES=0 && hub install deploy/hubserving/ocr_system/ && hub serving start -c deploy/hubserving/ocr_system/config.json "]

构建docker镜像

docker build -t hubserving_gpu:v1 .

运行

docker run -itd --network=host --gpus all --name hubserving_gpu hubserving_gpu:v1

检查运行状态,查看是否有错误,查看端口号

docker logs -f hubserving_gpu

客户端测试:

# coding:utf-8
import base64
import json
import os
import traceback import cv2
import requests test_img_dir = './imgs/1' def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8') for idx, img_file in enumerate(os.listdir(test_img_dir)):
try:
data = {'images': [cv2_to_base64(cv2.imread(os.path.join(test_img_dir, img_file)))]}
headers = {"Content-type": "application/json"}
url = "http://172.16.71.33:8868/predict/ocr_system"
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.text)
print(r.json()["results"])
except:
traceback.print_exc()
continue

CPU版

Dockerfile

FROM paddlepaddle/paddle:2.1.0
LABEL maintainer="steinven@qq.com"
LABEL version="1.0"
LABEL description="PaddleOCR hubserving CPU version" #github网速太慢或被墙,现用的cnpmjs加速,也可以更换为码云
ENV REPO_LINK=https://github.com.cnpmjs.org/PaddlePaddle/PaddleOCR.git #模型数据,现用的ch_ppocr_mobile_v2.0_xx,为中英文超轻量OCR模型,因为源码参数中配置的就为该模型,
#所以不用修改源码。如果切换为服务端模型,记得修改deploy/hubserving/ocr_system/params.py下对应的模型位置
ENV orc_detect_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar
ENV orc_direction_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
ENV orc_recognition_model=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar #安装所需的库文件
RUN pip3.7 install --upgrade pip paddlehub -i https://mirror.baidu.com/pypi/simple \
&& git clone $REPO_LINK /PaddleOCR \
&& pip3.7 install -r /PaddleOCR/requirements.txt -i https://mirror.baidu.com/pypi/simple \
&& mkdir -p /PaddleOCR/inference #下载模型数据并解压
WORKDIR /PaddleOCR/inference/
ADD $orc_detect_model .
ADD $orc_direction_model .
ADD $orc_recognition_model .
RUN for f in *.tar; do tar xf "$f"; done;rm -fr *.tar
WORKDIR /PaddleOCR
EXPOSE 8868 ENTRYPOINT ["/bin/bash","-c","hub install deploy/hubserving/ocr_system/ && hub serving start -m ocr_system -p 8868"]

构建镜像

docker build -t hubserving_cpu:v1 .

运行

docker run -itd --network=host --name hubserving_cpu hubserving_cpu:v1

后续测试请参见上方的GPU版本,不再赘述

四、hub install方式部署

GPU

Dockerfile

FROM paddlepaddle/paddle:2.1.0-gpu-cuda10.2-cudnn7
LABEL maintainer="steinven@qq.com"
LABEL version="1.0"
LABEL description="hub install GPU version" ENV CUDA_VISIBLE_DEVICES=0 #安装所需的库文件
RUN pip3.7 install --upgrade pip paddlehub shapely pyclipper -i https://mirror.baidu.com/pypi/simple ENTRYPOINT ["/bin/bash","-c","hub serving start -m chinese_ocr_db_crnn_server"]

构建镜像

docker build -t hub_install_gpu:v1 .

运行

docker run -itd --network=host --gpus all --name hub_install_gpu hub_install_gpu:v1

测试

# coding:utf-8
import base64
import json
import os import cv2
import requests def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8') url = "http://172.16.71.33:8866/predict/chinese_ocr_db_crnn_server"
test_img_dir = './images1'
for idx, img_file in enumerate(os.listdir(test_img_dir)):
data = {'images': [cv2_to_base64(cv2.imread(os.path.join(test_img_dir, img_file)))]}
headers = {"Content-type": "application/json"}
r = requests.post(url=url, headers=headers, data=json.dumps(data))
print(r.json()["results"])

CPU

Dockerfile

FROM paddlepaddle/paddle:2.1.0
LABEL maintainer="steinven@qq.com"
LABEL version="1.0"
LABEL description="hub install CPU version" #安装所需的库文件
RUN pip3.7 install --upgrade pip paddlehub shapely pyclipper -i https://mirror.baidu.com/pypi/simple ENTRYPOINT ["/bin/bash","-c","hub serving start -m chinese_ocr_db_crnn_server"]

构建镜像

docker build -t hub_install_cpu:v1 .

运行

docker run -itd --network=host --name hub_install_cpu hub_install_cpu:v1

后续测试请参见上方的GPU版本,不再赘述

PaddleOCR系列(二)--hubserving & pdserving & hub install的更多相关文章

  1. ANDROID Porting系列二、配置一个新产品

    ANDROID Porting系列二.配置一个新产品 详细说明 下面的步骤描述了如何配置新的移动设备和产品的makefile运行android. 1.         目录//vendor/创建一个公 ...

  2. ldap配置系列二:jenkins集成ldap

    ldap配置系列二:jenkins集成ldap jenkins简介 jenkins是一个独立的.开放源码的自动化服务器,它可以用于自动化与构建.测试.交付或部署软件相关的各种任务. jenkins官方 ...

  3. 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x-packV5.4.2安装

    相关博文: 搜索引擎ElasticSearchV5.4.2系列一之ES介绍 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+klanaV5.4.2+x-p ...

  4. Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群

    Redis总结(五)缓存雪崩和缓存穿透等问题   前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...

  5. SonarQube系列二、分析dotnet core/C#代码

    [前言] 本系列主要讲述sonarqube的安装部署以及如何集成jenkins自动化分析.netcore项目.目录如下: SonarQube系列一.Linux安装与部署 SonarQube系列二.分析 ...

  6. Grafana +Zabbix 系列二

    Grafana +Zabbix 系列二 Grafana 简介补充 Grafana自身并不存储数据,数据从其他地方获取.需要配置数据源 Grafana支持从Zabbix中获取数据 Grafana优化图形 ...

  7. 前端构建大法 Gulp 系列 (二):为什么选择gulp

    系列目录 前端构建大法 Gulp 系列 (一):为什么需要前端构建 前端构建大法 Gulp 系列 (二):为什么选择gulp 前端构建大法 Gulp 系列 (三):gulp的4个API 让你成为gul ...

  8. WPF入门教程系列二十三——DataGrid示例(三)

    DataGrid的选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改 ...

  9. Web 开发人员和设计师必读文章推荐【系列二十九】

    <Web 前端开发精华文章推荐>2014年第8期(总第29期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各类能够提升网站用户体验的优秀 jQuery 插件,展示前沿的 HTML5 ...

随机推荐

  1. XCTF练习题---MISC---reverseMe

    XCTF练习题---MISC---reverseMe flag:flag{4f7548f93c7bef1dc6a0542cf04e796e} 解题步骤: 1.观察题目,下载附件 2.拿到手以后发现是个 ...

  2. create-react-app的TS支持以及css模块化

    开始: 利用官方脚手架,搭建react工程.参考:https://react.docschina.org/docs/create-a-new-react-app.html. 过程: 1.暴露webpa ...

  3. react实战系列 —— 我的仪表盘(bizcharts、antd、moment)

    其他章节请看: react实战 系列 My Dashboard 上一篇我们在 spug 项目中模仿"任务计划"模块实现一个类似的一级导航页面("My任务计划") ...

  4. Linux强制用户首次登录修改密码

    一个执着于技术的公众号 地方 前言 Linux强制用户首次登陆修改密码,这应该是RHCE认证中用户管理部分, 属于很基础的内容了.可是我忘记了,所以就有了下面的记录~ 实验过程 1.创建用户并设置登录 ...

  5. a commponent required a bean of type XXXXXX that could not be found-2022新项目

    一.问题由来 目前刚入职一家新公司不久,公司的新项目采用DDD驱动领域设计来进行开发,架构这一块使用的是阿里巴巴开源的最新框架COLA4.0的架构. 主要是这个框架里面的分层设计.主要分为四层:ada ...

  6. 隔离这几天开发了一个带控制台的OAuth2授权服务器分享给大家

    停更这些天,业余时间和粉丝群的几个大佬合作写了一个基于Spring Authorization Server的OAuth2授权服务器的管理控制台项目Id Server,我觉得这个项目能够大大降低OAu ...

  7. Slab 分配器

    1.什么是Slab 分配器: 以下摘自维基百科:https://en.wikipedia.org/wiki/Slab_allocation Slab  firstly introduced in ke ...

  8. MongoDB 分片集群

    每日一句 Medalist don't grow on trees, you have to nurture them with love, with hard work, with dedicati ...

  9. mac安装git、node

    1.需要先安装homebrew(之前的文章里有) 2.安装git brew install git 3.安装node brew install node 3.1.安装成功后,查看版本号 node -v ...

  10. 关于VHDL中case语句多执行语句的书写方式(转载stackoverflow.com并做翻译汇总)

    很多国内的教材对于case语句的讲解非常单一,比如: 1 [标号:]CASE 多值表达式 IS 2 WHEN 选择值 => 被赋值变量 <=赋值变量: 3 WHEN 选择值 => 被 ...