参考:使用TensorRT7.0.0.11工具trtexec onnx模型转engine_kangkjz的博客-CSDN博客_onnx转engine
打开./samples/trtexec/trtexec.sln,编译之后可以看到
执行命令行:
trtexec.exe --onnx=C:\Project\TensorRT-8.0.3.4\data\mnist\mnist.onnx --saveEngine=C:\Project\TensorRT-8.0.3.4\data\mnist\mnist.engine
?需要花一点点时间:
在SampleOnnxMNIST这个实例中,对代码简单修改,把build函数中主动去读取mnist.engine文件,实例化ICudaEngine。
bool SampleOnnxMNIST::build()
{
#if 0
auto builder = SampleUniquePtr<nvinfer1::IBuilder>(nvinfer1::createInferBuilder(sample::gLogger.getTRTLogger()));
if (!builder)
{
return false;
}
const auto explicitBatch = 1U << static_cast<uint32_t>(NetworkDefinitionCreationFlag::kEXPLICIT_BATCH);
auto network = SampleUniquePtr<nvinfer1::INetworkDefinition>(builder->createNetworkV2(explicitBatch));
if (!network)
{
return false;
}
auto config = SampleUniquePtr<nvinfer1::IBuilderConfig>(builder->createBuilderConfig());
if (!config)
{
return false;
}
auto parser
= SampleUniquePtr<nvonnxparser::IParser>(nvonnxparser::createParser(*network, sample::gLogger.getTRTLogger()));
if (!parser)
{
return false;
}
auto constructed = constructNetwork(builder, network, config, parser);
if (!constructed)
{
return false;
}
// CUDA stream used for profiling by the builder.
auto profileStream = samplesCommon::makeCudaStream();
if (!profileStream)
{
return false;
}
config->setProfileStream(*profileStream);
SampleUniquePtr<IHostMemory> plan{builder->buildSerializedNetwork(*network, *config)};
if (!plan)
{
return false;
}
SampleUniquePtr<IRuntime> runtime{createInferRuntime(sample::gLogger.getTRTLogger())};
if (!runtime)
{
return false;
}
mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
runtime->deserializeCudaEngine(plan->data(), plan->size()), samplesCommon::InferDeleter());
if (!mEngine)
{
return false;
}
ASSERT(network->getNbInputs() == 1);
mInputDims = network->getInput(0)->getDimensions();
ASSERT(mInputDims.nbDims == 4);
std::cout << "[jhq] mInputDims: " << mInputDims.d[0] << " , " << mInputDims.d[1] << " , " << mInputDims.d[2] << " , " << mInputDims.d[3] << std::endl;
ASSERT(network->getNbOutputs() == 1);
mOutputDims = network->getOutput(0)->getDimensions();
ASSERT(mOutputDims.nbDims == 2);
std::cout << "[jhq] mOutputDims: " << mInputDims.d[0] << " , " << mInputDims.d[1] << std::endl;
return true;
#else
mInputDims.d[0] = 1;
mInputDims.d[1] = 1;
mInputDims.d[2] = 28;
mInputDims.d[3] = 28;
mOutputDims.d[0] = 1;
mOutputDims.d[1] = 1;
std::string engine_name = "./data/mnist.engine";
std::ifstream file(engine_name, std::ios::binary);
if (!file.good()) {
std::cerr << "read " << engine_name << " error!" << std::endl;
return -1;
}
char *trtModelStream = nullptr;
size_t size = 0;
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream = new char[size];
assert(trtModelStream);
file.read(trtModelStream, size);
file.close();
SampleUniquePtr<IRuntime> runtime{createInferRuntime(sample::gLogger.getTRTLogger())};
if (!runtime)
{
return false;
}
mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(
runtime->deserializeCudaEngine(trtModelStream, size), samplesCommon::InferDeleter());
if (!mEngine)
{
return false;
}
return true;
#endif
}
结果输出:
总体感觉engine模型是要比onnx模型要快一些,输出的log信息也相对少一些。
后续的话,Tensorflow的pb模型或者pytorch的onnx模型,都可以以某种方式(TensorRT模型装换 - 小小马进阶笔记 - 博客园)转换为TensorRT的engine模型,转换的过程应该是有对网络层进行优化,所以速度才提升了。但是也要留意精度的损失程度。
同时,在使用trtexec这个工具的时候,也可以考虑设置一些参数,比如int8,对模型进行量化
利用TensorRT实现INT8量化感知训练QAT_ZONGXP的博客-CSDN博客_int8量化训练
参考命令行如下:
trtexec.exe --onnx=C:\Project\TensorRT-8.0.3.4\data\mnist\mnist.onnx --saveEngine=C:\Project\TensorRT-8.0.3.4\data\mnist\mnist.engine --int8
|