bbox
sleap_nn.inference.bbox
¶
Decoding for the bounding-box detectors (bbox_centernet, bbox_ssd).
Each detection style has a pure-torch decoder that turns its raw head outputs into per-frame boxes, scores and class ids in model-input pixels:
- :func:
decode_centernet-- local maxima of the center heatmap (max-pool NMS), the box read from the size/offset maps at each peak. - :func:
decode_ssd-- per-anchor class probabilities and decoded deltas, thresholded and class-wise IoU-NMSed.
:class:BBoxDecoder binds a style and its parameters (built from a model's head
config) and is what both the LightningModules (val-time eval) and the inference
layer (:class:sleap_nn.inference.layers.bbox.BBoxLayer) call, so training metrics
and predictions come from the same code.
Decoded outputs are dense and padded: boxes (B, K, 4) xyxy, scores (B, K)
and classes (B, K), sorted by descending score, with NaN boxes/scores and class
-1 in unused slots.
Classes:
| Name | Description |
|---|---|
BBoxDecoder |
A detection style's decoder bound to its parameters. |
BBoxInferenceModel |
Inference model of a |
Functions:
| Name | Description |
|---|---|
clip_boxes_to_frame |
Clip padded |
decode_centernet |
Decode CenterNet head outputs into boxes. |
decode_ssd |
Decode SSD head outputs into boxes. |
BBoxDecoder
¶
A detection style's decoder bound to its parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
model_type |
|
|
categories |
Ordered class names (class ids index into it). |
|
output_stride |
CenterNet grid stride. |
|
score_threshold |
Minimum detection score. |
|
iou_threshold |
IoU-NMS threshold ( |
|
max_detections |
Detections kept per frame. |
|
anchor_encoder |
SSD only -- the target encoder that owns the anchors. |
Methods:
| Name | Description |
|---|---|
__call__ |
Decode a batch of head outputs (probabilities for CenterNet's heatmap). |
__init__ |
Initialize the decoder. |
from_head_configs |
Build the decoder of a trained model from its |
Source code in sleap_nn/inference/bbox.py
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 | |
__call__(outputs, img_hw, score_threshold=None, max_detections=None)
¶
Decode a batch of head outputs (probabilities for CenterNet's heatmap).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outputs
|
Dict[str, Tensor]
|
The model's head-name -> tensor dict. CenterNet expects
|
required |
img_hw
|
Tuple[int, int]
|
Model-input |
required |
score_threshold
|
Optional[float]
|
Per-call override of the score threshold. |
None
|
max_detections
|
Optional[int]
|
Per-call override of the detections kept per frame. |
None
|
Returns:
| Type | Description |
|---|---|
_Decoded
|
Padded |
Source code in sleap_nn/inference/bbox.py
__init__(model_type, categories, output_stride=1, score_threshold=0.2, iou_threshold=None, max_detections=DEFAULT_MAX_DETECTIONS, anchor_encoder=None)
¶
Initialize the decoder.
Source code in sleap_nn/inference/bbox.py
from_head_configs(model_type, head_configs, score_threshold=0.2, iou_threshold='default', max_detections=None)
classmethod
¶
Build the decoder of a trained model from its head_configs.
iou_threshold="default" picks the style's default (:data:DEFAULT_NMS_IOU).
Source code in sleap_nn/inference/bbox.py
BBoxInferenceModel
¶
Bases: LightningModule
Inference model of a bbox_* detector (an attribute bag, as for segmentation).
Holds the trained module and its :class:BBoxDecoder; the inference run itself
is driven by :class:sleap_nn.inference.layers.bbox.BBoxLayer, built from these
attributes by the predictor factory.
Attributes:
| Name | Type | Description |
|---|---|---|
torch_model |
The trained LightningModule (its |
|
decoder |
The style's :class: |
|
input_scale |
Input scale the model was trained with. |
|
output_stride |
Finest head stride. |
Methods:
| Name | Description |
|---|---|
__init__ |
Stash the model and its decoder. |
Source code in sleap_nn/inference/bbox.py
clip_boxes_to_frame(boxes, scores, classes, frame_hw)
¶
Clip padded (B, K, 4) boxes to the original frame.
The decoder clips to the padded model input; mapped back to the frame, a box
can still reach into the stride padding or the size-matcher's letterbox. Boxes
left with no area (predicted wholly outside the frame) become empty slots
(NaN box and score, class -1).
Source code in sleap_nn/inference/bbox.py
decode_centernet(heatmap, size, offset, output_stride, score_threshold=0.2, max_detections=DEFAULT_MAX_DETECTIONS, nms_kernel=3, iou_threshold=None, img_hw=None)
¶
Decode CenterNet head outputs into boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heatmap
|
Tensor
|
|
required |
size
|
Tensor
|
|
required |
offset
|
Optional[Tensor]
|
|
required |
output_stride
|
int
|
Grid stride in input pixels. |
required |
score_threshold
|
float
|
Minimum peak probability. |
0.2
|
max_detections
|
int
|
Peaks kept per frame (top-k before thresholding). |
DEFAULT_MAX_DETECTIONS
|
nms_kernel
|
int
|
Max-pool window of the peak NMS. |
3
|
iou_threshold
|
Optional[float]
|
Optional class-wise IoU-NMS on top of the peak NMS. |
None
|
img_hw
|
Optional[Tuple[int, int]]
|
Model-input |
None
|
Returns:
| Type | Description |
|---|---|
_Decoded
|
Padded |
Source code in sleap_nn/inference/bbox.py
decode_ssd(raw, anchors, variances, score_threshold=0.2, max_detections=DEFAULT_MAX_DETECTIONS, iou_threshold=0.45, pre_nms_top_k=1000, img_hw=None)
¶
Decode SSD head outputs into boxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw
|
Tensor
|
|
required |
anchors
|
Tensor
|
|
required |
variances
|
Sequence[float]
|
Box-coding variances. |
required |
score_threshold
|
float
|
Minimum class probability of a detection. |
0.2
|
max_detections
|
int
|
Detections kept per frame after NMS. |
DEFAULT_MAX_DETECTIONS
|
iou_threshold
|
Optional[float]
|
Class-wise IoU-NMS threshold ( |
0.45
|
pre_nms_top_k
|
int
|
Highest-scoring (anchor, class) pairs entering NMS. |
1000
|
img_hw
|
Optional[Tuple[int, int]]
|
Model-input |
None
|
Returns:
| Type | Description |
|---|---|
_Decoded
|
Padded |