飞桨模型部署至docker并使用FastAPI调用(二)-环境配置与模型部署
创始人
2024-02-19 11:06:14

文章首发及后续更新:https://mwhls.top/4083.html,无图/无目录/格式错误/更多相关请至首发页查看。
新的更新内容请到mwhls.top查看。
欢迎提出任何疑问及批评,非常感谢!

飞桨模型部署至docker并使用FastAPI调用

目录 PDRS 环境配置 飞桨静态图模型部署 参考文献

PDRS 环境配置

  • 安装 numpy:pip install numpy

  • 安装 PDRS (PaddleRS):

    • 具体过程见官方文档。
    1. 下载源码到本地并解压,拖动到 vscode 中
    2. 进入 PDRS 文件夹,安装 requirements.txt:pip install -r requirements.txt
      1. 安装 pycocotools 报错。
    3. 更新 apt:apt-get update
    4. 安装 GCC:apt-get install -y gcc
    5. 再次安装 requirements.txt:pip install -r requirements.txt
      1. 安装 lap 报错。
    6. 安装 G++ pip:apt-get install -y g++
    7. 再次安装 requirements.txt:pip install -r requirements.txt
    8. 安装 paddlepaddle
      1. 官方文档
      2. 安装命令:python -m pip install paddlepaddle-gpu==2.3.0 -i https://mirror.baidu.com/pypi/simple
      3. 我的部署环境只有 CPU,所以不应该用 GPU 版本的。空间占用:PD-GPU: 5.27G, 无PD: 3.04G, PD-CPU: 3.57G
      4. paddle 2.2.2-cpu 安装命令:python -m pip install paddlepaddle==2.2.2 -i https://mirror.baidu.com/pypi/simple
    9. 安装 PDRS:python setup.py install
      1. protobuf 版本过高,报错: Downgrade the protobuf package to 3.20.x or lower.
    10. 降级 protobuf:pip install protobuf~=3.19.0
    11. 再次安装 PDRS:python setup.py install
      1. 报错:ImportError: libGL.so.1: cannot open shared object file: No such file or directory
    12. 安装 libgl1-mesa-glx:apt install libgl1-mesa-glx
    13. 再次安装 PDRS:python setup.py install
      1. 报错:ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
    14. 安装 libglib2.0-0:apt-get install libglib2.0-0
    15. 再次安装 PDRS:python setup.py install

飞桨静态图模型部署

  1. 移动测试图片与静态图模型进 docker。
  2. 目录树:
root
└─ code├─ datasets│  └─ infer│     ├─ before.png│     ├─ label_no_use.png│     └─ later.png├─ inference_model│  ├─ .success│  ├─ model.pdiparams│  ├─ model.pdiparams.info│  ├─ model.pdmodel│  ├─ model.yml│  └─ pipeline.yml├─ main.py├─ predict.py└─ startup.py
  1. 推理代码 – predict.py
    • 参考:PaddleRS变化检测模型部署:以BIT为例
# modified from: https://aistudio.baidu.com/aistudio/projectdetail/4184759
from paddlers.deploy import Predictor
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt

predictor = Predictor(“./code/inference_model”, use_gpu=False)
res = predictor.predict((“./code/datasets/infer/before.png”, “./code/datasets/infer/later.png”))
cm_1024x1024 = res[0][‘label_map’]

print(res)

从左到右依次显示:第一时相影像、第二时相影像、整图推理结果以及真值标签

plt.figure(constrained_layout=True);

plt.subplot(141); plt.imshow(Image.open(“./code/datasets/infer/before.png”)); plt.gca().set_axis_off(); plt.title(“Image1”)

plt.subplot(142); plt.imshow(Image.open(“./code/datasets/infer/later.png”)); plt.gca().set_axis_off(); plt.title(“Image2”)

plt.subplot(143); plt.imshow((cm_1024x1024*255).astype(‘uint8’)); plt.gca().set_axis_off(); plt.title(“Pred”)

plt.subplot(144); plt.imshow((np.asarray(Image.open(“./code/datasets/infer/label_no_use.png”))*255).astype(‘uint8’)); plt.gca().set_axis_off(); plt.title(“GT”)

plt.imshow((cm_1024x1024*255).astype(‘uint8’)); plt.gca().set_axis_off(); plt.title(“Pred”)
plt.show()
plt.savefig(‘./code/datasets/infer/pred.png’)
print(“done”)

  1. 执行推理代码:成功执行,但出现很多警告,且预测图像无结果。

    • 报错:TypeError: __init__() got an unexpected keyword argument 'is_scale'
      • 注释掉 model.yml 中 is_scale 的那行,这个在 aistudio 上不会出报错,但注释该行也不会影响正确结果,应该又是哪个版本不同引起的。
  2. 检查报错并解决:

    1. DeprecationWarning: Please use spmatrix from the scipy.sparse namespace, the scipy.sparse.base namespace is deprecated.
      • 检查发现, aistudio 上的 scipy 版本为 1.6.3,而 docker 中为 1.8.1 ,尝试降级。
      • 降级 scipy:pip install scipy~=1.6.3
    2. DeprecationWarning: BILINEAR is deprecated and will be removed in Pillow 10 (2023-07-01). Use Resampling.BILINEAR instead.

      • 检查发现, aistudio 上的 Pillow 版本为 7.1.2,而 docker 中为 9.1.1 ,尝试降级。

      • 降级 Pillow:`pip install scipy~=7.1.2

    3. WARNING: type object 'QuantizationTransformPass' has no attribute '_supported_quantizable_op_type'
      WARNING: If you want to use training-aware and post-training quantization, please use Paddle >= 1.8.4 or develop version

      • 此警告会在 paddle 版本为 2.3.0 时出现,在2.2.2版本不会出现,上面的安装命令已经改为 paddle 2.2.2 版本的了。
    4. DeprecationWarning: np.object is a deprecated alias for the builtin object. To silence this warning, use object by itself. Doing this will not modify any behavior and is safe.
      • 此警告不是导致预测图像无结果的原因,aistudio 上亦会出现该警告,但结果正常。
    5. [WARNING] Cannot find raw_params. Default arguments will be used to construct the model.
      • 这条警告是红的,但我找不到解决的方法。
    6. 问题突然解决了,我更新了一下推理模型,和原本的模型就差了几KB,突然就正常推理了…可能我前面的操作都是无用功,只是模型出了点大问题罢了。
  3. 清空 pip 缓存:rm -r ~/.cache/pip,容器大小从 3.83G 变为 2.49G。

  4. 推理模型基本完成,接下来修改为适用于 API 的推理方式。

参考文献

  1. ubuntu 安装pycocotools pip inastll pycocotools
  2. 如何在 Ubuntu 18.04 上安装 GCC 编译器
  3. Caffe to Tensorflow (Kaffe by Ethereon) : TypeError: Descriptors should not be created directly, but only retrieved from their parent
  4. [Solved] ImportError: libGL.so.1: cannot open shared object file: No such file or directory
  5. ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
  6. PaddleRS变化检测模型部署:以BIT为例

相关内容

热门资讯

北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...
demo什么意思 demo版本... 618快到了,各位的小金库大概也在准备开闸放水了吧。没有小金库的,也该向老婆撒娇卖萌服个软了,一切只...
世界上最漂亮的人 世界上最漂亮... 此前在某网上,选出了全球265万颜值姣好的女性。从这些数量庞大的女性群体中,人们投票选出了心目中最美...
猫咪吃了塑料袋怎么办 猫咪误食... 你知道吗?塑料袋放久了会长猫哦!要说猫咪对塑料袋的喜爱程度完完全全可以媲美纸箱家里只要一有塑料袋的响...
埃菲尔铁塔在哪 中国仿建埃菲尔... 2019年4月26日,广西南宁市,街头惊现一座巨型山寨版埃菲尔铁塔,高约20米,白色塔身,造型逼真,...
苗族的传统节日 贵州苗族节日有... 【岜沙苗族芦笙节】岜沙,苗语叫“分送”,距从江县城7.5公里,是世界上最崇拜树木并以树为神的枪手部落...
北京的名胜古迹 北京最著名的景... 北京从元代开始,逐渐走上帝国首都的道路,先是成为大辽朝五大首都之一的南京城,随着金灭辽,金代从海陵王...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
脚上的穴位图 脚面经络图对应的... 人体穴位作用图解大全更清晰直观的标注了各个人体穴位的作用,包括头部穴位图、胸部穴位图、背部穴位图、胳...