deeplabcut.pose_estimation_pytorch.runners.dynamic_cropping
Modules to dynamically crop individuals out of videos to improve video analysis.
Classes:
| Name | Description |
|---|---|
DynamicCropper |
If the state is true, then dynamic cropping will be performed. That means that if |
TopDownDynamicCropper |
Dynamic cropping for top-down models used on single animal videos. |
DynamicCropper
dataclass
If the state is true, then dynamic cropping will be performed. That means that if an object is detected (i.e. any body part > detection threshold), then object boundaries are computed according to the smallest/largest x position and smallest/largest y position of all body parts. This window is expanded by the margin and from then on only the posture within this crop is analyzed (until the object is lost, i.e. < detection threshold). The current position is utilized for updating the crop window for the next frame (this is why the margin is important and should be set large enough given the movement of the animal).
Attributes:
| Name | Type | Description |
|---|---|---|
threshold |
float
|
float The threshold score for bodyparts above which an individual is deemed to have been detected. |
margin |
int
|
int The margin used to expand an individuals bounding box before cropping it. |
Examples:
>>> import deeplabcut.pose_estimation_pytorch.models as models
>>>
>>> model: models.PoseModel
>>> frames: torch.Tensor # shape (num_frames, 3, H, W)
>>>
>>> dynamic = DynamicCropper(threshold=0.6, margin=25)
>>> predictions = []
>>> for image in frames:
>>> image = dynamic.crop(image)
>>>
>>> outputs = model(image)
>>> preds = model.get_predictions(outputs)
>>> pose = preds["bodypart"]["poses"]
>>>
>>> dynamic.update(pose)
>>> predictions.append(pose)
>>>
Methods:
| Name | Description |
|---|---|
build |
Builds the DynamicCropper based on the given parameters. |
crop |
Crops an input image according to the dynamic cropping parameters. |
reset |
Resets the DynamicCropper to not crop the next frame. |
update |
Updates the dynamic crop according to the pose model output. |
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 | |
build
staticmethod
Builds the DynamicCropper based on the given parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
bool
|
Whether dynamic cropping should be used |
required |
|
float
|
The threshold score for bodyparts above which an individual is deemed to have been detected. |
required |
|
int
|
The margin used to expand an individuals bounding box before cropping it. |
required |
Returns:
| Type | Description |
|---|---|
Optional[DynamicCropper]
|
None if dynamic is False DynamicCropper to use if dynamic is True |
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
crop
Crops an input image according to the dynamic cropping parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
The image to crop, of shape (1, C, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The cropped image of shape (1, C, H', W'), where [H', W'] is the size of the crop. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if there is not exactly one image in the batch to crop, or if
|
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
reset
update
Updates the dynamic crop according to the pose model output.
Uses the pose predicted by the model to update the dynamic crop parameters for the next frame. Scales the pose predicted in the cropped image back to the original image space and returns it.
This method modifies the pose tensor in-place; so pass a copy of the tensor if you need to keep the original values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
The pose that was predicted by the pose estimation model in the cropped image coordinate space. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The pose, with coordinates updated to the full image space. |
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
TopDownDynamicCropper
Bases: DynamicCropper
Dynamic cropping for top-down models used on single animal videos.
The TopDownDynamicCropper can be used instead of an object detector to analyze
videos containing a single animal with top-down models.
At frame 0, the full frame is split into (n, m) image patches, with a given overlap between the patches. Patches are then - Resized to the input size required by the model with a top-down crop. - Stacked into a batch and given to the pose estimation model - The output poses for each patch are post-processed: the patch containing the highest average score prediction is selected as the patch containing the individual, and the pose from that patch is selected as the predicted pose.
At frame n, one of two things can happen: - If the individual was successfully detected at frame n - 1, a bounding box is generated from the predicted pose and used as the bounding box for the next frame. - If the individual was not detected at frame n - 1, patches are cropped as in frame 0 and the pose selected as in frame 0
An individual is considered to be successfully detected if
- at least
min_hq_keypointskeypoint have scores above thethreshold
The bounding box is generated from the keypoints (either from all keypoints or only the ones above the threshold) with a margin around the keypoints. If the bounding box is smaller than a set minimum size, it is expanded to that size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
tuple[int, int]
|
The (width, height) of to resize crops to. |
required |
|
tuple[int, int]
|
The number of patches along the (width, height) of the images when no crop is found. |
(3, 2)
|
|
int
|
The amount of overlapping pixels between adjacent patches. |
50
|
|
tuple[int, int]
|
The minimum (width, height) for a detected bounding box. If the bounding box computed from the keypoints is smaller than this value, it will be expanded to these values. |
(50, 50)
|
|
float
|
The threshold score for bodyparts above which an individual is considered to be detected. |
0.6
|
|
int
|
The margin to add around keypoints when generating bounding boxes. |
25
|
|
int
|
The minimum number of keypoints above the threshold required for the individual to be considered detected and a bounding box to be computed from the pose. |
2
|
|
bool
|
If True, only keypoints above the score threshold will be used to compute the bounding boxes. |
False
|
|
bool
|
Useful for debugging. When True, all crops are stored in the
|
False
|
|
Key-word arguments passed to the DynamicCropper base class. |
{}
|
Attributes:
| Name | Type | Description |
|---|---|---|
min_bbox_size |
tuple[int, int]. The minimum (width, height) for a detected bounding box. If the bounding box computed from the keypoints is smaller than this value, it will be expanded to these values. |
|
min_hq_keypoints |
int. The minimum number of keypoints above the threshold required for the individual to be considered detected and a bounding box to be computed from the pose. |
|
bbox_from_hq |
bool. If True, only keypoints above the score threshold will be used to compute the bounding boxes. |
|
store_crops |
bool. Useful for debugging. When True, all crops are stored in the
|
|
crop_history |
list[list[tuple[int, int, int, int]]. Empty list if |
Methods:
| Name | Description |
|---|---|
crop |
Crops an input image according to the dynamic cropping parameters. |
generate_patches |
Generates patch coordinates for splitting an image. |
num_patches |
Returns: the total number of patches created for an image. |
patch_counts |
Returns: the number of patches created for an image. |
split_array |
Splits an array into n segments of equal size, where the overlap between each |
update |
Updates the dynamic crop according to the pose model output. |
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 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 | |
crop
Crops an input image according to the dynamic cropping parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
The image to crop, of shape (1, C, H, W). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The cropped image of shape (B, C, H', W'), where [H', W'] is the size of the crop. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
if there is not exactly one image in the batch to crop, or if
|
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
generate_patches
Generates patch coordinates for splitting an image.
Returns:
| Type | Description |
|---|---|
list[tuple[int, int, int, int]]
|
A list of patch coordinates as tuples (x0, y0, x1, y1). |
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
num_patches
patch_counts
split_array
staticmethod
Splits an array into n segments of equal size, where the overlap between each segment is at least a given value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
int
|
The size of the array. |
required |
|
int
|
The number of segments to split the array into. |
required |
|
int
|
The minimum overlap between each segment. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
(start_index, end_index) pairs for each segment. The end index is exclusive. |
Source code in deeplabcut/pose_estimation_pytorch/runners/dynamic_cropping.py
update
Updates the dynamic crop according to the pose model output.
Uses the pose predicted by the model to update the dynamic crop parameters for the next frame. Scales the pose predicted in the cropped image back to the original image space and returns it.
This method modifies the pose tensor in-place; so pass a copy of the tensor if you need to keep the original values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Tensor
|
The pose that was predicted by the pose estimation model in the cropped image coordinate space. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The pose, with coordinates updated to the full image space. |