├── .gitignore
├── CMakeLists.txt
├── CMakeLists_deepsort-tensorrt_win10.txt
├── CMakeLists_yolov5-deepsort-tensorrt_win10.txt
├── LICENSE
├── README.md
├── assets
└── yolosort.gif
├── deepsort
├── include
│ ├── datatype.h
│ ├── deepsort.h
│ ├── deepsortenginegenerator.h
│ ├── featuretensor.h
│ ├── hungarianoper.h
│ ├── kalmanfilter.h
│ ├── linear_assignment.h
│ ├── logging.h
│ ├── matrix.h
│ ├── model.hpp
│ ├── munkres.h
│ ├── nn_matching.h
│ ├── track.h
│ └── tracker.h
└── src
│ ├── deepsort.cpp
│ ├── deepsortenginegenerator.cpp
│ ├── featuretensor.cpp
│ ├── hungarianoper.cpp
│ ├── kalmanfilter.cpp
│ ├── linear_assignment.cpp
│ ├── munkres.cpp
│ ├── nn_matching.cpp
│ ├── track.cpp
│ └── tracker.cpp
├── include
└── manager.hpp
├── src
├── main.cpp
└── manager.cpp
└── yolo
├── include
├── calibrator.h
├── common.hpp
├── cuda_utils.h
├── logging.h
├── macros.h
├── utils.h
├── yololayer.cu
├── yololayer.h
└── yolov5_lib.h
└── src
├── calibrator.cpp
└── yolov5_lib.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 | build/
131 | resources/
132 | python/
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.6)
2 |
3 | project(yolosort)
4 | list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
5 | set(CMAKE_CXX_FLAGS "-std=c++0x")
6 | find_package(OpenCV REQUIRED)
7 | add_definitions(-std=c++11)
8 | add_definitions(-DAPI_EXPORTS)
9 | option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
10 | set(CMAKE_CXX_STANDARD 11)
11 | set(CMAKE_BUILD_TYPE Release)
12 |
13 | find_package(CUDA REQUIRED)
14 | set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11;-g;-G;-gencode;arch=compute_53;code=sm_53)
15 |
16 | if(WIN32)
17 | enable_language(CUDA)
18 | endif(WIN32)
19 |
20 | include_directories(${PROJECT_SOURCE_DIR}/deepsort/include)
21 | # include and link dirs of cuda and tensorrt, you need adapt them if yours are different
22 | # cuda
23 | include_directories(/usr/local/cuda/include)
24 | link_directories(/usr/local/cuda/lib64)
25 | # tensorrt
26 | include_directories(/usr/include/aarch64-linux-gnu/)
27 | link_directories(/usr/lib/aarch64-linux-gnu/)
28 |
29 | include_directories(
30 | ${CUDA_INCLUDE_DIRS}
31 | ${OpenCV_INCLUDE_DIRS}
32 | ${PROJECT_SOURCE_DIR}/deepsort/include
33 | )
34 | aux_source_directory(${PROJECT_SOURCE_DIR}/deepsort/src SRC_DIR)
35 |
36 | # ===== deepsort =====
37 | add_library(deepsort SHARED ${SRC_DIR})
38 | target_link_libraries(deepsort
39 | ${CUDA_LIBS} ${OpenCV_LIBS}
40 | cudart nvinfer nvonnxparser
41 | )
42 |
43 | # ===== yolo =====
44 | include_directories(${PROJECT_SOURCE_DIR}/yolo/include)
45 | aux_source_directory(${PROJECT_SOURCE_DIR}/yolo/src YOLO_SRC_DIR)
46 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -Wfatal-errors -D_MWAITXINTRIN_H_INCLUDED")
47 |
48 | cuda_add_library(yolov5_trt SHARED ${PROJECT_SOURCE_DIR}/yolo/include/yololayer.cu ${PROJECT_SOURCE_DIR}/yolo/src/yolov5_lib.cpp)
49 | target_link_libraries(yolov5_trt nvinfer cudart deepsort)
50 |
51 |
52 | # ===== main =====
53 | aux_source_directory(${PROJECT_SOURCE_DIR}/src M_SRC_DIR)
54 | include_directories(${PROJECT_SOURCE_DIR}/include)
55 |
56 | add_executable(yolosort ${M_SRC_DIR})
57 |
58 | target_link_libraries(yolosort nvinfer cudart yolov5_trt)
59 |
60 | if(UNIX)
61 | add_definitions(-O2 -pthread)
62 | endif(UNIX)
63 | set(CMAKE_CXX_FLAGS "-std=c++0x")
64 |
--------------------------------------------------------------------------------
/CMakeLists_deepsort-tensorrt_win10.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.6)
2 | project(deepsort)
3 |
4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5 | set(CMAKE_BUILD_TYPE Release)
6 | set(CUDA_BIN_PATH C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1) #1.修改为自己的cuda路径
7 | set(TRT_DIR "D:\\lbq\\TensorRT-7.2.3.4") #2.修改为自己的tensorRT路径
8 | set(TRT_INCLUDE_DIRS ${TRT_DIR}\\include) #3.修改为自己的tensorRT头文件路径
9 | set(TRT_LIB_DIRS ${TRT_DIR}\\lib) #4.修改为自己的tensorRT库文件路径
10 | set(Eigen3_PATH D:\\lbq\\eigen) #5.修改为自己的Eigen路径
11 | option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
12 | find_package(OpenCV REQUIRED)
13 | find_package(CUDA REQUIRED)
14 |
15 |
16 | enable_language(CUDA)
17 | include_directories(
18 | ${CUDA_INCLUDE_DIRS}
19 | ${OpenCV_INCLUDE_DIRS}
20 | ${TRT_INCLUDE_DIRS}
21 | ${Eigen3_PATH}
22 | ${PROJECT_SOURCE_DIR}/include
23 | )
24 |
25 | # tensorRT
26 | link_directories(${TRT_LIB_DIRS})
27 | link_directories(${OpenCV_LIB_DIRS})
28 |
29 | aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_DIR)
30 |
31 | # ===== deepsort =====
32 | add_library(deepsort STATIC ${SRC_DIR})
33 | target_link_libraries(deepsort
34 | ${CUDA_LIBRARIES} ${OpenCV_LIBS}
35 | cudart nvinfer nvonnxparser
36 | )
37 |
38 | # ===== onnx2engine =====
39 | add_executable(onnx2engine ${PROJECT_SOURCE_DIR}/onnx2engine.cpp)
40 | target_link_libraries(onnx2engine
41 | ${CUDA_LIBRARIES}
42 | cudart nvinfer nvonnxparser deepsort
43 | )
44 |
45 | # ===== demo =====
46 | add_executable(demo ${PROJECT_SOURCE_DIR}/demo.cpp)
47 | target_link_libraries(demo
48 | ${CUDA_LIBRARIES} ${OpenCV_LIBS}
49 | cudart nvinfer nvonnxparser deepsort
50 | )
51 |
52 |
53 |
--------------------------------------------------------------------------------
/CMakeLists_yolov5-deepsort-tensorrt_win10.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.6)
2 |
3 | project(yolosort)
4 | list(APPEND CUDA_NVCC_FLAGS "-std=c++11")
5 | set(CMAKE_CXX_FLAGS "-std=c++0x")
6 | find_package(OpenCV REQUIRED)
7 | add_definitions(-std=c++11)
8 | add_definitions(-DAPI_EXPORTS)
9 | option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
10 | set(CMAKE_CXX_STANDARD 11)
11 | set(CMAKE_BUILD_TYPE Release)
12 |
13 | #1.设置CUDA路径(set the path of cuda root )
14 | set(CUDA_BIN_PATH C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1)
15 | #2.设置tensorrt路径(set the path of tensorrt root)
16 | set(TRT_DIR "D:\\lbq\\TensorRT-7.2.3.4")
17 | #3.设置tensorrt头文件路径(set the path of tensorrt .h format file)
18 | set(TRT_INCLUDE_DIRS ${TRT_DIR}\\include)
19 | #4.设置tensorrt库文件路径(set the path of tensorrt .lib format file)
20 | set(TRT_LIB_DIRS ${TRT_DIR}\\lib)
21 | #5.设置Eigen路径,我用的是Eigen-3.3.9.zip(set the path of Eigen)
22 | set(Eigen3_PATH D:\\lbq\\eigen)
23 | #6.设置dirent路径(set the path of Dirent, download it from https://codeload.github.com/tronkko/dirent/zip/refs/heads/master)
24 | set(Dirent_INCLUDE_DIRS "D:\\lbq\\dirent\\include") #11
25 | # ==编译完成后,在vs2019打开的时候需要在链接器->命令行->其他选项->%(AdditionalOptions) /machine:x64 /FORCE:MULTIPLE
26 |
27 | find_package(CUDA REQUIRED)
28 | set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11;-g;-G;-gencode;arch=compute_53;code=sm_53)
29 |
30 | if(WIN32)
31 | enable_language(CUDA)
32 | endif(WIN32)
33 |
34 | include_directories(${PROJECT_SOURCE_DIR}/deepsort/include)
35 | # include and link dirs of cuda and tensorrt, you need adapt them if yours are different
36 |
37 | # tensorrt
38 | link_directories(${TRT_LIB_DIRS})
39 | link_directories(${OpenCV_LIB_DIRS})
40 |
41 | include_directories(
42 | ${CUDA_INCLUDE_DIRS}
43 | ${OpenCV_INCLUDE_DIRS}
44 | ${TRT_INCLUDE_DIRS}
45 | ${Eigen3_PATH}
46 | ${Dirent_INCLUDE_DIRS}
47 | ${PROJECT_SOURCE_DIR}/deepsort/include
48 | )
49 | aux_source_directory(${PROJECT_SOURCE_DIR}/deepsort/src SRC_DIR)
50 |
51 | # ===== deepsort =====
52 | add_library(deepsort STATIC ${SRC_DIR})
53 | target_link_libraries(deepsort
54 | ${CUDA_LIBS} ${OpenCV_LIBS}
55 | cudart nvinfer nvonnxparser
56 | )
57 |
58 | # ===== yolo =====
59 | include_directories(${PROJECT_SOURCE_DIR}/yolo/include)
60 | aux_source_directory(${PROJECT_SOURCE_DIR}/yolo/src YOLO_SRC_DIR)
61 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Ofast -D_MWAITXINTRIN_H_INCLUDED")
62 | cuda_add_library(yolov5_trt STATIC ${PROJECT_SOURCE_DIR}/yolo/include/yololayer.cu ${PROJECT_SOURCE_DIR}/yolo/src/yolov5_lib.cpp)
63 | target_link_libraries(yolov5_trt nvinfer cudart deepsort)
64 |
65 |
66 | # ===== main =====
67 | aux_source_directory(${PROJECT_SOURCE_DIR}/src M_SRC_DIR)
68 | include_directories(${PROJECT_SOURCE_DIR}/include)
69 |
70 | add_executable(yolosort ${M_SRC_DIR})
71 |
72 | target_link_libraries(yolosort nvinfer cudart yolov5_trt)
73 |
74 | if(UNIX)
75 | add_definitions(-O2 -pthread)
76 | endif(UNIX)
77 | set(CMAKE_CXX_FLAGS "-std=c++0x")
78 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # A C++ implementation of Yolov5 and Deepsort in Jetson Xavier nx and Jetson nano
2 | [](https://opensource.org/licenses/MIT)
3 | [](https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt)
4 |
5 | This repository uses yolov5 and deepsort to follow human heads which can run in Jetson Xavier nx and Jetson nano.
6 | 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).
7 |
8 |
9 |
10 | Thanks for [B.Q Long](https://github.com/lbq779660843), offer the windows cmakelists.txt. If you want run this rep in windows, you can use [CMakeLists_deepsort-tensorrt_win10.txt](https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/blob/main/CMakeLists_deepsort-tensorrt_win10.txt) and [CMakeLists_yolov5-deepsort-tensorrt_win10.txt](https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/blob/main/CMakeLists_yolov5-deepsort-tensorrt_win10.txt).
11 |
12 | You can see video play in [BILIBILI](https://www.bilibili.com/video/BV1nR4y1H7sR/), or [YOUTUBE](https://www.youtube.com/watch?v=29vEi-7mEic) and [YOUTUBE](https://youtu.be/mVq3pWNjb3E).
13 |
14 | ## Requirement
15 | 1. Jetson nano or Jetson Xavier nx
16 | 2. Jetpack 4.5.1
17 | 3. python3 with default(jetson nano or jetson xavier nx has default python3 with tensorrt 7.1.3.0 )
18 | 4. tensorrt 7.1.3.0
19 | 5. torch 1.8.0
20 | 6. torchvision 0.9.0
21 | 7. torch2trt 0.3.0
22 | 8. onnx 1.4.1
23 | 9. opencv-python 4.5.3.56
24 | 10. protobuf 3.17.3
25 | 11. scipy 1.5.4
26 |
27 |
28 | if you have problem in this project, you can see this [artical](https://blog.csdn.net/weixin_42264234/article/details/120152117).
29 |
30 | ## Comming soon
31 | - [ ] Int8 .
32 | - [ ] IOU Tracking.
33 | - [ ] Faster and use less memory.
34 | ## Speed
35 |
36 | Whole process time from read image to finished deepsort (include every img preprocess and postprocess)
37 | 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.
38 | | Backbone | before TensorRT without tracking |before TensorRT with tracking |TensortRT(detection)| TensorRT(detection + tracking) | FPS(detection + tracking) |
39 | | :-------------- | --------------- | ------------------ |--------------|------------------------------ | ------------------------- |
40 | | Yolov5s_416 | 100ms | 0.9s|10-15ms|100-150ms | 8 ~ 9 |
41 | | Yolov5s-640 | 120ms | 1s|18-20ms|100-150ms | 8 ~ 9 |
42 |
43 | ------
44 |
45 | ## Build and Run
46 |
47 | ```shell
48 | git clone https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt.git
49 | cd yolov5-deepsort-tensorrt
50 | // before you cmake and make, please change ./src/main.cpp char* yolo_engine = ""; char* sort_engine = ""; to your own path
51 | mkdir build
52 | cmake ..
53 | make
54 | ```
55 | if you meet some errors in cmake and make, please see this [artical](https://blog.csdn.net/weixin_42264234/article/details/120152117) or see Attention.
56 |
57 | ## DataSet
58 | If you need to train your own model with head detection, you can use this [SCUT-HEAD](https://github.com/HCIILAB/SCUT-HEAD-Dataset-Release), this dataset has bbox with head and can download freely.
59 |
60 | ## Model
61 | You need two model, one is yolov5 model, for detection, generating from [tensorrtx](https://github.com/wang-xinyu/tensorrtx). And the other is deepsort model, for tracking. You should generate the model the same way.
62 | ### Generate yolov5 model
63 | For yolov5 detection model, I choose yolov5s, and choose `yolov5s.pt->yolov5s.wts->yolov5s.engine`
64 | Note that, used models can get from [yolov5](https://github.com/ultralytics/yolov5) and [deepsort](https://github.com/ZQPei/deep_sort_pytorch), and if you need to use your own model, you can follow the `Run Your Custom Model`.
65 | You can also see [tensorrtx official readme](https://github.com/wang-xinyu/tensorrtx/tree/master/yolov5)
66 | The following is deepsort.onnx and deesort.engine files, you can find in baiduyun and [https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/releases/tag/yolosort](https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt/releases/tag/yolosort)
67 | | Model| Url |
68 | | :-------------- | --------------- |
69 | | 百度云 | [BaiduYun url](https://pan.baidu.com/s/1vpQFsD346lP64O1nhOilkw ) passwd:`z68e`|
70 |
71 | 1. Get yolov5 repository
72 |
73 |
74 | 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.
75 |
76 | ```shell
77 | git clone -b v5.0 https://github.com/ultralytics/yolov5.git
78 | cd yolov5
79 | mkdir weights
80 | cd weights
81 | // download https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
82 | wget https://github.com/ultralytics/yolov5/releases/download/v5.0/yolov5s.pt
83 |
84 | ```
85 |
86 | 2. Get tensorrtx.
87 |
88 | For yolov5 v5.0, download .pt from [yolov5 release v5.0](https://github.com/ultralytics/yolov5/releases/tag/v5.0), `git clone -b v5.0 https://github.com/ultralytics/yolov5.git` and `git clone -b yolov5-v5.0 https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in [tensorrtx/yolov5-v5.0](https://github.com/wang-xinyu/tensorrtx/tree/yolov5-v5.0/yolov5).
89 |
90 |
91 | 3. Get xxx.wst model
92 |
93 | ```shell
94 | cp {tensorrtx}/yolov5/gen_wts.py {ultralytics}/yolov5/
95 | cd yolov5
96 | python3 gen_wts.py -w ./weights/yolov5s.pt -o ./weights/yolov5s.wts
97 | // a file 'yolov5s.wts' will be generated.
98 | ```
99 | You can get yolov5s.wts model in `yolov5/weights/`
100 |
101 | 4. Build tensorrtx/yolov5 and get tensorrt engine
102 |
103 | ```shell
104 | cd tensorrtx/yolov5
105 | // update CLASS_NUM in yololayer.h if your model is trained on custom dataset
106 | mkdir build
107 | cd build
108 | cp {ultralytics}/yolov5/yolov5s.wts {tensorrtx}/yolov5/build
109 | cmake ..
110 | make
111 | // yolov5s
112 | sudo ./yolov5 -s yolov5s.wts yolov5s.engine s
113 | // test your engine file
114 | sudo ./yolov5 -d yolov5s.engine ../samples
115 | ```
116 | Then you get the yolov5s.engine, and you can put `yolov5s.engine` in My project. For example
117 |
118 | ```shell
119 | cd {yolov5-deepsort-tensorrt}
120 | mkdir resources
121 | cp {tensorrtx}/yolov5/build/yolov5s.engine {yolov5-deepsort-tensorrt}/resources
122 | ```
123 |
124 | 5. Get deepsort engine file
125 | You can get deepsort pretrained model in this [drive url](https://drive.google.com/drive/folders/1xhG0kRH1EX5B9_Iz8gQJb7UNnn_riXi6)
126 | and ckpt.t7 is ok.
127 | ```shell
128 | git clone https://github.com/RichardoMrMu/deepsort-tensorrt.git
129 | // 根据github的说明
130 | cp {deepsort-tensorrt}/exportOnnx.py {deep_sort_pytorch}/
131 | python3 exportOnnx.py
132 | mv {deep_sort_pytorch}/deepsort.onnx {deepsort-tensorrt}/resources
133 | cd {deepsort-tensorrt}
134 | mkdir build
135 | cd build
136 | cmake ..
137 | make
138 | ./onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine
139 | // test
140 | ./demo ../resource/deepsort.engine ../resources/track.txt
141 | ```
142 | After all 5 step, you can get the yolov5s.engine and deepsort.engine.
143 |
144 | You may face some problems in getting yolov5s.engine and deepsort.engine, you can upload your issue in github or [csdn artical](https://blog.csdn.net/weixin_42264234/article/details/120152117).
145 |
146 | Different versions of yolov5
147 |
148 | Currently, tensorrt support yolov5 v1.0(yolov5s only), v2.0, v3.0, v3.1, v4.0 and v5.0.
149 |
150 | - For yolov5 v5.0, download .pt from [yolov5 release v5.0](https://github.com/ultralytics/yolov5/releases/tag/v5.0), `git clone -b v5.0 https://github.com/ultralytics/yolov5.git` and `git clone https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in current page.
151 | - For yolov5 v4.0, download .pt from [yolov5 release v4.0](https://github.com/ultralytics/yolov5/releases/tag/v4.0), `git clone -b v4.0 https://github.com/ultralytics/yolov5.git` and `git clone -b yolov5-v4.0 https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in [tensorrtx/yolov5-v4.0](https://github.com/wang-xinyu/tensorrtx/tree/yolov5-v4.0/yolov5).
152 | - For yolov5 v3.1, download .pt from [yolov5 release v3.1](https://github.com/ultralytics/yolov5/releases/tag/v3.1), `git clone -b v3.1 https://github.com/ultralytics/yolov5.git` and `git clone -b yolov5-v3.1 https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in [tensorrtx/yolov5-v3.1](https://github.com/wang-xinyu/tensorrtx/tree/yolov5-v3.1/yolov5).
153 | - For yolov5 v3.0, download .pt from [yolov5 release v3.0](https://github.com/ultralytics/yolov5/releases/tag/v3.0), `git clone -b v3.0 https://github.com/ultralytics/yolov5.git` and `git clone -b yolov5-v3.0 https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in [tensorrtx/yolov5-v3.0](https://github.com/wang-xinyu/tensorrtx/tree/yolov5-v3.0/yolov5).
154 | - For yolov5 v2.0, download .pt from [yolov5 release v2.0](https://github.com/ultralytics/yolov5/releases/tag/v2.0), `git clone -b v2.0 https://github.com/ultralytics/yolov5.git` and `git clone -b yolov5-v2.0 https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in [tensorrtx/yolov5-v2.0](https://github.com/wang-xinyu/tensorrtx/tree/yolov5-v2.0/yolov5).
155 | - For yolov5 v1.0, download .pt from [yolov5 release v1.0](https://github.com/ultralytics/yolov5/releases/tag/v1.0), `git clone -b v1.0 https://github.com/ultralytics/yolov5.git` and `git clone -b yolov5-v1.0 https://github.com/wang-xinyu/tensorrtx.git`, then follow how-to-run in [tensorrtx/yolov5-v1.0](https://github.com/wang-xinyu/tensorrtx/tree/yolov5-v1.0/yolov5).
156 |
157 |
158 |
159 |
160 | Config
161 |
162 | - Choose the model s/m/l/x/s6/m6/l6/x6 from command line arguments.
163 | - Input shape defined in yololayer.h
164 | - Number of classes defined in yololayer.h, **DO NOT FORGET TO ADAPT THIS, If using your own model**
165 | - INT8/FP16/FP32 can be selected by the macro in yolov5.cpp, **INT8 need more steps, pls follow `How to Run` first and then go the `INT8 Quantization` below**
166 | - GPU id can be selected by the macro in yolov5.cpp
167 | - NMS thresh in yolov5.cpp
168 | - BBox confidence thresh in yolov5.cpp
169 | - Batch size in yolov5.cpp
170 |
171 |
172 | ## Run Your Custom Model
173 | You may need train your own model and transfer your trained-model to tensorRT. So you can follow the following steps.
174 | 1. Train Custom Model
175 | You can follow the [official wiki](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data
176 | ) to train your own model on your dataset. For example, I choose yolov5-s to train my model.
177 | 2. Transfer Custom Model
178 | Just like [tensorRT official guideline](https://github.com/wang-xinyu/tensorrtx/edit/master/yolov5/).When your follow ` Generate yolov5 model` to get yolov5 and tensorrt rep, next step is to transfer your pytorch model to tensorrt.
179 | Before this, you need to change yololayer.h file 20,21 and 22 line(CLASS_NUM,INPUT_H,INPUT_W) to your own parameters.
180 |
181 | ```shell
182 | // before
183 | static constexpr int CLASS_NUM = 80; // 20
184 | static constexpr int INPUT_H = 640; // 21 yolov5's input height and width must be divisible by 32.
185 | static constexpr int INPUT_W = 640; // 22
186 |
187 | // after
188 | // if your model is 2 classfication and image size is 416*416
189 | static constexpr int CLASS_NUM = 2; // 20
190 | static constexpr int INPUT_H = 416; // 21 yolov5's input height and width must be divisible by 32.
191 | static constexpr int INPUT_W = 416; // 22
192 | ```
193 |
194 | ```shell
195 | cd {tensorrtx}/yolov5/
196 | // update CLASS_NUM in yololayer.h if your model is trained on custom dataset
197 |
198 | mkdir build
199 | cd build
200 | cp {ultralytics}/yolov5/yolov5s.wts {tensorrtx}/yolov5/build
201 | cmake ..
202 | make
203 | sudo ./yolov5 -s [.wts] [.engine] [s/m/l/x/s6/m6/l6/x6 or c/c6 gd gw] // serialize model to plan file
204 | sudo ./yolov5 -d [.engine] [image folder] // deserialize and run inference, the images in [image folder] will be processed.
205 | // For example yolov5s
206 | sudo ./yolov5 -s yolov5s.wts yolov5s.engine s
207 | sudo ./yolov5 -d yolov5s.engine ../samples
208 | // For example Custom model with depth_multiple=0.17, width_multiple=0.25 in yolov5.yaml
209 | sudo ./yolov5 -s yolov5_custom.wts yolov5.engine c 0.17 0.25
210 | sudo ./yolov5 -d yolov5.engine ../samples
211 | ```
212 |
213 | In this way, you can get your own tensorrt yolov5 model. Enjoy it!
214 |
215 | # Other Project
216 | - [yolov5-deepsort-tensorrt](https://github.com/RichardoMrMu/yolov5-deepsort-tensorrt)
217 | - [facial-emotion-recognition](https://github.com/RichardoMrMu/facial-emotion-recognition)
218 | - [yolov5-fire-smoke-detect-python](https://github.com/RichardoMrMu/yolov5-fire-smoke-detect-python)
219 | - [yolov5-helmet-detection](https://github.com/RichardoMrMu/yolov5-helmet-detection)
220 | - [yolov5-fire-smoke-detect](https://github.com/RichardoMrMu/yolov5-fire-smoke-detect)
221 | - [gazecapture-web gaze estimation](https://github.com/RichardoMrMu/gazecapture-web)
222 | - [yolov5-helmet-detection-python](https://github.com/RichardoMrMu/yolov5-helmet-detection-python)
223 | - [yolov5-reflective-clothes-detect-python](https://github.com/RichardoMrMu/yolov5-reflective-clothes-detect-python)
224 | - [yolov5-reflective-clothes-detect](https://github.com/RichardoMrMu/yolov5-reflective-clothes-detect)
225 | - [yolov5-smoking-detect](https://github.com/RichardoMrMu/yolov5-smoking-detect)
226 | - [yolov5-mask-detect](https://github.com/RichardoMrMu/yolov5-mask-detect)
227 | - [yolov5-mask-detection-python](https://github.com/RichardoMrMu/yolov5-mask-detection-python)
228 | - [yolov5-smoke-detection-python](https://github.com/RichardoMrMu/yolov5-smoke-detection-python)
229 | - [deepsort-tensorrt](https://github.com/RichardoMrMu/deepsort-tensorrt)
230 |
231 |
232 |
233 |
234 |
235 |
--------------------------------------------------------------------------------
/assets/yolosort.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RichardoMrMu/yolov5-deepsort-tensorrt/33cd5cc49371fab325fe6970a4cc73f8f690af70/assets/yolosort.gif
--------------------------------------------------------------------------------
/deepsort/include/datatype.h:
--------------------------------------------------------------------------------
1 | #ifndef DATATYPE_H
2 | #define DATATYPE_H
3 |
4 | typedef struct DetectBox {
5 | DetectBox(float x1=0, float y1=0, float x2=0, float y2=0,
6 | float confidence=0, float classID=-1, float trackID=-1) {
7 | this->x1 = x1;
8 | this->y1 = y1;
9 | this->x2 = x2;
10 | this->y2 = y2;
11 | this->confidence = confidence;
12 | this->classID = classID;
13 | this->trackID = trackID;
14 | }
15 | float x1, y1, x2, y2;
16 | float confidence;
17 | float classID;
18 | float trackID;
19 | } DetectBox;
20 |
21 | #endif // DATATYPE_H
22 |
23 | #ifndef DEEPSORTDATATYPE_H
24 | #define DEEPSORTDATATYPE_H
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | typedef struct CLSCONF {
31 | CLSCONF() {
32 | this->cls = -1;
33 | this->conf = -1;
34 | }
35 | CLSCONF(int cls, float conf) {
36 | this->cls = cls;
37 | this->conf = conf;
38 | }
39 | int cls;
40 | float conf;
41 | } CLSCONF;
42 |
43 | typedef Eigen::Matrix DETECTBOX;
44 | typedef Eigen::Matrix DETECTBOXSS;
45 | typedef Eigen::Matrix FEATURE;
46 | typedef Eigen::Matrix FEATURESS;
47 | //typedef std::vector FEATURESS;
48 |
49 | //Kalmanfilter
50 | //typedef Eigen::Matrix KAL_FILTER;
51 | typedef Eigen::Matrix KAL_MEAN;
52 | typedef Eigen::Matrix KAL_COVA;
53 | typedef Eigen::Matrix KAL_HMEAN;
54 | typedef Eigen::Matrix KAL_HCOVA;
55 | using KAL_DATA = std::pair;
56 | using KAL_HDATA = std::pair;
57 |
58 | //main
59 | using RESULT_DATA = std::pair;
60 |
61 | //tracker:
62 | using TRACKER_DATA = std::pair;
63 | using MATCH_DATA = std::pair;
64 | typedef struct t{
65 | std::vector matches;
66 | std::vector unmatched_tracks;
67 | std::vector unmatched_detections;
68 | }TRACHER_MATCHD;
69 |
70 | //linear_assignment:
71 | typedef Eigen::Matrix DYNAMICM;
72 |
73 | #endif //DEEPSORTDATATYPE_H
--------------------------------------------------------------------------------
/deepsort/include/deepsort.h:
--------------------------------------------------------------------------------
1 | #ifndef DEEPSORT_H
2 | #define DEEPSORT_H
3 |
4 | #include
5 | #include
6 | #include "featuretensor.h"
7 | #include "tracker.h"
8 | #include "datatype.h"
9 | #include
10 |
11 | using std::vector;
12 | using nvinfer1::ILogger;
13 |
14 | class DeepSort {
15 | public:
16 | DeepSort(std::string modelPath, int batchSize, int featureDim, int gpuID, ILogger* gLogger);
17 | ~DeepSort();
18 |
19 | public:
20 | void sort(cv::Mat& frame, vector& dets);
21 |
22 | private:
23 | void sort(cv::Mat& frame, DETECTIONS& detections);
24 | void sort(cv::Mat& frame, DETECTIONSV2& detectionsv2);
25 | void sort(vector& dets);
26 | void sort(DETECTIONS& detections);
27 | void init();
28 |
29 | private:
30 | std::string enginePath;
31 | int batchSize;
32 | int featureDim;
33 | cv::Size imgShape;
34 | float confThres;
35 | float nmsThres;
36 | int maxBudget;
37 | float maxCosineDist;
38 |
39 | private:
40 | vector result;
41 | vector> results;
42 | tracker* objTracker;
43 | FeatureTensor* featureExtractor;
44 | ILogger* gLogger;
45 | int gpuID;
46 | };
47 |
48 | #endif //deepsort.h
49 |
--------------------------------------------------------------------------------
/deepsort/include/deepsortenginegenerator.h:
--------------------------------------------------------------------------------
1 | #ifndef DEEPSORT_ENGINE_GENERATOR_H
2 | #define DEEPSORT_ENGINE_GENERATOR_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | using namespace nvinfer1;
9 |
10 | const int IMG_HEIGHT = 128;
11 | const int IMG_WIDTH = 64;
12 | const int MAX_BATCH_SIZE = 128;
13 | const std::string INPUT_NAME("input");
14 |
15 | class DeepSortEngineGenerator {
16 | public:
17 | DeepSortEngineGenerator(ILogger* gLogger);
18 | ~DeepSortEngineGenerator();
19 |
20 | public:
21 | void setFP16(bool state);
22 | void createEngine(std::string onnxPath, std::string enginePath);
23 |
24 | private:
25 | std::string modelPath, engingPath;
26 | ILogger* gLogger;
27 | bool useFP16;
28 | };
29 |
30 | #endif
--------------------------------------------------------------------------------
/deepsort/include/featuretensor.h:
--------------------------------------------------------------------------------
1 | #ifndef FEATURETENSOR_H
2 | #define FEATURETENSOR_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include "model.hpp"
10 | #include "datatype.h"
11 | #include "cuda_runtime_api.h"
12 |
13 | using std::vector;
14 | using nvinfer1::ILogger;
15 |
16 | class FeatureTensor {
17 | public:
18 | FeatureTensor(const int maxBatchSize, const cv::Size imgShape, const int featureDim, int gpuID, ILogger* gLogger);
19 | ~FeatureTensor();
20 |
21 | public:
22 | bool getRectsFeature(const cv::Mat& img, DETECTIONS& det);
23 | bool getRectsFeature(DETECTIONS& det);
24 | void loadEngine(std::string enginePath);
25 | void loadOnnx(std::string onnxPath);
26 | int getResult(float*& buffer);
27 | void doInference(vector& imgMats);
28 |
29 | private:
30 | void initResource();
31 | void doInference(float* inputBuffer, float* outputBuffer);
32 | void mat2stream(vector& imgMats, float* stream);
33 | void stream2det(float* stream, DETECTIONS& det);
34 |
35 | private:
36 | nvinfer1::IRuntime* runtime;
37 | nvinfer1::ICudaEngine* engine;
38 | nvinfer1::IExecutionContext* context;
39 | const int maxBatchSize;
40 | const cv::Size imgShape;
41 | const int featureDim;
42 |
43 | private:
44 | int curBatchSize;
45 | const int inputStreamSize, outputStreamSize;
46 | bool initFlag;
47 | float* const inputBuffer;
48 | float* const outputBuffer;
49 | int inputIndex, outputIndex;
50 | void* buffers[2];
51 | cudaStream_t cudaStream;
52 | // BGR format
53 | float means[3], std[3];
54 | const std::string inputName, outputName;
55 | ILogger* gLogger;
56 | };
57 |
58 | #endif
59 |
--------------------------------------------------------------------------------
/deepsort/include/hungarianoper.h:
--------------------------------------------------------------------------------
1 | #ifndef HUNGARIANOPER_H
2 | #define HUNGARIANOPER_H
3 |
4 | #include "munkres.h"
5 | #include "datatype.h"
6 |
7 |
8 | class HungarianOper {
9 | public:
10 | static Eigen::Matrix Solve(const DYNAMICM &cost_matrix);
11 | };
12 |
13 | #endif // HUNGARIANOPER_H
14 |
--------------------------------------------------------------------------------
/deepsort/include/kalmanfilter.h:
--------------------------------------------------------------------------------
1 | #ifndef KALMANFILTER_H
2 | #define KALMANFILTER_H
3 |
4 | #include "datatype.h"
5 |
6 | class KalmanFilter {
7 | public:
8 | static const double chi2inv95[10];
9 | KalmanFilter();
10 | KAL_DATA initiate(const DETECTBOX& measurement);
11 | void predict(KAL_MEAN& mean, KAL_COVA& covariance);
12 | KAL_HDATA project(const KAL_MEAN& mean, const KAL_COVA& covariance);
13 | KAL_DATA update(const KAL_MEAN& mean,
14 | const KAL_COVA& covariance,
15 | const DETECTBOX& measurement);
16 |
17 | Eigen::Matrix gating_distance(
18 | const KAL_MEAN& mean,
19 | const KAL_COVA& covariance,
20 | const std::vector& measurements,
21 | bool only_position = false);
22 |
23 | private:
24 | Eigen::Matrix _motion_mat;
25 | Eigen::Matrix _update_mat;
26 | float _std_weight_position;
27 | float _std_weight_velocity;
28 | };
29 |
30 | #endif // KALMANFILTER_H
31 |
--------------------------------------------------------------------------------
/deepsort/include/linear_assignment.h:
--------------------------------------------------------------------------------
1 | #ifndef LINEAR_ASSIGNMENT_H
2 | #define LINEAR_ASSIGNMENT_H
3 |
4 | #include "datatype.h"
5 | #include "tracker.h"
6 |
7 | #define INFTY_COST 1e5
8 | class tracker;
9 | //for matching;
10 | class linear_assignment
11 | {
12 | linear_assignment();
13 | linear_assignment(const linear_assignment& );
14 | linear_assignment& operator=(const linear_assignment&);
15 | static linear_assignment* instance;
16 |
17 | public:
18 | static linear_assignment* getInstance();
19 | TRACHER_MATCHD matching_cascade(tracker* distance_metric,
20 | tracker::GATED_METRIC_FUNC distance_metric_func,
21 | float max_distance,
22 | int cascade_depth,
23 | std::vector