altametris.sara.core.base_detector¶
Base detector class for inference.
Provides a common interface for running inference on ML models with support for: - Model loading (local and Azure remote paths) - Batch and single image prediction - Device management - Warmup for performance optimization - Azure weight resolution via WeightResolver
Example
>>> class MyDetector(BaseDetector):
... def _load_model(self, model_path):
... self.model = torch.load(model_path)
... def predict(self, source, **kwargs):
... return self.model(source)
>>> # With Azure support
>>> detector = MyDetector(model_path="azure://models/best.pt", enable_azure=True)
>>> # Via APIM with OAuth2
>>> detector = MyDetector(model_path="https://apim.azure.com/weights/best.pt")
Attributes¶
Classes¶
Abstract base class for model inference/detection. |
Module Contents¶
- altametris.sara.core.base_detector.logger¶
- altametris.sara.core.base_detector.HAS_WEIGHT_RESOLVER = True¶
- class altametris.sara.core.base_detector.BaseDetector(model_path: str | pathlib.Path, device: str = 'auto', warmup: bool = False, enable_azure: bool = True, credentials: Any | None = None, weight_resolver: Any | None = None, **kwargs: Any)¶
Bases:
abc.ABCAbstract base class for model inference/detection.
Provides common inference infrastructure: - Model loading and initialization (local and Azure remote paths) - Device management - Warmup capability - Prediction interface - Azure weight resolution via WeightResolver
- Parameters:
model_path – Path to model weights (local, azure://, or https://)
device – Device for inference (“cpu”, “cuda”, “mps”, “auto”)
warmup – Whether to run warmup inference on initialization
enable_azure – Enable Azure weight resolution for remote paths
credentials – DsCredentials instance for APIM OAuth2 authentication
weight_resolver – Custom WeightResolver instance (auto-created if None)
Example
>>> # Local path >>> detector = MyDetector(model_path="weights/best.pt", device="cuda") >>> results = detector.predict(source="image.jpg") >>> # Azure Blob Storage (SDK direct) >>> detector = MyDetector(model_path="azure://models/best.pt") >>> # Via APIM with OAuth2 (auto-creates DsCredentials) >>> detector = MyDetector(model_path="https://apim.azure.com/weights/best.pt") >>> # Via APIM with explicit credentials >>> from altametris.sara.core.auth import DsCredentials >>> creds = DsCredentials() >>> detector = MyDetector(model_path="https://apim.azure.com/weights/best.pt", credentials=creds)
- model = None¶
- model_path¶
- property device: torch.device¶
Get current device.
- property is_initialized: bool¶
Check if detector is initialized.
- abstract predict(source: Any, **kwargs: Any) Any¶
Run inference on source.
- Parameters:
source – Input source (image path, array, video, etc.)
**kwargs – Inference parameters (conf, iou, etc.)
- Returns:
Prediction results (format depends on detector type)
- Raises:
InferenceError – If prediction fails
Note
Must be implemented by subclasses
- warmup(iterations: int = 3) None¶
Warmup the model with dummy inference.
Useful for GPU models to pre-allocate memory and compile kernels.
- Parameters:
iterations – Number of warmup iterations
Example
>>> detector.warmup(iterations=5)
- validate_source(source: Any) None¶
Validate input source.
- Parameters:
source – Input source to validate
- Raises:
InferenceError – If source is invalid