prompts
sleap_nn.inference.sam.prompts
¶
Prompt builders for SAM-prompted instance segmentation (PR-A).
A prompt is the geometric hint handed to a SAM backend
(:mod:sleap_nn.inference.sam.backends) for one instance: positive point
coordinates, an optional box, and the keypoint-box used by the candidate
rejection heuristic (:func:sleap_nn.inference.sam.backends._pick). All
coordinates are in the pixel space of the full frame the backend will encode.
Three prompt modes (v1 = prompted-only, PLAN L3), each validated by a prototype:
- :func:
pose_prompt— every visible keypoint as a positive point plus the padded keypoint box (the locked exp-07 recipe; cleanest — P1). - :func:
centroid_prompt— a single positive point (the predicted centroid / anchor, or the mean of the visible keypoints);multimask_outputis essential and the keypoint box is kept only for candidate rejection (P1/P2). - :func:
box_prompt— the padded pose box as the only prompt, no points (leaks between adjacent animals; secondary — P1/P2).
The product rule (PLAN L3 / P2): use the pose prompt when an instance has
visible keypoints, else fall back to the centroid point.
:func:prompt_for_instance implements exactly that dispatch.
Classes:
| Name | Description |
|---|---|
SamPrompt |
A built SAM prompt for one instance. |
Functions:
| Name | Description |
|---|---|
box_prompt |
Box prompt: the padded pose/crop box as the only prompt, no points. |
centroid_prompt |
Centroid prompt: a single positive point, |
kpt_box |
Padded keypoint bounding box |
pose_prompt |
Pose prompt: visible keypoints as positive points + the padded kpt-box. |
prompt_for_instance |
Dispatch to the right prompt builder, applying the L3 product rule. |
visible_keypoints |
Return the finite |
SamPrompt
dataclass
¶
A built SAM prompt for one instance.
Attributes:
| Name | Type | Description |
|---|---|---|
point_coords |
Optional[ndarray]
|
|
point_labels |
Optional[ndarray]
|
|
box |
Optional[ndarray]
|
|
reject_box |
ndarray
|
|
mode |
str
|
The originating mode tag ( |
Source code in sleap_nn/inference/sam/prompts.py
box_prompt(keypoints, hw, margin_frac=SAM_BOX_MARGIN_FRAC, margin_min=SAM_BOX_MARGIN_MIN)
¶
Box prompt: the padded pose/crop box as the only prompt, no points.
Secondary mode (P1/P2: leaks between adjacent animals). The same box is both the SAM prompt and the reject box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keypoints
|
ndarray
|
|
required |
hw
|
Tuple[int, int]
|
|
required |
margin_frac
|
float
|
Keypoint-box margin fraction (:func: |
SAM_BOX_MARGIN_FRAC
|
margin_min
|
float
|
Keypoint-box minimum margin (:func: |
SAM_BOX_MARGIN_MIN
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SamPrompt
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no keypoints are visible. |
Source code in sleap_nn/inference/sam/prompts.py
centroid_prompt(point, hw, keypoints=None, margin_frac=SAM_BOX_MARGIN_FRAC, margin_min=SAM_BOX_MARGIN_MIN)
¶
Centroid prompt: a single positive point, multimask_output essential.
The box is not passed to SAM (a lone point is maximally ambiguous —
P1/P2); it is computed only for the candidate-rejection heuristic. When
keypoints is given the keypoint box is used; otherwise a fixed margin box
around the point is used as the reject box.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
ndarray
|
The |
required |
hw
|
Tuple[int, int]
|
|
required |
keypoints
|
Optional[ndarray]
|
Optional |
None
|
margin_frac
|
float
|
Reject-box margin fraction. |
SAM_BOX_MARGIN_FRAC
|
margin_min
|
float
|
Reject-box minimum margin. |
SAM_BOX_MARGIN_MIN
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SamPrompt
|
class: |
Source code in sleap_nn/inference/sam/prompts.py
kpt_box(pos, hw, margin_frac=SAM_BOX_MARGIN_FRAC, margin_min=SAM_BOX_MARGIN_MIN)
¶
Padded keypoint bounding box [x0, y0, x1, y1] clamped to hw.
Harvested verbatim from #642 _kpt_box (the locked exp-07 recipe): the
per-axis margin is max(margin_min, margin_frac * side), so the box grows
with the instance but never collapses below margin_min px for a small or
degenerate (single-point) instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pos
|
ndarray
|
|
required |
hw
|
Tuple[int, int]
|
|
required |
margin_frac
|
float
|
Box margin as a fraction of the box side length. |
SAM_BOX_MARGIN_FRAC
|
margin_min
|
float
|
Minimum box margin (px) per axis. |
SAM_BOX_MARGIN_MIN
|
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in sleap_nn/inference/sam/prompts.py
pose_prompt(keypoints, hw, margin_frac=SAM_BOX_MARGIN_FRAC, margin_min=SAM_BOX_MARGIN_MIN)
¶
Pose prompt: visible keypoints as positive points + the padded kpt-box.
The strongest prompt (P1: containment 1.0, 72% single-component, no blow-ups). No negative points (automatic prompting only; PLAN §2.2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keypoints
|
ndarray
|
|
required |
hw
|
Tuple[int, int]
|
|
required |
margin_frac
|
float
|
Keypoint-box margin fraction (:func: |
SAM_BOX_MARGIN_FRAC
|
margin_min
|
float
|
Keypoint-box minimum margin (:func: |
SAM_BOX_MARGIN_MIN
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
SamPrompt
|
class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no keypoints are visible (caller should fall back to a
center point — see :func: |
Source code in sleap_nn/inference/sam/prompts.py
prompt_for_instance(mode, hw, keypoints=None, centroid=None, margin_frac=SAM_BOX_MARGIN_FRAC, margin_min=SAM_BOX_MARGIN_MIN)
¶
Dispatch to the right prompt builder, applying the L3 product rule.
The product rule (PLAN L3 / P2): for mode="pose" use the pose prompt when
the instance has visible keypoints, else fall back to a center point (the
centroid if given, else an error). "centroid" / "box" always use their
named builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
str
|
One of :data: |
required |
hw
|
Tuple[int, int]
|
|
required |
keypoints
|
Optional[ndarray]
|
|
None
|
centroid
|
Optional[ndarray]
|
|
None
|
margin_frac
|
float
|
Keypoint-box margin fraction. |
SAM_BOX_MARGIN_FRAC
|
margin_min
|
float
|
Keypoint-box minimum margin. |
SAM_BOX_MARGIN_MIN
|
Returns:
| Type | Description |
|---|---|
SamPrompt
|
A built :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in sleap_nn/inference/sam/prompts.py
visible_keypoints(points)
¶
Return the finite (m, 2) rows of an (n, 2) keypoint array.
Drops any keypoint with a non-finite (NaN / inf) x or y, matching the
prototype's k[np.isfinite(k).all(1)] visibility filter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
ndarray
|
|
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|