Skip to content

Conversation

@MMathisLab
Copy link
Member

@MMathisLab MMathisLab commented Jun 13, 2024

New main backend Deep Learning Library (Engine) for DeepLabCut: PyTorch 🔥

This post serves to provide the source code of DeepLabCut 3.0, which marks the first major release change since 2 (in late 2018). With this change, we move to PyTorch.

PyTorch combines the GPU-accelerated backend libraries from Torch with an intuitive Python frontend that focuses on rapid prototyping, readable code, and support for the widest possible variety of deep learning models.

It is high-performance, easier to use, easier to install, and developer-friendly.

img-1
Image reference https://pytorch.org/features/

Why:

We wanted to modernize our stack and also make it more user-friendly for all our stakeholders. Namely, the stakeholders in DeepLabCut are:

  • end users in life sciences
  • computer vision researchers
  • animal pose estimation developers

What is new:

We have dedicated documentation that walks you through the new options
(note, these links point to a PR, but will be in main docs upon merger)

What to expect:

Importantly, the high-level API stays the same 🪄.
The dev team worked very hard to keep both DeepLabCut PyTorch and (for the time) DeepLabCut TensorFlow for you and keep the workflow identical. Truly, you can use the same 12 steps you know and love! ❤️

In short:

Screenshot 2024-06-12 at 16 12 29 (1) Screenshot 2024-06-12 at 16 12 54

You have the GUI, with the same launch code:

python -m deeplabcut

And at the command line interface you have the same 12 core steps!

Get started by testing this PR:

First, this is not yet on pypi, so you will need to test this branch 💜. This is straightforward to do:

(1) We recommend building a new conda environment. In your anaconda terminal, please type:

conda create -n deeplabcut3 python=3.10

You will be prompted (y/n) to install, and then wait for the magic to happen. At the end, check the terminal, it should prompt you to then type:

conda activate deeplabcut3

Now, we are going to install the core dependencies. The way this works is that there are "package managers" such as conda itself and python's pip. We are going to deploy a mix based on what we know works across ooperating systems.

Install PyTorch

To select the right version, head to the “Install PyTorch” instructions in the official PyTorch Docs. Select your desired PyTorch build, operating system, and Python as the language. Select your compute platform (either a CUDA version or CPU only). Then, use the command to install the PyTorch package. Below are a few possible examples:

  • GPU version of PyTorch for Linux, for CUDA 11.8
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu118
  • CPU only version of PyTorch for Linux, using the latest version
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu

Install DeepLabCut PyTorch

Alright! Next, we will install Tables (also called pytables), which is a package to read the HDF5 files that make up the backbone of data management in DeepLabCut, then we will install all the deeplabcut source code 🔥. Please type:

conda install -c conda-forge pytables==3.8.0
pip install "git+https://github.com/DeepLabCut/DeepLabCut.git@pytorch_dlc#egg=deeplabcut[gui,modelzoo,wandb]"

Then launch DeepLabCut! for example, to use this in the GUI:

python -m deeplabcut

You will see version 3.0.0rc1 loading: Loading DLC 3.0.0rc1...

Please also see the related PR with the Documentation Updates: #2605

Benchmarking:

This will be continually updated, but in short, here is the report for the classic DLC-openfield dataset:

Benchmarking Results on DeepLabCut-Openfield data:

Please note the exact data splits are not identical to the TF 1.0 splits, but we provide metrics for ease of understanding the TL;DR, which is, we have better performance with the PyTorch Engine than the TensorFlow engine. The 0.5 data ratio below means training with 506 randomly drawn frames.

Engine Method Data Ratio RMSE mAP
HUMAN 👤 human GT labeling (Mathis et al. 2018) 1 2.69 n/a
TF 1.0 🟠 ImageNet Transfer-learning ResNet-50* 0.5 3.09 n/a
TF 1.0 🟠 ImageNet Transfer-learning ResNet-101* 0.5 2.90 n/a
TF 1.0 🟠 ImageNet Transfer-learning ResNet-101is* 0.5 2.88 n/a
TF 2.12 🟠 ImageNet Transfer-learning ResNet-50 v2.3.10 0.5 2.77 n/a
PyTorch 🔥 ImageNet Transfer-learning ResNet-50 0.5 2.01 99.38
PyTorch 🔥 ImageNet Transfer-learning HRNet-w32 0.5 2.36 99.01
PyTorch 🔥 SuperAnimal Transfer-learning HRNet-w32 0.5 2.41 99.12
PyTorch 🔥 SuperAnimal Naive Finetuning HRNet-w32 0.5 2.50 99.33
PyTorch 🔥 SuperAnimal Memory-replay HRNet-w32 0.5 2.52 99.67

*Values from Mathis et al. 2018: https://www.nature.com/articles/s41593-018-0209-y
ResNet-50, 3.09 ± 0.04; ResNet-101, 2.90 ± 0.09; ResNet-101 with intermediate supervision, 2.88 ± 0.06; pixel mean ± s.e.m

Here are more benchmarking results on DLC-openfield considering various data ratios. The Data Ratio is the % of frames used for training. 0.01 is 10, 0.10 is 101, 0.5 is 506, 1 is 1012 frames. To note, for more information on these various SuperAnimal methods, please see Ye et al. 2023/2024 (arxiv/Nature Communications).

Engine Method Data Ratio RMSE mAP
TF 2.12 🟠 ImageNet Transfer-learning ResNet-50* (v2.3.10) 1 2.50 n/a
PyTorch 🔥 ImageNet Transfer-learning HRNet-w32 0.01 6.66 81.73
PyTorch 🔥 ImageNet Transfer-learning HRNet-w32 0.1 3.44 94.82
PyTorch 🔥 ImageNet Transfer-learning HRNet-w32 0.5 2.36 99.01
PyTorch 🔥 ImageNet Transfer-learning HRNet-w32 1 2.31 99.33
PyTorch 🔥 SuperAnimal Transfer-learning HRNet-w32 0.01 5.27 91.99
PyTorch 🔥 SuperAnimal Transfer-learning HRNet-w32 0.1 2.88 96.62
PyTorch 🔥 SuperAnimal Transfer-learning HRNet-w32 0.5 2.41 99.12
PyTorch 🔥 SuperAnimal Transfer-learning HRNet-w32 1 2.17 99.33
PyTorch 🔥 SuperAnimal Naive Finetuning HRNet-w32 0.01 4.52 94.28
PyTorch 🔥 SuperAnimal Naive Finetuning HRNet-w32 0.1 2.98 98.09
PyTorch 🔥 SuperAnimal Naive Finetuning HRNet-w32 0.5 2.50 99.33
PyTorch 🔥 SuperAnimal Naive Finetuning HRNet-w32 1 2.35 99.01
PyTorch 🔥 SuperAnimal Memory-replay HRNet-w32 0.01 4.94 93.38
PyTorch 🔥 SuperAnimal Memory-replay HRNet-w32 0.1 3.32 97.2
PyTorch 🔥 SuperAnimal Memory-replay HRNet-w32 0.5 2.52 99.67
PyTorch 🔥 SuperAnimal Memory-replay HRNet-w32 1 2.64 98.37

Benchmarking Results tri-mouse multi-animal:

Engine Model RMSE mAP
TensorFlow 🟠 DLCRNet_ms4 (30k) 3.55 93.2
TensorFlow 🟠 EfficientNet B7_s4 (30k) 4.93 94.3
TensorFlow 🟠 ResNet50 (30k) 3.97 92.8
PyTorch 🔥 DEKR w32 4.13 96.2
PyTorch 🔥 Top-Down HRNet w32 4.10 95.0

Contributors:

This has been a huge team effort across several years. We will write more about this process in a forthcoming blog post, but in the meantime, we greatly thank the community, users, and these core authors:

DeepLabCut 3.0 Toolbox
Mackenzie Mathis, [email protected] | https://github.com/MMathisLab
Alexander Mathis, [email protected] | https://github.com/AlexEMG
Niels Poulsen, [email protected] | https://github.com/n-poulsen
Shaokai Ye, [email protected] | https://github.com/yeshaokai
Anastasiia Filippova, [email protected] | https://github.com/nastya236
Quentin Macé | https://github.com/QuentinJGMace
Jessy Lauer, [email protected] | https://github.com/jeylau
Lucas Stoffl, [email protected] | https://github.com/LucZot

We also greatly thank the 2023 DeepLabCut AI Residents who contributed:
Anna Teruel-Sanchis | https://github.com/anna-teruel
Riza Rae Pineda | https://github.com/rizarae-p
Konrad Danielewski | https://github.com/KonradDanielewski

Products:
PyTorch backend for DeepLabCut
Expanded SuperAnimal capabilities
New model architectures (WIP: stay tuned, but includes BUCTD)

n-poulsen and others added 30 commits March 19, 2024 15:06
* improved augmenter selection
* improved pytorch config docs
* added documentation for missing parameters
* remove unused param from DLCParams
* bug fix: add a shuffle
- makes usage of TQDM in evaluate_assembly optional (so it doesn't log during training)
- only computes rmse_pcutoff if there are keypoints above the cutoff (otherwise a warning was printed)
- force the logger updates when starting to train, otherwise it could be buggy
- don't use `verbose`for the LR scheduler as it's deprecated
- log a single time will all metrics instead of multiple
- logs metrics and losses to different prefixes so they are put in the correct panels on wandb
- log input images, target and output heatmaps to WandB
* fixed superanimal video inference
* added demo notebook for transfer learning
updated code to get scorer for pytorch models, fixed bugs for video analysis
* Fix NaN centroids with 0 likelihood
* Minor compatibility fixes
* Improved error message
* Fix config dict key setting
* Delete redundant neighbor search implementations
…n data augmentation, add tests (#190)

* bug fix: incorrectly reading snapshot index from config in analyze videos
* added files for backwards compatibility (e.g. when loading pickles)
* update default top down augmentation
* fixed tokenpose cfg
* added tests for topdown config creation, fixed bug
* add coarse dropout test, pin albu
* switch default resnet50 to GN model
* changed default resnet output stride to 16
* freeze BN stats but not weights as default
* fixed tests
* fix topdown aug
* Loading SuperAnimal weights for model training
* various bug fixes
* bug fix when creating train dataset, updated code to use model stride correctly
* stride can be float
* prepare for 3.0.0rc1 (bump version)
* Update version.py
* Update reinstall.sh
* Update NOTICE.yml (pytorch license)
Co-authored-by: Niels Poulsen <[email protected]>
* added CSV logger
* fixed imports
* Fix matching w/ single body parts
* Minor fixes
hmaarrfk and others added 13 commits February 15, 2025 19:06
* Tmp fix create_pretrained_project: create TF trainingset metadata

* Type hints

* Split create_pretrained_project() into tf and torch

* Add individuals param to create_new_project()

* Fix list_videos_in_folder()

In original/tf methods (f.ex. analyze_videos), default value of videotype was "", this default value was not treated properly in list_videos_in_folder()

* Refactor _create_and_save_fake_metadata

* Refactor: _process_videos

* Implement create_pretrained_project for torch

* Update create_pretrained_project usages

* Refactoring

Move read_config_as_dict, write_config and pretty_print to core

* Fix get_super_animal_project_cfg

When calling read_config, the superanimal yaml config in the repo was modified

* Write integration script

* Fix other Superanimal tests

* Run black
n-poulsen and others added 4 commits February 26, 2025 14:58
* improve testing
* fix ID-based tracklet creation
* fix ID metric computation when NaNs are present
* fix id assignment during ID-only tracking
* Updates to the documentation - Maxim (#2869)

* Updates to the docs (#2871)

* add more docs in single animal userguide

* unify train_network docs

* unify train_network docs

* improved augmentation code

* remove link to 3mice TF Colab on homepage

* Update readme and conda env

* Update readme

* docs/pytorch/user_guide Installation: point to docs/installation

* Standardize subtitiles accross single and multi user guides

* Add missing docstring

* improved installation docs

* added quick commands

* improved docs

* install torch and torchvision with pip in the DeepLabCut Conda file

* updated labelling in userguides

* Update DEEPLABCUT.yaml

* install deeplabcut through the pre-release

---------

Co-authored-by: Niels <[email protected]>
Co-authored-by: Niels Poulsen <[email protected]>
Co-authored-by: Mackenzie Mathis <[email protected]>
…ne (#2868)

* update openfield COLAB

* updated COLAB_YOURDATA_TrainNetwork_VideoAnalysis and COLAB_maDLC_TrainNetwork_VideoAnalysis

* fix ReID notebook

* fix ReID notebook

* save predictions to destfolder

* update superanimal notebook

* fixed demo_data.py, Demo_labeledexample_MouseReaching.ipynb

* set default batch size to 8, fix notebooks

* updated notebooks

* fix edited config

* fix edited config

* fix edited config

* update 3mice demo

* install deeplabcut through prerelease

---------

Co-authored-by: Mackenzie Mathis <[email protected]>
* bug fix for bbox dict building

* add option to export top-down models without detectors

* add option to export top-down models without detectors

* Update examples/openfield-Pranav-2018-10-30/config.yaml

* Update examples/openfield-Pranav-2018-10-30/config.yaml

---------

Co-authored-by: Mackenzie Mathis <[email protected]>
@MMathisLab MMathisLab changed the title [wip] 🔥 New main backend Deep Learning Library (Engine) for DeepLabCut: PyTorch 🔥 New main backend Deep Learning Library (Engine) for DeepLabCut: PyTorch Feb 28, 2025
Copy link
Contributor

@maximpavliv maximpavliv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go! 🚀🚀🚀

Copy link
Contributor

@n-poulsen n-poulsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🐭🚀

@n-poulsen n-poulsen merged commit fe0e6da into main Feb 28, 2025
5 checks passed
@MMathisLab MMathisLab deleted the pytorch_dlc branch April 14, 2025 11:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.