Implementation:Bentoml BentoML Framework Detectron
| Knowledge Sources | |
|---|---|
| Domains | ML Framework, Object Detection, Model Serialization |
| Last Updated | 2026-02-13 15:00 GMT |
Overview
The bentoml.detectron module provides BentoML integration for Facebook's Detectron2 object detection framework, enabling save, load, and serving of Detectron2 models and predictors.
Description
This module implements the standard BentoML framework adapter pattern for Detectron2 models. It supports two types of checkpointables:
- DefaultPredictor: A Detectron2 predictor that wraps a model with its configuration.
- torch.nn.Module: A raw Detectron2 model (e.g., built via
Modeling.build_model()).
The module saves models as .pth checkpoint files using Detectron2's Checkpointer and stores the CfgNode configuration as a custom object. On load, it reconstructs either a DefaultPredictor or a raw model depending on the _is_predictor metadata flag.
The get_runnable() factory creates a Detectron2Runnable class that supports GPU/CPU execution, automatic tensor conversion from NumPy arrays, and PyTorch inference mode.
Usage
Use this module to save and load Detectron2 object detection, instance segmentation, or panoptic segmentation models within the BentoML model store for serving via BentoML services.
Code Reference
Source Location
- Repository: Bentoml_BentoML
- File: src/bentoml/_internal/frameworks/detectron.py
- Lines: 1-333
Signature
def get(tag_like: str | Tag) -> Model: ...
def load_model(bento_model: str | Tag | Model,
device_id: str = "cpu") -> Engine.DefaultPredictor | nn.Module: ...
def save_model(name: Tag | str,
checkpointables: Engine.DefaultPredictor | nn.Module,
config: Config.CfgNode | None = None,
*, signatures: ModelSignaturesType | None = None,
labels: dict[str, str] | None = None,
custom_objects: dict[str, Any] | None = None,
external_modules: List[ModuleType] | None = None,
metadata: dict[str, Any] | None = None) -> bentoml.Model: ...
def get_runnable(bento_model: bentoml.Model) -> type[bentoml.legacy.Runnable]: ...
Import
import bentoml
# Via public API
model = bentoml.detectron.save_model(...)
loaded = bentoml.detectron.load_model(...)
I/O Contract
Inputs
save_model()
| Name | Type | Required | Description |
|---|---|---|---|
| name | Tag or str | Yes | Name/tag for the model in the BentoML store |
| checkpointables | DefaultPredictor or nn.Module | Yes | The Detectron2 predictor or model to save |
| config | CfgNode or None | No | Required when checkpointables is nn.Module; ignored for DefaultPredictor |
| signatures | ModelSignaturesType or None | No | Inference method signatures (default: {"__call__": {"batchable": False}}) |
| labels | dict[str, str] or None | No | User-defined labels for model management |
| custom_objects | dict[str, Any] or None | No | Additional objects to serialize with the model |
| external_modules | List[ModuleType] or None | No | Additional Python modules to save alongside |
| metadata | dict[str, Any] or None | No | Custom metadata for the model |
load_model()
| Name | Type | Required | Description |
|---|---|---|---|
| bento_model | str, Tag, or Model | Yes | Tag or Model instance to load from the store |
| device_id | str | No | Device to load the model on (default: "cpu") |
Outputs
| Method | Return Type | Description |
|---|---|---|
| save_model() | bentoml.Model | A BentoML Model with the saved Detectron2 checkpoint |
| load_model() | DefaultPredictor or nn.Module | The loaded Detectron2 predictor or model |
| get() | Model | The BentoML Model reference from the store |
| get_runnable() | type[Runnable] | A Detectron2Runnable class for BentoML Runner serving |
Usage Examples
import bentoml
from detectron2 import model_zoo
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
# Configure model
model_url = "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file(model_url))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(model_url)
cloned = cfg.clone()
cloned.MODEL.DEVICE = "cpu"
# Save predictor
predictor = DefaultPredictor(cloned)
bento_model = bentoml.detectron.save_model("mask_rcnn", predictor)
# Load model back
loaded_predictor = bentoml.detectron.load_model("mask_rcnn:latest")
# Get model reference
model_ref = bentoml.detectron.get("mask_rcnn:latest")