A wrapper around the traced model in caffe2’s pb format.
Returns: core.Net: the underlying caffe2 predict net
Returns: core.Net: the underlying caffe2 init net
Save the model as caffe2’s protobuf format.
output_dir (str) – the output directory to save protobuf files.
Save the graph as SVG format.
output_file (str) – a SVG file
inputs – optional inputs given to the model. If given, the inputs will be used to run the graph to record shape of every tensor. The shape information will be saved together with the graph.
An interface that wraps around a caffe2 model and mimics detectron2’s models’ input & output format. This is used to compare the outputs of caffe2 model with its original torch model.
Due to the extra conversion between torch/caffe2, this method is not meant for benchmark.
Caffe2Tracer(cfg, model, inputs)¶
Make a detectron2 model traceable with caffe2 style.
An original detectron2 model may not be traceable, or cannot be deployed directly after being traced, due to some reasons:
control flow in some ops
complicated pre/post processing
This class provides a traceable version of a detectron2 model by:
Rewrite parts of the model using ops in caffe2. Note that some ops do not have GPU implementation.
Define the inputs “after pre-processing” as inputs to the model
Remove post-processing and produce raw layer outputs
More specifically about inputs: all builtin models take two input tensors.
NCHW float “data” which is an image (usually in [0, 255])
Nx3 float “im_info”, each row of which is (height, width, 1.0)
After making a traceable model, the class provide methods to export such a model to different deployment formats.
The class currently only supports models using builtin meta architectures.
__init__(cfg, model, inputs)¶
model (nn.Module) – a model built by
detectron2.modeling.build_model(). Weights have to be already loaded to this model.
inputs – sample inputs that the given model takes for inference. Will be used to trace the model. Random input with no detected objects will not work if the model has data-dependent control flow (e.g., R-CNN).
Export the model to Caffe2’s protobuf format. The returned object can be saved with
.save_protobuf()method. The result can be loaded and executed using Caffe2 runtime.
Export the model to ONNX format. Note that the exported model contains custom ops only available in caffe2, therefore it cannot be directly executed by other runtime. Post-processing or transformation passes may be applied on the model to accommodate different runtimes, but we currently do not provide support for them.
onnx.ModelProto – an onnx model.
Export the model to a
torch.jit.TracedModuleby tracing. The returned object can be saved to a file by
torch.jit.TracedModule – a torch TracedModule