TensorRT icon indicating copy to clipboard operation
TensorRT copied to clipboard

Example on how to serialize and deserialize networks with custom plugins

Open sotpapathe opened this issue 6 years ago • 9 comments

I want to serialize and deserialize the network from sampleUffMaskRCNN in order to avoid the large startup time impact (5-6 mins) of converting the Uff model to the native format. sampleUffMaskRCNN uses an IPluginV2Ext plugin, ResizeNearest, which need to be properly serialized and deserialized.

Serialization

If I understand correctly, for IPluginV2Ext plugins, it suffices to use nvinfer1::ICudaEngine::serialize() and save the resulting nvinfer1::IHostMemory object to a binary file. So something like

IHostMemory* serialized_model (mEngine->serialize());
std::ofstream f ("some_file.bin", std::ios::binary);
f.write(reinterpret_cast<char*>(serialized_model->data()), serialized_model->size());
serialized_model->destroy();

Please correct me if I'm wrong.

Deserialization

According to issue #102 , for IPluginV2Ext plugins, the nvinfer1::IPluginRegistry class should be used, but the linked documentation does not seem to contain a full example on how to do this. This is what I'm struggling with. Any help is appreciated.

Thanks!

sotpapathe avatar Apr 13 '20 15:04 sotpapathe

Hi,

The UFF parser is deprecated in favor of the ONNX parser as of TensorRT 7, and I believe ResizeNearest is supported by the ONNX parser so it should not require a plugin.

Would you mind trying the ONNX parser? Seems like there's an existing MaskRCNN ONNX model here you could play around with: https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/mask-rcnn

With TensorRT 7 installed, you could use the trtexec command-line tool like so to parse the model and build/serialize engine to a file:

trtexec --explicitBatch --onnx=model.onnx --saveEngine=model.engine

rmccorm4 avatar Apr 16 '20 08:04 rmccorm4

Thanks for the help! However, I get the same fatal error as in issue #386 when using trtexec to convert the model. I have attached the short and verbose logs.

log.txt log_verbose.txt

TensorRT Version: 7.0.0.11 (9b2d78a87e3d45eca22b6bfcb6e10777b932ef43) GPU Type: Quadro P1000 Nvidia Driver Version: 440.64.00 CUDA Version: 10.0 CUDNN Version: 7.6.5 Operating System + Version: Ubuntu 18.04

sotpapathe avatar Apr 21 '20 08:04 sotpapathe

Any suggestions @rmccorm4 ?

sotpapathe avatar May 06 '20 16:05 sotpapathe

@sotpapathe 

// serialize engine if (mEngine) { unique_ptr<IHostMemory, MaskRCNNConfig::Destroy> engine_plan{mEngine->serialize()}; writeBuffer(engine_plan->data(), engine_plan->size(), enginePath); }

I modified sampleUffMaskRCNN.cpp with these lines. serialize engine and save it as file successfully.

wangzhangup avatar Jun 12 '20 03:06 wangzhangup

Thanks for the suggestion @wangzhangup ! However this is more or less what I've been doing for serialization which I think I got working.

On a sidenote, MaskRCNNConfig::Destroy seems to not be defined anywhere and writeBuffer() does not look like a TensoRT function, maybe something you wrote?

sotpapathe avatar Jun 12 '20 10:06 sotpapathe

Hi,

You can serialize and deserialize using the following code, I have used it in the example "sampleUffMaskRCNN" and it is working

// Create and save engine
std::string precision = ((mParams.fp16==1) ? "fp16" : "fp32");
std::string name_engine = "mrcnn_nchw.uff." + precision + ".engine";
bool create_engine = true;
if (create_engine)
{
    // Create engine
    mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(builder->buildCudaEngine(*network), samplesCommon::InferDeleter());
    if (!mEngine)
    {
        return false;
    }

    // Save engine
    sample::gLogInfo << "Saving engine : " << name_engine << std::endl;
    nvinfer1::IHostMemory *serializedModel = mEngine->serialize();
    std::ofstream ofs(name_engine, std::ios::out | std::ios::binary);
    ofs.write((char*)(serializedModel ->data()), serializedModel ->size());
    ofs.close();
}
// load the engine
else
{
    // cout << "Loading TensorRT engine from plan file..." << endl;
    sample::gLogInfo << "Loading TensorRT engine from plan file " << name_engine << std::endl;

    std::ifstream planFile(name_engine);

    if (!planFile.is_open())
    {
        sample::gLogError << "Could not open plan file: " << name_engine << std::endl;
        return false;
    }

    std::stringstream planBuffer;
    planBuffer << planFile.rdbuf();
    std::string plan = planBuffer.str();
    IRuntime *runtime = createInferRuntime(gLogger);
    ICudaEngine *engine = runtime->deserializeCudaEngine((void*)plan.data(), plan.size(), nullptr);

    mEngine = std::shared_ptr<nvinfer1::ICudaEngine>(engine, samplesCommon::InferDeleter());
}

ivanhc avatar Sep 14 '20 13:09 ivanhc

@sotpapathe are you still having issues with serializing/deserializing? Or are you stuck on the resize issue you mentioned above?

kevinch-nv avatar Sep 30 '20 07:09 kevinch-nv

Closing since no response for 3 weeks, please reopen if you still hit the issue, thanks!

ttyio avatar Oct 26 '20 09:10 ttyio

Thanks @ivanhc , your code works fine on a Jetson Xavier NX. Sorry for the very late reply, I was working on other stuff.

sotpapathe avatar Apr 30 '21 15:04 sotpapathe