TensorRT笔记系列传送门(不定期更新): 深度框架|TensorRT
记录NvCaffeParser.h头文件的阅读笔记。
参考
TensorRT5.1.5.0入门 TensorRT头文件学习
顾名思义,这个头文件就是用来解析caffe的
这个头文件主要包扩如下几个类
namespace nvcaffeparser1 {
class IBlobNameToTensor
class IBinaryProtoBlob
class IPluginFactory
class IPluginFactoryExt : public IPluginFactory
class IPluginFactoryV2
class ICaffeParser
TENSORRTAPI ICaffeParser* createCaffeParser();
TENSORRTAPI void shutdownProtobufLibrary();
}
extern "C" TENSORRTAPI void* createNvCaffeParser_INTERNAL();
一、IBlobNameToTensor类
使用ICaffeParser提取caffe模型后,用来存储和查询tensor
class IBlobNameToTensor
{
public:
//! 内部只有一个find函数,根据name,返回对应的Tensor层
virtual nvinfer1::ITensor* find(const char* name) const TRTNOEXCEPT = 0;
protected:
virtual ~IBlobNameToTensor() {}
};
二、IBinaryProtoBlob类
这个类用于ICaffeParser对a binaryproto file 进行解析之后,存储和查询数据。和上个类完全不同。
主要有三个函数
class IBinaryProtoBlob
{
public:
virtual const void* getData() TRTNOEXCEPT = 0;
virtual nvinfer1::DimsNCHW getDimensions() TRTNOEXCEPT = 0;
virtual nvinfer1::DataType getDataType() TRTNOEXCEPT = 0;
virtual void destroy() TRTNOEXCEPT = 0;
protected:
virtual ~IBinaryProtoBlob() {}
};
三、IPluginFactory和IPluginFactoryExt 类
这两个类以及v2都只有两个函数
class IPluginFactory
{
public:
//! 用于确定是否提供了层配置
virtual bool isPlugin(const char* layerName) TRTNOEXCEPT = 0;
//! 用于创建plugin对象
virtual nvinfer1::IPlugin* createPlugin(const char* layerName, const nvinfer1::Weights* weights, int nbWeights) TRTNOEXCEPT = 0;
virtual ~IPluginFactory() {}
};
四、ICaffeParser类
此类是用于解析caffeModel的一个类
class ICaffeParser
{
public:
//! 需要prototxt文件和一个binaryproto格式的权重数据文件( .caffemodel)
virtual const IBlobNameToTensor* parse(const char* deploy,
const char* model,
nvinfer1::INetworkDefinition& network,
nvinfer1::DataType weightType) TRTNOEXCEPT = 0;
// 和parse功能相同,只不过是从内存缓冲区中按照长度读数据。
virtual const IBlobNameToTensor* parseBuffers(const char* deployBuffer,
std::size_t deployLength,
const char* modelBuffer,
std::size_t modelLength,
nvinfer1::INetworkDefinition& network,
nvinfer1::DataType weightType) TRTNOEXCEPT = 0;
//! 用来解析和分离存储在binaryproto文件中的数据(.caffemodel)
//一般来说,输入的.caffemodel文件是一个binaryproto格式的文件,他储存了binary blob中的数据,
//而这个parserBinaryProto()函数将这个数据转化(存储)成了IBinaryProtoBlob对象,
//在这个对象的基类里面写了读数据的接口,所以这个函数就是数据转成对象的作用。
virtual IBinaryProtoBlob* parseBinaryProto(const char* fileName) TRTNOEXCEPT = 0;
//! \note Default size is 2^30 bytes.
//!用来设置缓冲区大小以解析和存储学习模型。
virtual void setProtobufBufferSize(size_t size) TRTNOEXCEPT = 0;
//! 用来设置创建user定义的plugin(自定义层)当使用PluginFactory的时候,要提前设置。。
virtual void setPluginFactory(IPluginFactory* factory) TRTNOEXCEPT = 0;
//!用来设置创建user定义的plugin(自定义层)当使用PluginFactory的时候,要提前设置。
virtual void setPluginFactoryExt(IPluginFactoryExt* factory) TRTNOEXCEPT = 0;
virtual void destroy() TRTNOEXCEPT = 0;
//!
//! \brief Set the IPluginFactoryV2 used to create the user defined pluginV2 objects.
//!
//! \param factory Pointer to an instance of the user implmentation of IPluginFactoryV2.
//!
virtual void setPluginFactoryV2(IPluginFactoryV2* factory) TRTNOEXCEPT = 0;
//!
//! \brief Set the namespace used to lookup and create plugins in the network.
//!来设置用于查找的命名空间,并在命名空间中创建plugin。
//(个人的理解)可以创建很多plugin库,写好,链接好之后编译就能使用。
//不同库有不同的plugin定义(参数定义)方式,所以每个库都可以存在不同的namespace中,易于管理,
//所以这个函数应该是选择自己要用的自定义plugin的namespace。
virtual void setPluginNamespace(const char* libNamespace) TRTNOEXCEPT = 0;
protected:
virtual ~ICaffeParser() {}
public:
//!
//! \brief Set the ErrorRecorder for this interface
//!
//! Assigns the ErrorRecorder to this interface. The ErrorRecorder will track all errors during execution.
//! This function will call incRefCount of the registered ErrorRecorder at least once. Setting
//! recorder to nullptr unregisters the recorder with the interface, resulting in a call to decRefCount if
//! a recorder has been registered.
//!
//! \param recorder The error recorder to register with this interface.
//!
//! \see getErrorRecorder
//!
virtual void setErrorRecorder(nvinfer1::IErrorRecorder* recorder) TRTNOEXCEPT = 0;
//!
//! \brief get the ErrorRecorder assigned to this interface.
//!
//! Retrieves the assigned error recorder object for the given class. A default error recorder does not exist,
//! so a nullptr will be returned if setErrorRecorder has not been called.
//!
//! \return A pointer to the IErrorRecorder object that has been registered.
//!
//! \see setErrorRecorder
//!
virtual nvinfer1::IErrorRecorder* getErrorRecorder() const TRTNOEXCEPT = 0;
};
parse()用来解析caffe模型(prototxt + .caffemodel),返回一个指向IBlobNameToTensor对象的指针。
输入是:
-deploy:保存网络参数的prototxt
-model:保存权重的文件
-network:内置数据结构,可以对其进行layer的填充
-weightType:权重的type
输出是:
-IBlobNameToTensor* :是一个指针。
parseBuffers()用来从memory buffers读取caffe模型(prototxt + .caffemodel),‘
返回一个指向IBlobNameToTensor对象的指针。
和parse功能相同,只不过是从内存缓冲区中按照长度读数据。
parserBinnaryProto()用来解析和分离存储在binaryproto文件中的数据(.caffemodel)
一般来说,输入的.caffemodel文件是一个binaryproto格式的文件,他储存了binary blob中的数据,而这个parserBinaryProto()函数将这个数据转化(存储)成了IBinaryProtoBlob对象,
在这个对象的基类里面写了读数据的接口,所以这个函数就是数据转成对象的作用。
输出是:
-IBinaryProtoBlob*:指针。
serProtobufBufferSize()用来设置缓冲区大小以解析和存储学习模型。
setPluginFactory()用来设置创建user定义的plugin(自定义层)
当使用PluginFactory的时候,要提前设置。
setPluginFactoryExt()用来设置创建user定义的pluginExt(自定义层)
当使用PluginFactoryExt的时候,要提前设置。
serPluginFactoryV2()用来设置创建user定义的pluginV2(自定义层)
当使用PluginFactoryV2的时候,要提前设置。
setPluginNamespace()用来设置用于查找的命名空间,并在命名空间中创建plugin。
(个人的理解)可以创建很多plugin库,写好,链接好之后编译就能使用。不同库有不同的plugin定义(参数定义)方式,所以每个库都可以存在不同的namespace中,易于管理,所以这个函数应该是选择自己要用的自定义plugin的namespace。
六、其他
//!
//! \brief Creates a ICaffeParser object.
//!
//! \return A pointer to the ICaffeParser object is returned.
//!
//! \see nvcaffeparser1::ICaffeParser
//!
TENSORRTAPI ICaffeParser* createCaffeParser() TRTNOEXCEPT;
//!
//! \brief Shuts down protocol buffers library.
//!
//! \note No part of the protocol buffers library can be used after this function is called.
//!
TENSORRTAPI void shutdownProtobufLibrary() TRTNOEXCEPT;
} // namespace nvcaffeparser1
//!
//! Internal C entry point for creating ICaffeParser.
//! @private
//!
extern "C" TENSORRTAPI void* createNvCaffeParser_INTERNAL();