A c++ implementation of yolov5 and deepsort

Overview

A C++ implementation of Yolov5 and Deepsort in Jetson Xavier nx and Jetson nano

This repository uses yolov5 and deepsort to follow humna heads which can run in Jetson Xavier nx and Jetson nano. In Jetson Xavier Nx, it can achieve 10 FPS when images contain heads about 70+(you can try python version, when you use python version, you can find it very slow in Jetson Xavier nx , and Deepsort can cost nearly 1s).

Requirement

  1. Jetson nano or Jetson Xavier nx
  2. Jetpack 4.5.1
  3. python3 with default(jetson nano or jetson xavier nx has default python3 with tensorrt 7.1.3.0 )
  4. tensorrt 7.1.3.0
  5. torch 1.8.0
  6. torchvision 0.9.0
  7. torch2trt 0.3.0

if you have problem in this project, you can see this artical.

Speed

Whole process time from read image to finished deepsort (include every img preprocess and postprocess) and attention!!! the number of deepsort tracking is 70+, not single or 10-20 persons, is 70+. And all results can get in Jetson Xavier nx.

Backbone before TensorRT without tracking before TensorRT with tracking TensorRT(detection + tracking) FPS(detection + tracking)
Yolov5s_416 100ms 0.9s 450ms 1.5 ~ 2
Yolov5s-640 120ms 1s 100-150ms 8 ~ 9

Build and Run

git clone https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt.git
cd yolov5-deepsort-tensorrt
mkdir build 
cmake ..
make 

if you meet some errors in cmake and make, please see this artical or see Attention.

Model

You need two model, one is yolov5 model, for detection, generating from tensorrtx. And the other is deepsort model, for tracking. You should generate the model the same way.

Generate yolov5 model

For yolov5 detection model, I choose yolov5s, and choose yolov5s.pt->yolov5s.wts->yolov5s.engine Note that, used models can get from yolov5 and deepsort, and if you need to use your own model, you can follow the Custom model,waiting for complete. You can see tensorrtx official readme

  1. Get yolov5 repository

Note that, here uses the official pertained model.And I use yolov5-5, v5.0. So if you train your own model, please be sure your yolov5 code is v5.0.

git clone -b v5.0 https://github.com/ultralytics/yolov5.git
cd yolov5
mkdir weights
cd weights
// download https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
wget https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
  1. Get tensorrtx.
git clone https://github.com/wang-xinyu/tensorrtx
  1. Get xxx.wst model
cp tensorrtx/gen_wts.py yolov5/
cd yolov5 
python3 gen_wts.py -w ./weights/yolov5s.pt -o ./weights/yolov5s.wts
// a file 'yolov5s.wts' will be generated.

You can get yolov5s.wts model in yolov5/weights/

  1. Build tensorrtx/yolov5 and get tensorrt engine
cd tensorrtx/yolov5
// update CLASS_NUM in yololayer.h if your model is trained on custom dataset
mkdir build
cd build
cp {ultralytics}/yolov5/yolov5s.wts {tensorrtx}/yolov5/build
cmake ..
make
// yolov5s
sudo ./yolov5 -s yolov5s.wts yolov5s.engine s
// test your engine file
sudo ./yolov5 -d yolov5s.engine ../samples

Then you get the yolov5s.engine, and you can put yolov5s.engine in My project. For example

cd {yolov5-deepsort-tensorrt}
mkdir resources
cp {tensorrtx}/yolov5/build/yolov5s.engine {yolov5-deepsort-tensorrt}/resources
  1. Get deepsort engine file You can get deepsort pretrained model in this drive url and ckpt.t7 is ok.
git clone https://github.com/RichardoMrMu/deepsort-tensorrt.git
// 根据github的说明
cp {deepsort-tensorrt}/exportOnnx.py {deep_sort_pytorch}/
python3 exportOnnx.py
mv {deep_sort_pytorch}/deepsort.onnx {deepsort-tensorrt}/resources
cd {deepsort-tensorrt}
mkdir build
cd build
cmake ..
make 
.onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine
// test
./demo ../resource/deepsort.engine ../resources/track.txt

After all 5 step, you can get the yolov5s.engine and deepsort.engine.

You may face some problems in getting yolov5s.engine and deepsort.engine, you can upload your issue in github or csdn artical.

Comments
  • Deserialize the cuda engine failed.

    Deserialize the cuda engine failed.

    when i run the command line of ./demo ../resource/deepsort.engine ../resources/track.txt but, show below [10/31/2021-21:09:01] [E] [TRT] INVALID_ARGUMENT: Cannot deserialize with an empty memory buffer. [10/31/2021-21:09:01] [E] [TRT] INVALID_CONFIG: Deserialize the cuda engine failed. Segmentation fault (core dumped)

    opened by likehengqq 8
  • Deserialize the cuda engine failed.

    Deserialize the cuda engine failed.

    你好,我是在win10下进行工程部署,成功编译后,将自己序列化生成的yolov5和deepsort的engine文件写到main.cpp中对应接口后,运行yolosort.exe时报错结果如下: [E] [TRT] C:\source\rtSafe\coreReadArchive.cpp (58) - Serialization Error in nvinfer1::rt::CoreReadArchive::verifyHeader: 0 (Length in header does not match remaining archive length)。 想知道报错原因及解决办法,非常期待您的回复!

    opened by liyanxi-0310 7
  • 运行yolosort时发生错误

    运行yolosort时发生错误

    在jetson nano上运行时 nano@nano-desktop:~/yolov5-deepsort-tensorrt-main/build$ ./yolosort yolov5_trt_create ... terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Aborted (core dumped)

    opened by 1997wzk 4
  • 運行的問題

    運行的問題

    我在win10上跑 char* yolo_engine = "d:/yolov5-deepsort-tensorrt_1/yolov5s.engine"; char* sort_engine = "d:/yolov5-deepsort-tensorrt_1/deepsort.engine"; float conf_thre = 0.4; Trtyolosort yosort(yolo_engine,sort_engine); VideoCapture capture; cv::Mat frame; //frame = capture.open("d:/yolov5-deepsort-tensorrt_1/test.mp4"); frame = capture.open(0); if (!capture.isOpened()){ std::cout<<"can not open"<<std::endl; return -1 ; } capture.read(frame); 結果出現 yolov5_trt_create ... yolov5_trt_create cuda engine... yolov5_trt_create buffer ... yolov5_trt_create stream ... yolov5_trt_create done ... create yolov5-trt , instance = 000002B801D48290 delay_proress:6ms, delay_infer:2ms delay_infer:13ms delay_proress:7ms, delay_infer:2ms delay_infer:13ms delay_proress:1ms, delay_infer:2ms ... 這應該是都對的,但沒有opencv 的windows出來,請問我該加些甚麼? 謝謝

    opened by tuteming 3
  • .onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine

    .onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine

    when I run .onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine, I meet an error that .onnx2engine:command not found, deer Mu ,do you know how to solve this error?

    opened by ChenYang-1996 3
  • 测试长视频时,deepsort_tet会一直申请内存导致内存爆掉

    测试长视频时,deepsort_tet会一直申请内存导致内存爆掉

    运行正常的时候: vector: 24 origin img size:960 540 yolov5_trt_detect start preprocess img after preprocess_img pr_img size:640 640 after preprocess_img frame size:960 540 yolov5_trt_detect start convert img to float yolov5_trt_detect start do inference image

    运行一段时间后,当显存被占满时,deepsort直接停止工作,框会一直停留在那一帧,但是yolo还在继续跑: CUDA error 2 at F:\Detections\yolov5-deepsort-tensorrt\yolo\src\yolov5_lib.cpp:30[04/23/2022-18:43:35] [E] [TRT] C:\source\rtSafe\safeContext.cpp (133) - Cudnn Error in nvinfer1::rt::CommonContext::configure: 7 (CUDNN_STATUS_MAPPING_ERROR) [04/23/2022-18:43:35] [E] [TRT] FAILED_EXECUTION: Unknown exception

    image

    我的配置: GTX1050ti-Notebook 4GB CUDAA10.1 cudnn-10.1-windows10-x64-v7.6.5.32 NVIDIA Drive 497.33 opencv-3.3-vc14 cmake-3.17.1-windows-x86_64 TensorRT-7.0.0.11.Windows10.x86_64.cuda-10.0.cudnn7.6

    opened by ChaiYuanYuan-bit 1
  • 编译完成运行发现这个问题

    编译完成运行发现这个问题

    jetson nano上运行报错

    yolov5_trt_create ... yolov5_trt_create cuda engine... yolov5_trt_create buffer ... yolov5_trt_create stream ... yolov5_trt_create done ... create yolov5-trt , instance = 0x55761cdab0 [02/12/2022-23:52:05] [W] [TRT] The logger passed into createInferRuntime differs from one already provided for an existing builder, runtime, or refitter. TensorRT maintains only a single logger pointer at any given time, so the existing value, which can be retrieved with getLogger(), will be used instead. In order to use a new logger, first destroy all existing builder, runner or refitter objects.

    [02/12/2022-23:52:06] [E] [TRT] 1: [stdArchiveReader.cpp::StdArchiveReader::34] Error Code 1: Serialization (Serialization assertion safeVersionRead == safeSerializationVersion failed.Version tag does not match. Note: Current Version: 43, Serialized Engine Version: 97) [02/12/2022-23:52:06] [E] [TRT] 4: [runtime.cpp::deserializeCudaEngine::76] Error Code 4: Internal Error (Engine deserialization failed.) Segmentation fault (core dumped)

    opened by jiafeng-1 1
  • [question]How can I use this repository with my camera?

    [question]How can I use this repository with my camera?

    Hello, @RichardoMrMu Thanks for posting good stuff.

    I am using a csi camera or usb camera like a pi camera. How can I use this repository with my camera? I want to test it with live video.

    Thank you. by neuezeal

    opened by leeyunhome 1
  • c++代码中 很多new的变量最后没释放

    c++代码中 很多new的变量最后没释放

    • DS https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/blob/9ce6c5576bb2e4a41b9c90346d7883b418d27061/src/manager.cpp#L6-L13
    • featureExtractor https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/blob/9ce6c5576bb2e4a41b9c90346d7883b418d27061/deepsort/src/deepsort.cpp#L15-L27
    • this->metric, this->kf https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/blob/9ce6c5576bb2e4a41b9c90346d7883b418d27061/deepsort/src/tracker.cpp#L12-L27
    • instance https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/blob/9ce6c5576bb2e4a41b9c90346d7883b418d27061/deepsort/src/linear_assignment.cpp#L10-L14
    opened by jiujiangluck 1
  • 求助

    求助

    -- Generating done -- Build files have been written to: /home/mql/work/yolov5-deepsort-tensorrt-main/build [ 64%] Built target deepsort [ 70%] Building NVCC (Device) object CMakeFiles/yolov5_trt.dir/yolo/include/yolov5_trt_generated_yololayer.cu.o nvcc fatal : redefinition of argument 'std' CMake Error at yolov5_trt_generated_yololayer.cu.o.Release.cmake:219 (message): Error generating /home/mql/work/yolov5-deepsort-tensorrt-main/build/CMakeFiles/yolov5_trt.dir/yolo/include/./yolov5_trt_generated_yololayer.cu.o

    CMakeFiles/yolov5_trt.dir/build.make:326: recipe for target 'CMakeFiles/yolov5_trt.dir/yolo/include/yolov5_trt_generated_yololayer.cu.o' failed make[2]: *** [CMakeFiles/yolov5_trt.dir/yolo/include/yolov5_trt_generated_yololayer.cu.o] Error 1 CMakeFiles/Makefile2:142: recipe for target 'CMakeFiles/yolov5_trt.dir/all' failed make[1]: *** [CMakeFiles/yolov5_trt.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2

    make出现问题,不知道咋解决0-0

    opened by 754467737 1
  • [WARNING]::Batch size overflow, input will be truncated!

    [WARNING]::Batch size overflow, input will be truncated!

    Hi, when I test the program using my own data, I meet one question that the output shows that the batch size is overflow, so I wonder whether can I change the imgShape to solve that problem? And how can I tune that parameter? The value of imgShape in deepsort.cpp is cv::Size(64, 128), and I don't know why the value is (64, 128), can you help me?

    opened by MrRangerss 1
  • Get the features from ReID model

    Get the features from ReID model

    How to get the feature vector from the ReID model for each id in each frame along with the classid , confidence, and bbox in order to save the data for future reference

    opened by vishalk2999 0
  • How to map from class_id to class_name?

    How to map from class_id to class_name?

    Hi, In this project, I saw that model return class_id of each detected object. How can I map from that class_id to class_name of objects in the right order? Thank you!

    opened by tiensu 0
  • 更换ReID模型之后,提示Eigen数据对齐错误

    更换ReID模型之后,提示Eigen数据对齐错误

    将ReID模型的输入改成 1x3x32x32大小,但是发现,Eigen一直报错

    eigen/include/eigen3/Eigen/src/Core/CoreEvaluators.h:1226: Eigen::internal::block_evaluator<ArgType, BlockRows, BlockCols, InnerPanel, true>::block_evaluator(const XprType&) [with ArgType = const Eigen::Transpose<const Eigen::Matrix<float, 1, 8, 1> >; int BlockRows = 8; int BlockCols = 1; bool InnerPanel = true; Eigen::internal::block_evaluator<ArgType, BlockRows, BlockCols, InnerPanel, true>::XprType = Eigen::Block<const Eigen::Transpose<const Eigen::Matrix<float, 1, 8, 1> >, 8, 1, true>]: Assertion `((internal::UIntPtr(block.data()) % (((int)1 >= (int)evaluator::Alignment) ? (int)1 : (int)evaluator::Alignment)) == 0) && "data is not aligned"' failed.

    大佬们有思路嘛?

    opened by LinoSun 0
  • ./onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine

    ./onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine

    博主您好,我在onnx转engine哪一步遇到问题Network has dynamic or shape inputs, but no optimization profile has been defined. 我尝试通过将exportOnxx中的torch.onnx.export中的参数dynamic_axes={'input': {0: 'batch_size'}, 'output': {0: 'batch_size'}}删除后,发现可以生成engine并且能跑通整个流程,但是在运行./yolosort时报错Parameter check failed at: engine.cpp::setBindingDimensions::1044......(使用摄像头能运行就是会报错) 我尝试查阅tensortrt的官方文档发现有部分人存在这个问题,官方文档确实提供了一些说明,我查阅了您的代码后发现您写的代码和官方提供的代码sampleDynamicReshape.cpp都是通过定义profile的方式来实现动态推理,但是我这边仍报Error,请问您方便回复下吗

    opened by Mingjie-Chen 0
Owner
Working in human-computer-interaction, gaze-estimation and class education analysis. CSDN:https://blog.csdn.net/weixin_42264234
null
The MOT implement by Solov2+DeepSORT with C++ (Libtorch, TensorRT).

Tracking-Solov2-Deepsort This project implement the Multi-Object-Tracking(MOT) base on SOLOv2 and DeepSORT with C++。 The instance segmentation model S

ChenJianqu 38 Dec 22, 2022
A C++ implementation of Yolov5 helmet detection in Jetson Xavier nx and Jetson nano

A C++ implementation of Yolov5 to detect head or helmet in the wild in Jetson Xavier nx and Jetson nano This repository uses yolov5 to detect humnan h

null 11 Dec 3, 2022
A C++ implementation of Yolov5 to detect mask running in Jetson Xavier nx and Jetson nano.

yolov5-mask-detect A C++ implementation of Yolov5 to detect mask running in Jetson Xavier nx and Jetson nano.In Jetson Xavier Nx, it can achieve 33 FP

null 4 Dec 19, 2022
Implement yolov5 with Tensorrt C++ api, and integrate batchedNMSPlugin. A Python wrapper is also provided.

yolov5 Original codes from tensorrtx. I modified the yololayer and integrated batchedNMSPlugin. A yolov5s.wts is provided for fast demo. How to genera

weiwei zhou 46 Dec 6, 2022
shufflev2-yolov5: lighter, faster and easier to deploy

shufflev2-yolov5: lighter, faster and easier to deploy. Evolved from yolov5 and the size of model is only 1.7M (int8) and 3.3M (fp16). It can reach 10+ FPS on the Raspberry Pi 4B when the input size is 320×320~

pogg 1.5k Dec 30, 2022
Real-time object detection with YOLOv5 and TensorRT

YOLOv5-TensorRT The goal of this library is to provide an accessible and robust method for performing efficient, real-time inference with YOLOv5 using

Noah van der Meer 43 Dec 27, 2022
This is a c++ implement of yolov5 and fire/smoke detect.

A C++ implementation of Yolov5 to detect fire or smoke in the wild in Jetson Xavier nx and Jetson nano This repository uses yolov5 and deepsort to fol

null 16 Nov 15, 2022
yolov5 onnx caffe

环境配置 ubuntu:18.04 cuda:10.0 cudnn:7.6.5 caffe: 1.0 OpenCV:3.4.2 Anaconda3:5.2.0 相关的安装包我已经放到百度云盘,可以从如下链接下载: https://pan.baidu.com/s/17bjiU4H5O36psGrHlF

null 61 Dec 29, 2022
vs2015上使用tensorRT加速yolov5推理(Using tensorrt to accelerate yolov5 reasoning on vs2015)

1、安装环境 CUDA10.2 TensorRT7.2 OpenCV3.4(工程中已给出,不需安装) vs2015 下载相关工程:https://github.com/wang-xinyu/tensorrtx.git 2、生成yolov5s.wts文件 在生成yolov5s.wts前,首先需要下载模

null 16 Apr 19, 2022
ncnn of yolov5 v5.0 branch

YOLOv5转NCNN 基于YOLOv5最新v5.0 release,和NCNN官方给出example的差别主要有: 激活函数hardswish变为siLu; 流程和详细记录u版YOLOv5目标检测ncnn实现略微不同 编译运行 动态库用的是官方编译好的ncnn-20210507-ubuntu-16

null 75 Dec 16, 2022
Lite.AI 🚀🚀🌟 is a user-friendly C++ lib for awesome🔥🔥🔥 AI models based on onnxruntime, ncnn or mnn. YOLOX, YoloV5, YoloV4, DeepLabV3, ArcFace, CosFace, Colorization, SSD

Lite.AI ?????? is a user-friendly C++ lib for awesome?????? AI models based on onnxruntime, ncnn or mnn. YOLOX??, YoloV5??, YoloV4??, DeepLabV3??, ArcFace??, CosFace??, Colorization??, SSD??, etc.

Def++ 2.4k Jan 4, 2023
Lite.AI 🚀🚀🌟 is a user friendly C++ lib of 60+ awesome AI models. YOLOX🔥, YoloV5🔥, YoloV4🔥, DeepLabV3🔥, ArcFace🔥, CosFace🔥, RetinaFace🔥, SSD🔥, etc.

Lite.AI ?? ?? ?? Introduction. Lite.AI ?? ?? ?? is a simple and user-friendly C++ library of awesome ?? ?? ?? AI models. It's a collection of personal

Def++ 2.4k Dec 28, 2022
A rknn cpp/c++ inference codebase for yolov5.

Yolov5 RKNN Cpp This is a code base for yolov5 cpp inference. This code is built for android arm v8 test. NDK Version: r16b Install Download and set N

null 23 Nov 16, 2022
Use c++ train yolov5

LibTorch-YoLoV5-train-detection use c++ train yolov5 Just for learning!!! Just for learning!!! Just for learning!!! Dependencies Windows10 Qt5.13.2(ki

Jie Li 9 May 19, 2022
Deploy ultralytics Yolov5 pretained model with C++ language

Introdution Deploy ultralytics Yolov5 pretained model with C++ language ; Env GCC 7.5 Opencv 4.5.4 Get ONNX Model go to yolov5 release page download y

Xee 62 Jan 6, 2023
Depoly yolov5.ncnn in android.

Deploy yolov5.ncnn(v6.0) in android 测试效果 如何构建? 1. 下载 Android studio Android studio 下载地址: https://developer.android.com/studio 2. git clone 项目构建 Androi

yangcheng 10 Dec 25, 2022
The official implementation of our CVPR 2021 paper - Hybrid Rotation Averaging: A Fast and Robust Rotation Averaging Approach

Graph Optimizer This repo contains the official implementation of our CVPR 2021 paper - Hybrid Rotation Averaging: A Fast and Robust Rotation Averagin

Chenyu 109 Dec 23, 2022
ResNet Implementation, Training, and Inference Using LibTorch C++ API

LibTorch C++ ResNet CIFAR Example Introduction ResNet implementation, training, and inference using LibTorch C++ API. Because there is no native imple

Lei Mao 23 Oct 29, 2022