architecture_estimates
sleap_nn.config_generator.architecture_estimates
¶
Shared architecture-related estimates for UNet config recommendations.
Single source of truth for the formulas used by the config generator (TUI),
recommender, generator, and YAML emitter. Mirrors the formulas used by the
config picker web app at docs/configuration/config-picker/app.html so the
two surfaces produce equivalent recommendations and estimates.
References:
- Canonical receptive-field formula: example_notebooks/receptive_field_guide.py
(https://distill.pub/2019/computing-receptive-fields/, Eq. 2)
- UNet implementation (ground truth for parameter count):
sleap_nn/architectures/unet.py
- Web-app counterparts: computeReceptiveField, estimateParamsAccurate,
computeAugmentationPadding, computeSuggestedCropSize in app.html.
Functions:
| Name | Description |
|---|---|
compute_augmentation_padding |
Pixels of padding required so a rotated/scaled bbox stays in bounds. |
compute_backbone_context_margin |
Half the surrounding context (px) a tile edge needs to keep seams valid. |
compute_max_stride_for_animal_size |
Smallest max_stride whose receptive field covers the animal. |
compute_pad_to_stride |
Round (height, width) up so each is a multiple of |
compute_receptive_field |
Compute the receptive field of the deepest encoder layer of a UNet. |
compute_suggested_crop_size |
Suggest a crop size that fits the largest instance with optional padding. |
compute_suggested_tile_overlap |
Overlap (px) large enough that a seam-straddling object is whole in one tile. |
compute_suggested_tile_size |
Square tile side that fits an object plus context on both sides. |
decoder_blocks |
Number of decoder upsampling blocks needed to reach |
encoder_blocks |
Number of encoder downsampling blocks for the given max stride. |
estimate_unet_params |
Estimate trainable parameter count of a UNet head + body. |
recommend_default_max_stride |
Bucket-based default |
compute_augmentation_padding(bbox_size, rotation_max=0.0, scale_max=1.0)
¶
Pixels of padding required so a rotated/scaled bbox stays in bounds.
For a square bbox rotated by angle theta, the worst-case bounding-box
expansion is |cos(theta)| + |sin(theta)|, which peaks at sqrt(2) at 45°.
Scaling expands the bbox by max(scale_max, 1.0).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox_size
|
float
|
Original bbox dimension in pixels. |
required |
rotation_max
|
float
|
Max absolute rotation in degrees. |
0.0
|
scale_max
|
float
|
Max scale factor (1.0 == no scaling). |
1.0
|
Returns:
| Type | Description |
|---|---|
int
|
Padding in pixels (ceiling), 0 if no augmentation expansion needed. |
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_backbone_context_margin(backbone_type, max_stride, convs_per_block=2, kernel_size=3)
¶
Half the surrounding context (px) a tile edge needs to keep seams valid.
A tile-edge output pixel has part of its receptive field off-tile; sizing the overlap by this margin ensures an adjacent tile's center (full RF) covers that region.
- UNet: half the deepest-encoder receptive field (
compute_receptive_field). - ConvNext / SwinT: a fixed per-family constant (windowed attention / patch-merging make an analytic RF invalid; see design DQ4).
- Any other backbone (pretrained / unsupported): raises, since tiling is not supported there.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backbone_type
|
str
|
One of |
required |
max_stride
|
int
|
Backbone total downsampling factor (UNet only). |
required |
convs_per_block
|
int
|
Convs per UNet down block (UNet only). |
2
|
kernel_size
|
int
|
UNet conv kernel size (UNet only). |
3
|
Returns:
| Type | Description |
|---|---|
int
|
Context margin in input pixels. |
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_max_stride_for_animal_size(animal_size, candidates=SUPPORTED_MAX_STRIDES)
¶
Smallest max_stride whose receptive field covers the animal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
animal_size
|
float
|
Maximum animal bounding-box dimension in input pixels
(already scaled by |
required |
candidates
|
Tuple[int, ...]
|
Strides to consider, ascending. |
SUPPORTED_MAX_STRIDES
|
Returns:
| Type | Description |
|---|---|
int
|
Smallest stride in |
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_pad_to_stride(height, width, max_stride)
¶
Round (height, width) up so each is a multiple of max_stride.
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_receptive_field(max_stride, convs_per_block=2, kernel_size=3)
¶
Compute the receptive field of the deepest encoder layer of a UNet.
Each downsampling block has convs_per_block convolutions (stride 1,
kernel kernel_size) followed by a 2x2 stride-2 pool. RF is built up
layer-by-layer with the canonical formula::
RF = 1 + sum((kernel[l] - 1) * prod(strides[:l])) for l in 0..L-1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_stride
|
int
|
Total downsampling factor of the encoder (must be a positive power of 2). |
required |
convs_per_block
|
int
|
Number of conv layers per down block. |
2
|
kernel_size
|
int
|
Kernel size of the conv layers. |
3
|
Returns:
| Type | Description |
|---|---|
int
|
Receptive field in input pixels. |
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_suggested_crop_size(max_bbox_dim, max_stride, use_augmentation=False, user_padding=None, rotation_max=0.0, scale_max=1.0)
¶
Suggest a crop size that fits the largest instance with optional padding.
Mirrors the web app's computeSuggestedCropSize (app.html:3402).
- If
user_paddingis provided, it overrides any auto-computed padding (including 0, which means "no padding"). - Else if
use_augmentation, padding is computed fromrotation_max/scale_max. - Result is rounded UP to the next multiple of
max_stride.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_bbox_dim
|
float
|
Largest instance bbox dimension (height or width). |
required |
max_stride
|
int
|
Network max stride; result will be a multiple of this. |
required |
use_augmentation
|
bool
|
Whether to add padding for rotation/scale aug. |
False
|
user_padding
|
Optional[int]
|
Explicit padding override. |
None
|
rotation_max
|
float
|
Max rotation in degrees (used when use_augmentation). |
0.0
|
scale_max
|
float
|
Max scale factor (used when use_augmentation). |
1.0
|
Returns:
| Type | Description |
|---|---|
int
|
Suggested crop size in pixels, divisible by |
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_suggested_tile_overlap(tile_size, max_bbox_dim, confmap_sigma, output_stride, backbone_margin, min_overlap_fraction=0.25, sigma_multiple=3.0)
¶
Overlap (px) large enough that a seam-straddling object is whole in one tile.
Covers half the object extent + a few confmap sigmas + backbone context, is
at least min_overlap_fraction of the tile, rounded UP to a multiple of
output_stride, and clamped to leave a positive stride (>= output_stride).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tile_size
|
int
|
Square tile side (from |
required |
max_bbox_dim
|
float
|
Largest instance bbox dimension. |
required |
confmap_sigma
|
float
|
Confidence-map Gaussian sigma (input pixels). |
required |
output_stride
|
int
|
Head output stride. |
required |
backbone_margin
|
int
|
Per-side context margin. |
required |
min_overlap_fraction
|
float
|
Minimum overlap as a fraction of |
0.25
|
sigma_multiple
|
float
|
How many sigmas of blob to keep whole across a seam. |
3.0
|
Returns:
| Type | Description |
|---|---|
int
|
Suggested overlap in pixels, divisible by |
Source code in sleap_nn/config_generator/architecture_estimates.py
compute_suggested_tile_size(max_bbox_dim, max_stride, output_stride, backbone_margin, object_multiple=2.0, min_tile_multiples=2)
¶
Square tile side that fits an object plus context on both sides.
Rounded UP to a multiple of lcm(max_stride, output_stride) so both the
confmap target grid (subsamples by output_stride) and the backbone
(divides by max_stride) stay exact. Depends ONLY on object extent +
margin (no overlap) to avoid a cycle with compute_suggested_tile_overlap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_bbox_dim
|
float
|
Largest instance bbox dimension (height or width). |
required |
max_stride
|
int
|
Backbone total downsampling factor. |
required |
output_stride
|
int
|
Head output stride. |
required |
backbone_margin
|
int
|
Per-side context margin ( |
required |
object_multiple
|
float
|
Multiple of the object extent to span. |
2.0
|
min_tile_multiples
|
int
|
Floor on the tile side, in units of the divisor. |
2
|
Returns:
| Type | Description |
|---|---|
int
|
Suggested square tile side in pixels, divisible by both strides. |
Source code in sleap_nn/config_generator/architecture_estimates.py
decoder_blocks(max_stride, output_stride)
¶
Number of decoder upsampling blocks needed to reach output_stride.
Source code in sleap_nn/config_generator/architecture_estimates.py
encoder_blocks(max_stride)
¶
estimate_unet_params(filters, max_stride, output_stride, in_channels, num_keypoints, filters_rate=1.5)
¶
Estimate trainable parameter count of a UNet head + body.
Mirrors the web app's estimateParamsAccurate (app.html:3446) and
matches the structure of the real UNet (architectures/unet.py):
encoder + middle/bottleneck block + decoder + 1x1 head.
Each encoder block is 2x (kxk conv + bias) with k=3. Decoder blocks
take a skip connection from the matching encoder level so their input
channel count is f + skip_f.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
int
|
Base filter count in the first encoder block. |
required |
max_stride
|
int
|
Determines encoder depth ( |
required |
output_stride
|
int
|
Determines decoder depth ( |
required |
in_channels
|
int
|
Network input channels (1 grayscale, 3 RGB). |
required |
num_keypoints
|
int
|
Number of output channels in the head. |
required |
filters_rate
|
float
|
Multiplier applied to filter count per encoder block. |
1.5
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated parameter count (weights + biases). |
Source code in sleap_nn/config_generator/architecture_estimates.py
recommend_default_max_stride(avg_animal_size, scale=1.0)
¶
Bucket-based default max_stride recommendation.
Mirrors setDefaultParameters in
docs/configuration/config-picker/app.html (lines 5371–5375): pick
the stride based on the average animal bbox size after input scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
avg_animal_size
|
float
|
Average instance bbox diagonal in original pixels. |
required |
scale
|
float
|
Input scale factor (multiplier applied before pickup). |
1.0
|
Returns:
| Type | Description |
|---|---|
int
|
Recommended max_stride: 8 if effective size < 40, 32 if > 100, else 16. |