model.py
Code adapted from the Mathis Lab MIT License Copyright (c) 2022 Mackenzie Mathis DataJoint Schema for DeepLabCut 2.x, Supports 2D and 3D DLC via triangulation.
activate(model_schema_name, *, create_schema=True, create_tables=True, linking_module=None)
¶
Activate this schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_schema_name |
str
|
schema name on the database server |
required |
create_schema |
bool
|
when True (default), create schema in the database if it does not yet exist. |
True
|
create_tables |
bool
|
when True (default), create schema tables in the database if they do not yet exist. |
True
|
linking_module |
str
|
a module (or name) containing the required dependencies. |
None
|
Dependencies:
Upstream tables
Session: A parent table to VideoRecording, identifying a recording session. Equipment: A parent table to VideoRecording, identifying a recording device.
Functions
get_dlc_root_data_dir(): Returns absolute path for root data director(y/ies) with all behavioral recordings, as (list of) string(s). get_dlc_processed_data_dir(): Optional. Returns absolute path for processed data. Defaults to session video subfolder.
Source code in element_deeplabcut/model.py
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 |
|
get_dlc_root_data_dir()
¶
Pulls relevant func from parent namespace to specify root data dir(s).
It is recommended that all paths in DataJoint Elements stored as relative paths, with respect to some user-configured "root" director(y/ies). The root(s) may vary between data modalities and user machines. Returns a full path string or list of strings for possible root data directories.
Source code in element_deeplabcut/model.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
get_dlc_processed_data_dir()
¶
Pulls relevant func from parent namespace. Defaults to DLC's project /videos/.
Method in parent namespace should provide a string to a directory where DLC output files will be stored. If unspecified, output files will be stored in the session directory 'videos' folder, per DeepLabCut default.
Source code in element_deeplabcut/model.py
100 101 102 103 104 105 106 107 108 109 110 |
|
VideoRecording
¶
Bases: dj.Manual
Set of video recordings for DLC inferences.
Attributes:
Name | Type | Description |
---|---|---|
Session |
foreign key
|
Session primary key. |
recording_id |
int
|
Unique recording ID. |
Device |
foreign key
|
Device table primary key, used for default output directory path information. |
Source code in element_deeplabcut/model.py
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 |
|
File
¶
Bases: dj.Part
File IDs and paths associated with a given recording_id
Attributes:
Name | Type | Description |
---|---|---|
VideoRecording |
foreign key
|
Video recording primary key. |
file_path |
varchar(255)
|
file path of video, relative to root data dir. |
Source code in element_deeplabcut/model.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
|
RecordingInfo
¶
Bases: dj.Imported
Automated table with video file metadata.
Attributes:
Name | Type | Description |
---|---|---|
VideoRecording |
foreign key
|
Video recording key. |
px_height |
smallint
|
Height in pixels. |
px_width |
smallint
|
Width in pixels. |
nframes |
int
|
Number of frames. |
fps |
int
|
Optional. Frames per second, Hz. |
recording_datetime |
datetime
|
Optional. Datetime for the start of recording. |
recording_duration |
float
|
video duration (s) from nframes / fps. |
Source code in element_deeplabcut/model.py
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 |
|
key_source
property
¶
Defines order of keys for make function when called via populate()
make(key)
¶
Populates table with video metadata using CV2.
Source code in element_deeplabcut/model.py
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 |
|
BodyPart
¶
Bases: dj.Lookup
Body parts tracked by DeepLabCut models
Attributes:
Name | Type | Description |
---|---|---|
body_part |
varchar(32)
|
Body part short name. |
body_part_description |
varchar(1000),optional
|
Full description |
Source code in element_deeplabcut/model.py
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 |
|
extract_new_body_parts(dlc_config, verbose=True)
classmethod
¶
Returns list of body parts present in dlc config, but not BodyPart table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dlc_config |
str or dict
|
path to a config.y*ml, or dict of such contents. |
required |
verbose |
bool
|
Default True. Print both existing and new items to console. |
True
|
Source code in element_deeplabcut/model.py
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 |
|
insert_from_config(dlc_config, descriptions=None, prompt=True)
classmethod
¶
Insert all body parts from a config file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dlc_config |
str or dict
|
path to a config.y*ml, or dict of such contents. |
required |
descriptions |
list
|
Optional. List of strings describing new body parts. |
None
|
prompt |
bool
|
Optional, default True. Prompt for confirmation before insert. |
True
|
Source code in element_deeplabcut/model.py
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 |
|
Model
¶
Bases: dj.Manual
DeepLabCut Models applied to generate pose estimations.
Attributes:
Name | Type | Description |
---|---|---|
model_name |
varchar(64)
|
User-friendly model name. |
task |
varchar(32)
|
Task in the config yaml. |
date |
varchar(16)
|
Date in the config yaml. |
iteration |
int
|
Iteration/version of this model. |
snapshotindex |
int
|
Which snapshot for prediction (if -1, latest). |
shuffle |
int
|
Which shuffle of the training dataset. |
trainingsetindex |
int
|
Which training set fraction to generate model. |
scorer |
varchar(64)
|
Scorer/network name - DLC's GetScorerName(). |
config_template |
longblob
|
Dictionary of the config for analyze_videos(). |
project_path |
varchar(255)
|
DLC's project_path in config relative to root. |
model_prefix |
varchar(32)
|
Optional. Prefix for model files. |
model_description |
varchar(1000)
|
Optional. User-entered description. |
TrainingParamSet |
foreign key
|
Optional. Training parameters primary key. |
Note
Models are uniquely identified by the union of task, date, iteration, shuffle, snapshotindex, and trainingsetindex.
Source code in element_deeplabcut/model.py
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 |
|
BodyPart
¶
Bases: dj.Part
Body parts associated with a given model
Attributes:
Name | Type | Description |
---|---|---|
body_part |
varchar(32)
|
Short name. Also called joint. |
body_part_description |
varchar(1000)
|
Optional. Longer description. |
Source code in element_deeplabcut/model.py
339 340 341 342 343 344 345 346 347 348 349 |
|
insert_new_model(model_name, dlc_config, *, shuffle, trainingsetindex, project_path=None, model_description='', model_prefix='', paramset_idx=None, prompt=True, params=None)
classmethod
¶
Insert new model into the dlc.Model table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name |
str
|
User-friendly name for this model. |
required |
dlc_config |
str or dict
|
path to a config.y*ml, or dict of such contents. |
required |
shuffle |
int
|
Shuffled or not as 1 or 0. |
required |
trainingsetindex |
int
|
Index of training fraction list in config.yaml. |
required |
model_description |
str
|
Optional. Description of this model. |
''
|
model_prefix |
str
|
Optional. Filename prefix used across DLC project |
''
|
paramset_idx |
int
|
Optional. Index from the TrainingParamSet table |
None
|
prompt |
bool
|
Optional. Prompt the user with all info before inserting. |
True
|
params |
dict
|
Optional. If dlc_config is path, dict of override items |
None
|
Source code in element_deeplabcut/model.py
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 |
|
ModelEvaluation
¶
Bases: dj.Computed
Performance characteristics model calculated by deeplabcut.evaluate_network
Attributes:
Name | Type | Description |
---|---|---|
Model |
foreign key
|
Model name. |
train_iterations |
int
|
Training iterations. |
train_error |
float
|
Optional. Train error (px). |
test_error |
float
|
Optional. Test error (px). |
p_cutoff |
float
|
Optional. p-cutoff used. |
train_error_p |
float
|
Optional. Train error with p-cutoff. |
test_error_p |
float
|
Optional. Test error with p-cutoff. |
Source code in element_deeplabcut/model.py
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 |
|
make(key)
¶
.populate() method will launch evaluation for each unique entry in Model.
Source code in element_deeplabcut/model.py
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 |
|
PoseEstimationTask
¶
Bases: dj.Manual
Staging table for pairing of video recording and model before inference.
Attributes:
Name | Type | Description |
---|---|---|
VideoRecording |
foreign key
|
Video recording key. |
Model |
foreign key
|
Model name. |
task_mode |
load or trigger
|
Optional. Default load. Or trigger computation. |
pose_estimation_output_dir |
varchar(255)
|
Optional. Output dir relative to get_dlc_root_data_dir. |
pose_estimation_params |
longblob
|
Optional. Params for DLC's analyze_videos params, if not default. |
Source code in element_deeplabcut/model.py
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 |
|
infer_output_dir(key, relative=False, mkdir=False)
classmethod
¶
Return the expected pose_estimation_output_dir.
Spaces in model name are replaced with hyphens. Based on convention: / video_dir / Device_{}Recording{}Model{}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
dict
|
DataJoint key specifying a pairing of VideoRecording and Model. |
required |
relative |
bool
|
Report directory relative to get_dlc_processed_data_dir(). |
False
|
mkdir |
bool
|
Default False. Make directory if it doesn't exist. |
False
|
Source code in element_deeplabcut/model.py
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 |
|
insert_estimation_task(key, task_mode='trigger', params=None, relative=True, mkdir=True, skip_duplicates=False)
classmethod
¶
Insert PoseEstimationTask in inferred output dir.
Based on the convention / video_dir / device_{}recording{}model{}
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
dict
|
DataJoint key specifying a pairing of VideoRecording and Model. |
required |
task_mode |
bool
|
Default 'trigger' computation. Or 'load' existing results. |
'trigger'
|
params |
dict
|
Optional. Parameters passed to DLC's analyze_videos: videotype, gputouse, save_as_csv, batchsize, cropping, TFGPUinference, dynamic, robust_nframes, allow_growth, use_shelve |
None
|
relative |
bool
|
Report directory relative to get_dlc_processed_data_dir(). |
True
|
mkdir |
bool
|
Default False. Make directory if it doesn't exist. |
True
|
Source code in element_deeplabcut/model.py
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 |
|
PoseEstimation
¶
Bases: dj.Computed
Results of pose estimation.
Attributes:
Name | Type | Description |
---|---|---|
PoseEstimationTask |
foreign key
|
Pose Estimation Task key. |
post_estimation_time |
datetime
|
time of generation of this set of DLC results. |
Source code in element_deeplabcut/model.py
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 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
|
BodyPartPosition
¶
Bases: dj.Part
Position of individual body parts by frame index
Attributes:
Name | Type | Description |
---|---|---|
PoseEstimation |
foreign key
|
Pose Estimation key. |
Model.BodyPart |
foreign key
|
Body Part key. |
frame_index |
longblob
|
Frame index in model. |
x_pos |
longblob
|
X position. |
y_pos |
longblob
|
Y position. |
z_pos |
longblob
|
Optional. Z position. |
likelihood |
longblob
|
Model confidence. |
Source code in element_deeplabcut/model.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 |
|
make(key)
¶
.populate() method will launch training for each PoseEstimationTask
Source code in element_deeplabcut/model.py
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 720 721 722 723 724 725 726 |
|
get_trajectory(key, body_parts='all')
classmethod
¶
Returns a pandas dataframe of coordinates of the specified body_part(s)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
dict
|
A DataJoint query specifying one PoseEstimation entry. |
required |
body_parts |
list
|
Body parts as a list. If "all", all joints |
'all'
|
Returns:
Name | Type | Description |
---|---|---|
df |
pd.DataFrame
|
multi index pandas dataframe with DLC scorer names, body_parts and x/y coordinates of each joint name for a camera_id, similar to output of DLC dataframe. If 2D, z is set of zeros |
Source code in element_deeplabcut/model.py
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
|
str_to_bool(value)
¶
Return whether the provided string represents true. Otherwise false.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
any
|
Any input |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if value in ("y", "yes", "t", "true", "on", "1") |
Source code in element_deeplabcut/model.py
767 768 769 770 771 772 773 774 775 776 777 778 779 780 |
|