Open Kingxudong opened 2 months ago
可以先用这个脚本测一下导出的结果
有测试,无论是官方提供的权重文件还是我自己的权重文件,都出向没有“create_execution_context”这个模块,未定义
trt版本是多少
box不对看下是不是数据类型的问题
trt版本是多少
box不对看下是不是数据类型的问题
trt版本是8.6.1; 但是标签和得分都是对的
遇见相同的问题,在onnx转tensorrt后,使用https://github.com/lyuwenyu/RT-DETR/blob/main/rtdetrv2_pytorch/references/deploy/rtdetrv2_tensorrt.py脚本推理结果不对。
pth转换为onnx,用官方的代码转,并且测试是正确。但是onnx转换为trt,用的是tensorRT8.6.1,转换没有问题,但是推理出现box坐标是特别大的值,标签和得分是正确的。 D:\tool\TensorRT-8.6.1.6.Windows10.x86_64.cuda-11.8\TensorRT-8.6.1.6\bin\trtexec.exe --onnx=model.onnx --workspace=4096 --avgRuns=100 --shapes=images:1x3x640x640 --saveEngine=model.trt
我的C++推理代码如下
include
include
include
include
include
include
include <opencv2/opencv.hpp>
include "NvInfer.h"
include "NvInferRuntimeCommon.h"
include
include <opencv2/core/utils/filesystem.hpp>
using namespace nvinfer1; using namespace std;
define CUDA_CHECK(call) \
class Logger : public ILogger { public: void log(Severity severity, const char* msg) noexcept override { if (severity != Severity::kINFO) std::cout << msg << std::endl; } } gLogger; class TensorRTInference { public: TensorRTInference(const std::string& enginePath); ~TensorRTInference(); void doInference(const std::vector& image_paths);
private: IRuntime runtime; ICudaEngine engine; IExecutionContext* context; void* buffers[5]; int inputIndex1; int inputIndex2; int outputIndex1; int outputIndex2; int outputIndex3; int batchSize; int inputSize1; int inputSize2; int outputSize1; int outputSize2; int outputSize3;
};
int volume(const Dims& dims) { int vol = 1; for (int i = 0; i < dims.nbDims; ++i) { vol *= dims.d[i]; } return vol; } TensorRTInference::TensorRTInference(const std::string& enginePath) : engine(nullptr), context(nullptr) { std::ifstream engineFile(enginePath, std::ios::binary); if (!engineFile.good()) { std::cerr << "Error opening engine file: " << enginePath << std::endl; return; }
}
std::vector ToTensor(cv::Mat image) {
if (image.empty()) {
std::cerr << "Error: Empty image" << std::endl;
return {};
}
} std::vector ToTensorAndNormalize(cv::Mat image) {
if (image.empty()) {
std::cerr << "Error: Empty image" << std::endl;
return {};
}
}
TensorRTInference::~TensorRTInference() { if (context) { context->destroy(); } if (engine) { engine->destroy(); } if (runtime) { runtime->destroy(); } for (int i = 0; i < 5; ++i) { if (buffers[i]) cudaFree(buffers[i]); } }
void TensorRTInference::preprocess(const cv::Mat& image, float buffer1, float buffer2) { cv::Mat rgb_image;
}
void TensorRTInference::postprocess(float output1, float output2, float output3, cv::Mat& image) { float numDetections = outputSize2_ / (4 sizeof(float)); float confThreshold = 0.5f;
}
std::string replaceFolderName(const std::string& path, const std::string& oldFolder, const std::string& newFolder) { size_t pos = path.find(oldFolder); if (pos != std::string::npos) { std::string newPath = path.substr(0, pos) + newFolder + path.substr(pos + oldFolder.length()); return newPath; } else { return path; } }
void softmax(float output, std::vector& probs) {
probs.clear();
float sum = 0.0f;
for (int i = 0; i < 2; ++i) {
probs.push_back(std::exp(output[i]));
sum += probs.back();
}
for (int i = 0; i < 2; ++i) {
probs[i] /= sum;
}
}
void TensorRTInference::allocateBuffers() {
std::cout << "Allocating buffers..." << std::endl;
CUDACHECK(cudaMalloc(&buffers[inputIndex1], inputSize1));
CUDACHECK(cudaMalloc(&buffers[inputIndex2], inputSize2));
CUDACHECK(cudaMalloc(&buffers[outputIndex1], outputSize1));
CUDACHECK(cudaMalloc(&buffers[outputIndex2], outputSize2));
CUDACHECK(cudaMalloc(&buffers[outputIndex3], outputSize3));
std::cout << "Buffers allocated successfully." << std::endl;
}
void TensorRTInference::doInference(const std::vector& image_paths) {
float inputBuffer1 = new float[inputSize1 / sizeof(float)];
float* inputBuffer2 = new float[inputSize2 / sizeof(float)];
}
int main(int argc, char* argv) { try { cudaSetDevice(0); TensorRTInference inference("D:\tool\TensorRT-8.6.1.6.Windows10.x86_64.cuda-11.8\TensorRT-8.6.1.6\bin\cfg_model.trt"); std::string img_dir = "E:\YOLOv8-main\val2017\debug"; std::vector image_paths;
cv::utils::fs::glob(img_dir, " .png", image_paths);
inference.doInference(image_paths);
}
catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
能帮忙检查一下吗