├── include
├── hungarianoper.h
├── deepsortenginegenerator.h
├── kalmanfilter.h
├── model.hpp
├── deepsort.h
├── nn_matching.h
├── linear_assignment.h
├── featuretensor.h
├── tracker.h
├── datatype.h
├── track.h
├── matrix.h
├── munkres.h
└── logging.h
├── README.md
├── onnx2engine.cpp
├── src
├── munkres.cpp
├── hungarianoper.cpp
├── deepsortenginegenerator.cpp
├── track.cpp
├── deepsort.cpp
├── kalmanfilter.cpp
├── nn_matching.cpp
├── featuretensor.cpp
├── linear_assignment.cpp
└── tracker.cpp
├── CMakeLists.txt
├── CMakeLists_deepsort-tensorrt_win10.txt
├── exportOnnx.py
├── demo.cpp
└── LICENSE
/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 |
--------------------------------------------------------------------------------
/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
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DeepSort_TensorRT
2 | Use tensorrt to accelerate deepsort tracking.
3 | 使用tensorrt来加速deepsort的特征提取。
4 | ## Install
5 | [REQUIRE] TensorRT 7
6 | [REQUIRE] OpenCV
7 | 下载本库和deepsort的pytorch库
8 | ```
9 | git clone git@github.com:ZQPei/deep_sort_pytorch.git
10 | git clone https://github.com/RichardoMrMu/deepsort-tensorrt.git
11 | ```
12 | 配置好pytorch工程后,将exportOnnx.py复制到pytorch工程的项目目录下运行,将会生成deepsort.onnx。
13 | 把deepsort.onnx移到本工程的resources目录下。然后回到工程的根目录
14 | ```
15 | mkdir build
16 | cd build
17 | cmake ..
18 | make
19 | ./onnx2engine ../resources/deepsort.onnx ../resources/deepsort.engine
20 | ```
21 | 然后就可以运行demo测试了
22 | ```
23 | ./demo ../resources/deepsort.engine ../resources/track.txt
24 | ```
25 | 然后就可以把自己TensorRT的目标检测工程进行对接。
26 | ```
27 | git clone https://github.com/wang-xinyu/tensorrtx.git
28 | ```
29 | 注:视频文件太大,所以只把检测结果保存成txt进行测试。
30 |
--------------------------------------------------------------------------------
/onnx2engine.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "deepsortenginegenerator.h"
4 | #include "cuda_runtime_api.h"
5 | #include "logging.h"
6 |
7 | using namespace nvinfer1;
8 |
9 | static Logger gLogger;
10 |
11 | int main(int argc, char** argv) {
12 | cudaSetDevice(0);
13 | if (argc < 3) {
14 | std::cout << "./onnx2engine [input .onnx path] [output .engine path]" << std::endl;
15 | return -1;
16 | }
17 | std::string onnxPath = argv[1];
18 | std::string enginePath = argv[2];
19 | DeepSortEngineGenerator* engG = new DeepSortEngineGenerator(&gLogger);
20 | engG->setFP16(true);
21 | engG->createEngine(onnxPath, enginePath);
22 | std::cout << "==============" << std::endl;
23 | std::cout << "| SUCCESS! |" << std::endl;
24 | std::cout << "==============" << std::endl;
25 | return 0;
26 | }
27 |
28 |
29 |
--------------------------------------------------------------------------------
/src/munkres.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2007 John Weaver
3 | * Copyright (c) 2015 Miroslav Krajicek
4 | *
5 | * This program is free software; you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation; either version 2 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License
16 | * along with this program; if not, write to the Free Software
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 | */
19 |
20 | #include "munkres.h"
21 |
22 | template class Munkres;
23 | template class Munkres;
24 | template class Munkres;
25 |
26 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/CMakeLists.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 |
7 | find_package(CUDA REQUIRED)
8 | find_package(OpenCV REQUIRED)
9 |
10 |
11 | include_directories(
12 | ${CUDA_INCLUDE_DIRS}
13 | ${OpenCV_INCLUDE_DIRS}
14 | ${PROJECT_SOURCE_DIR}/include
15 | )
16 | link_directories(
17 | /usr/local/cuda/lib64
18 | )
19 | aux_source_directory(${PROJECT_SOURCE_DIR}/src SRC_DIR)
20 |
21 | # ===== deepsort =====
22 | add_library(deepsort SHARED ${SRC_DIR})
23 | target_link_libraries(deepsort
24 | ${CUDA_LIBS} ${OpenCV_LIBS}
25 | cudart nvinfer nvonnxparser
26 | )
27 |
28 | # ===== onnx2engine =====
29 | add_executable(onnx2engine ${PROJECT_SOURCE_DIR}/onnx2engine.cpp)
30 | target_link_libraries(onnx2engine
31 | ${CUDA_LIBS}
32 | cudart nvinfer nvonnxparser deepsort
33 | )
34 |
35 | # ===== demo =====
36 | add_executable(demo ${PROJECT_SOURCE_DIR}/demo.cpp)
37 | target_link_libraries(demo
38 | ${CUDA_LIBS} ${OpenCV_LIBS}
39 | cudart nvinfer nvonnxparser deepsort
40 | )
41 |
42 |
43 |
--------------------------------------------------------------------------------
/src/hungarianoper.cpp:
--------------------------------------------------------------------------------
1 | #include "hungarianoper.h"
2 |
3 | Eigen::Matrix HungarianOper::Solve(const DYNAMICM &cost_matrix) {
4 | int rows = cost_matrix.rows();
5 | int cols = cost_matrix.cols();
6 | Matrix matrix(rows, cols);
7 | for (int row = 0; row < rows; row++) {
8 | for (int col = 0; col < cols; col++) {
9 | matrix(row, col) = cost_matrix(row, col);
10 | }
11 | }
12 | //Munkres get matrix;
13 | Munkres m;
14 | m.solve(matrix);
15 |
16 | //
17 | std::vector> pairs;
18 | for (int row = 0; row < rows; row++) {
19 | for (int col = 0; col < cols; col++) {
20 | int tmp = (int)matrix(row, col);
21 | if (tmp == 0) pairs.push_back(std::make_pair(row, col));
22 | }
23 | }
24 | //
25 | int count = pairs.size();
26 | Eigen::Matrix re(count, 2);
27 | for (int i = 0; i < count; i++) {
28 | re(i, 0) = pairs[i].first;
29 | re(i, 1) = pairs[i].second;
30 | }
31 | return re;
32 | }//end Solve;
33 |
--------------------------------------------------------------------------------
/include/model.hpp:
--------------------------------------------------------------------------------
1 | #ifndef MODEL_HPP
2 | #define MODEL_HPP
3 |
4 | #include
5 | #include "datatype.h"
6 |
7 |
8 | // * Each rect's data structure.
9 | // * tlwh: topleft point & (w,h)
10 | // * confidence: detection confidence.
11 | // * feature: the rect's 256d feature.
12 | // */
13 |
14 | const float kRatio=0.5;
15 | enum DETECTBOX_IDX {IDX_X = 0, IDX_Y, IDX_W, IDX_H };
16 |
17 | class DETECTION_ROW {
18 | public:
19 | DETECTBOX tlwh;
20 | float confidence;
21 | FEATURE feature;
22 | DETECTBOX to_xyah() const {
23 | //(centerx, centery, ration, h)
24 | DETECTBOX ret = tlwh;
25 | ret(0, IDX_X) += (ret(0, IDX_W)*kRatio);
26 | ret(0, IDX_Y) += (ret(0, IDX_H)*kRatio);
27 | ret(0, IDX_W) /= ret(0, IDX_H);
28 | return ret;
29 | }
30 | DETECTBOX to_tlbr() const {
31 | //(x,y,xx,yy)
32 | DETECTBOX ret = tlwh;
33 | ret(0, IDX_X) += ret(0, IDX_W);
34 | ret(0, IDX_Y) += ret(0, IDX_H);
35 | return ret;
36 | }
37 | };
38 |
39 | typedef std::vector DETECTIONS;
40 | typedef std::pair, DETECTIONS> DETECTIONSV2;
41 |
42 | #endif // MODEL_HPP
43 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/include/nn_matching.h:
--------------------------------------------------------------------------------
1 | #ifndef NN_MATCHING_H
2 | #define NN_MATCHING_H
3 |
4 | #include "datatype.h"
5 |
6 | #include