• 飞桨模型部署至docker并使用FastAPI调用(二)-环境配置与模型部署


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

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

    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    1. 推理代码 – predict.py
    # 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
    
    • 1
    • 2
    • 3
    • 4
    • 5

    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为例
  • 相关阅读:
    牛客网论坛最具争议的 Java 面试成神笔记,GitHub 已下载量已过百万
    Git Bash/GUI Here “找不到应用程序问题
    华为机试 - 查找接口成功率最优时间段
    Java正常加锁但是没有起作用的问题(纪实)
    第十四届蓝桥杯模拟赛(第三期)Excel表
    IDEA工具第一篇:细节使用-习惯设置
    正则表达式
    Ae 效果:CC Slant
    CentOS 7.8 kubeadm安装 k8s 1.26
    Redis笔记
  • 原文地址:https://blog.csdn.net/asd123pwj/article/details/128065117