Detectron¶
关于此页
这是 BentoML 中 Detectron 的 API 参考。有关如何在 BentoML 中使用 Detectron 的更多信息,请参阅Detectron 指南。
- bentoml.detectron.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, t.Any] | None = None, external_modules: t.List[ModuleType] | None = None, metadata: dict[str, t.Any] | None = None) bentoml.Model ¶
将模型实例保存到 BentoML 模型库。
- 参数:
name – 给定模型实例的名称。此名称应符合 Python 标识符的要求。
checkpointables – 要保存的模型实例。可以是
detectron2.engine.DefaultPredictor
或torch.nn.Module
。config – 可选的模型
CfgNode
。当 checkpointables 是torch.nn.Module
时必需。signatures – 用于在目标模型上运行推理时暴露的方法。Signatures 用于在使用
Service
提供模型服务时创建Runner
实例。labels – 用户定义的标签,用于管理模型,例如
team=nlp
,stage=dev
。custom_objects – 与模型一起保存的自定义对象。例如
{"my-normalizer": normalizer}
。自定义对象目前使用 cloudpickle 序列化,但此实现可能会发生变化。external_modules – 用户定义的额外 Python 模块,与模型或自定义对象一起保存,例如 tokenizer 模块、预处理器模块、模型配置模块。
metadata – 给定模型的自定义元数据。
- 返回值:
一个
tag
,格式为name:version
,其中name
是用户定义的模型名称,version
是生成的版本。- 返回类型:
Tag
示例
import bentoml import detectron2 from detectron2 import model_zoo from detectron2.config import get_cfg from detectron2.modeling import build_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)) # set threshold for this model 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" bento_model = bentoml.detectron2.save_model('mask_rcnn', build_model(cloned), config=cloned)
import bentoml import detectron2 from detectron2.engine import DefaultPredictor from detectron2 import model_zoo from detectron2.config import get_cfg from detectron2.modeling import build_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)) # set threshold for this model 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" predictor = DefaultPredictor(cloned) bento_model = bentoml.detectron2.save_model('mask_rcnn', predictor)
- bentoml.detectron.load_model(bento_model: str | Tag | Model, device_id: str = 'cpu') Engine.DefaultPredictor | nn.Module ¶
从 BentoML 本地模型库加载指定名称的 detectron2 模型。
- 参数:
bento_model – 要从模型库获取的模型标签,或用于加载模型的 BentoML
Model
实例。device_id – 加载模型的设备。默认为 “cpu”。
- 返回值:
如果 checkpointables 保存为 Predictor,则返回
detectron2.engine.DefaultPredictor
。如果 checkpointables 保存为 nn.Module,则返回
torch.nn.Module
。
- 返回类型:
以下之一
示例
import bentoml predictor = bentoml.detectron2.load_model('predictor:latest') model = bentoml.detectron2.load_model('model:latest')