Load a DetectionModel from given path. Args: model_type: str Name of the detection framework (example: "ultralytics", "huggingface", "torchvision") model_path: str Path of the detection model (ex. 'model.pt') model: Any
(
model_type: str,
model_path: str | None = None,
model: object | None = None,
config_path: str | None = None,
device: str | None = None,
mask_threshold: float = 0.5,
confidence_threshold: float = 0.3,
category_mapping: dict | None = None,
category_remapping: dict | None = None,
load_at_init: bool = True,
image_size: int | None = None,
**kwargs: object,
)
| 28 | |
| 29 | @staticmethod |
| 30 | def from_pretrained( |
| 31 | model_type: str, |
| 32 | model_path: str | None = None, |
| 33 | model: object | None = None, |
| 34 | config_path: str | None = None, |
| 35 | device: str | None = None, |
| 36 | mask_threshold: float = 0.5, |
| 37 | confidence_threshold: float = 0.3, |
| 38 | category_mapping: dict | None = None, |
| 39 | category_remapping: dict | None = None, |
| 40 | load_at_init: bool = True, |
| 41 | image_size: int | None = None, |
| 42 | **kwargs: object, |
| 43 | ) -> DetectionModel: |
| 44 | """Load a DetectionModel from given path. |
| 45 | |
| 46 | Args: |
| 47 | model_type: str |
| 48 | Name of the detection framework (example: "ultralytics", "huggingface", "torchvision") |
| 49 | model_path: str |
| 50 | Path of the detection model (ex. 'model.pt') |
| 51 | model: Any |
| 52 | A pre-initialized model instance, if available |
| 53 | config_path: str |
| 54 | Path of the config file (ex. 'mmdet/configs/cascade_rcnn_r50_fpn_1x.py') |
| 55 | device: str |
| 56 | Device, "cpu" or "cuda:0" |
| 57 | mask_threshold: float |
| 58 | Value to threshold mask pixels, should be between 0 and 1 |
| 59 | confidence_threshold: float |
| 60 | All predictions with score < confidence_threshold will be discarded |
| 61 | category_mapping: dict: str to str |
| 62 | Mapping from category id (str) to category name (str) e.g. {"1": "pedestrian"} |
| 63 | category_remapping: dict: str to int |
| 64 | Remap category ids based on category names, after performing inference e.g. {"car": 3} |
| 65 | load_at_init: bool |
| 66 | If True, automatically loads the model at initialization |
| 67 | image_size: int |
| 68 | Inference input size. |
| 69 | **kwargs: object |
| 70 | Additional keyword arguments to pass to the model. |
| 71 | |
| 72 | Returns: |
| 73 | Returns an instance of a DetectionModel |
| 74 | |
| 75 | Raises: |
| 76 | ImportError: If given {model_type} framework is not installed |
| 77 | """ |
| 78 | if model_type in ULTRALYTICS_MODEL_NAMES: |
| 79 | model_type = "ultralytics" |
| 80 | model_class_name = MODEL_TYPE_TO_MODEL_CLASS_NAME[model_type] |
| 81 | DetectionModel = import_model_class(model_type, model_class_name) |
| 82 | |
| 83 | return DetectionModel( |
| 84 | model_path=model_path, |
| 85 | model=model, |
| 86 | config_path=config_path, |
| 87 | device=device, |