void caffeTotensorrtModel(const string& deployFile,const string& modelFile,
const vector<string>&outputs,unsigned int batchsize,
unsigned int workSpaceSize, IHostMemory *&gieModelStream,
int force_use_fp16)
{
// create API root class - must span the lifetime of the engine usage
IBuilder* builder = createInferBuilder(gLogger);
INetworkDefinition* network = builder->createNetwork();
// parse the caffe model to populate the network, then set the outputs
ICaffeParser* parser = createCaffeParser();
bool useFp16 = builder->platformHasFastFp16();
if (! force_use_fp16)
useFp16 = 0;
cout<<"useFp16: "<<useFp16<<std::endl;
nvinfer1::DataType modelDataType = useFp16 ? nvinfer1::DataType::kHALF : nvinfer1::DataType::kFLOAT;
cout<<" caffe model exchange start"<<endl;
cout<<deployFile.c_str()<<" "<<modelFile.c_str()<<" "<<endl;
const IBlobNameToTensor *blobNameToTensor =
parser->parse(deployFile.c_str(), modelFile.c_str(),*network,modelDataType);
cout<<"caffe parser done"<<endl;
assert(blobNameToTensor != nullptr);
// the caffe file has no notion of outputs, so we need to manually say
// which tensors the engine should generate
for (auto& s : outputs)
network->markOutput(*blobNameToTensor->find(s.c_str()));
// Build the engine
builder->setMaxBatchSize(batchsize);
builder->setMaxWorkspaceSize(workSpaceSize);
// Eliminate the side-effect from the delay of GPU frequency boost
builder->setMinFindIterations(3);
builder->setAverageFindIterations(2);
// set up the network for paired-fp16 format, only on DriveCX
if(useFp16)
builder->setHalf2Mode(true);
ICudaEngine* engine = builder->buildCudaEngine(*network);
assert(engine);
// we don't need the network any more, and we can destroy the parser
network->destroy();
parser->destroy();
// serialize the engine, then close everything down
engine->serialize();
engine->destroy();
builder->destroy();
shutdownProtobufLibrary();
}
Thank you for your reply. I have another question. When I change the type of custom network layer in the prototxt file to Iplugin, do I need to retrain caffemodel ?
Plugin layer is considered to be a replacement for a non-supported layer.
Although the type is changed, the functionality of network should be the same.
Thank you very much for your reply.I will learn in the follow-up work under IPlugin usage, since I am in the project of using the multiple TensorRT engine method implementation, prototxt file into three files, I want to ask next, is can’t use caffe parser to translate directly?Can you use the TensorRT API to build network layer implementation layer by layer?
Thank you very much for your reply.I implement custom layer with plugin API,but the test results are wrong. I used caffeParser to exchange model,and I want to ask, how do I locate which layer is the problem?