利用yolov5和TensorRT从0到1实现目标检测的模型训练到模型部署全过程

Overview

写在前面

利用TensorRT加速推理速度是以时间换取精度的做法,意味着在推理速度上升的同时将会有精度的下降,不过不用太担心,精度下降微乎其微。此外,要有NVIDIA显卡,经测试,CUDA10.2可以支持20系列显卡及以下,30系列显卡需要CUDA11.x的支持,并且目前有bug。

默认你已经完成了 yolov5的训练过程并得到了.pt模型权值文件。

本文目的仅是带着走通流程。

注意要对应yolov5和tensorrtx的版本。

  • ./yolov5包含yolov5训练以及模型初转化阶段的代码
  • ./model_process是将.wts模型转化为.engine模型的代码
  • ./detector是利用.engine模型进行前向推理阶段的代码

我的运行环境(注意OpenCV要选择适合你的visual studio的版本等问题):

win10

Visual Studio 2019

NVIDIA GeForce RTX 2060

opencv-3.4.3-vc14_vc15

cuda_10.2.89_441.22_win10

cudnn-10.2-windows10-x64-v7.6.5.32

TensorRT-7.0.0.11.Windows10.x86_64.cuda-10.2.cudnn7.6

cmake-3.21.2-windows-x86_64

上述环境的百度云(测试10、20系列可用):

链接:https://pan.baidu.com/s/1AyaloTzLap8X2hsJBvyeBw
提取码:dwr7

其他版本下载地址:

CUDA cudnn TensorRT CMake OpenCV

环境安装:

1、安装OpenCV并配置好环境变量

2、安装CUDA

一路默认。一般的安装路径为:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2

3、安装cudnn和TensorRT

cudnn和TensorRT的安装仅是将下载的对应版本的压缩包解压并复制*.h、*.lib、*.dll到CUDA的安装路径。

1 将cuDNN压缩包解压

2 将cuda\bin中的文件复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin

3 将cuda\include中的文件复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include

4 将cuda\lib中的文件复制到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib

另外,

1 将TensorRT压缩包解压

2 将 TensorRT-7.0.0.11\include中头文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\include

3 将TensorRT-7.0.0.11\lib中所有lib文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib\x64

4 将TensorRT-7.0.0.11\lib中所有dll文件复制到C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin

4、安装CMake软件备用

一、将训练阶段得到的.pt模型转化为.wts中间模型

把tensorrtx里面的yolov5\gen_wts.py加入到yolov5里面,执行

python gen_wts.py -w [.pt权值文件路径] 

runs\train\exp\weights\best.pt为训练过程生成的.pt模型,生成的best.wts会保存到同目录下,此best.wts待会会用到。

cuda版本每个电脑不一样

配置好的tensorrtx,包括Cmakelist.txt的设定以及dirent.h的配置。

若使用原作者的请参照tensorrtx源码https://github.com/wang-xinyu/tensorrtx ,配置过程中会遇到一些问题,挨个解决,问题不大。

1、在yolov5目录下新建build文件夹

2、修改CMakelist.txt

add_definitions(-DAPI_EXPORTS)

3、打开CMake

​​ generate后关闭

4、yolov5/include/dirent.h

​​ 也可使用我的配置好的

二、利用Cmake软件创建VS工程

修改CMakeLists.txt中此处为你的opencv安装路径。

配置好上方两个目录之后,点击Configure,根据你的环境选择配置,

点击Gnerate,警告可忽视,

现在关闭Cmake即可。

三、wts转化为engine

VS打开刚刚在bulid目录下创建的工程。

build处vs打开,生成

问题:我的模型只识别一个类,需要更改


cd {tensorrtx}/yolov5/

// update CLASS_NUM in yololayer.h if your model is trained on custom dataset

为1

生成项目。

把之前生成的best.wts复制到build\release目录里面

cmd里面运行:

.\test.exe -s .\best.wts best.engine s

运行成功在同文件夹下面会得到best.engine转换后的文件。之后的推理过程使用的都是这个文件。

测试:

.\yolov5.exe -d best.engine .\samples

至此,流程走完。

如果想要进一步封装,可以按照我的示例。

注释掉yolov5.cpp,并取消 几个文件的注释。重新生成项目。按照你的需求更改。

Owner
Helium
Helium
Cluster-GCN: An Efficient Algorithm for Training Deep and Large Graph Convolutional Networks

Cluster-GCN: An Efficient Algorithm for Training Deep and Large Graph Convolutional Networks This repository contains a TensorFlow implementation of "

Jingwei Zheng 5 Jan 08, 2023
Generate text captions for images from their CLIP embeddings. Includes PyTorch model code and example training script.

clip-text-decoder Generate text captions for images from their CLIP embeddings. Includes PyTorch model code and example training script. Example Predi

Frank Odom 36 Dec 21, 2022
Implementation of ConvMixer for "Patches Are All You Need? 🤷"

Patches Are All You Need? 🤷 This repository contains an implementation of ConvMixer for the ICLR 2022 submission "Patches Are All You Need?" by Asher

CMU Locus Lab 934 Jan 08, 2023
Python implementation of cover trees, near-drop-in replacement for scipy.spatial.kdtree

This is a Python implementation of cover trees, a data structure for finding nearest neighbors in a general metric space (e.g., a 3D box with periodic

Patrick Varilly 28 Nov 25, 2022
The materials used in the SaxonJS tutorial presented at Declarative Amsterdam, 2021

SaxonJS-Tutorial-2021, version 1.0.4 Last updated on 4 November, 2021. Table of contents Background Prerequisites Starting a web server Running a Java

Saxonica 11 Oct 23, 2022
EASY - Ensemble Augmented-Shot Y-shaped Learning: State-Of-The-Art Few-Shot Classification with Simple Ingredients.

EASY - Ensemble Augmented-Shot Y-shaped Learning: State-Of-The-Art Few-Shot Classification with Simple Ingredients. This repository is the official im

Yassir BENDOU 57 Dec 26, 2022
AdaSpeech 2: Adaptive Text to Speech with Untranscribed Data

AdaSpeech 2: Adaptive Text to Speech with Untranscribed Data [WIP] Unofficial Pytorch implementation of AdaSpeech 2. Requirements : All code written i

Rishikesh (ऋषिकेश) 63 Dec 28, 2022
[ICSE2020] MemLock: Memory Usage Guided Fuzzing

MemLock: Memory Usage Guided Fuzzing This repository provides the tool and the evaluation subjects for the paper "MemLock: Memory Usage Guided Fuzzing

Cheng Wen 54 Jan 07, 2023
A curated list of awesome open source libraries to deploy, monitor, version and scale your machine learning

Awesome production machine learning This repository contains a curated list of awesome open source libraries that will help you deploy, monitor, versi

The Institute for Ethical Machine Learning 12.9k Jan 04, 2023
This repository contains the re-implementation of our paper deSpeckNet: Generalizing Deep Learning Based SAR Image Despeckling

deSpeckNet-TF-GEE This repository contains the re-implementation of our paper deSpeckNet: Generalizing Deep Learning Based SAR Image Despeckling publi

Adugna Mullissa 16 Sep 07, 2022
A PyTorch-based Semi-Supervised Learning (SSL) Codebase for Pixel-wise (Pixel) Vision Tasks

PixelSSL is a PyTorch-based semi-supervised learning (SSL) codebase for pixel-wise (Pixel) vision tasks. The purpose of this project is to promote the

Zhanghan Ke 255 Dec 11, 2022
An executor that loads ONNX models and embeds documents using the ONNX runtime.

ONNXEncoder An executor that loads ONNX models and embeds documents using the ONNX runtime. Usage via Docker image (recommended) from jina import Flow

Jina AI 2 Mar 15, 2022
Checkout some cool self-projects you can try your hands on to curb your boredom this December!

SoC-Winter Checkout some cool self-projects you can try your hands on to curb your boredom this December! These are short projects that you can do you

Web and Coding Club, IIT Bombay 29 Nov 08, 2022
Official PyTorch code of Holistic 3D Scene Understanding from a Single Image with Implicit Representation (CVPR 2021)

Implicit3DUnderstanding (Im3D) [Project Page] Holistic 3D Scene Understanding from a Single Image with Implicit Representation Cheng Zhang, Zhaopeng C

Cheng Zhang 149 Jan 08, 2023
PyTorch code for Composing Partial Differential Equations with Physics-Aware Neural Networks

FInite volume Neural Network (FINN) This repository contains the PyTorch code for models, training, and testing, and Python code for data generation t

Cognitive Modeling 20 Dec 18, 2022
Content shared at DS-OX Meetup

Streamlit-Projects Streamlit projects available in this repo: An introduction to Streamlit presented at DS-OX (Feb 26, 2020) meetup Streamlit 101 - Ja

Arvindra 69 Dec 23, 2022
A PyTorch implementation of Radio Transformer Networks from the paper "An Introduction to Deep Learning for the Physical Layer".

An Introduction to Deep Learning for the Physical Layer An usable PyTorch implementation of the noisy autoencoder infrastructure in the paper "An Intr

Gram.AI 120 Nov 21, 2022
The implementation of 'Image synthesis via semantic composition'.

Image synthesis via semantic synthesis [Project Page] by Yi Wang, Lu Qi, Ying-Cong Chen, Xiangyu Zhang, Jiaya Jia. Introduction This repository gives

DV Lab 71 Jan 06, 2023
Repository for the Bias Benchmark for QA dataset.

BBQ Repository for the Bias Benchmark for QA dataset. Authors: Alicia Parrish, Angelica Chen, Nikita Nangia, Vishakh Padmakumar, Jason Phang, Jana Tho

ML² AT CILVR 18 Nov 18, 2022
Lightweight, Portable, Flexible Distributed/Mobile Deep Learning with Dynamic, Mutation-aware Dataflow Dep Scheduler; for Python, R, Julia, Scala, Go, Javascript and more

Apache MXNet (incubating) for Deep Learning Master Docs License Apache MXNet (incubating) is a deep learning framework designed for both efficiency an

ROCm Software Platform 29 Nov 16, 2022