使用C++API部署推理(重点)
step1:创建runtime
step2:反序列化创建engine
step3:创建context
step4:获取输入输出索引
step5:创建buffers
step6:为输入输出开辟GPU显存
step7:创建cuda流
step8:从CPU到GPU----拷贝input数据
step9:异步推理
step10:从GPU到CPU----拷贝output数据
step10:同步cuda流
step11:释放资源
//1:创建.logger
class Logger:public ILogger
{
void log(Severity severity,const char* msg) override
{
if(severity != Severity::kINFO)
srd::cout<<msg<<std::endl;
}
}gLogger;
//反序列化
string engine_path="xxxxx.engine";
ifstream fin(engine_path);
string sensen_engine=""; //将engine数据写入string;
while(fin.peek()!=EOF)
{
stringstream buff;
buff << fin.rdbuf();
sensen_engine.append(buffer.str());
}
fin.close();
//创建runtime
IRuntime* runtime=createrInferRuntime(gLogger);
//根据写入string的数据创建engine
ICudaEngine* engine = runtime->deserializeCudaEngine(sensen_engine.data(),sensen_endgine.size(),nullptr);
//创建context
IExecutionContext* context = engine->createExecutionContext();
//图片处理
string img_path = "xxx.png";
Mat img = imread(img_path);
int h=img.rows;//获取图片的长
int w=img.cols;//获取图片的宽
cvtColor(img,img,COLOR_BGR2YCrCb);//将图片转换为YCrCb
voctor<Mat> over;
split(img,over);
over[0].convertTo(over[0],CV_32F,1/255.0) //将Y通道归一化
//创建buffers 指向输入输出流
void *buffers[2];
int inputIndex;
int outputIndex;
for(int bi = 0;bi < engine->getNbBindings();bi++)
{
if(engine->bindingIsInput(bi)==true)
inputIndex=bi;
else
outputIndex=bi;
}
// 分配buffers空间
cudaMalloc(&buffers[inputIndex],h*w*sizeof(float ));
cudaMalloc(&buffers[outputIndex],h*2*w*2*sizeof(float ));//超分后的大小
//创建cuda流
cudaStream_t stram;
cudaStreamCreate(&stream);
//复制图片数据到GPU
cudaMemcpyAsync(buffers[inputIndex],over[0],h*w*sizeof(float),cudaMemcpyHostToDevice,stream);
//执行推理
context->enqueue(1,buffers,stream,nullptr);
//将3个通道都转为超分后的大小
for(int i=0;i<3;i++)
resize(over[i],over[i],Size(w*2,h*2),0,0,INTER_CUBIC);
//将GPU数据拷贝回CPU
cudaMemcpy(over[0],buffers[outputIndex],h*2*w*2*sizeof(float),cudaMemcpyDeviceToHost,stream);
//反归一化,转换数据类型
over[0],convertTo(over[0],CV_8U,1*255);
//合并3通道,写入文件
Mat png;
merge(over,png);
//将YCrCb转回RGB(opencv中是BGR),写入文件;
cvtColor(png,png,COLOR_YCrCb2BGR);
imwrite("xxxx.png",png);
//释放资源
cudaStreamDestroy(stream);
context->destroy();
engine->destroy();
runtime->destroy();
cudaFree(buffers[inputIndex]);
cudaFree(buffers[outputIndex]);
参考:
(55条消息) Pytorch导出onnx模型,C++转化为TensorRT并实现推理过程_Adenialzz的博客-CSDN博客_c++ onnx转tensorrt
(11条消息) 【代码片段】ONNX 模型转成Tensorrt 及推理测试_乐亦亦乐的博客-CSDN博客_onnx转tensorrt
(55条消息) Pytorch导出onnx模型,C++转化为TensorRT并实现推理过程_Adenialzz的博客-CSDN博客_c++ onnx转tensorrt