Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 去除Onnxruntime的依赖,使用cv::DNN进行推理feature.onnx #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,9 @@ set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


find_package (Eigen3 3.3 REQUIRED NO_MODULE)
find_package (Eigen3 REQUIRED NO_MODULE)


set(ONNXRUNTIME_DIR "/home/a/lib/onnxruntime-linux-x64-1.12.1")

include_directories("${ONNXRUNTIME_DIR}/include"
)
find_package(OpenCV 4 REQUIRED )


Expand Down Expand Up @@ -45,7 +41,7 @@ add_executable(DeepSORT
tracker/bytetrack/src/utils.cpp

main.cpp)
target_link_libraries(DeepSORT PRIVATE "${ONNXRUNTIME_DIR}/lib/libonnxruntime.so" ${OpenCV_LIBS} Eigen3::Eigen)
target_link_libraries(DeepSORT PRIVATE ${OpenCV_LIBS} Eigen3::Eigen)



Expand Down
26 changes: 2 additions & 24 deletions tracker/deepsort/include/FeatureTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@
#include <string>
#include <vector>
#include <stdexcept>
#include <onnxruntime_cxx_api.h>
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
typedef unsigned char uint8;

template <typename T>
Expand Down Expand Up @@ -50,7 +45,6 @@ class FeatureTensor
public:
static FeatureTensor *getInstance();
bool getRectsFeature(const cv::Mat &img, DETECTIONS &d);
void preprocess(cv::Mat &imageBGR, std::vector<float> &inputTensorValues, size_t &inputTensorSize);

private:
FeatureTensor();
Expand All @@ -60,26 +54,10 @@ class FeatureTensor
bool init();
~FeatureTensor();

void tobuffer(const std::vector<cv::Mat> &imgs, uint8 *buf);

void preprocess(cv::Mat &imageBGR);
public:
void test();

static constexpr const int width_ = 64;
static constexpr const int height_ = 128;

std::array<float, width_ * height_> input_image_{};

std::array<float, k_feature_dim> results_{};

Ort::Env env;
Ort::Session session_{env, k_feature_model_path.c_str(), Ort::SessionOptions{nullptr}};

Ort::Value input_tensor_{nullptr};
std::array<int64_t, 4> input_shape_{1, 3, width_, height_};

Ort::Value output_tensor_{nullptr};
std::array<int64_t, 2> output_shape_{1, k_feature_dim};

std::vector<int64_t> inputDims_;
cv::dnn::Net net;
};
92 changes: 11 additions & 81 deletions tracker/deepsort/src/FeatureTensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,12 @@ FeatureTensor::~FeatureTensor()
bool FeatureTensor::init()
{

Ort::TypeInfo inputTypeInfo = session_.GetInputTypeInfo(0);
auto inputTensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();

ONNXTensorElementDataType inputType = inputTensorInfo.GetElementType();
std::cout << "Input Type: " << inputType << std::endl;

inputDims_ = inputTensorInfo.GetShape();
std::cout << "Input Dimensions: " << inputDims_ << std::endl; // [-1, 3, 128, 64]
inputDims_[0] = 1;
this->net = cv::dnn::readNetFromONNX(k_feature_model_path);
std::cout << "FeatureTensor::init() " << std::endl;


return true;
}

void FeatureTensor::preprocess(cv::Mat &imageBGR, std::vector<float> &inputTensorValues, size_t &inputTensorSize)
void FeatureTensor::preprocess(cv::Mat &imageBGR)
{

// pre-processing the Image
Expand All @@ -65,9 +55,9 @@ void FeatureTensor::preprocess(cv::Mat &imageBGR, std::vector<float> &inputTenso

// step 2: Resize the image.
cv::Mat resizedImageBGR, resizedImageRGB, resizedImage, preprocessedImage;
cv::resize(imageBGR, resizedImageBGR,
cv::Size(inputDims_.at(3), inputDims_.at(2)),
cv::InterpolationFlags::INTER_CUBIC);
cv::resize(imageBGR, resizedImageBGR,
cv::Size(width_, height_),
cv::InterpolationFlags::INTER_CUBIC);

// cv::resize(imageBGR, resizedImageBGR,
// cv::Size(64, 128));
Expand Down Expand Up @@ -96,11 +86,7 @@ void FeatureTensor::preprocess(cv::Mat &imageBGR, std::vector<float> &inputTenso
// step 8: Convert the image to CHW RGB float format.
// HWC to CHW
cv::dnn::blobFromImage(resizedImage, preprocessedImage);
inputTensorSize = vectorProduct(inputDims_);
inputTensorValues.assign(preprocessedImage.begin<float>(),
preprocessedImage.end<float>());

std::cout << "inputTensorSize:" << inputTensorValues.size() << std::endl;
this->net.setInput(preprocessedImage);
}

bool FeatureTensor::getRectsFeature(const cv::Mat &img, DETECTIONS &d)
Expand All @@ -118,72 +104,16 @@ bool FeatureTensor::getRectsFeature(const cv::Mat &img, DETECTIONS &d)
rc.height = (rc.y + rc.height <= img.rows ? rc.height : (img.rows - rc.y));

cv::Mat mattmp = img(rc).clone();
preprocess(mattmp);

std::vector<float> inputTensorValues;
size_t inputTensorSize;
preprocess(mattmp, inputTensorValues, inputTensorSize);

const char *input_names[] = {"input"}; //输入节点名
const char *output_names[] = {"output"}; //输出节点名

auto memory_info = Ort::MemoryInfo::CreateCpu(OrtDeviceAllocator, OrtMemTypeDefault);
output_tensor_ = Ort::Value::CreateTensor<float>(memory_info, results_.data(), results_.size(), output_shape_.data(), output_shape_.size());

std::vector<Ort::Value> inputTensors;
inputTensors.push_back(Ort::Value::CreateTensor<float>(
memory_info, inputTensorValues.data(), inputTensorSize, inputDims_.data(),
inputDims_.size()));


session_.Run(Ort::RunOptions{nullptr}, input_names, inputTensors.data(), 1, output_names, &output_tensor_, 1);

Ort::TensorTypeAndShapeInfo shape_info = output_tensor_.GetTensorTypeAndShapeInfo();


size_t dim_count = shape_info.GetDimensionsCount();
std::cout << "dim_count:" << dim_count << std::endl;
cv::Mat preds = this->net.forward("output");
auto *ptr = preds.ptr<float>(0);


int64_t dims[2];
shape_info.GetDimensions(dims, sizeof(dims) / sizeof(dims[0]));
std::cout << "output shape:" << dims[0] << "," << dims[1] << std::endl;


float *f = output_tensor_.GetTensorMutableData<float>();
for (int i = 0; i < dims[1]; i++) //sisyphus
for (int i = 0; i < preds.cols; i++)
{
dbox.feature[i] = f[i];
dbox.feature[i] = ptr[i];
}
}

return true;
}

void FeatureTensor::tobuffer(const std::vector<cv::Mat> &imgs, uint8 *buf)
{
int pos = 0;
for (const cv::Mat &img : imgs)
{
int Lenth = img.rows * img.cols * 3;
int nr = img.rows;
int nc = img.cols;
if (img.isContinuous())
{
nr = 1;
nc = Lenth;
}
for (int i = 0; i < nr; i++)
{
const uchar *inData = img.ptr<uchar>(i);
for (int j = 0; j < nc; j++)
{
buf[pos] = *inData++;
pos++;
}
} // end for
} // end imgs;
}
void FeatureTensor::test()
{
return;
}