1.模型推理:
1.1创建IExecutionContext
IExecutionContext *context = engine->createExecutionContext();
获取输入输出tensor的索引:
int inputIndex = engine->getBindingIndex(INPUT_BLOB_NAME);
int outputIndex = engine->getBindingIndex(OUTPUT_BLOB_NAME);
1.2准备buffer:
void* buffers[2];
buffers[inputIndex] = inputBuffer;
buffers[outputIndex] = outputBuffer;
1.3推理:
同步接口:execute()/executeV2()
异步接口:enqueue()/enqueueV2()
创建IExecutionContext过程中的内存管理:
当调用类似CreateExcutionContext时,会在显卡上开辟空间,并接收数据。避免内存开辟可以使用
createExecutionContextWithoutDeviceMemory,当需要时。调用 IExecutionContext::setDeviceMemory()
申请内存。获取内存大小:ICudaEngine::getDeviceMemorySize()
2.直接修改engine中的权重
步骤:
※※自定义层:
自定义层要继承:IPluginV2Ext/IPluginV2IOExt/IPluginV2DynamicExt 之一
三个基类的异同:
创建自定义层需要写:
1.继承IPluginV2Ext/IPluginV2IOExt/IPluginV2DynamicExt 之一 的 CustomPlugin
2.继承IPluginCreator 实现 CustomPluginCreator 并调用REGISTER_TENSORRT_PLUGIN(CustomPluginCreator ) 注册
3.如果是转换caffe框架的模型,需要继承IPluginV2 实现 PluginFactory
caffe 自定义层的例子:
tips:
1 定义 plugin 注意eneueue 是具体算法实现,
2 定义 PluginFactory 注意 isPlugin isPluginExt
3 调用:parser->setPluginFactoryExt(&parserPluginFactory);
UFF 模型的自定义层的例子:
1.创建自定义plugin
2.使用 graphsuregeon 将需要由自定义插件替换的层进行map
定义支持可变形状的自定义层:
继承IPluginV2DynamicExt
要点:
getOutputDimensions 根据输入尺寸求得输出尺寸:
创建支持int8 I/o的自定义层
1.plugin继承 IPluginVI2IOExt 或者 IPluginV2DynamicExt
2.创建factory/creator 同时registry
基于UFF的例子:
使用python添加一个uff不支持的自定义层
使用c++实现:IPluginExt and IPluginCreator
步骤:
IPluginCreator API: