unet_utils
sleap.gui.learning.unet_utils
¶
Pure Python deterministic UNet architecture calculations.
This module provides functions to compute UNet channel counts and parameter estimates without requiring PyTorch or sleap-nn dependencies. Used by the training config GUI to display model architecture information.
Ported from sleap-nn investigations: - D:/sleap-nn/scratch/2025-12-21-deterministic-unet-channels/unet_channels.py - D:/sleap-nn/scratch/2025-12-21-deterministic-unet-params/unet_params.py
Functions:
| Name | Description |
|---|---|
compute_unet_channels |
Compute UNet output channels at each stride level. |
compute_unet_params |
Estimate total UNet parameter count. |
format_params |
Format parameter count with appropriate units. |
compute_unet_channels(filters=32, filters_rate=1.5, max_stride=16, output_stride=2, stem_stride=None, block_contraction=False)
¶
Compute UNet output channels at each stride level.
This function deterministically computes the number of output channels at each stride level of a UNet architecture based on the configuration parameters. This matches the behavior of sleap_nn.architectures.unet.UNet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
int
|
Base filter count. Default 32. |
32
|
filters_rate
|
float
|
Multiplicative factor per encoder/decoder level. Default 1.5. |
1.5
|
max_stride
|
int
|
Maximum stride of the encoder (bottleneck). Default 16. |
16
|
output_stride
|
int
|
Final output stride of the decoder. Default 2. |
2
|
stem_stride
|
Optional[int]
|
Stride of the stem blocks. If None, no stem blocks. Default None. |
None
|
block_contraction
|
bool
|
If True, reduces channels at bottleneck. Default False. |
False
|
Returns:
| Type | Description |
|---|---|
Dict[int, int]
|
Dictionary mapping stride to number of output channels at that stride. Keys are strides (e.g., 16, 8, 4, 2), values are channel counts. |
Example
compute_unet_channels( ... filters=32, filters_rate=1.5, max_stride=16, output_stride=2)
Source code in sleap/gui/learning/unet_utils.py
compute_unet_params(filters=32, filters_rate=1.5, max_stride=16, output_stride=2, stem_stride=None, kernel_size=3, stem_kernel_size=7, convs_per_block=2, middle_block=True, up_interpolate=True, in_channels=1, block_contraction=False)
¶
Estimate total UNet parameter count.
This function deterministically estimates the number of trainable parameters in a UNet architecture based on configuration parameters. Validated to have 0% error against actual PyTorch model instantiation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
int
|
Base filter count. Default 32. |
32
|
filters_rate
|
float
|
Multiplicative factor per level. Default 1.5. |
1.5
|
max_stride
|
int
|
Maximum stride of encoder (bottleneck). Default 16. |
16
|
output_stride
|
int
|
Final output stride of decoder. Default 2. |
2
|
stem_stride
|
Optional[int]
|
Stride of stem blocks. If None, no stem. Default None. |
None
|
kernel_size
|
int
|
Kernel size for encoder/decoder convs. Default 3. |
3
|
stem_kernel_size
|
int
|
Kernel size for stem convs. Default 7. |
7
|
convs_per_block
|
int
|
Number of convolutions per block. Default 2. |
2
|
middle_block
|
bool
|
Whether to include middle block. Default True. |
True
|
up_interpolate
|
bool
|
If True, use bilinear (0 params). If False, transposed conv. Default True. |
True
|
in_channels
|
int
|
Number of input channels. Default 1. |
1
|
block_contraction
|
bool
|
If True, reduces channels at bottleneck. Default False. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
Estimated total trainable parameter count. |
Example
compute_unet_params( ... filters=32, filters_rate=1.5, max_stride=16, output_stride=2) 1295032
Source code in sleap/gui/learning/unet_utils.py
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 | |
format_params(params)
¶
Format parameter count with appropriate units.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
int
|
Number of parameters. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Human-readable string like "1.30M" or "533.4K". |