tensorrt 部署yolov5s (v1.0)

环境:ubuntu18.01  tensorrt6.0.1.5 yolov5s opencv3.4.7 (提前编译好的)cuda10.1  pytorch1.6  onnx

yolov5 项目:https://github.com/ultralytics/yolov5

tensorrt 项目:https://github.com/wang-xinyu/tensorrtx  (还有一个更完整的https://github.com/AIpakchoi/yolov5_tensorrt

v1.0模型下载链接:https://github.com/ultralytics/yolov5/releases

安装tensorrt  https://blog.csdn.net/mathlxj/article/details/107810548

首先,下载以上两个项目代码并解压,下载yolov5s.pt 权重模型。

将tensorrt项目中的yolo5/gen_wts.py 拷贝 到yolov5 项目中:

 copy tensorrtx/yolov5/gen_wts.py into ultralytics/yolov5

pytorch  .pt模型转onnx,生成yolov5s.wts文件:

go to ultralytics/yolov5
python gen_wts.py

 在tensorrt 项目中编译yolov5:

注意以下配置:

  • Choose the model s/m/l/x by NET macro in yolov5.cpp # 注意模型用的是那哪个s/m/l/x
  • Input shape defined in yololayer.h #
  • Number of classes defined in yololayer.h, DO NOT FORGET TO ADAPT THIS, If using your own model  # 记得改类别数量
  • FP16/FP32 can be selected by the macro in yolov5.cpp
  • GPU id can be selected by the macro in yolov5.cpp
  • NMS thresh in yolov5.cpp
  • BBox confidence thresh in yolov5.cpp
  • Batch size in yolov5.cpp

# 要看自己的电脑配置是否支持FP16推理,如果要用FP32就在yolov5.cpp 中将  define USE_FP16 这句注释掉。

cd tensorrtx/yolov5 中的 CMakeLists.txt 中添加opencv 路径:

set(OpenCV_DIR /home/yao/opencv-3.4.7/opencv3/share/OpenCV) # 添加

否则会报错找不到opencv。

 

另一个注意更改tensorrt6.0.1.5的路径:

原文件中是:

include_directories(/usr/include/x86_64-linux-gnu/)
link_directories(/usr/lib/x86_64-linux-gnu/)

改成自己的安装路径:

include_directories(/home/yao/Downloads/TensorRT-6.0.1.5.Ubuntu-18.04.x86_64-gnu.cuda-10.1.cudnn7.6/TensorRT-6.0.1.5/include)
link_directories(/home/yao/Downloads/TensorRT-6.0.1.5.Ubuntu-18.04.x86_64-gnu.cuda-10.1.cudnn7.6/TensorRT-6.0.1.5/lib)

否则会报错找不到NvInfer.h  等等:

/home/yao/Documents/tensorrtx/yolov5/yololayer.cu(126): error: identifier "PluginTensorDesc" is undefined

/home/yao/Documents/tensorrtx/yolov5/yololayer.cu(126): error: identifier "PluginTensorDesc" is undefined

/home/yao/Documents/tensorrtx/yolov5/yololayer.cu(154): error: identifier "IPluginV2IOExt" is undefined

/home/yao/Documents/tensorrtx/yolov5/yololayer.cu(270): error: identifier "IPluginV2IOExt" is undefined

/home/yao/Documents/tensorrtx/yolov5/yololayer.cu(304): error: identifier "IPluginV2IOExt" is undefined

 

开始编译:

在tensorrtx/yolov5/ 下新建文件夹samples 放入两张照片:

// put yolov5s.wts into tensorrtx/yolov5
// go to tensorrtx/yolov5
// ensure the macro NET in yolov5.cpp is s
mkdir build
cd build
cmake ..
make
sudo ./yolov5 -s             // serialize model to plan file i.e. 'yolov5s.engine'
sudo ./yolov5 -d  ../samples // deserialize plan file and run inference, the images in samples will be processed.

编译结果如下:

在tensorrtx/yolov5/build 文件夹下得到两张图像的结果:

 

其余v1.0 的m/l/x 版本模型同上部署。

下一部训练自己的模型部署。待续。

### 部署 YOLOv5 模型于 Windows 系统 #### 准备工作 为了在 Windows 上部署 YOLOv5 模型,需完成以下准备工作。首先确认已安装 `Git` 和 Python 的适当版本(建议使用 Python 3.8 或更高版本)。可以通过命令 `protoc --version` 来验证 Protocol Buffers 编译器是否正确安装并返回其版本号[^1]。 #### 克隆项目仓库 通过 Git 命令获取指定版本的 YOLOv5 源码。如果目标是特定版本(如 v6.1),可以执行以下命令来克隆代码库: ```bash git clone -b v6.1 https://github.com/ultralytics/yolov5.git ``` 此操作会下载对应分支的源码文件[^2]。对于其他版本需求(例如 v7.0),只需调整 `-b` 参数至所需标签即可。 #### 安装依赖项 进入刚创建好的 yolov5 文件夹目录下运行 pip 工具自动解析和加载所需的 python 库包: ```bash cd yolov5 pip install -r requirements.txt ``` #### 获取预训练权重 官方提供了多种不同大小以及精度水平下的预训练模型供用户选择下载。以 COCO 数据集为例,默认情况下可以直接从网络拉取最新版的 pt 格式的参数文件;当然也可以手动访问 [release 页面](https://github.com/ultralytics/yolov5/releases/) 找到适合自己的具体型号链接再单独保存下来[^3]。 #### 测试环境配置 确保 GPU 加速功能正常开启 (如果有 NVIDIA 显卡的话),这通常涉及到 CUDA Toolkit 及 cuDNN SDK 的额外设置过程。另外值得注意的是,在某些特殊场景里可能还需要借助 TensorRT 技术进一步优化性能表现——比如参考 Wang Xinyu 提供的相关实现方案。 #### 推理流程概述 当一切准备就绪之后就可以调用 detect.py 脚本来启动图像检测任务了。下面给出一段简单的例子展示基本用法: ```python from pathlib import Path import torch from models.common import DetectMultiBackend from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams from utils.general import LOGGER, check_file, increment_path, non_max_suppression, scale_coords, xyxy2xywh from utils.plots import Annotator, colors, save_one_box from utils.torch_utils import select_device, time_sync weights='yolov5s.pt' # model path or triton URL source='./data/images/zidane.jpg' device='' # cuda device, i.e. 0 or 0,1,2,3 or cpu imgsz=(640, 640) # inference size (height, width) # Initialize device = select_device(device) model = DetectMultiBackend(weights, device=device, dnn=False, data=None, fp16=False) stride, names, pt = model.stride, model.names, model.pt imgsz = check_img_size(imgsz, s=stride) # check image size dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt) bs = 1 # batch_size for path, im, im0s, vid_cap, s in dataset: t1 = time_sync() im = torch.from_numpy(im).to(device) im = im.half() if model.fp16 else im.float() # uint8 to fp16/32 im /= 255 # 0 - 255 to 0.0 - 1.0 if len(im.shape) == 3: im = im[None] # expand for batch dim pred = model(im, augment=False, visualize=False) ... ``` 上述脚本片段展示了如何加载自定义图片作为输入数据,并利用已经初始化完毕的对象来进行预测分析[^5]。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值