sleap-nn 0.0.5__py3-none-any.whl → 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. sleap_nn/__init__.py +9 -2
  2. sleap_nn/architectures/convnext.py +5 -0
  3. sleap_nn/architectures/encoder_decoder.py +25 -6
  4. sleap_nn/architectures/swint.py +8 -0
  5. sleap_nn/cli.py +489 -46
  6. sleap_nn/config/data_config.py +51 -8
  7. sleap_nn/config/get_config.py +32 -24
  8. sleap_nn/config/trainer_config.py +88 -0
  9. sleap_nn/data/augmentation.py +61 -200
  10. sleap_nn/data/custom_datasets.py +433 -61
  11. sleap_nn/data/instance_cropping.py +71 -6
  12. sleap_nn/data/normalization.py +45 -2
  13. sleap_nn/data/providers.py +26 -0
  14. sleap_nn/data/resizing.py +2 -2
  15. sleap_nn/data/skia_augmentation.py +414 -0
  16. sleap_nn/data/utils.py +135 -17
  17. sleap_nn/evaluation.py +177 -42
  18. sleap_nn/export/__init__.py +21 -0
  19. sleap_nn/export/cli.py +1778 -0
  20. sleap_nn/export/exporters/__init__.py +51 -0
  21. sleap_nn/export/exporters/onnx_exporter.py +80 -0
  22. sleap_nn/export/exporters/tensorrt_exporter.py +291 -0
  23. sleap_nn/export/metadata.py +225 -0
  24. sleap_nn/export/predictors/__init__.py +63 -0
  25. sleap_nn/export/predictors/base.py +22 -0
  26. sleap_nn/export/predictors/onnx.py +154 -0
  27. sleap_nn/export/predictors/tensorrt.py +312 -0
  28. sleap_nn/export/utils.py +307 -0
  29. sleap_nn/export/wrappers/__init__.py +25 -0
  30. sleap_nn/export/wrappers/base.py +96 -0
  31. sleap_nn/export/wrappers/bottomup.py +243 -0
  32. sleap_nn/export/wrappers/bottomup_multiclass.py +195 -0
  33. sleap_nn/export/wrappers/centered_instance.py +56 -0
  34. sleap_nn/export/wrappers/centroid.py +58 -0
  35. sleap_nn/export/wrappers/single_instance.py +83 -0
  36. sleap_nn/export/wrappers/topdown.py +180 -0
  37. sleap_nn/export/wrappers/topdown_multiclass.py +304 -0
  38. sleap_nn/inference/__init__.py +6 -0
  39. sleap_nn/inference/bottomup.py +86 -20
  40. sleap_nn/inference/peak_finding.py +93 -16
  41. sleap_nn/inference/postprocessing.py +284 -0
  42. sleap_nn/inference/predictors.py +339 -137
  43. sleap_nn/inference/provenance.py +292 -0
  44. sleap_nn/inference/topdown.py +55 -47
  45. sleap_nn/legacy_models.py +65 -11
  46. sleap_nn/predict.py +224 -19
  47. sleap_nn/system_info.py +443 -0
  48. sleap_nn/tracking/tracker.py +8 -1
  49. sleap_nn/train.py +138 -44
  50. sleap_nn/training/callbacks.py +1258 -5
  51. sleap_nn/training/lightning_modules.py +902 -220
  52. sleap_nn/training/model_trainer.py +424 -111
  53. sleap_nn/training/schedulers.py +191 -0
  54. sleap_nn/training/utils.py +367 -2
  55. {sleap_nn-0.0.5.dist-info → sleap_nn-0.1.0.dist-info}/METADATA +35 -33
  56. sleap_nn-0.1.0.dist-info/RECORD +88 -0
  57. {sleap_nn-0.0.5.dist-info → sleap_nn-0.1.0.dist-info}/WHEEL +1 -1
  58. sleap_nn-0.0.5.dist-info/RECORD +0 -63
  59. {sleap_nn-0.0.5.dist-info → sleap_nn-0.1.0.dist-info}/entry_points.txt +0 -0
  60. {sleap_nn-0.0.5.dist-info → sleap_nn-0.1.0.dist-info}/licenses/LICENSE +0 -0
  61. {sleap_nn-0.0.5.dist-info → sleap_nn-0.1.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,284 @@
1
+ """Inference-level postprocessing filters for pose predictions.
2
+
3
+ This module provides filters that run after model inference but before tracking.
4
+ These filters are independent of tracking configuration and can be used standalone.
5
+ """
6
+
7
+ from typing import List, Literal
8
+
9
+ import numpy as np
10
+ import sleap_io as sio
11
+
12
+
13
+ def filter_overlapping_instances(
14
+ labels: sio.Labels,
15
+ threshold: float = 0.8,
16
+ method: Literal["iou", "oks"] = "iou",
17
+ ) -> sio.Labels:
18
+ """Filter overlapping instances using greedy non-maximum suppression.
19
+
20
+ Removes duplicate/overlapping instances by applying greedy NMS based on
21
+ either bounding box IOU or Object Keypoint Similarity (OKS). When two
22
+ instances overlap above the threshold, the lower-scoring one is removed.
23
+
24
+ This filter runs independently of tracking and can be used to clean up
25
+ model outputs before saving or further processing.
26
+
27
+ Args:
28
+ labels: Labels object with predicted instances to filter.
29
+ threshold: Similarity threshold for considering instances as overlapping.
30
+ Instances with similarity > threshold are candidates for removal.
31
+ Lower values are more aggressive (remove more).
32
+ Typical values: 0.3 (aggressive) to 0.8 (permissive).
33
+ method: Similarity metric to use for comparing instances.
34
+ "iou": Bounding box intersection-over-union.
35
+ "oks": Object Keypoint Similarity (pose-based).
36
+
37
+ Returns:
38
+ The input Labels object with overlapping instances removed.
39
+ Modification is done in place, but the object is also returned
40
+ for convenience.
41
+
42
+ Example:
43
+ >>> # Filter instances with >80% bounding box overlap
44
+ >>> labels = filter_overlapping_instances(labels, threshold=0.8, method="iou")
45
+ >>> # Filter using OKS similarity
46
+ >>> labels = filter_overlapping_instances(labels, threshold=0.5, method="oks")
47
+
48
+ Note:
49
+ - Only affects frames with 2+ predicted instances
50
+ - Uses instance.score for ranking; higher scores are preferred
51
+ - For IOU: bounding boxes computed from non-NaN keypoints
52
+ - For OKS: uses standard COCO OKS formula with bbox-derived scale
53
+ """
54
+ for lf in labels.labeled_frames:
55
+ if len(lf.instances) <= 1:
56
+ continue
57
+
58
+ # Separate predicted instances (have scores) from other instances
59
+ predicted = []
60
+ other = []
61
+ for inst in lf.instances:
62
+ if isinstance(inst, sio.PredictedInstance):
63
+ predicted.append(inst)
64
+ else:
65
+ other.append(inst)
66
+
67
+ # Only filter predicted instances
68
+ if len(predicted) <= 1:
69
+ continue
70
+
71
+ # Get scores
72
+ scores = np.array([_instance_score(inst) for inst in predicted])
73
+
74
+ # Apply greedy NMS with selected method
75
+ if method == "iou":
76
+ bboxes = np.array([_instance_bbox(inst) for inst in predicted])
77
+ keep_indices = _nms_greedy_iou(bboxes, scores, threshold)
78
+ elif method == "oks":
79
+ points = [inst.numpy() for inst in predicted]
80
+ keep_indices = _nms_greedy_oks(points, scores, threshold)
81
+ else:
82
+ raise ValueError(f"Unknown method: {method}. Use 'iou' or 'oks'.")
83
+
84
+ # Reconstruct instance list: kept predicted + other instances
85
+ kept_predicted = [predicted[i] for i in keep_indices]
86
+ lf.instances = kept_predicted + other
87
+
88
+ return labels
89
+
90
+
91
+ def _instance_bbox(instance: sio.PredictedInstance) -> np.ndarray:
92
+ """Compute axis-aligned bounding box from instance keypoints.
93
+
94
+ Args:
95
+ instance: Instance with keypoints.
96
+
97
+ Returns:
98
+ Bounding box as [xmin, ymin, xmax, ymax].
99
+ Returns [0, 0, 0, 0] if no valid keypoints.
100
+ """
101
+ pts = instance.numpy() # (n_nodes, 2)
102
+ valid = ~np.isnan(pts).any(axis=1)
103
+
104
+ if not valid.any():
105
+ return np.array([0.0, 0.0, 0.0, 0.0])
106
+
107
+ pts = pts[valid]
108
+ return np.array(
109
+ [pts[:, 0].min(), pts[:, 1].min(), pts[:, 0].max(), pts[:, 1].max()]
110
+ )
111
+
112
+
113
+ def _instance_score(instance: sio.PredictedInstance) -> float:
114
+ """Get instance confidence score.
115
+
116
+ Args:
117
+ instance: Predicted instance.
118
+
119
+ Returns:
120
+ Instance score, or 1.0 if not available.
121
+ """
122
+ return getattr(instance, "score", 1.0)
123
+
124
+
125
+ def _nms_greedy_iou(
126
+ bboxes: np.ndarray,
127
+ scores: np.ndarray,
128
+ threshold: float,
129
+ ) -> List[int]:
130
+ """Apply greedy NMS using bounding box IOU.
131
+
132
+ Args:
133
+ bboxes: Bounding boxes of shape (N, 4) as [xmin, ymin, xmax, ymax].
134
+ scores: Confidence scores of shape (N,).
135
+ threshold: IOU threshold for suppression.
136
+
137
+ Returns:
138
+ List of indices to keep, in order of decreasing score.
139
+ """
140
+ if len(bboxes) == 0:
141
+ return []
142
+
143
+ # Sort by score descending
144
+ order = scores.argsort()[::-1].tolist()
145
+
146
+ keep = []
147
+ while order:
148
+ # Take highest scoring remaining instance
149
+ i = order.pop(0)
150
+ keep.append(i)
151
+
152
+ if not order:
153
+ break
154
+
155
+ # Compute IOU with all remaining instances
156
+ remaining_indices = np.array(order)
157
+ similarities = _compute_iou_one_to_many(bboxes[i], bboxes[remaining_indices])
158
+
159
+ # Keep only instances with similarity <= threshold
160
+ mask = similarities <= threshold
161
+ order = [order[j] for j in range(len(order)) if mask[j]]
162
+
163
+ return keep
164
+
165
+
166
+ def _nms_greedy_oks(
167
+ points_list: List[np.ndarray],
168
+ scores: np.ndarray,
169
+ threshold: float,
170
+ ) -> List[int]:
171
+ """Apply greedy NMS using Object Keypoint Similarity (OKS).
172
+
173
+ Args:
174
+ points_list: List of keypoint arrays, each of shape (n_nodes, 2).
175
+ scores: Confidence scores of shape (N,).
176
+ threshold: OKS threshold for suppression.
177
+
178
+ Returns:
179
+ List of indices to keep, in order of decreasing score.
180
+ """
181
+ if len(points_list) == 0:
182
+ return []
183
+
184
+ # Sort by score descending
185
+ order = scores.argsort()[::-1].tolist()
186
+
187
+ keep = []
188
+ while order:
189
+ # Take highest scoring remaining instance
190
+ i = order.pop(0)
191
+ keep.append(i)
192
+
193
+ if not order:
194
+ break
195
+
196
+ # Compute OKS with all remaining instances
197
+ similarities = np.array(
198
+ [_compute_oks(points_list[i], points_list[j]) for j in order]
199
+ )
200
+
201
+ # Keep only instances with similarity <= threshold
202
+ mask = similarities <= threshold
203
+ order = [order[j] for j in range(len(order)) if mask[j]]
204
+
205
+ return keep
206
+
207
+
208
+ def _compute_iou_one_to_many(box: np.ndarray, boxes: np.ndarray) -> np.ndarray:
209
+ """Compute IOU between one box and multiple boxes.
210
+
211
+ Args:
212
+ box: Single box of shape (4,) as [xmin, ymin, xmax, ymax].
213
+ boxes: Multiple boxes of shape (N, 4).
214
+
215
+ Returns:
216
+ IOU values of shape (N,).
217
+ """
218
+ # Intersection coordinates
219
+ inter_xmin = np.maximum(box[0], boxes[:, 0])
220
+ inter_ymin = np.maximum(box[1], boxes[:, 1])
221
+ inter_xmax = np.minimum(box[2], boxes[:, 2])
222
+ inter_ymax = np.minimum(box[3], boxes[:, 3])
223
+
224
+ # Intersection area (0 if no overlap)
225
+ inter_w = np.maximum(0.0, inter_xmax - inter_xmin)
226
+ inter_h = np.maximum(0.0, inter_ymax - inter_ymin)
227
+ inter_area = inter_w * inter_h
228
+
229
+ # Individual areas
230
+ area_a = (box[2] - box[0]) * (box[3] - box[1])
231
+ area_b = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
232
+
233
+ # Union area
234
+ union_area = area_a + area_b - inter_area
235
+
236
+ # IOU (avoid division by zero)
237
+ return np.where(union_area > 0, inter_area / union_area, 0.0)
238
+
239
+
240
+ def _compute_oks(
241
+ points_a: np.ndarray,
242
+ points_b: np.ndarray,
243
+ kappa: float = 0.1,
244
+ ) -> float:
245
+ """Compute Object Keypoint Similarity (OKS) between two instances.
246
+
247
+ Uses a simplified OKS formula where all keypoints have equal weight
248
+ and scale is derived from the bounding box of the reference instance.
249
+
250
+ Args:
251
+ points_a: Keypoints of first instance, shape (n_nodes, 2).
252
+ points_b: Keypoints of second instance, shape (n_nodes, 2).
253
+ kappa: Per-keypoint constant controlling falloff. Default 0.1.
254
+
255
+ Returns:
256
+ OKS value in [0, 1]. Higher means more similar.
257
+ """
258
+ # Find valid keypoints (present in both instances)
259
+ valid_a = ~np.isnan(points_a).any(axis=1)
260
+ valid_b = ~np.isnan(points_b).any(axis=1)
261
+ valid = valid_a & valid_b
262
+
263
+ if not valid.any():
264
+ return 0.0
265
+
266
+ # Compute scale from bounding box area of instance A
267
+ pts_a_valid = points_a[valid_a]
268
+ if len(pts_a_valid) < 2:
269
+ return 0.0
270
+
271
+ bbox_w = pts_a_valid[:, 0].max() - pts_a_valid[:, 0].min()
272
+ bbox_h = pts_a_valid[:, 1].max() - pts_a_valid[:, 1].min()
273
+ scale_sq = bbox_w * bbox_h
274
+
275
+ if scale_sq <= 0:
276
+ return 0.0
277
+
278
+ # Compute squared distances for valid keypoints
279
+ d_sq = np.sum((points_a[valid] - points_b[valid]) ** 2, axis=1)
280
+
281
+ # OKS formula: mean of exp(-d^2 / (2 * s^2 * k^2))
282
+ oks_per_kpt = np.exp(-d_sq / (2 * scale_sq * kappa**2))
283
+
284
+ return float(np.mean(oks_per_kpt))