当前位置: 首页 > news >正文

YOLOv11快速上手:如何在本地使用TorchServe部署目标检测模型

引言

YOLOv11 是最新的目标检测模型,以其高效和准确著称,广泛应用于图像分割、姿态估计等任务。本文将详细介绍如何使用 YOLOv11 训练你的第一个目标检测模型,并通过 TorchServe 在本地进行部署,实现模型的快速推理。

环境准备

在开始之前,确保你的开发环境满足以下要求:
Python 版本:3.8 或以上
PyTorch:1.9 或以上
CUDA:如果使用 GPU,加速训练和推理
TorchServe:用于模型的部署和服务

使用 TorchServe 部署模型

1.模型训练

模型训练看这篇: YOLOv11 快速上手:训练你的第一个目标检测模型

2. 导出模型为 TorchScript

将训练好的模型导出为 TorchScript 格式:

模型格式转换看这篇: 一文带你了解 YOLOv11:入门操作与配置详解

3. 创建模型存档

使用 torch-model-archiver 创建模型存档:

torch-model-archiver --model-name sycmore --version 1.0 --serialized-file ./yolo11n.torchscript.pt --handler ./handler.py --extra-files ./handler.py -f

运行完上述命令之后会在当前目录生成一个.mar文件

注意:需要编写一个自定义的 handler.py,用于定义模型的输入和输出处理。

可加Q群从群文件获取handler.py文件,群号:700511185
可加Q提供付费咨询/远程协助,Q:1005270560

4. 启动 TorchServe

将模型存档移动到模型库目录,并启动 TorchServe:

torchserve --start --ncs --model-store ./ --models sycmore.mar

输出如下所示:

(base) [root@k8s-work3 ultralytics]# WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
2024-10-22T11:04:44,293 [WARN ] main org.pytorch.serve.util.ConfigManager - Your torchserve instance can access any URL to load models. When deploying to production, make sure to limit the set of allowed_urls in config.properties
2024-10-22T11:04:44,305 [INFO ] main org.pytorch.serve.servingsdk.impl.PluginsManager - Initializing plugins manager...
2024-10-22T11:04:44,369 [INFO ] main org.pytorch.serve.metrics.configuration.MetricConfiguration - Successfully loaded metrics configuration from /path_to_python_lib/ts/configs/metrics.yaml
2024-10-22T11:04:44,469 [INFO ] main org.pytorch.serve.ModelServer - 
Torchserve version: 0.10.0
TS Home: /path_to_python_lib
Current directory: /path_to_project/ultralytics
Temp directory: /tmp
Metrics config path: /path_to_python_lib/ts/configs/metrics.yaml
Number of GPUs: 1
Number of CPUs: 12
Max heap size: 8004 M
Python executable: /path_to_python_bin/python3.10
Config file: N/A
Inference address: http://127.0.0.1:8080
Management address: http://127.0.0.1:8081
Metrics address: http://127.0.0.1:8082
Model Store: /path_to_project/ultralytics
Initial Models: sycmore.mar
Log dir: /path_to_project/ultralytics/logs
Metrics dir: /path_to_project/ultralytics/logs
Netty threads: 0
Netty client threads: 0
Default workers per model: 1
Blacklist Regex: N/A
Maximum Response Size: 6553500
Maximum Request Size: 6553500
Limit Maximum Image Pixels: true
Prefer direct buffer: false
Allowed Urls: [file://.*|http(s)?://.*]
Custom python dependency for model allowed: false
Enable metrics API: true
Metrics mode: LOG
Disable system metrics: false
Workflow Store: /path_to_project/ultralytics
CPP log config: N/A
Model config: N/A
System metrics command: default

5. 测试部署的模型

测试使用图片:
请添加图片描述
使用 curl 命令测试部署的模型:

curl http://127.0.0.1:8080/predictions/sycmore -T ./VOC2007_171.jpg

输出结果:

[{"box": [[104.19720458984375,9.23455810546875,635.0165405273438,630.9921875]],"confidence": [0.7475572824478149],"class": "dog"}

box:检测框的坐标,格式为 [x1, y1, x2, y2],表示检测框的左上角和右下角坐标。
confidence:检测结果的置信度分数,范围为 [0, 1]。
class:检测到的物体类别名称。

6. 停止部署的模型

当不再需要模型服务时,可以停止 TorchServe:

torchserve --stop

http://www.mrgr.cn/news/55892.html

相关文章:

  • 算法笔记day07
  • 通过OpenCV实现 Lucas-Kanade 算法
  • 在win系统上做生信数据分析如何快速检查和填写正确的文件路径
  • qt编译报错大量error C2065: 未声明的标识符
  • open3d 读取 pcd
  • 12 django管理系统 - 注册与登录 - 登录
  • 深入探索Python集合(Set)的高效应用:数据处理、性能优化与实际案例分析
  • 【python openai function2json小工具】
  • Vuejs设计与实现 —— 实现响应式系统
  • 【MATLAB源码-第186期】matlab基于MLE算法的8天线阵列DOA估计仿真,对比粗估计、精确估计输出RMSE对比图。
  • DeepSpeed:所有人都能用的超大规模模型训练工具
  • 开源神器!CodeFormer:一键去除马赛克,高清修复照片视频
  • 将VSCode界面的显示语言改为简体中文,切换VScode界面的显示语言
  • 智能码二维码ZHINENGMA.CN在设备巡检中的使用效果如何?
  • 前端性能优化之卡顿篇
  • 数据的全量加载和增量加载
  • pdman在关系图中展示表的后排字段
  • WORFBENCH:一个创新的评估基准,目的是全面测试大型语言模型在生成复杂工作流 方面的性能。
  • Centos7.6版本安装mysql详细步骤
  • Vue3 学习笔记(一)Vue3 介绍及环境部署
  • Docker 教程十一(Docker Machine)
  • 如何借助通达信API构建自动化交易系统?
  • # 更正 Excel 表格中 #VALUE! 错误
  • 信号源(函数发生器)的输出幅度是什么
  • 目前我国网络安全人才市场状况
  • MyBatis入门之一对一关联关系(示例)