receptivefield
sleap.gui.learning.receptivefield
¶
Widget for previewing receptive field on sample image using model hyperparams.
Classes:
| Name | Description |
|---|---|
ReceptiveFieldImageWidget |
Widget for showing image with receptive field and optional crop box. |
ReceptiveFieldWidget |
Widget for previewing receptive field on sample image, with caption. |
Functions:
| Name | Description |
|---|---|
compute_anchor_point |
Computes the anchor point for an instance. |
compute_augmentation_padding |
Compute padding needed to accommodate augmentation transforms. |
compute_crop_size_from_cfg |
Computes crop size from model configuration. |
compute_eff_scale |
Return the scale the size matcher would apply to a frame of this size. |
compute_rf |
Computes receptive field for specified model architecture. |
find_instance_crop_size |
Compute a crop size that contains every labeled instance. |
find_max_instance_bbox_size |
Find the maximum bounding box dimension across all instances in labels. |
get_first_labeled_frame_and_instance |
Gets the first frame with ground truth labels and the first instance. |
iter_required_crop_sizes |
Yield the crop size each labeled instance needs to avoid being clipped. |
receptive_field_info_from_model_cfg |
Gets receptive field and architecture information from model configuration. |
resolve_anchor_ind |
Resolve an anchor node name to its index in the labels' skeleton. |
resolve_centroid_method |
Resolve the head config's centroid knobs into |
resolve_crop_centroid |
Resolve the centroid definition the crops will be centered on. |
resolve_crop_inputs |
Resolve everything the crop-size computation needs from the GUI forms. |
resolve_max_hw |
Resolve the size-matcher target the way the trainer's first pass does. |
ReceptiveFieldImageWidget
¶
Bases: GraphicsView
Widget for showing image with receptive field and optional crop box.
Methods:
| Name | Description |
|---|---|
viewportEvent |
Re-draw receptive field and crop box when needed. |
Source code in sleap/gui/learning/receptivefield.py
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 | |
viewportEvent(event)
¶
Re-draw receptive field and crop box when needed.
Source code in sleap/gui/learning/receptivefield.py
ReceptiveFieldWidget
¶
Bases: QWidget
Widget for previewing receptive field on sample image, with caption.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_name
|
Text
|
If given, then used in caption to show which model the preview is for. |
''
|
show_crop_box
|
bool
|
If True, shows a crop size box centered on anchor point. This is intended for centered_instance and multi_class_topdown heads. |
False
|
Usage
Create, then call setImage and setModelConfig methods.
For crop box display, also call setLabels and setCropConfig.
Methods:
| Name | Description |
|---|---|
addButtonWidget |
Add a widget (typically a button) between the legend and explanation. |
setCropConfig |
Sets crop box configuration. |
setImage |
Sets image on which receptive field box will be drawn. |
setLabels |
Sets labels and displays the first labeled frame. |
setModelConfig |
Updates receptive field preview from model config. |
Source code in sleap/gui/learning/receptivefield.py
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 | |
addButtonWidget(widget)
¶
Add a widget (typically a button) between the legend and explanation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
widget
|
QWidget
|
The widget to add (e.g., QPushButton for "Analyze Sizes...") |
required |
Source code in sleap/gui/learning/receptivefield.py
setCropConfig(crop_size, scale, anchor_part=None, max_hw=None)
¶
Sets crop box configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crop_size
|
Optional[int]
|
The crop size in pixels, in the size-matched space that
training crops in (scaled by |
required |
scale
|
float
|
The scale factor applied to the image during training. |
required |
anchor_part
|
Optional[Text]
|
The name of the body part to use as anchor. If None, the mean of all keypoints is used. |
None
|
max_hw
|
Optional[Tuple[Optional[int], Optional[int]]]
|
The resolved |
None
|
Source code in sleap/gui/learning/receptivefield.py
setImage(*args, **kwargs)
¶
setLabels(labels, fallback_video=None)
¶
Sets labels and displays the first labeled frame.
This finds the first frame with ground truth labels, displays that frame, and stores the instance for crop box anchor point calculation (if enabled).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Optional[Labels]
|
The Labels object containing labeled frames. |
required |
fallback_video
|
Video to use for getting test frame if labeled frame cannot be loaded. |
None
|
Source code in sleap/gui/learning/receptivefield.py
setModelConfig(model_cfg, scale)
¶
Updates receptive field preview from model config.
Source code in sleap/gui/learning/receptivefield.py
compute_anchor_point(instance, anchor_part=None)
¶
Computes the anchor point for an instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Optional[Instance]
|
The instance to compute the anchor point for. |
required |
anchor_part
|
Optional[Text]
|
The name of the body part to use as anchor. If None, the mean of all visible keypoints is used. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Tuple[float, float]]
|
A tuple (x, y) representing the anchor point coordinates, or None if the anchor cannot be computed. |
Source code in sleap/gui/learning/receptivefield.py
compute_augmentation_padding(bbox_size, rotation_max=0.0, scale_max=1.0)
¶
Compute padding needed to accommodate augmentation transforms.
A local copy of sleap_nn.data.instance_cropping.compute_augmentation_padding
-- the sleap_nn module imports torch, which would add ~2s the first time the
crop preview updated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bbox_size
|
float
|
The size of the instance bounding box (max of width/height). |
required |
rotation_max
|
float
|
Maximum absolute rotation angle in degrees. For symmetric rotation ranges like [-180, 180], pass 180. |
0.0
|
scale_max
|
float
|
Maximum scaling factor. For scale range [0.9, 1.1], pass 1.1. |
1.0
|
Returns:
| Type | Description |
|---|---|
int
|
Padding in pixels to add around the bounding box (total, not per side). |
Source code in sleap/gui/learning/receptivefield.py
compute_crop_size_from_cfg(data_cfg, model_cfg, labels=None, aug_form_data=None)
¶
Computes crop size from model configuration.
When crop_size is not set (None/auto), computes it the way
ModelTrainer._setup_preprocessing_config does: from the reach of every
labeled instance around the centroid its crop is centered on, measured in
the size-matched pixel space cropping happens in, floored at
min_crop_size, plus augmentation padding, rounded up to the backbone's
max stride.
Two differences from training remain, and neither is resolvable from the dialog: the crop size is sized here from all labeled frames rather than from the training split (the split is drawn at training time, from a seed), and only the first labels file is considered.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_cfg
|
OmegaConf
|
Data configuration OmegaConf from the data form. |
required |
model_cfg
|
OmegaConf
|
Model configuration OmegaConf from the model form. |
required |
labels
|
Optional[Labels]
|
Labels object for computing instance bounding boxes. |
None
|
aug_form_data
|
Optional[dict]
|
Raw dict from the augmentation form's get_form_data(), used to compute augmentation padding from virtual fields. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The network's input crop size in pixels -- the crop size in size-matched
pixels, scaled by |
Source code in sleap/gui/learning/receptivefield.py
compute_eff_scale(img_hw, max_hw=None)
¶
Return the scale the size matcher would apply to a frame of this size.
Mirrors sleap_nn.data.resizing.compute_eff_scale: the size matcher fits
each frame into (max_height, max_width) preserving aspect ratio, so the
scale is the smaller of the two ratios.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_hw
|
Tuple[int, int]
|
The frame's |
required |
max_hw
|
Optional[Tuple[Optional[int], Optional[int]]]
|
The configured |
None
|
Returns:
| Type | Description |
|---|---|
float
|
The scale factor, |
Source code in sleap/gui/learning/receptivefield.py
compute_rf(down_blocks, convs_per_block=2, kernel_size=3)
¶
Computes receptive field for specified model architecture.
Ref: https://distill.pub/2019/computing-receptive-fields/ (Eq. 2)
Source code in sleap/gui/learning/receptivefield.py
find_instance_crop_size(labels, padding=0, maximum_stride=2, min_crop_size=None, max_hw=None, anchor_ind=None, centroid_method=None, centroid_fallback=None, user_instances_only=True)
¶
Compute a crop size that contains every labeled instance.
This is a local implementation that avoids importing sleap_nn (which would
trigger importing torch, adding ~2s to startup time). It mirrors
sleap_nn.data.instance_cropping.find_instance_crop_size: the size is
measured the way the crop is actually taken -- centered on each instance's
centroid (iter_required_crop_sizes) and, when max_hw is given, in the
size-matched pixel space cropping happens in. Both matter; a bounding-box
measurement in native pixels under-sizes the crop whenever the centroid is
off-center or the video is rescaled to max_hw (sleap-nn #748).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
A |
required |
padding
|
int
|
Integer number of pixels to add to the bounds as margin padding. |
0
|
maximum_stride
|
int
|
Ensure that the returned crop size is divisible by this value. Useful for ensuring that the crop size will not be truncated in a given architecture. |
2
|
min_crop_size
|
Optional[int]
|
A floor for the returned crop size, before padding. |
None
|
max_hw
|
Optional[Tuple[Optional[int], Optional[int]]]
|
The configured |
None
|
anchor_ind
|
Optional[int]
|
Index of the anchor node the crop is centered on, or
|
None
|
centroid_method
|
Optional[Text]
|
The resolved centroid method, or |
None
|
centroid_fallback
|
Optional[Text]
|
The reduce method used when the anchor node is not visible on an instance. |
None
|
user_instances_only
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
int
|
An integer crop size denoting the length of the side of the bounding
boxes that will contain the instances when cropped. The returned crop
size will be larger or equal to the input |
Source code in sleap/gui/learning/receptivefield.py
find_max_instance_bbox_size(labels, max_hw=None, user_instances_only=True)
¶
Find the maximum bounding box dimension across all instances in labels.
This is a local implementation that avoids importing sleap_nn (which would
trigger importing torch, adding ~2s to startup time). It mirrors
sleap_nn.data.instance_cropping.find_max_instance_bbox_size, except that
user_instances_only defaults to True here: the GUI form has no such
field, and training's config default is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
A |
required |
max_hw
|
Optional[Tuple[Optional[int], Optional[int]]]
|
The configured |
None
|
user_instances_only
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
float
|
The maximum bounding box dimension (max of width or height) across all instances. |
Source code in sleap/gui/learning/receptivefield.py
get_first_labeled_frame_and_instance(labels)
¶
Gets the first frame with ground truth labels and the first instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Optional[Labels]
|
The Labels object containing labeled frames. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Optional[ndarray], Optional[Instance], Optional[Video]]
|
A tuple of (frame_image, instance, video) where frame_image is a numpy array, instance is the first user instance and video is the video the frame was read from -- needed to know the pixel space the image is in, since crop sizes are measured after size matching. Returns (None, None, None) if no labeled frames with user instances are found. |
Source code in sleap/gui/learning/receptivefield.py
iter_required_crop_sizes(labels, max_hw=None, anchor_ind=None, centroid_method=None, centroid_fallback=None, user_instances_only=True)
¶
Yield the crop size each labeled instance needs to avoid being clipped.
Mirrors sleap_nn.data.instance_cropping.iter_required_crop_sizes. Crops
are centered on the instance's centroid, not on its bounding-box
midpoint, so the size an instance requires is twice its greatest node
offset from that centroid -- not its bounding-box extent. The two agree
only when the centroid happens to sit mid-bbox; for an anchor near one end
of the animal the required size approaches twice the extent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Labels
|
A |
required |
max_hw
|
Optional[Tuple[Optional[int], Optional[int]]]
|
The configured |
None
|
anchor_ind
|
Optional[int]
|
Index of the anchor node, or |
None
|
centroid_method
|
Optional[Text]
|
The resolved centroid method, or |
None
|
centroid_fallback
|
Optional[Text]
|
The reduce method used when the anchor node is not visible on an instance. |
None
|
user_instances_only
|
bool
|
When |
True
|
Yields:
| Type | Description |
|---|---|
|
The required crop side length, in size-matched pixels, per instance. Instances whose centroid is undefined are skipped. |
Source code in sleap/gui/learning/receptivefield.py
receptive_field_info_from_model_cfg(cfg)
¶
Gets receptive field and architecture information from model configuration.
Returns a dict with
- size: Receptive field size in pixels
- max_stride: Maximum stride (bottleneck)
- down_blocks: Number of encoder downsampling blocks
- convs_per_block: Convolutions per block (fixed at 2)
- kernel_size: Convolution kernel size (fixed at 3)
- output_stride: Minimum head output stride (for RF calculation)
- params: Total backbone parameter count
- params_formatted: Human-readable param count (e.g., "1.30M")
- head_features: List of (head_name, output_stride, channels) for each head
- backbone_type: Type of backbone (unet, convnext, swint)
- model_type: For convnext/swint, the model variant (tiny, small, base, large)
Source code in sleap/gui/learning/receptivefield.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 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 | |
resolve_anchor_ind(labels, anchor_part)
¶
Resolve an anchor node name to its index in the labels' skeleton.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Optional[Labels]
|
The labels whose first skeleton the name is resolved against,
as |
required |
anchor_part
|
Optional[Text]
|
The node name, or |
required |
Returns:
| Type | Description |
|---|---|
Optional[int]
|
The node index, or |
Source code in sleap/gui/learning/receptivefield.py
resolve_centroid_method(anchor_part=None, centroid_method=None, centroid_fallback=None)
¶
Resolve the head config's centroid knobs into (method, fallback).
Mirrors sleap_nn.data.instance_centroids.resolve_centroid_method, minus
its validation: this is a preview, so an unrecognized value degrades to the
historical default instead of raising over a config the training run will
report on properly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
anchor_part
|
Optional[Text]
|
The configured anchor node name, or |
None
|
centroid_method
|
Optional[Text]
|
One of |
None
|
centroid_fallback
|
Optional[Text]
|
The reduce method for a non-visible anchor node. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Text, Optional[Text]]
|
|
Source code in sleap/gui/learning/receptivefield.py
resolve_crop_centroid(model_cfg, labels=None)
¶
Resolve the centroid definition the crops will be centered on.
The GUI twin of ModelTrainer._resolve_crop_centroid. The crop center is
what makes a crop size sufficient or not, so the preview has to resolve it
the same way training does. Resolution is lenient: an anchor_part that
does not name a node in the skeleton degrades to its fallback rather than
raising, which is what training does too (degrade_anchor_if_unresolved).
The head is identified by which leaf actually carries the knobs rather than
by get_head_from_omegaconf, so a partial config -- which is what the form
hands over while the user is still filling it in -- resolves instead of
raising.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_cfg
|
OmegaConf
|
Model configuration OmegaConf from the model form. |
required |
labels
|
Optional[Labels]
|
Labels whose first skeleton the anchor name is resolved against. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Optional[int], Optional[Text], Text, Optional[Text]]
|
|
Source code in sleap/gui/learning/receptivefield.py
resolve_crop_inputs(data_cfg, model_cfg, labels=None)
¶
Resolve everything the crop-size computation needs from the GUI forms.
Three of these are not on the training form at all (max_height /
max_width, min_crop_size, user_instances_only), but training
resolves them from its config defaults and applies them, so the preview
reads them where present and falls back to the same defaults.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_cfg
|
OmegaConf
|
Data configuration OmegaConf from the data form. |
required |
model_cfg
|
OmegaConf
|
Model configuration OmegaConf from the model form. |
required |
labels
|
Optional[Labels]
|
Labels used to resolve the size-matcher target and the anchor. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict of keyword arguments for |
Source code in sleap/gui/learning/receptivefield.py
resolve_max_hw(labels, max_height=None, max_width=None)
¶
Resolve the size-matcher target the way the trainer's first pass does.
ModelTrainer._setup_preprocessing_config fills in an unset
max_height/max_width from the maximum over every training video, so
a project with mixed resolutions is size-matched whether or not the user
typed a target. The GUI form does not expose these fields at all, which
means leaving them unresolved would measure crops in native pixels and
reproduce the bug sleap-nn #748 fixed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Optional[Labels]
|
The labels whose videos supply the fallback target. |
required |
max_height
|
Optional[int]
|
The configured max height, or |
None
|
max_width
|
Optional[int]
|
The configured max width, or |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Tuple[int, int]]
|
|