【深度学习】【部署】Flask快速部署Pytorch模型【入门】

本文介绍了如何使用Flask轻量级框架来部署Pytorch的人脸检测模型Retinaface,通过创建简单的Web服务接口,实现模型的动态加载和预测功能。相较于直接打包exe,此方法能更高效地利用资源。文章还提供了win10环境下将服务打包成exe的步骤,并附带了一个测试脚本以验证服务的正确性。
摘要由CSDN通过智能技术生成

【深度学习】【部署】Flask快速部署Pytorch模型【入门】

提示:博主取舍了很多大佬的博文并亲测有效,分享笔记邀大家共同学习讨论


前言

Django和Flask都是python的服务框架,Flask相较于Django的优势是更加轻量级,因此尝试用Flask构建API服务,Flask快速部署深度学习模型再打包exe与深度学习模型直接打包exe相比,前者模型只需要加载一次权重就可以一直使用,而后者每一次预测都需要重新加载一次模型权重,严重浪费了时间和资源。
打包exe参考
环境安装参考

# 安装flask
pip install flask 
# 安装pytorch环境
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117

搭建简单的Web服务

将以下代码段保存在名为app.py的文件中,并运行代码。

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == "__main__":
	app.run()

在web浏览器中访问http://127.0.0.1:5000/时,查看到Hello World的文本!


搭建深度学习的Web服务

博主以人脸检测之Retinaface算法为例讲解代码:【retinaface-pytorch代码】。有兴趣的朋友可以看看完整的代码,这里博主只保留了与预测相关的代码,并做了部分修改。
app.py中与Web服务相关的部分,博主设置了俩个路由路径:

  1. 一个是/http://127.0.0.1:5000/setmodel–用于修改模型的主干网络及其权重,因为一个检测模型可能有多个backbone,本例是有resnet50和alpha不同的多个mobilenetV1作为backbone。
  2. 另一个是/http://127.0.0.1:5000/predict–用于检测,这里博主保留了源码中图片单一和批量俩种预测方式。
# 初始化模型 默认mobilenetX1.00
Model = retinaface("mobilenetX1.00")
from flask import Flask, request
app = Flask(__name__)
# /setmodel用于修改模型
@app.route('/setmodel', methods=['POST'])
def initialization():
    if request.method == 'POST':
        model_mold = request.form
        Model.setModel(model_mold["model_mold"])
    return "initialization " + model_mold["model_mold"] + " finish"

# /predict用于预测
@app.route('/predict', methods=['POST'])
def predict():
    if request.method == 'POST':
        file = request.form
        keys_Points = Model.detect_image(file['mode'], file['path'])
        return keys_Points

if __name__ == '__main__':
    app.run()

app.py中与Retinaface算法相关的部分,用retinaface类进一步封装了Retinaface模型,设置了俩个方法,detect_image方法对于flask中的predict,setModel方法对应flask中的initialization。

class retinaface(object):
    def __init__(self, model_mold):
        if model_mold == "mobilenetX1.00":
            model_path= r"model_save/mobilenetX1.00.pth"
        elif model_mold == "mobilenetX0.75":
            model_path = r"model_save/mobilenetX0.75.pth"
        elif model_mold == "mobilenetX0.50":
            model_path = r"model_save/mobilenetX0.50.pth"
        elif model_mold == "mobilenetX0.25":
            model_path = r"model_save/mobilenetX0.25.pth"
        else:
            model_path = r"model_save/resnet50.pth"
        self.retinaface = Retinaface(model_path=model_path, backbone=model_mold)

    def detect_image(self, mode, path):
        import os
        if mode == "predict" and (path.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
            while True:
                image = cv2.imread(path)
                if image is None:
                    print('Open Error! Try again!')
                    continue
                else:
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    r_image_points = self.retinaface.detect_image(image).tolist()

        elif mode == "dir_predict" and os.path.isdir(path):
            import os
            import time
            r_image_points = []
            img_names = os.listdir(path)
            for img_name in img_names:
                t = time.time()
                if img_name.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff')):
                    image_path = os.path.join(path, img_name)
                    image = cv2.imread(image_path)
                    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                    r_image_points.append(self.retinaface.detect_image(image).tolist())
        else:
            return []
        return r_image_points

    def setModel(self,model_mold):
        if model_mold == "mobilenetX1.00":
            model_path= r"model_save/mobilenetX1.00.pth"
        elif model_mold == "mobilenetX0.75":
            model_path = r"model_save/mobilenetX0.75.pth"
        elif model_mold == "mobilenetX0.50":
            model_path = r"model_save/mobilenetX0.50.pth"
        elif model_mold == "mobilenetX0.25":
            model_path = r"model_save/mobilenetX0.25.pth"
        else:
            model_path = r"model_save/resnet50.pth"
        self.retinaface = Retinaface(model_path=model_path, backbone=model_mold)

test.py用于HTTP Post请求过程模拟。
"http://localhost:5000/setmodel"加载指定backbone,假设不指定backbone,默认加载mobilenetX1.00。

import requests
resp = requests.post("http://localhost:5000/setmodel",
                     data={'model_mold': 'mobilenetX0.25'})
print(resp.text)

"http://localhost:5000/predict"指定模式mode(单张图片或批量图片)和图片路径path。

import requests
resp = requests.post("http://localhost:5000/predict",
                     data={'mode': 'dir_predict', 'path': 'img'})
print(resp.text)

启动服务后,post发送http请求,默认backbone加载mobilenetX1.00,path指定批量图片的文件夹地址,dir_predict(批量预测图片)mode方式预测所以图片的五个关键点(双眼、鼻子和俩边嘴角)xy像素坐标,源码其实还有框的坐标和高宽,博主不需要就去掉了。
多张图片,每张图片数据保存在一个list里–5个关键点xy像素坐标共10个值。

代码上传】,只保留预测相关文件,功能如下图所示,不做过多讲解,主要讲解部署和使用。

win10下打包成exe(选看)

零基础先参考python程序打包成可执行文件【进阶篇】

# 安装PyInstaller包,5.4.0版本
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple Pyinstaller==5.4.0
# 生成spec文件
pyi-makespec -w app.py

安装了5.7.0及以后版本的Pyinstaller,打包部署启动的时候会报如下错误:

解决方式:pip指定5.4.0版本的安装包安装。
修改app.spec内容:由于打包的主文件是app.py,因此生成的spec文件名称为app.spec,俩者在同一个目录下。

    ['app.py',
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/retinaface.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/nets/layers.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/nets/mobilenet.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/nets/resnet.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/nets/retinaface.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/nets/retinaface_training.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/anchors.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/callbacks.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/config.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/dataloader.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/utils.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/utils_bbox.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/utils_fit.py",
    "E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/utils/utils_map.py",
    ],
    pathex=["E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface"],
    binaries=[  ("E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/model_save/mobilenetX0.25.pth", "model_save"),
                ("E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/model_save/mobilenetX0.50.pth", "model_save"),
                ("E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/model_save/mobilenetX0.75.pth", "model_save"),
                ("E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/model_save/mobilenetX1.00.pth", "model_save"),
                ("E:/deep-learning-for-image-processing-master/face_detector/flask-retinaface/model_save/resnet50.pth", "model_save"),
    ],
# 运行spec文件进行打包
pyinstaller app.spec

完成打包后在pathex指定文件夹下产生build(可以删除)和dist文件夹,所需的可执行文件在dist目录内

双击运行exe,运行test.py进行Post请求,测试结果如下图:


总结

尽可能简单、详细的介绍Flask快速部署深度学习模型的过程。

  • 6
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
要使用Flask部署深度学习模型,你可以按照以下步骤进行操作: 1. 首先,导入所需的库,包括Flask、numpy和pickle。在代码中,你可以看到这些库的导入语句\[1\]。 2. 创建一个Flask应用程序对象,并加载你的深度学习模型。在代码中,你可以看到这些操作\[1\]。 3. 创建一个路由函数,用于处理用户的请求。在代码中,你可以看到这个函数被命名为`predict()`,它接受POST请求,并从请求中获取特征值。然后,将特征值转换为numpy数组,并使用加载的模型进行预测。最后,将预测结果返回给用户。在代码中,你可以看到这些操作\[1\]。 4. 创建一个HTML模板,用于在Web界面上显示表单和预测结果。在代码中,你可以看到这个模板被命名为`page.html`,它包含一个表单,用户可以输入房子的英尺数,并点击按钮进行预测。预测结果将显示在页面上。在代码中,你可以看到这个HTML模板的结构\[2\]。 5. 运行Flask应用程序,并指定端口号。在代码中,你可以看到这个操作\[1\]。 通过以上步骤,你就可以使用Flask部署深度学习模型,并在Web界面上进行房价预测了。这个例子展示了如何使用Flask和HTML模板来创建一个简单的机器学习模型部署应用。你可以根据自己的需求进行修改和扩展。 #### 引用[.reference_title] - *1* *2* *3* [flask部署深度学习pytorch模型](https://blog.csdn.net/zsyyugong/article/details/130822636)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值