topdown_multiclass
sleap_nn.export.wrappers.topdown_multiclass
¶
ONNX wrapper for top-down multiclass (supervised ID) models.
Classes:
| Name | Description |
|---|---|
TopDownMultiClassCombinedONNXWrapper |
ONNX-exportable wrapper for combined centroid + multiclass instance models. |
TopDownMultiClassONNXWrapper |
ONNX-exportable wrapper for top-down multiclass (supervised ID) models. |
TopDownMultiClassCombinedONNXWrapper
¶
Bases: BaseExportWrapper
ONNX-exportable wrapper for combined centroid + multiclass instance models.
This wrapper combines a centroid detection model with a centered instance multiclass model. It performs: 1. Centroid detection on full images 2. Cropping around each centroid using vectorized grid_sample 3. Instance keypoint detection + identity classification on each crop
Expects input images as uint8 tensors in [0, 255].
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the combined wrapper. |
forward |
Run combined top-down multiclass inference. |
Source code in sleap_nn/export/wrappers/topdown_multiclass.py
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 | |
__init__(centroid_model, instance_model, max_instances=20, crop_size=(192, 192), centroid_output_stride=4, instance_output_stride=2, centroid_input_scale=1.0, instance_input_scale=1.0, n_nodes=13, n_classes=2, centroid_peak_threshold=0.2, instance_peak_threshold=0.2)
¶
Initialize the combined wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centroid_model
|
Module
|
Model for centroid detection. |
required |
instance_model
|
Module
|
Model for instance keypoints + class prediction. |
required |
max_instances
|
int
|
Maximum number of instances to detect. |
20
|
crop_size
|
tuple
|
Size of crops around centroids (height, width). |
(192, 192)
|
centroid_output_stride
|
int
|
Output stride of centroid model. |
4
|
instance_output_stride
|
int
|
Output stride of instance model. |
2
|
centroid_input_scale
|
float
|
Input scale for centroid model. |
1.0
|
instance_input_scale
|
float
|
Input scale for instance model. |
1.0
|
n_nodes
|
int
|
Number of keypoint nodes per instance. |
13
|
n_classes
|
int
|
Number of identity classes. |
2
|
centroid_peak_threshold
|
float
|
Minimum confidence for centroid peaks. |
0.2
|
instance_peak_threshold
|
float
|
Minimum confidence for instance peaks. |
0.2
|
Source code in sleap_nn/export/wrappers/topdown_multiclass.py
forward(image)
¶
Run combined top-down multiclass inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input image tensor of shape (batch, channels, height, width). Expected to be uint8 in [0, 255]. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dictionary with keys: - "centroids": Detected centroids (batch, max_instances, 2). - "centroid_vals": Centroid confidence values (batch, max_instances). - "peaks": Instance peaks (batch, max_instances, n_nodes, 2). - "peak_vals": Peak values (batch, max_instances, n_nodes). - "class_logits": Class logits per instance (batch, max_instances, n_classes). - "instance_valid": Validity mask (batch, max_instances). |
Source code in sleap_nn/export/wrappers/topdown_multiclass.py
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 | |
TopDownMultiClassONNXWrapper
¶
Bases: BaseExportWrapper
ONNX-exportable wrapper for top-down multiclass (supervised ID) models.
This wrapper handles models that output both confidence maps for keypoint detection and class logits for identity classification. It runs on instance crops (centered around detected centroids).
Expects input images as uint8 tensors in [0, 255].
Attributes:
| Name | Type | Description |
|---|---|---|
model |
The underlying PyTorch model (centered instance + class vectors heads). |
|
output_stride |
Output stride of the confmap head. |
|
input_scale |
Scale factor applied to input images before inference. |
|
n_classes |
Number of identity classes. |
Methods:
| Name | Description |
|---|---|
__init__ |
Initialize the wrapper. |
forward |
Run top-down multiclass inference on crops. |
Source code in sleap_nn/export/wrappers/topdown_multiclass.py
__init__(model, output_stride=2, input_scale=1.0, n_classes=2, peak_threshold=0.2)
¶
Initialize the wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The underlying PyTorch model. |
required |
output_stride
|
int
|
Output stride of the confidence maps. |
2
|
input_scale
|
float
|
Scale factor for input images. |
1.0
|
n_classes
|
int
|
Number of identity classes (e.g., 2 for male/female). |
2
|
peak_threshold
|
float
|
Minimum confidence for a peak to be considered valid. |
0.2
|
Source code in sleap_nn/export/wrappers/topdown_multiclass.py
forward(image)
¶
Run top-down multiclass inference on crops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input image tensor of shape (batch, channels, height, width). Expected to be uint8 in [0, 255]. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Tensor]
|
Dictionary with keys: - "peaks": Predicted peak coordinates (batch, n_nodes, 2) in (x, y). - "peak_vals": Peak confidence values (batch, n_nodes). - "class_logits": Raw class logits (batch, n_classes). The class assignment is done on CPU using Hungarian matching
via |