deeplabcut.pose_estimation_pytorch.apis.videos
Classes:
| Name | Description |
|---|---|
VideoIterator |
A class to iterate over videos, with possible added context. |
Functions:
| Name | Description |
|---|---|
analyze_videos |
Makes prediction based on a trained network. |
video_inference |
Runs inference on a video. |
VideoIterator
Bases: VideoReader
A class to iterate over videos, with possible added context.
Methods:
| Name | Description |
|---|---|
set_crop |
Sets the cropping parameters for the video. |
Source code in deeplabcut/pose_estimation_pytorch/apis/videos.py
set_crop
Sets the cropping parameters for the video.
Source code in deeplabcut/pose_estimation_pytorch/apis/videos.py
analyze_videos
analyze_videos(
config: str,
videos: str | list[str],
video_extensions: str | Sequence[str] | None = None,
shuffle: int = 1,
trainingsetindex: int = 0,
save_as_csv: bool = False,
in_random_order: bool = False,
snapshot_index: int | str | None = None,
detector_snapshot_index: int | str | None = None,
device: str | None = None,
destfolder: str | None = None,
batch_size: int | None = None,
detector_batch_size: int | None = None,
dynamic: tuple[bool, float, int] = (False, 0.5, 10),
ctd_conditions: dict | CondFromModel | None = None,
ctd_tracking: bool | dict | CTDTrackingConfig = False,
top_down_dynamic: dict | None = None,
modelprefix: str = "",
use_shelve: bool = False,
robust_nframes: bool = False,
transform: Compose | None = None,
auto_track: bool | None = True,
n_tracks: int | None = None,
animal_names: list[str] | None = None,
calibrate: bool = False,
identity_only: bool | None = False,
overwrite: bool = False,
cropping: list[int] | None = None,
save_as_df: bool = False,
show_gpu_memory: bool = False,
inference_cfg: InferenceConfig | dict | None = None,
) -> str
Makes prediction based on a trained network.
The index of the trained network is specified by parameters in the config file (in particular the variable 'snapshot_index').
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
full path of the config.yaml file for the project |
required |
|
str | list[str]
|
a str (or list of strings) containing the full paths to videos for analysis or a path to the directory, where all the videos with same extension are stored. |
required |
|
str | Sequence[str] | None
|
Controls how |
None
|
|
int
|
An integer specifying the shuffle index of the training dataset used for training the network. |
1
|
|
int
|
Integer specifying which TrainingsetFraction to use. |
0
|
|
bool
|
For multi-animal projects and when |
False
|
|
bool
|
Whether or not to analyze videos in a random order. This is
only relevant when specifying a video directory in |
False
|
|
str | None
|
the device to use for video analysis |
None
|
|
str | None
|
specifies the destination folder for analysis data. If |
None
|
|
int | str | None
|
index (starting at 0) of the snapshot to use to analyze the videos. To evaluate the last one, use -1. For example if we have - snapshot-0.pt - snapshot-50.pt - snapshot-100.pt - snapshot-best.pt and we want to evaluate snapshot-50.pt, snapshotindex should be 1. If None, the snapshot index is loaded from the project configuration. |
None
|
|
int | str | None
|
(only for top-down models) index of the detector
snapshot to use, used in the same way as |
None
|
|
tuple[bool, float, int]
|
(state, detection threshold, margin) triplet. 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). |
(False, 0.5, 10)
|
|
dict | CondFromModel | None
|
Only for CTD models. If None, the configuration for the condition provider will be loaded from the pytorch_config file (under the "inference": "conditions"). If the ctd_conditions is given as a dict, creates a CondFromModel from the dict. Otherwise, a CondFromModel can be given directly. Example configuration: |
None
|
|
bool | dict | CTDTrackingConfig
|
Only for CTD models. Conditional top-down models can be used to directly track individuals. Poses from frame T are given as conditions for frame T+1. This also means a BU model is only needed to "initialize" the pose in the first frame, and for the remaining frames only the CTD model is needed. To configure conditional pose tracking differently, you can pass a CTDTrackingConfig instance. |
False
|
|
dict | None
|
Configuration for a top-down dynamic cropper. If None,
top-down dynamic cropping is not used. Can only be used when running
inference on a single animal. If an empty dict is given, default parameters
are used. This is not recommended, as parameters should be customized for
your data. Possible parameters are:
"top_down_crop_size": tuple[int, int]
The (width, height) to resize the crop to. If not specified, will
be loaded from the |
None
|
|
str
|
directory containing the deeplabcut models to use when evaluating the network. By default, they are assumed to exist in the project folder. |
''
|
|
int | None
|
the batch size to use for inference. Takes the value from the project config as a default. |
None
|
|
int | None
|
the batch size to use for detector inference. Takes the value from the project config as a default. |
None
|
|
Compose | None
|
Optional custom transforms to apply to the video |
None
|
|
bool
|
Overwrite any existing videos |
False
|
|
bool
|
By default, data are dumped in a pickle file at the end of the video analysis. Otherwise, data are written to disk on the fly using a "shelf"; i.e., a pickle-based, persistent, database-like object by default, resulting in constant memory footprint. |
False
|
|
bool
|
Evaluate a video's number of frames in a robust manner. This option is slower (as the whole video is read frame-by-frame), but does not rely on metadata, hence its robustness against file corruption. |
False
|
|
bool | None
|
By default, tracking and stitching are automatically performed, producing the final h5 data file. This is equivalent to the behavior for single-animal projects. If |
True
|
|
int | None
|
Number of tracks to reconstruct. By default, taken as the number of individuals defined in the config.yaml. Another number can be passed if the number of animals in the video is different from the number of animals the model was trained on. |
None
|
|
list[str] | None
|
If you want the names given to individuals in the labeled data
file, you can specify those names as a list here. If given and |
None
|
|
bool | None
|
sub-call for auto_track. If |
False
|
|
list[int] | None
|
List of cropping coordinates as [x1, x2, y1, y2]. Note that the same
cropping parameters will then be used for all videos. If different video
crops are desired, run |
None
|
|
bool
|
Cannot be used when |
False
|
|
bool
|
When true, the tqdm progress bar shows the gpu memory usage of the current process. |
False
|
|
InferenceConfig | dict | None
|
InferenceConfig to use
If None, the configuration from the |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The scorer used to analyze the videos |
Source code in deeplabcut/pose_estimation_pytorch/apis/videos.py
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 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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 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 | |
video_inference
video_inference(
video: str | Path | VideoIterator,
pose_runner: InferenceRunner,
detector_runner: InferenceRunner | None = None,
cropping: list[int] | None = None,
shelf_writer: ShelfWriter | None = None,
robust_nframes: bool = False,
show_gpu_memory: bool = False,
) -> list[dict[str, np.ndarray]]
Runs inference on a video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path | VideoIterator
|
The video to analyze |
required |
|
InferenceRunner
|
The pose runner to run inference with |
required |
|
InferenceRunner | None
|
When the pose model is a top-down model, a detector runner can be given to obtain bounding boxes for the video. If the pose model is a top-down model and no detector_runner is given, the bounding boxes must already be set in the VideoIterator (see examples). |
None
|
|
list[int] | None
|
Optionally, video inference can be run on a cropped version of the
video. To do so, pass a list containing 4 elements to specify which area
of the video should be analyzed: |
None
|
|
ShelfWriter | None
|
By default, data are dumped in a pickle file at the end of the video analysis. Passing a shelf manager writes data to disk on-the-fly using a "shelf" (a pickle-based, persistent, database-like object by default, resulting in constant memory footprint). The returned list is then empty. |
None
|
|
bool
|
Evaluate a video's number of frames in a robust manner. This option is slower (as the whole video is read frame-by-frame), but does not rely on metadata, hence its robustness against file corruption. |
False
|
|
bool
|
When true, the tqdm progress bar shows the gpu memory usage of the current process. |
False
|
Returns:
| Type | Description |
|---|---|
list[dict[str, ndarray]]
|
Predictions for each frame in the video. If a shelf_manager is given, this list will be empty and the predictions will exclusively be stored in the file written by the shelf. |
Examples:
Bottom-up video analysis:
>>> import deeplabcut.pose_estimation_pytorch as pep
>>> from deeplabcut.core.config import read_config_as_dict
>>> model_cfg = read_config_as_dict("pytorch_config.yaml")
>>> runner = pep.get_pose_inference_runner(model_cfg, "snapshot.pt")
>>> video_predictions = pep.video_inference("video.mp4", runner)
>>>
Top-down video analysis:
>>> import deeplabcut.pose_estimation_pytorch as pep
>>> from deeplabcut.core.config import read_config_as_dict
>>> model_cfg = read_config_as_dict("pytorch_config.yaml")
>>> runner = pep.get_pose_inference_runner(model_cfg, "snapshot.pt")
>>> d_runner = pep.get_pose_inference_runner(model_cfg, "snapshot-detector.pt")
>>> video_predictions = pep.video_inference("video.mp4", runner, d_runner)
>>>
Top-Down pose estimation with pre-computed bounding boxes:
>>> import numpy as np
>>> import deeplabcut.pose_estimation_pytorch as pep
>>> from deeplabcut.core.config import read_config_as_dict
>>>
>>> video_iterator = pep.VideoIterator("video.mp4")
>>> video_iterator.set_context([
>>> { # frame 1 context
>>> "bboxes": np.array([[12, 17, 4, 5]]), # format (x0, y0, w, h)
>>> },
>>> { # frame 1 context
>>> "bboxes": np.array([[12, 17, 4, 5], [18, 92, 54, 32]]),
>>> },
>>> ...
>>> ])
>>> model_cfg = read_config_as_dict("pytorch_config.yaml")
>>> runner = pep.get_pose_inference_runner(model_cfg, "snapshot.pt")
>>> video_predictions = pep.video_inference(video_iterator, runner)
>>>
Source code in deeplabcut/pose_estimation_pytorch/apis/videos.py
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 | |