Predictors API
culicidaelab.predictors
__all__ = ['MosquitoClassifier', 'MosquitoDetector', 'MosquitoSegmenter', 'ModelWeightsManager']
module-attribute
MosquitoClassifier
Classifies mosquito species from an image using a FastAI model.
This class provides methods to load a pre-trained model, predict species from single or batches of images, evaluate model performance, and visualize the classification results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings
|
The main settings object for the library, which contains configuration for paths, models, and species. |
required |
load_model
|
bool
|
If True, the model weights are loaded immediately upon initialization. Defaults to False. |
False
|
Attributes:
Name | Type | Description |
---|---|---|
arch |
str
|
The model architecture (e.g., 'convnext_tiny'). |
data_dir |
Path
|
The directory where datasets are stored. |
species_map |
dict[int, str]
|
A mapping from class indices to species names. |
num_classes |
int
|
The total number of species classes. |
learner |
int
|
The loaded FastAI learner object, available after |
Source code in culicidaelab/predictors/classifier.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
|
settings = settings
instance-attribute
predictor_type = predictor_type
instance-attribute
config: PredictorConfig
property
Get the predictor configuration Pydantic model.
model_loaded: bool
property
Check if the model is loaded.
model_path: Path
property
Gets the path to the model weights file.
arch: str | None = self.config.model_arch
instance-attribute
data_dir: Path = self.settings.dataset_dir
instance-attribute
species_map: dict[int, str] = self.settings.species_config.species_map
instance-attribute
num_classes: int = len(self.species_map)
instance-attribute
__call__(input_data: np.ndarray, **kwargs: Any) -> Any
Convenience method that calls predict()
.
__enter__()
__exit__(exc_type, exc_val, exc_tb)
model_context()
A context manager for temporary model loading.
Ensures the model is loaded upon entering the context and unloaded upon exiting. This is useful for managing memory in pipelines.
Yields:
Name | Type | Description |
---|---|---|
BasePredictor |
The predictor instance itself. |
Example
with predictor.model_context(): ... predictions = predictor.predict(data)
Source code in culicidaelab/core/base_predictor.py
evaluate(ground_truth: GroundTruthType, prediction: PredictionType | None = None, input_data: np.ndarray | None = None, **predict_kwargs: Any) -> dict[str, float]
Evaluate a prediction against a ground truth.
Either prediction
or input_data
must be provided. If prediction
is provided, it is used directly. If prediction
is None, input_data
is used to generate a new prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth
|
GroundTruthType
|
The ground truth annotation. |
required |
prediction
|
PredictionType
|
A pre-computed prediction. |
None
|
input_data
|
ndarray
|
Input data to generate a prediction from, if one isn't provided. |
None
|
**predict_kwargs
|
Any
|
Additional arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing evaluation metrics for a |
dict[str, float]
|
single item. |
Raises:
Type | Description |
---|---|
ValueError
|
If neither |
Source code in culicidaelab/core/base_predictor.py
evaluate_batch(ground_truth_batch: list[GroundTruthType], predictions_batch: list[PredictionType] | None = None, input_data_batch: list[np.ndarray] | None = None, num_workers: int = 4, show_progress: bool = True, **predict_kwargs: Any) -> dict[str, float]
Evaluate on a batch of items using parallel processing.
Either predictions_batch
or input_data_batch
must be provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth_batch
|
list[GroundTruthType]
|
List of corresponding ground truth annotations. |
required |
predictions_batch
|
list[PredictionType]
|
A pre-computed list of predictions. |
None
|
input_data_batch
|
list[ndarray]
|
List of input data to generate predictions from. |
None
|
num_workers
|
int
|
Number of parallel workers for calculating metrics. |
4
|
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**predict_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing aggregated evaluation metrics. |
Raises:
Type | Description |
---|---|
ValueError
|
If the number of predictions does not match the number of ground truths. |
Source code in culicidaelab/core/base_predictor.py
get_model_info() -> dict[str, Any]
Gets information about the loaded model.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary containing details about the model, such |
dict[str, Any]
|
as architecture, path, etc. |
Source code in culicidaelab/core/base_predictor.py
load_model() -> None
Loads the model if it is not already loaded.
This is a convenience wrapper around _load_model
that prevents
reloading.
Raises:
Type | Description |
---|---|
RuntimeError
|
If model loading fails. |
Source code in culicidaelab/core/base_predictor.py
unload_model() -> None
Unloads the model to free memory.
__init__(settings: Settings, load_model: bool = False) -> None
Initializes the MosquitoClassifier.
Source code in culicidaelab/predictors/classifier.py
get_class_index(species_name: str) -> int | None
Retrieves the class index for a given species name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
species_name
|
str
|
The name of the species. |
required |
Returns:
Type | Description |
---|---|
int | None
|
int | None: The corresponding class index if found, otherwise None. |
Source code in culicidaelab/predictors/classifier.py
get_species_names() -> list[str]
Gets a sorted list of all species names known to the classifier.
The list is ordered by the class index.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of species names. |
Source code in culicidaelab/predictors/classifier.py
predict(input_data: np.ndarray, **kwargs: Any) -> ClassificationPredictionType
Classifies the mosquito species in a single image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
An input image as a NumPy array with a shape of (H, W, 3) in RGB format. Values can be uint8 [0, 255] or float [0, 1]. |
required |
**kwargs
|
Any
|
Additional arguments (not used). |
{}
|
Returns:
Name | Type | Description |
---|---|---|
ClassificationPredictionType |
ClassificationPredictionType
|
A list of (species_name, confidence) |
ClassificationPredictionType
|
tuples, sorted in descending order of confidence. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model has not been loaded. |
ValueError
|
If the input data has an invalid shape, data type, or is not a valid image. |
Source code in culicidaelab/predictors/classifier.py
predict_batch(input_data_batch: list[Any], show_progress: bool = False, **kwargs: Any) -> list[ClassificationPredictionType]
Classifies mosquito species in a batch of images.
Note: This method currently iterates and calls predict
for each image.
True batch processing is not yet implemented.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data_batch
|
list[Any]
|
A list of input images, where each image is a NumPy array. |
required |
show_progress
|
bool
|
If True, a progress bar is displayed. Defaults to False. |
False
|
**kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
list[ClassificationPredictionType]
|
list[ClassificationPredictionType]: A list of prediction results, |
list[ClassificationPredictionType]
|
where each result corresponds to an input image. |
Source code in culicidaelab/predictors/classifier.py
visualize(input_data: np.ndarray, predictions: ClassificationPredictionType, save_path: str | Path | None = None) -> np.ndarray
Overlays classification results on an image.
This method draws the top-k predictions and their confidence scores onto the input image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The original image (H, W, 3) as a NumPy array. |
required |
predictions
|
ClassificationPredictionType
|
The prediction output from
the |
required |
save_path
|
str | Path | None
|
If provided, the visualized image will be saved to this path. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A new image array with the prediction text overlaid. |
Raises:
Type | Description |
---|---|
ValueError
|
If the input data is not a 3D image or if the predictions list is empty. |
Source code in culicidaelab/predictors/classifier.py
MosquitoDetector
Detects mosquitos in images using a YOLO model.
This class loads a YOLO model and provides methods for predicting bounding boxes on single or batches of images, visualizing results, and evaluating detection performance against ground truth data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings
|
The main settings object for the library. |
required |
load_model
|
bool
|
If True, the model is loaded upon initialization. Defaults to False. |
False
|
Attributes:
Name | Type | Description |
---|---|---|
confidence_threshold |
float
|
The minimum confidence score for a detection to be considered valid. |
iou_threshold |
float
|
The IoU threshold for non-maximum suppression. |
max_detections |
int
|
The maximum number of detections to return per image. |
Source code in culicidaelab/predictors/detector.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
settings = settings
instance-attribute
predictor_type = predictor_type
instance-attribute
config: PredictorConfig
property
Get the predictor configuration Pydantic model.
model_loaded: bool
property
Check if the model is loaded.
model_path: Path
property
Gets the path to the model weights file.
confidence_threshold: float = self.config.confidence or 0.5
instance-attribute
iou_threshold: float = self.config.params.get('iou_threshold', 0.45)
instance-attribute
max_detections: int = self.config.params.get('max_detections', 300)
instance-attribute
__call__(input_data: np.ndarray, **kwargs: Any) -> Any
Convenience method that calls predict()
.
__enter__()
__exit__(exc_type, exc_val, exc_tb)
model_context()
A context manager for temporary model loading.
Ensures the model is loaded upon entering the context and unloaded upon exiting. This is useful for managing memory in pipelines.
Yields:
Name | Type | Description |
---|---|---|
BasePredictor |
The predictor instance itself. |
Example
with predictor.model_context(): ... predictions = predictor.predict(data)
Source code in culicidaelab/core/base_predictor.py
evaluate(ground_truth: GroundTruthType, prediction: PredictionType | None = None, input_data: np.ndarray | None = None, **predict_kwargs: Any) -> dict[str, float]
Evaluate a prediction against a ground truth.
Either prediction
or input_data
must be provided. If prediction
is provided, it is used directly. If prediction
is None, input_data
is used to generate a new prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth
|
GroundTruthType
|
The ground truth annotation. |
required |
prediction
|
PredictionType
|
A pre-computed prediction. |
None
|
input_data
|
ndarray
|
Input data to generate a prediction from, if one isn't provided. |
None
|
**predict_kwargs
|
Any
|
Additional arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing evaluation metrics for a |
dict[str, float]
|
single item. |
Raises:
Type | Description |
---|---|
ValueError
|
If neither |
Source code in culicidaelab/core/base_predictor.py
evaluate_batch(ground_truth_batch: list[GroundTruthType], predictions_batch: list[PredictionType] | None = None, input_data_batch: list[np.ndarray] | None = None, num_workers: int = 4, show_progress: bool = True, **predict_kwargs: Any) -> dict[str, float]
Evaluate on a batch of items using parallel processing.
Either predictions_batch
or input_data_batch
must be provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth_batch
|
list[GroundTruthType]
|
List of corresponding ground truth annotations. |
required |
predictions_batch
|
list[PredictionType]
|
A pre-computed list of predictions. |
None
|
input_data_batch
|
list[ndarray]
|
List of input data to generate predictions from. |
None
|
num_workers
|
int
|
Number of parallel workers for calculating metrics. |
4
|
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**predict_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing aggregated evaluation metrics. |
Raises:
Type | Description |
---|---|
ValueError
|
If the number of predictions does not match the number of ground truths. |
Source code in culicidaelab/core/base_predictor.py
get_model_info() -> dict[str, Any]
Gets information about the loaded model.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary containing details about the model, such |
dict[str, Any]
|
as architecture, path, etc. |
Source code in culicidaelab/core/base_predictor.py
load_model() -> None
Loads the model if it is not already loaded.
This is a convenience wrapper around _load_model
that prevents
reloading.
Raises:
Type | Description |
---|---|
RuntimeError
|
If model loading fails. |
Source code in culicidaelab/core/base_predictor.py
unload_model() -> None
Unloads the model to free memory.
__init__(settings: Settings, load_model: bool = False) -> None
Initializes the MosquitoDetector.
Source code in culicidaelab/predictors/detector.py
predict(input_data: np.ndarray, **kwargs: Any) -> DetectionPredictionType
Detects mosquitos in a single image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The input image as a NumPy array. |
required |
**kwargs
|
Any
|
Optional keyword arguments, including: confidence_threshold (float): Override the default confidence threshold for this prediction. |
{}
|
Returns:
Name | Type | Description |
---|---|---|
DetectionPredictionType |
DetectionPredictionType
|
A list of detection tuples. Each tuple is |
DetectionPredictionType
|
(center_x, center_y, width, height, confidence). Returns an empty |
|
DetectionPredictionType
|
list if no mosquitos are found. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model fails to load or if prediction fails. |
Source code in culicidaelab/predictors/detector.py
predict_batch(input_data_batch: list[np.ndarray], show_progress: bool = True, **kwargs: Any) -> list[DetectionPredictionType]
Detects mosquitos in a batch of images using YOLO's native batching.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data_batch
|
list[ndarray]
|
A list of input images. |
required |
show_progress
|
bool
|
If True, a progress bar is shown. Defaults to True. |
True
|
**kwargs
|
Any
|
Additional arguments (not used). |
{}
|
Returns:
Type | Description |
---|---|
list[DetectionPredictionType]
|
list[DetectionPredictionType]: A list where each item is the list |
list[DetectionPredictionType]
|
of detections for the corresponding image in the input batch. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model is not loaded. |
Source code in culicidaelab/predictors/detector.py
visualize(input_data: np.ndarray, predictions: DetectionPredictionType, save_path: str | Path | None = None) -> np.ndarray
Draws predicted bounding boxes on an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The original image. |
required |
predictions
|
DetectionPredictionType
|
The list of detections from |
required |
save_path
|
str | Path | None
|
If provided, the output image is saved to this path. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A new image array with bounding boxes and confidence |
ndarray
|
scores drawn on it. |
Source code in culicidaelab/predictors/detector.py
MosquitoSegmenter
Segments mosquitos in images using the SAM2 model.
This class provides methods to load a SAM2 model, generate segmentation masks for entire images or specific regions defined by bounding boxes, and visualize the resulting masks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings
|
The main settings object for the library. |
required |
load_model
|
bool
|
If True, the model is loaded upon initialization. Defaults to False. |
False
|
Source code in culicidaelab/predictors/segmenter.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
|
settings = settings
instance-attribute
predictor_type = predictor_type
instance-attribute
config: PredictorConfig
property
Get the predictor configuration Pydantic model.
model_loaded: bool
property
Check if the model is loaded.
model_path: Path
property
Gets the path to the model weights file.
__call__(input_data: np.ndarray, **kwargs: Any) -> Any
Convenience method that calls predict()
.
__enter__()
__exit__(exc_type, exc_val, exc_tb)
model_context()
A context manager for temporary model loading.
Ensures the model is loaded upon entering the context and unloaded upon exiting. This is useful for managing memory in pipelines.
Yields:
Name | Type | Description |
---|---|---|
BasePredictor |
The predictor instance itself. |
Example
with predictor.model_context(): ... predictions = predictor.predict(data)
Source code in culicidaelab/core/base_predictor.py
evaluate(ground_truth: GroundTruthType, prediction: PredictionType | None = None, input_data: np.ndarray | None = None, **predict_kwargs: Any) -> dict[str, float]
Evaluate a prediction against a ground truth.
Either prediction
or input_data
must be provided. If prediction
is provided, it is used directly. If prediction
is None, input_data
is used to generate a new prediction.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth
|
GroundTruthType
|
The ground truth annotation. |
required |
prediction
|
PredictionType
|
A pre-computed prediction. |
None
|
input_data
|
ndarray
|
Input data to generate a prediction from, if one isn't provided. |
None
|
**predict_kwargs
|
Any
|
Additional arguments passed to the |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing evaluation metrics for a |
dict[str, float]
|
single item. |
Raises:
Type | Description |
---|---|
ValueError
|
If neither |
Source code in culicidaelab/core/base_predictor.py
evaluate_batch(ground_truth_batch: list[GroundTruthType], predictions_batch: list[PredictionType] | None = None, input_data_batch: list[np.ndarray] | None = None, num_workers: int = 4, show_progress: bool = True, **predict_kwargs: Any) -> dict[str, float]
Evaluate on a batch of items using parallel processing.
Either predictions_batch
or input_data_batch
must be provided.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ground_truth_batch
|
list[GroundTruthType]
|
List of corresponding ground truth annotations. |
required |
predictions_batch
|
list[PredictionType]
|
A pre-computed list of predictions. |
None
|
input_data_batch
|
list[ndarray]
|
List of input data to generate predictions from. |
None
|
num_workers
|
int
|
Number of parallel workers for calculating metrics. |
4
|
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**predict_kwargs
|
Any
|
Additional arguments passed to |
{}
|
Returns:
Type | Description |
---|---|
dict[str, float]
|
dict[str, float]: Dictionary containing aggregated evaluation metrics. |
Raises:
Type | Description |
---|---|
ValueError
|
If the number of predictions does not match the number of ground truths. |
Source code in culicidaelab/core/base_predictor.py
get_model_info() -> dict[str, Any]
Gets information about the loaded model.
Returns:
Type | Description |
---|---|
dict[str, Any]
|
dict[str, Any]: A dictionary containing details about the model, such |
dict[str, Any]
|
as architecture, path, etc. |
Source code in culicidaelab/core/base_predictor.py
load_model() -> None
Loads the model if it is not already loaded.
This is a convenience wrapper around _load_model
that prevents
reloading.
Raises:
Type | Description |
---|---|
RuntimeError
|
If model loading fails. |
Source code in culicidaelab/core/base_predictor.py
predict_batch(input_data_batch: list[np.ndarray], show_progress: bool = True, **kwargs: Any) -> list[PredictionType]
Makes predictions on a batch of inputs.
This base implementation processes items serially. Subclasses with native batching capabilities SHOULD override this method.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data_batch
|
list[ndarray]
|
List of input data to make predictions on. |
required |
show_progress
|
bool
|
Whether to show a progress bar. |
True
|
**kwargs
|
Any
|
Additional arguments passed to each |
{}
|
Returns:
Type | Description |
---|---|
list[PredictionType]
|
list[PredictionType]: List of predictions. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If model fails to load or predict. |
Source code in culicidaelab/core/base_predictor.py
unload_model() -> None
Unloads the model to free memory.
__init__(settings: Settings, load_model: bool = False) -> None
Initializes the MosquitoSegmenter.
Source code in culicidaelab/predictors/segmenter.py
predict(input_data: np.ndarray, **kwargs: Any) -> np.ndarray
Generates a segmentation mask for mosquitos in an image.
If detection_boxes
are provided in kwargs, the model will generate
masks only for those specific regions. Otherwise, it will attempt to
segment the most prominent object in the image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The input image as a NumPy array (H, W, 3). |
required |
**kwargs
|
Any
|
Optional keyword arguments, including: detection_boxes (list, optional): A list of bounding boxes in (cx, cy, w, h, conf) format to guide segmentation. |
{}
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A binary segmentation mask of shape (H, W), where pixels |
ndarray
|
belonging to a mosquito are marked as 1 (or True) and 0 otherwise. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the model is not loaded or fails to load. |
Source code in culicidaelab/predictors/segmenter.py
visualize(input_data: np.ndarray, predictions: SegmentationPredictionType, save_path: str | Path | None = None) -> np.ndarray
Overlays a segmentation mask on the original image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_data
|
ndarray
|
The original image. |
required |
predictions
|
SegmentationPredictionType
|
The binary segmentation mask
from the |
required |
save_path
|
str | Path | None
|
If provided, the visualized image is saved to this path. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: A new image array with the segmentation mask visualized |
ndarray
|
as a colored overlay. |
Source code in culicidaelab/predictors/segmenter.py
ModelWeightsManager
Manages the download and local availability of model weights.
This class implements the WeightsManagerProtocol and serves as the bridge between a predictor and the provider service that can download model files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings
|
Settings
|
The application's global settings object. |
required |
provider_service
|
ProviderService
|
The service for downloading artifacts. |
required |
Source code in culicidaelab/predictors/model_weights_manager.py
settings = settings
instance-attribute
provider_service = provider_service
instance-attribute
__init__(settings: Settings, provider_service: ProviderService)
ensure_weights(model_type: str) -> Path
Ensures model weights exist locally, downloading them if needed.
This method checks for the local existence of a model's weights. If they are not found, it uses the provider service to download them based on the configuration associated with the given model type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_type
|
str
|
The type of the model for which to ensure weights, e.g., 'classifier', 'detector'. |
required |
Returns:
Name | Type | Description |
---|---|---|
Path |
Path
|
The absolute, canonical path to the local model weights file. |
Raises:
Type | Description |
---|---|
RuntimeError
|
If the weights cannot be downloaded or if the configuration for the provider is missing or invalid. |
Source code in culicidaelab/predictors/model_weights_manager.py
selection:
members: true