embedding
sleap_nn.inference.layers.embedding
¶
Embedding (crop -> appearance vector, re-ID) inference layers.
Two pieces, mirroring the keypoint / segmentation top-down stacks:
-
:class:
EmbeddingLayer— the single-stage (precropped / mask-driven) embedder. Runs a trainedembeddingmodel on per-instance crops and returns one L2-normalized appearance vector per crop onOutputs.pred_embeddings(B, I=1, D). It ALSO populatesinstance_scores/instance_validso the detection enumerates correctly (pred_embeddingsalone is insufficient —n_instancesignores it andto_instancescompacts all-NaN slots). The crop pipeline (mask burn-in + per-crop standardize) is IDENTICAL to training, via the LightningModule's_build_input— so the embeddings match the validation retrieval metrics. -
:class:
TopDownEmbeddingLayer— subclasses :class:~sleap_nn.inference.layers.topdown.TopDownLayerto reuse its stage-1 (centroid) + sizematch + crop machinery verbatim, overriding only stage 2 to run the embedder on each crop and packOutputs.pred_embeddings(B, I, D)(+pred_centroids/instance_scores/instance_valid). The GT-centroid fallback (CentroidLayer(use_gt_centroids=True)) covers the mask-only data the same way :class:TopDownSegmentationLayerdoes -- available when this layer is built directly, since a lone embedding dir routes to the mask-driven :class:EmbeddingLayerinstead.
Classes:
| Name | Description |
|---|---|
EmbeddingLayer |
Single-stage (precropped / mask-driven) appearance-embedding layer. |
TopDownEmbeddingLayer |
Composed centroid + per-crop-embedding two-stage layer. |
EmbeddingLayer
¶
Bases: InferenceLayer
Single-stage (precropped / mask-driven) appearance-embedding layer.
Runs a trained embedding model on per-instance crops and returns one
L2-normalized vector per crop on Outputs.pred_embeddings (B, I=1, D).
The structural twin of
:class:~sleap_nn.inference.layers.centered_instance.CenteredInstanceLayer,
but returns appearance vectors rather than keypoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
Runtime backend wrapping |
required |
embedding_module
|
The trained |
required | |
embedding_dim
|
int
|
The output vector dimension |
required |
output_stride
|
int
|
Head map → crop-pixel stride (cosmetic for embeddings). |
1
|
max_stride
|
int
|
Backbone max stride. |
1
|
preprocess_config / postprocess_config
|
Standard knobs. The crops are
already sized; only the model's own |
required |
Methods:
| Name | Description |
|---|---|
__call__ |
Alias for :meth: |
__init__ |
Stash the embedder + configs. |
postprocess |
Package the embedder output into |
predict |
Embed a batch of crops → |
Attributes:
| Name | Type | Description |
|---|---|---|
warmup_input_shape |
Tiny single-channel warmup shape. |
Source code in sleap_nn/inference/layers/embedding.py
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 | |
warmup_input_shape
property
¶
Tiny single-channel warmup shape.
__call__(crops, masks=None)
¶
__init__(backend, embedding_module, embedding_dim, output_stride=1, max_stride=1, input_channels=1, preprocess_config=None, postprocess_config=None)
¶
Stash the embedder + configs.
Source code in sleap_nn/inference/layers/embedding.py
postprocess(raw_out, info)
¶
Package the embedder output into Outputs.pred_embeddings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_out
|
dict
|
Backend dict carrying the |
required |
info
|
Optional[PreprocInfo]
|
Unused (crops are already sized). |
required |
Returns:
| Type | Description |
|---|---|
Outputs
|
|
Source code in sleap_nn/inference/layers/embedding.py
predict(crops, masks=None)
¶
Embed a batch of crops → Outputs.pred_embeddings (N, 1, D).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crops
|
ImageInput
|
|
required |
masks
|
Optional[ImageInput]
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
Outputs
|
|
Source code in sleap_nn/inference/layers/embedding.py
TopDownEmbeddingLayer
¶
Bases: TopDownLayer
Composed centroid + per-crop-embedding two-stage layer.
Subclasses :class:TopDownLayer to reuse stage 1 (centroid) + sizematch +
crop extraction verbatim, overriding only :meth:_run_stage_2 to emit one
appearance vector per crop into Outputs.pred_embeddings instead of
keypoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centroid_layer
|
CentroidLayer
|
Stage-1 :class: |
required |
centered_instance_layer
|
EmbeddingLayer
|
Stage-2 :class: |
required |
crop_size
|
Tuple[int, int]
|
|
required |
centroid_nms / centroid_nms_threshold
|
Optional centroid dedup (inherited). |
required |
Methods:
| Name | Description |
|---|---|
__init__ |
Stash inner layers + crop size. |
Source code in sleap_nn/inference/layers/embedding.py
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 | |
__init__(centroid_layer, centered_instance_layer, crop_size, centroid_nms=False, centroid_nms_threshold=0.5)
¶
Stash inner layers + crop size.