skia_augmentation
sleap_nn.data.skia_augmentation
¶
Skia-based augmentation functions that operate on uint8 tensors.
This module provides augmentation functions using skia-python that: 1. Match the exact API of sleap_nn.data.augmentation 2. Operate on uint8 tensors throughout (avoiding float32 conversions) 3. Provide ~1.5x faster augmentation compared to Kornia
The implementation uses array-backed Skia surfaces (following the sleap-io pattern) which avoids platform-specific BGR/RGBA pixel format issues. By creating surfaces with an existing numpy array as the backing store, Skia writes directly to that array in the specified color format, eliminating the need for channel swapping.
Usage
from sleap_nn.data.skia_augmentation import ( apply_intensity_augmentation_skia, apply_geometric_augmentation_skia, )
Apply augmentations (uint8 in, uint8 out)¶
image, instances = apply_intensity_augmentation_skia(image, instances, **config) image, instances = apply_geometric_augmentation_skia(image, instances, **config)
Functions:
| Name | Description |
|---|---|
apply_flip_augmentation_skia |
Randomly mirror an image and its keypoints left/right, swapping symmetries. |
apply_geometric_augmentation_skia |
Apply geometric augmentations using Skia. |
apply_intensity_augmentation_skia |
Apply intensity augmentations on uint8 image tensor. |
crop_and_resize_skia |
Crop and resize image regions using Skia. |
crop_and_resize_skia_window |
:func: |
crop_window_bounds |
The frame window |
apply_flip_augmentation_skia(image, instances, symmetric_inds=None, flip_p=0.0, masks=None)
¶
Randomly mirror an image and its keypoints left/right, swapping symmetries.
When an image is mirrored left/right, left/right symmetric body parts physically
exchange sides, so their slots in the instance array must be swapped to keep
semantic labels correct (e.g. left_paw must remain left_paw). This
mirrors the behavior of SLEAP v1.4's RandomFlipper (horizontal flip).
The flip is applied to the whole sample with probability flip_p (sampled once
per call). Mirroring is exact and lossless (a tensor reverse, no interpolation).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input tensor of shape |
required |
instances
|
Tensor
|
Keypoints tensor of shape |
required |
symmetric_inds
|
Optional[Sequence[Tuple[int, int]]]
|
Iterable of |
None
|
flip_p
|
float
|
Probability of applying the flip. |
0.0
|
masks
|
Optional[Tensor]
|
Optional segmentation masks of shape |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, ...]
|
|
Source code in sleap_nn/data/skia_augmentation.py
apply_geometric_augmentation_skia(image, instances, rotation_min=-15.0, rotation_max=15.0, rotation_p=None, scale_min=0.9, scale_max=1.1, scale_p=None, translate_width=0.02, translate_height=0.02, translate_p=None, affine_p=0.0, erase_scale_min=0.0001, erase_scale_max=0.01, erase_ratio_min=1.0, erase_ratio_max=1.0, erase_p=0.0, mixup_lambda_min=0.01, mixup_lambda_max=0.05, mixup_p=0.0, flip_p=0.0, symmetric_inds=None, masks=None)
¶
Apply geometric augmentations using Skia.
Matches API of sleap_nn.data.augmentation.apply_geometric_augmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input tensor of shape (1, C, H, W) with dtype uint8 or float32. |
required |
instances
|
Tensor
|
Keypoints tensor of shape (1, n_instances, n_nodes, 2) or (1, n_nodes, 2). |
required |
rotation_min
|
float
|
Minimum rotation angle in degrees. |
-15.0
|
rotation_max
|
float
|
Maximum rotation angle in degrees. |
15.0
|
rotation_p
|
Optional[float]
|
Probability of rotation (independent). None = use affine_p. |
None
|
scale_min
|
float
|
Minimum scale factor. |
0.9
|
scale_max
|
float
|
Maximum scale factor. |
1.1
|
scale_p
|
Optional[float]
|
Probability of scaling (independent). None = use affine_p. |
None
|
translate_width
|
float
|
Max horizontal translation as fraction of width. |
0.02
|
translate_height
|
float
|
Max vertical translation as fraction of height. |
0.02
|
translate_p
|
Optional[float]
|
Probability of translation (independent). None = use affine_p. |
None
|
affine_p
|
float
|
Probability of bundled affine transform. |
0.0
|
erase_scale_min
|
float
|
Min proportion of image to erase. |
0.0001
|
erase_scale_max
|
float
|
Max proportion of image to erase. |
0.01
|
erase_ratio_min
|
float
|
Min aspect ratio of erased area. |
1.0
|
erase_ratio_max
|
float
|
Max aspect ratio of erased area. |
1.0
|
erase_p
|
float
|
Probability of random erasing. |
0.0
|
mixup_lambda_min
|
float
|
Min mixup strength (not implemented). |
0.01
|
mixup_lambda_max
|
float
|
Max mixup strength (not implemented). |
0.05
|
mixup_p
|
float
|
Probability of mixup (not implemented). |
0.0
|
flip_p
|
float
|
Probability of mirroring the sample left/right (with symmetric-node swap). |
0.0
|
symmetric_inds
|
Optional[Sequence[Tuple[int, int]]]
|
Node-index pairs to swap after mirroring (see
|
None
|
masks
|
Optional[Tensor]
|
Optional segmentation masks of shape |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, ...]
|
|
Source code in sleap_nn/data/skia_augmentation.py
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
apply_intensity_augmentation_skia(image, instances, uniform_noise_min=0.0, uniform_noise_max=0.04, uniform_noise_p=0.0, gaussian_noise_mean=0.0, gaussian_noise_std=0.02, gaussian_noise_p=0.0, contrast_min=0.9, contrast_max=1.1, contrast_p=0.0, brightness_min=0.9, brightness_max=1.1, brightness_p=0.0)
¶
Apply intensity augmentations on uint8 image tensor.
Matches API of sleap_nn.data.augmentation.apply_intensity_augmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input tensor of shape (1, C, H, W) with dtype uint8 or float32. |
required |
instances
|
Tensor
|
Keypoints tensor (not modified, just passed through). |
required |
uniform_noise_min
|
float
|
Minimum uniform noise (0-1 scale, maps to 0-255). |
0.0
|
uniform_noise_max
|
float
|
Maximum uniform noise (0-1 scale). |
0.04
|
uniform_noise_p
|
float
|
Probability of uniform noise. |
0.0
|
gaussian_noise_mean
|
float
|
Gaussian noise mean (0-1 scale). |
0.0
|
gaussian_noise_std
|
float
|
Gaussian noise std (0-1 scale). |
0.02
|
gaussian_noise_p
|
float
|
Probability of Gaussian noise. |
0.0
|
contrast_min
|
float
|
Minimum contrast factor. |
0.9
|
contrast_max
|
float
|
Maximum contrast factor. |
1.1
|
contrast_p
|
float
|
Probability of contrast adjustment. |
0.0
|
brightness_min
|
float
|
Minimum brightness factor. |
0.9
|
brightness_max
|
float
|
Maximum brightness factor. |
1.1
|
brightness_p
|
float
|
Probability of brightness adjustment. |
0.0
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
Tuple of (augmented_image, instances). Image dtype matches input. |
Source code in sleap_nn/data/skia_augmentation.py
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 | |
crop_and_resize_skia(image, boxes, size)
¶
Crop and resize image regions using Skia.
Replacement for kornia.geometry.transform.crop_and_resize.
Uses array-backed Skia surface pattern from sleap-io to avoid platform-specific BGR/RGBA issues. By creating the surface with an existing numpy array as backing store, Skia writes directly to that array in RGBA format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input tensor of shape (1, C, H, W). |
required |
boxes
|
Tensor
|
Bounding boxes tensor of shape (1, 4, 2) with corners: [top-left, top-right, bottom-right, bottom-left]. |
required |
size
|
Tuple[int, int]
|
Output size (height, width). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Cropped and resized tensor of shape (1, C, out_h, out_w). |
Source code in sleap_nn/data/skia_augmentation.py
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
crop_and_resize_skia_window(window, origin, frame_hw, boxes, size)
¶
:func:crop_and_resize_skia of a frame, given only a window of it.
Returns the same bytes :func:crop_and_resize_skia returns for the whole frame,
when window covers :func:crop_window_bounds of the box, in time that grows
with the crop instead of the frame.
Skia maps each output pixel to the source through the box's transform, in float32.
Cropping a window and shifting the box (or drawing the window at an offset) changes
that arithmetic, and crops can then differ by a grey level. So the
transform is left as it is: the window is written into a frame-sized RGBA buffer
(reused, one per thread) that Skia wraps without a copy. The rest of that buffer
is never sampled, so what it holds does not matter, and nothing frame-sized is
built or copied. Keep the transform and the drawing below in step with
:func:crop_and_resize_skia.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
window
|
Tensor
|
The frame window, |
required |
origin
|
Tuple[int, int]
|
The window's top-left |
required |
frame_hw
|
Tuple[int, int]
|
The frame's |
required |
boxes
|
Tensor
|
Box corners |
required |
size
|
Tuple[int, int]
|
Output size |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The crop, |
Source code in sleap_nn/data/skia_augmentation.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | |
crop_window_bounds(boxes, frame_hw)
¶
The frame window (y0, y1, x0, x1) (half-open) a crop box samples from.
An output pixel center of :func:crop_and_resize_skia maps strictly inside the
box, and bilinear sampling reads the two source pixels around it, so every tap
lies within one pixel of the box. The window adds a margin of
_CROP_WINDOW_MARGIN pixels and is clipped to the frame; it can be empty
when the box lies entirely outside the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
boxes
|
Tensor
|
Box corners of shape |
required |
frame_hw
|
Tuple[int, int]
|
The |
required |