java调用onnx模型_使用TensorRT对caffe和pytorch onnx模型进行fp32和fp16推理

本文介绍了如何使用TensorRT将Caffe和PyTorch的ONNX模型转换为TRT模型,并进行FP32和FP16推理。示例代码展示了从PGM文件读取MNIST数据,加载模型并进行推理的过程。
摘要由CSDN通过智能技术生成

tensorrt fp32 fp16 tutorial with caffe pytorch minist model

Series

Code Example

include headers

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include "NvCaffeParser.h"

#include "NvOnnxConfig.h"

#include "NvOnnxParser.h"

#include "NvInfer.h"

#include "common.h"

using namespace nvinfer1;

using namespace nvcaffeparser1;

static Logger gLogger;

// Attributes of MNIST Caffe model

static const int INPUT_H = 28;

static const int INPUT_W = 28;

static const int OUTPUT_SIZE = 10;

//const char* INPUT_BLOB_NAME = "data";

const char* OUTPUT_BLOB_NAME = "prob";

const std::string mnist_data_dir = "data/mnist/";

// Simple PGM (portable greyscale map) reader

void readPGMFile(const std::string& fileName, uint8_t buffer[INPUT_H * INPUT_W])

{

readPGMFile(fileName, buffer, INPUT_H, INPUT_W);

}

caffe model to tensorrt

void caffeToTRTModel(const std::string& deployFilepath, // Path of Caffe prototxt file

const std::string& modelFilepath, // Path of Caffe model file

const std::vector<:string>& outputs, // Names of network outputs

unsigned int maxBatchSize, // Note: Must be at least as large as the batch we want to run with

IHostMemory*& trtModelStream) // Output buffer for the TRT model

{

// Create builder

IBuilder* builder = createInferBuilder(gLogger);

// Parse caffe model to populate network, then set the outputs

std::cout << "Reading Caffe prototxt: " << deployFilepath << "\n";

std::cout << "Reading Caffe model: " << modelFilepath << "\n";

INetworkDefinition* network = builder->createNetwork();

ICaffeParser* parser = createCaffeParser();

bool useFp16 = builder->platformHasFastFp16();

std::cout << "platformHasFastFp16: " << useFp16 << "\n";

bool useInt8 = builder->platformHasFastInt8();

std::cout << "platformHasFastInt8: " << useInt8 << "\n";

// create a 16-bit model if it's natively supported

DataType modelDataType = useFp16 ? DataType::kHALF : DataType::kFLOAT;

const IBlobNameToTensor* blobNameToTensor = parser->parse(deployFilepath.c_str(),

modelFilepath.c_str(),

*network,

modelDataType);

// Specify output tensors of network

// ERROR: Network must have at least one output

for (auto& s : outputs){

std::cout

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值