1,common.cpp 使用tensorrt API 搭建yolov5模块单元
- (1)头文件
- #include "NvInfer.h" //tensorrt API的头文件
- #include "yololayer.h" //tensorrt 插件头文件
- #include <opencv2/opencv.hpp> //opencv 头文件
-
- (2)命名空间
- using namespace nvinfer1;
-
- (3)网络layer实现及其他函数实现
- //yolov5 需要实现的网络layer包括:convBlock, focus,bottleneck, bottleneckCSP, C3,SPP,SPPF,yololayer的插件layer
-
-
-
- 3.1 convBlock
-
- ILayer* convBlock(INetworkDefinition *network, std::map<std::string, Weights>& weightMap, ITensor& input, int outch, int ksize, int s, int g, std::string lname) {
- Weights emptywts{ DataType::kFLOAT, nullptr, 0 };
- int p = ksize / 3;
- IConvolutionLayer* conv1 = network->addConvolutionNd(input, outch, DimsHW{ ksize, ksize }, weightMap[lname + ".conv.weight"], emptywts);
- assert(conv1);
- conv1->setStrideNd(DimsHW{ s, s });
- conv1->setPaddingNd(DimsHW{ p, p });
- conv1->setNbGroups(g);
- IScaleLayer* bn1 = addBatchNorm2d(network, weightMap, *conv1->getOutput(0), lname + ".bn", 1e-3);
-
- // silu = x * sigmoid
- auto sig = network->addActivation(*bn1->getOutput(0), ActivationType::kSIGMOID);
- assert(sig);
- auto ew = network->addElementWise(*bn1->getOutput(0), *sig->getOutput(0), ElementWiseOperation::kPROD);
- assert(ew);
- return ew;
- }
2, yolov5.cpp
-
-
-
- // 1,创建一个网络生成器
- IBuilder* builder = createInferBuilder(gLogger);
-
- // 2,生成一个空的网络
- INetworkDefinition* network = builder->createNetwork();
-
- // 3,添加输入
- ITensor* data = network->addInput(INPUT_BLOB_NAME, dt, Dims3{ 3, INPUT_H, INPUT_W });
-
- // 4, 添加网络层 common.h
1,分配显卡
cudaSetDevice(DEVICE);
2, 加载engine模型
- #创建推理进行时runtime
- IRuntime* runtime = createInferRuntime(gLogger);
- assert(runtime != nullptr);
-
- #创建引擎,反序列化二值引擎文件
- ICudaEngine* engine = runtime->deserializeCudaEngine(trtModelStream, size);
- assert(engine != nullptr);
-
- #创建执行上下文contex,用于推理
- IExecutionContext* context = engine->createExecutionContext();