将本机电脑上TensorRT6升级到TensorRT8后,原来的模型报以下错误
ERROR: ModelImporter.cpp:574 In function importModel:
[4] Assertion failed: !_importer_ctx.network()->hasImplicitBatchDimension() && "This version of the ONNX parser only supports TensorRT INetworkDefinitions with an explicit batch dimension. Please ensure the network was created using the EXPLICIT_BATCH NetworkDefinitionCreationFlag.".
I1012 18:02:20.039438 13296 model_loader.cpp:39] Assertion failed: !_importer_ctx.network()->hasImplicitBatchDimension() && "This version of the ONNX parser only supports TensorRT INetworkDefinitions with an explicit batch dimension. Please ensure the network was created using the EXPLICIT_BATCH NetworkDefinitionCreationFlag."
问题出在这行代码上
parser->parseFromFile(model_path_.c_str(), static_cast<int>(ILogger::Severity::kWARNING));
错误描述大概意思是这个版本的onnx解析器指支持特定batch的网络。
解决方法
其实这个错误是创建网络时Flag没设置正确,这里将0改成1就可以了。
nvinfer1::INetworkDefinition* network = builder->createNetworkV2(0);
原因如下
enum class NetworkDefinitionCreationFlag : int32_t
{
//! Dynamic shape support requires that the kEXPLICIT_BATCH flag is set.
//! With dynamic shapes, any of the input dimensions can vary at run-time,
//! and there are no implicit dimensions in the network specification. This is specified by using the
//! wildcard dimension value -1.
kEXPLICIT_BATCH = 0, //!< Mark the network to be an explicit batch network
//! Setting the network to be an explicit precision network has the following implications:
//! 1) Precision of all input tensors to the network have to be specified with ITensor::setType() function
//! 2) Precision of all layer output tensors in the network have to be specified using ILayer::setOutputType()
//! function
//! 3) The builder will not quantize the weights of any layer including those running in lower precision(INT8). It
//! will
//! simply cast the weights into the required precision.
//! 4) Dynamic ranges must not be provided to run the network in int8 mode. Dynamic ranges of each tensor in the
//! explicit
//! precision network is [-127,127].
//! 5) Quantizing and dequantizing activation values between higher (FP32) and lower (INT8) precision
//! will be performed using explicit Scale layers with input/output precision set appropriately.
kEXPLICIT_PRECISION TRT_DEPRECATED_ENUM = 1, //! <-- Deprecated, used for backward compatibility
};