Get a model specified by relative path under Detectron2's official ``configs/`` directory. Args: config_path (str): config file name relative to detectron2's "configs/" directory, e.g., "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml" trained (bool):
(config_path, trained: bool = False, device: Optional[str] = None)
| 178 | |
| 179 | |
| 180 | def get(config_path, trained: bool = False, device: Optional[str] = None): |
| 181 | """ |
| 182 | Get a model specified by relative path under Detectron2's official ``configs/`` directory. |
| 183 | |
| 184 | Args: |
| 185 | config_path (str): config file name relative to detectron2's "configs/" |
| 186 | directory, e.g., "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml" |
| 187 | trained (bool): see :func:`get_config`. |
| 188 | device (str or None): overwrite the device in config, if given. |
| 189 | |
| 190 | Returns: |
| 191 | nn.Module: a detectron2 model. Will be in training mode. |
| 192 | |
| 193 | Example: |
| 194 | :: |
| 195 | from annotator.oneformer.detectron2 import model_zoo |
| 196 | model = model_zoo.get("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml", trained=True) |
| 197 | """ |
| 198 | cfg = get_config(config_path, trained) |
| 199 | if device is None and not torch.cuda.is_available(): |
| 200 | device = "cpu" |
| 201 | if device is not None and isinstance(cfg, CfgNode): |
| 202 | cfg.MODEL.DEVICE = device |
| 203 | |
| 204 | if isinstance(cfg, CfgNode): |
| 205 | model = build_model(cfg) |
| 206 | DetectionCheckpointer(model).load(cfg.MODEL.WEIGHTS) |
| 207 | else: |
| 208 | model = instantiate(cfg.model) |
| 209 | if device is not None: |
| 210 | model = model.to(device) |
| 211 | if "train" in cfg and "init_checkpoint" in cfg.train: |
| 212 | DetectionCheckpointer(model).load(cfg.train.init_checkpoint) |
| 213 | return model |
nothing calls this directly
no test coverage detected