bbox
sleap_nn.inference.layers.bbox
¶
BBoxLayer — whole-frame bounding-box detection inference.
Wraps a trained bbox_centernet / bbox_ssd LightningModule (whose forward
returns the decoder-ready head dict) and decodes its outputs with the model's
:class:~sleap_nn.inference.bbox.BBoxDecoder into Outputs.pred_bboxes /
pred_bbox_scores / pred_bbox_classes in original-image pixels, which
Outputs.to_labels packages as sio.PredictedBoundingBox on
LabeledFrame.bboxes.
Classes:
| Name | Description |
|---|---|
BBoxLayer |
Bounding-box detection prediction layer (CenterNet- or SSD-style). |
BBoxStageLayer |
A box detector as the first stage of a top-down layer. |
ExportedBBoxLayer |
|
Functions:
| Name | Description |
|---|---|
gt_boxes_from_instances |
Tight, padded boxes of GT instances' visible keypoints. |
BBoxLayer
¶
Bases: InferenceLayer
Bounding-box detection prediction layer (CenterNet- or SSD-style).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
Runtime backend wrapping the detector's Lightning module. |
required |
decoder
|
BBoxDecoder
|
The model's :class: |
required |
output_stride
|
int
|
Finest head stride relative to the model input. |
required |
max_stride
|
int
|
Backbone max stride (the input is padded to a multiple of it). |
1
|
preprocess_config
|
Optional[PreprocessConfig]
|
Standard preprocessing knobs. |
None
|
postprocess_config
|
Optional[PostprocessConfig]
|
|
None
|
Methods:
| Name | Description |
|---|---|
__init__ |
Store the decoder and standard layer configs. |
postprocess |
Decode the head outputs into boxes in original-image pixels. |
Attributes:
| Name | Type | Description |
|---|---|---|
categories |
List[str]
|
Ordered class names ( |
Source code in sleap_nn/inference/layers/bbox.py
categories
property
¶
Ordered class names (Outputs.pred_bbox_classes indexes into it).
__init__(backend, decoder, output_stride, max_stride=1, preprocess_config=None, postprocess_config=None)
¶
Store the decoder and standard layer configs.
Source code in sleap_nn/inference/layers/bbox.py
postprocess(raw_out, info)
¶
Decode the head outputs into boxes in original-image pixels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_out
|
dict
|
Backend output dict (the module's decoder-ready head dict). |
required |
info
|
PreprocInfo
|
Preprocessing metadata to map boxes back to the original frame. |
required |
Returns:
| Type | Description |
|---|---|
Outputs
|
|
Source code in sleap_nn/inference/layers/bbox.py
BBoxStageLayer
¶
Bases: BBoxLayer
A box detector as the first stage of a top-down layer.
Stands in for :class:~sleap_nn.inference.layers.centroid.CentroidLayer inside
:class:~sleap_nn.inference.layers.topdown.TopDownLayer (and its multi-class /
segmentation / embedding subclasses): predict returns each detection's box
center as pred_centroids and its score as pred_centroid_values, plus the
boxes themselves (pred_bboxes / pred_bbox_scores / pred_bbox_classes)
for box-scaled crops (preprocess.crop_mode="bbox") and for packaging. The
detector's peak_threshold (score threshold) and max_instances play the
centroid stage's roles, so the per-stage threshold overrides route to it
unchanged.
With use_gt_boxes=True no detector runs: boxes are derived from the
ground-truth instances passed to predict (the tight box of each instance's
visible keypoints, padded by gt_bbox_padding pixels, as the centered-instance
model's training boxes were), the box analog of the GT-centroid fallback.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
The detector's backend (with |
required |
decoder
|
Optional[BBoxDecoder]
|
The detector's :class: |
required |
output_stride
|
int
|
Finest head stride relative to the model input. |
1
|
max_stride
|
int
|
Backbone max stride. |
1
|
use_gt_boxes
|
bool
|
Read boxes from ground-truth instances instead. |
False
|
gt_bbox_padding
|
float
|
Pixels added on every side of ground-truth boxes. |
0.0
|
preprocess_config / postprocess_config
|
As for :class: |
required |
Methods:
| Name | Description |
|---|---|
__init__ |
Store the detector (or the GT-box fallback) and its configs. |
predict |
Detect boxes (or read GT boxes) and report their centers as centroids. |
Attributes:
| Name | Type | Description |
|---|---|---|
categories |
List[str]
|
Detector class names ( |
use_gt_centroids |
bool
|
Whether |
Source code in sleap_nn/inference/layers/bbox.py
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 | |
categories
property
¶
Detector class names ([] for the GT-box fallback).
use_gt_centroids
property
¶
Whether predict needs GT instances (the TopDownLayer contract).
__init__(backend, decoder, output_stride=1, max_stride=1, use_gt_boxes=False, gt_bbox_padding=0.0, preprocess_config=None, postprocess_config=None)
¶
Store the detector (or the GT-box fallback) and its configs.
Source code in sleap_nn/inference/layers/bbox.py
predict(image, instances=None, boxes=None)
¶
Detect boxes (or read GT boxes) and report their centers as centroids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Full frames, as for :meth: |
required | |
instances
|
Optional[Tensor]
|
|
None
|
boxes
|
Optional[Tensor]
|
|
None
|
Returns:
| Type | Description |
|---|---|
Outputs
|
|
Source code in sleap_nn/inference/layers/bbox.py
ExportedBBoxLayer
¶
Bases: BBoxLayer
BBoxLayer for an ONNX/TRT-exported detector.
The exported graph (:class:~sleap_nn.export.wrappers.bbox.BBoxONNXWrapper)
holds only the network: it maps a model-input-sized uint8 frame to the
decoder-ready head outputs. Everything else is this layer's -- the same
preprocess chain as the checkpoint path (channel coercion, size matcher,
input_scale resize, stride padding; a resize traced into the graph would be
frozen to the trace input's size) and the same postprocess (decoder, coord
ladder, clipping) -- so exported and native boxes match by construction. Only
the frames handed to the backend differ: rounded to uint8 and without the
n_samples axis, as the graph expects.
Methods:
| Name | Description |
|---|---|
preprocess |
Native preprocessing, emitted as the graph's |
Source code in sleap_nn/inference/layers/bbox.py
preprocess(image)
¶
Native preprocessing, emitted as the graph's (B, C, H, W) uint8 input.
Source code in sleap_nn/inference/layers/bbox.py
gt_boxes_from_instances(instances, padding=0.0)
¶
Tight, padded boxes of GT instances' visible keypoints.
Matches sio.Instance.to_bbox(mode="tight", padding=padding), the derivation
of the centered-instance model's training boxes for bbox_source="instances".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instances
|
Tensor
|
|
required |
padding
|
float
|
Pixels added on every side. |
0.0
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor, Tensor]
|
|