# 1、安装环境
* **CUDA10.2**
* **TensorRT7.2**
* **OpenCV3.4(工程中已给出,不需安装)**
* **vs2015**
下载相关工程:[https://github.com/wang-xinyu/tensorrtx.git](https://github.com/wang-xinyu/tensorrtx.git)
# 2、生成yolov5s.wts文件
在生成`yolov5s.wts`前,首先需要[下载](https://github.com/ultralytics/yolov5/releases/download/v3.1/yolov5s.pt)模型。同时,需要我们安装[ultralytics/yolov5](https://github.com/ultralytics/yolov5)环境。这里可以参考网上其它文章或github教程进行配置安装,这里不详加说明。
* 将`tensorrtx-master\yolov5`文件夹下的`gen_wts.py`拷贝到`ultralytics/yolov5`文件夹下
![<img src="D:\工作\yolov3\image\32.png" alt="32" style="zoom:50%;" />](https://img-blog.csdnimg.cn/20201221010339605.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d4cGxvbA==,size_16,color_FFFFFF,t_70#pic_center)
在当前目录下执行:
```bash
python gen_wts.py
```
最终我们会在当前目录下得到一个`yolov5s.wts`文件。
# 3、vs2015环境搭建
这里我们使用别人已经编好的库,下载连接:**[ tensorrtx](https://github.com/wang-xinyu/tensorrtx)**。(注意:部分头文件、lib文件已在我之后的工程中给出,有需要的同学可以直接下载)
* 创建vs工程,命名为yolov5_Trt,并将`tensorrtx-master\yolov5`文件夹下的文件拷贝到创建的项目工程中
![<img src="D:\工作\yolov3\image\31.png" alt="31" style="zoom:50%;" />](https://img-blog.csdnimg.cn/20201221010359749.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d4cGxvbA==,size_16,color_FFFFFF,t_70#pic_center)
* 添加头文件
```bash
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\include
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\include\tensorrt
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\include\cudnn
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\include\opencv\opencv2
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\include\opencv
```
* 添加lib
```bash
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib\x64
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\lib\trt
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\lib\opencv
C:\Users\Administrator\Documents\Visual Studio 2015\Projects\yolov5_Trt\yolov5_Trt\lib\cudnn
```
* 添加依赖项
```bash
cudart.lib
cublas.lib
cudnn.lib
cudnn64_8.lib
myelin64_1.lib
nvinfer.lib
nvinfer_plugin.lib
nvonnxparser.lib
nvparsers.lib
opencv_world340.lib
```
# 4、TensorRt加速实现
* 修改`yolov5.cpp`源码
由于源码需要在命令行下执行,所以我们需要改改,能够直接执行
将421~449行的代码:
```c++
if (argc == 2 && std::string(argv[1]) == "-s") {
IHostMemory* modelStream{ nullptr };
APIToModel(BATCH_SIZE, &modelStream);
assert(modelStream != nullptr);
std::ofstream p(engine_name, std::ios::binary);
if (!p) {
std::cerr << "could not open plan output file" << std::endl;
return -1;
}
p.write(reinterpret_cast<const char*>(modelStream->data()), modelStream->size());
modelStream->destroy();
return 0;
} else if (argc == 3 && std::string(argv[1]) == "-d") {
std::ifstream file(engine_name, std::ios::binary);
if (file.good()) {
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream = new char[size];
assert(trtModelStream);
file.read(trtModelStream, size);
file.close();
}
} else {
std::cerr << "arguments not right!" << std::endl;
std::cerr << "./yolov5 -s // serialize model to plan file" << std::endl;
std::cerr << "./yolov5 -d ../samples // deserialize plan file and run inference" << std::endl;
return -1;
}
```
替换为:
```c++
if (true) {
IHostMemory* modelStream{ nullptr };
APIToModel(BATCH_SIZE, &modelStream);
assert(modelStream != nullptr);
std::ofstream p(engine_name, std::ios::binary);
if (!p) {
std::cerr << "could not open plan output file" << std::endl;
return -1;
}
p.write(reinterpret_cast<const char*>(modelStream->data()), modelStream->size());
modelStream->destroy();
return 0;
}
else {
std::ifstream file(engine_name, std::ios::binary);
if (file.good()) {
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream = new char[size];
assert(trtModelStream);
file.read(trtModelStream, size);
file.close();
}
}
```
* 生成`yolov5s.engine`文件
直接运行代码,会出现以下问题
1. <font color='red'>fatal error C1083: 无法打开包括文件: “dirent.h”: No such file or directory</font>
解决方法:[VS2017/2019 无法打开包括文件: “dirent.h”: No such file or directory](https://blog.csdn.net/weixin_39956356/article/details/108555345)
* 运行yolov5s 下的TensorRT
将423行的`if(true)`改为`if(false)`,直接运行。这里我们测试了`"./test_data/3.jpg"`下的图片,该图片大小为:640\*480
![<img src="D:\工作\yolov3\image\3_1111.jpg" alt="3_1111" style="zoom:50%;" />](https://img-blog.csdnimg.cn/20201221010438305.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d4cGxvbA==,size_16,color_FFFFFF,t_70#pic_center)
最终测试的结果为:
![<img src="D:\工作\yolov3\image\res.jpg" alt="res" style="zoom:50%;" />](https://img-blog.csdnimg.cn/20201221010454240.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3d4cGxvbA==,size_16,color_FFFFFF,t_70#pic_center)
不包括第一次加载模型的时间,在我自己的笔记本GeForce MX250上进行一次推理大概耗时30ms左右。
没有合适的资源?快使用搜索试试~ 我知道了~
win10_yolov5_tensorRT
共254个文件
hpp:155个
h:83个
jpg:5个
需积分: 43 31 下载量 170 浏览量
2021-03-30
10:22:52
上传
评论 1
收藏 2.75MB ZIP 举报
温馨提示
1,安装环境 CUDA10.2 张量RT7.2 OpenCV3.4(工程中已指定,不需安装) vs2015下载相关工程: : 2,生成yolov5s.wts文件 在生成yolov5s.wts前,首先需要模型。同时,需要我们安装环境。这里可以参考网上其他文章或github教程进行配置安装,这里不详加说明。 将tensorrtx-master\yolov5文件夹下的gen_wts.py拷贝到ultralytics/yolov5文件夹下 在当前目录下执行: python gen_wts.py 最终我们会在当前目录下得到一个yolov5s.wts文件。 3,vs2015环境建设 这里我们使用别人已经编好的库,下载连接: 。(注意:部分头文件,lib文件已在我之后的工程中命名,有需要的同学可以直接下载) 创建vs工程,命名为yolov5_Trt,重新连接tensorrtx-master
资源详情
资源评论
资源推荐
收起资源包目录
win10_yolov5_tensorRT (254个子文件)
yolov5.cpp 28KB
yololayer.cu 12KB
yolov5_Trt.vcxproj.filters 1KB
NvInfer.h 259KB
core_c.h 128KB
NvInferRuntime.h 85KB
types_c.h 60KB
NvInferRuntimeCommon.h 55KB
imgproc_c.h 51KB
cudnn_ops_infer.h 48KB
kmeans_index.h 36KB
videoio_c.h 36KB
cudnn_adv_infer.h 28KB
cudnn_cnn_infer.h 28KB
cudnn_adv_train.h 27KB
dist.h 27KB
hierarchical_clustering_index.h 25KB
cudnn_ops_train.h 25KB
dirent.h 22KB
autotuned_index.h 20KB
kdtree_single_index.h 20KB
calib3d_c.h 20KB
kdtree_index.h 19KB
lsh_table.h 18KB
types_c.h 17KB
logging.h 16KB
lsh_index.h 15KB
result_set.h 15KB
NvInferPlugin.h 14KB
cvdef.h 13KB
NvCaffeParser.h 13KB
NvInferPluginUtils.h 12KB
cudnn_backend.h 12KB
tracking_c.h 11KB
index_testing.h 11KB
NvUffParser.h 10KB
highgui_c.h 10KB
cudnn_cnn_train.h 10KB
cv_cpu_helper.h 10KB
NvOnnxParser.h 9KB
any.h 8KB
NvOnnxConfig.h 7KB
hdf5.h 7KB
NvUtils.h 7KB
allocator.h 6KB
nn_index.h 6KB
objdetect_c.h 6KB
composite_index.h 6KB
all_indices.h 6KB
saving.h 6KB
simplex_downhill.h 6KB
imgcodecs_c.h 5KB
cvconfig.h 5KB
cv_cpu_dispatch.h 5KB
cap_ios.h 5KB
dynamic_bitset.h 4KB
interface.h 4KB
defines.h 4KB
yololayer.h 4KB
random.h 4KB
heap.h 4KB
logger.h 4KB
linear_index.h 4KB
ground_truth.h 3KB
matrix.h 3KB
params.h 3KB
cv.h 3KB
NvInferVersion.h 3KB
utils.h 3KB
cudnn.h 3KB
object_factory.h 3KB
sampling.h 3KB
cudnn_version.h 3KB
photo_c.h 3KB
timer.h 2KB
cvaux.h 2KB
ios.h 2KB
cxcore.h 2KB
highgui.h 2KB
cvwimage.h 2KB
ml.h 2KB
general.h 2KB
config.h 2KB
interface.h 481B
dummy.h 234B
cxmisc.h 129B
imgproc.hpp 221KB
color_detail.hpp 217KB
mat.hpp 150KB
core.hpp 144KB
calib3d.hpp 125KB
mat.inl.hpp 96KB
ml.hpp 86KB
intrin_sse.hpp 72KB
types.hpp 64KB
features2d.hpp 60KB
intrin_cpp.hpp 54KB
videoio.hpp 52KB
vec_math.hpp 49KB
persistence.hpp 48KB
共 254 条
- 1
- 2
- 3
佐罗先生
- 粉丝: 31
- 资源: 4750
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0