Huggingface Model¶
sahi.models.huggingface
¶
HuggingFace Transformers detection model wrapper for SAHI.
Provides integration with Hugging Face Transformers library for object detection and instance segmentation models like DETR variants.
Classes¶
HuggingfaceDetectionModel
¶
HuggingfaceDetectionModel(
model_path: str | None = None,
model: object | None = None,
processor: 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,
token: str | None = None,
text_prompt: str | None = None,
text_labels: list[str] | None = None,
text_threshold: float = 0.25,
)
Bases: DetectionModel
HuggingFace Transformers object detection model.
Supports DETR-style object detection models and GroundingDINO-style zero-shot detection models.
Initialize HuggingFace detection model.
Source code in sahi/models/huggingface.py
Attributes¶
Methods:¶
load_model
¶
Load model from HuggingFace.
Source code in sahi/models/huggingface.py
set_model
¶
Set the detection model and processor.
Source code in sahi/models/huggingface.py
perform_inference
¶
perform_inference(image: list | ndarray) -> None
Prediction is performed using self.model and the prediction result is set to self._original_predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
¶ |
list | ndarray
|
np.ndarray A numpy array that contains the image to be predicted. 3 channel image should be in RGB order. |
required |
Source code in sahi/models/huggingface.py
perform_batch_inference
¶
perform_batch_inference(images: list[ndarray]) -> None
Native batch inference: process all images in a single processor + model call.
Unlike the base-class default (which runs images sequentially), this feeds the entire list to the HuggingFace processor at once and executes one batched forward pass. The processor pads images to a uniform size internally, so images of different resolutions are handled correctly.
This avoids setting _batch_images so
convert_original_predictions uses the standard multi-image path
rather than the sequential fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
¶ |
list[ndarray]
|
List of numpy arrays (H, W, C) in RGB order. |
required |
Source code in sahi/models/huggingface.py
get_valid_predictions
¶
get_valid_predictions(
logits: Any, pred_boxes: Any
) -> tuple
Get predictions above confidence threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logits
¶ |
Any
|
torch.Tensor |
required |
pred_boxes
¶ |
Any
|
torch.Tensor |
required |
Returns:
| Name | Type | Description |
|---|---|---|
scores |
tuple
|
torch.Tensor |
cat_ids |
tuple
|
torch.Tensor |
boxes |
tuple
|
torch.Tensor |