在AI Studio 平台训练好的PPYOLOE权重使用FastDeploy 在本地部署

该文介绍了如何使用FastDeploy进行PPYOLOE目标检测模型的CPU部署,包括环境安装、模型权重下载、快速测试和视频推理的改造。FastDeploy支持多种模型的推理,提供了一种便捷的部署方案。
摘要由CSDN通过智能技术生成

PPYOLOE目标检测训练框架使用说明

参考博客:https://blog.csdn.net/qq_41251963/article/details/129667684

AI Studio 地址:https://aistudio.baidu.com/aistudio/projectdetail/5756078?contributionType=1

FastDeploy 环境安装

FastDeploy GitHub地址:https://github.com/PaddlePaddle/FastDeploy

参考readme文件,进行快速安装。安装前提:有Python环境,最好使用Anaconda。

  1. 进入 https://github.com/PaddlePaddle/FastDeploy#fastdeploy-quick-start-python

  1. 如果机器有显卡,可以使用GPU安装,本文演示使用CPU。

  1. 创建虚拟环境

conda create -n fastdeploy python=3.8
  1. 激活虚拟环境

conda activate fastdeploy
  1. 快速安装

1.Install FastDeploy SDK with only CPU support

pip install fastdeploy-python -f https://www.paddlepaddle.org.cn/whl/fastdeploy.html


2. Install FastDeploy SDK with both CPU and GPU support

pip install fastdeploy-gpu-python -f https://www.paddlepaddle.org.cn/whl/fastdeploy.html

本文使用CPU作为演示

  1. 安装成功

  1. 下载AI Studio平台训练的PPYOLOE模型权重

打包当前文件夹,或者进入目录直接下载到本地。下载好的文件如下图所示(4个)。

  1. 使用快速测试来测试模型是否可用

detect.py

# For deployment of GPU/TensorRT, please refer to examples/vision/detection/paddledetection/python
import cv2
import fastdeploy.vision as vision

im = cv2.imread("1679146167823.jpg")
model = vision.detection.PPYOLOE("part_model/model.pdmodel",
                                 "part_model/model.pdiparams",
                                 "part_model/infer_cfg.yml")

result = model.predict(im)
print(result)

vis_im = vision.vis_detection(im, result, score_threshold=0.5)
cv2.imwrite("vis_image.jpg", vis_im)
  1. 执行python detect.py 即可(由于安装CPU版本,这里默认使用OPenVINO进行推理)

其他后端推理

可参考github中readme:https://github.com/PaddlePaddle/FastDeploy/tree/develop/examples/vision/detection/paddledetection

选择ppyoloe的python 部署:

https://github.com/PaddlePaddle/FastDeploy/blob/develop/examples/vision/detection/paddledetection/python/infer_ppyoloe.py

注意修改parse_arguments函数中的参数,

  • --model_dir 导出模型所在路径

  • --image 要预测图像的路径

  • --device 使用cpu还是gpu

  • --use_trt 是否使用tensorrt 推理(如果安装的是GPU环境,建议使用,推理速度更快)

import cv2
import os

import fastdeploy as fd


def parse_arguments():
    import argparse
    import ast
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--model_dir",
        default="part_model",
        help="Path of PaddleDetection model directory")
    parser.add_argument(
        "--image", default='1679146167823.jpg', help="Path of test image file.")
    parser.add_argument(
        "--device",
        type=str,
        default='cpu',
        help="Type of inference device, support 'kunlunxin', 'cpu' or 'gpu'.")
    parser.add_argument(
        "--use_trt",
        type=ast.literal_eval,
        default=False,
        help="Wether to use tensorrt.")
    return parser.parse_args()


def build_option(args):
    option = fd.RuntimeOption()

    if args.device.lower() == "kunlunxin":
        option.use_kunlunxin()

    if args.device.lower() == "ascend":
        option.use_ascend()

    if args.device.lower() == "gpu":
        option.use_gpu()

    if args.use_trt:
        option.use_trt_backend()
    return option


args = parse_arguments()

if args.model_dir is None:
    model_dir = fd.download_model(name='ppyoloe_crn_l_300e_coco')
else:
    model_dir = args.model_dir

model_file = os.path.join(model_dir, "model.pdmodel")
params_file = os.path.join(model_dir, "model.pdiparams")
config_file = os.path.join(model_dir, "infer_cfg.yml")

# 配置runtime,加载模型
runtime_option = build_option(args)
model = fd.vision.detection.PPYOLOE(
    model_file, params_file, config_file, runtime_option=runtime_option)

# 预测图片检测结果
if args.image is None:
    image = fd.utils.get_detection_test_image()
else:
    image = args.image
im = cv2.imread(image)
result = model.predict(im)
print(result)

# 预测结果可视化
vis_im = fd.vision.vis_detection(im, result, score_threshold=0.5)
cv2.imwrite("visualized_result.jpg", vis_im)
print("Visualized result save in ./visualized_result.jpg")

运行成功

本文简单对FastDeploy的使用进行简单的介绍,FastDeploy支持多种模型(分类,检测,语义分割,OCR等)推理。详细使用参加github仓库官方文档。

请尝试将单张图片推理更改为视频推理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐亦亦乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值