You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#define CUDA_CHECK(call)
do {
cudaError_t status = call;
if (status != cudaSuccess) {
fprintf(stderr, "CUDA Error in file '%s' in line %d: %s\n", FILE, LINE, cudaGetErrorString(status));
exit(EXIT_FAILURE);
}
} while (0)
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::vectorcv::String& 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_;
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]);
}
}
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 <windows.h>
#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)
do {
cudaError_t status = call;
if (status != cudaSuccess) {
fprintf(stderr, "CUDA Error in file '%s' in line %d: %s\n",
FILE, LINE, cudaGetErrorString(status));
exit(EXIT_FAILURE);
}
} while (0)
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::vectorcv::String& 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;
CUDA_CHECK(cudaMalloc(&buffers_[inputIndex1_], inputSize1_));
CUDA_CHECK(cudaMalloc(&buffers_[inputIndex2_], inputSize2_));
CUDA_CHECK(cudaMalloc(&buffers_[outputIndex1_], outputSize1_));
CUDA_CHECK(cudaMalloc(&buffers_[outputIndex2_], outputSize2_));
CUDA_CHECK(cudaMalloc(&buffers_[outputIndex3_], outputSize3_));
std::cout << "Buffers allocated successfully." << std::endl;
}
void TensorRTInference::doInference(const std::vectorcv::String& 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::vectorcv::String 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;
}
能帮忙检查一下吗
The text was updated successfully, but these errors were encountered: