diff --git a/.github/workflows/publish-book.yml b/.github/workflows/publish-book.yml index 426e87370e..6aff2725d2 100644 --- a/.github/workflows/publish-book.yml +++ b/.github/workflows/publish-book.yml @@ -11,15 +11,15 @@ jobs: steps: - uses: actions/checkout@v4 - - name: Set up Python 3.9 + - name: Set up Python 3.10 uses: actions/setup-python@v4 with: - python-version: 3.9 + python-version: 3.10 - name: Install dependencies run: | python -m pip install --upgrade pip - python -m pip install .[tf,docs] + python -m pip install .[docs] pip install jupyter-book sphinxcontrib-mermaid - name: Build the book diff --git a/README.md b/README.md index 28ef4e09a8..68d40733ef 100644 --- a/README.md +++ b/README.md @@ -60,15 +60,24 @@ Please click the link above for all the information you need to get started! Please note that currently we support only Python 3.10+ (see conda files for guidance). -Developers Stable Release: -- Very quick start: You need to have TensorFlow installed (up to v2.10 supported across platforms) `pip install "deeplabcut[gui,tf]"` that includes all functions plus GUIs, or `pip install deeplabcut[tf]` (headless version with PyTorch and TensorFlow). - -Developers Alpha Release: -- We also have an alpha release of PyTorch DeepLabCut available! [Please see here for instructions and information](https://github.com/DeepLabCut/DeepLabCut/blob/pytorch_docs/docs/pytorch/user_guide.md). +Developers Stable Release: very quick start (Python 3.10+ required) to install +DeepLabCut with the PyTorch engine + +- [Install PyTorch](https://pytorch.org/get-started/locally/) (**select the desired +CUDA version if you want to use a GPU**): `pip install torch torchvision` +- Then, [install `pytables`](https://www.pytables.org/usersguide/installation.html): `conda install -c conda-forge pytables==3.8.0` +- Finally, install `DeepLabCut` (with all functions + the GUI): +`pip install --pre "deeplabcut[gui]"` or `pip install --pre "deeplabcut"` (headless +version with PyTorch)! + +To use the TensorFlow engine (requires Python 3.10; TF up to v2.10 supported on Windows, +up to v2.12 on other platforms): you'll need to run `pip install "deeplabcut[gui,tf]"` +(which includes all functions plus GUIs) or `pip install "deeplabcut[tf]"` (headless +version with PyTorch and TensorFlow). We recommend using our conda file, see [here](https://github.com/DeepLabCut/DeepLabCut/blob/main/conda-environments/README.md) or the new [`deeplabcut-docker` package](https://github.com/DeepLabCut/DeepLabCut/tree/main/docker). -# [Documentation: The DeepLabCut Process](https://deeplabcut.github.io/DeepLabCut) +# [Documentation: The DeepLabCut Process](https://deeplabcut.github.io/DeepLabCut/README.html) Our docs walk you through using DeepLabCut, and key API points. For an overview of the toolbox and workflow for project management, see our step-by-step at [Nature Protocols paper](https://doi.org/10.1038/s41596-019-0176-0). @@ -82,9 +91,7 @@ For a deeper understanding and more resources for you to get started with Python 🐭 pose tracking of single animals demo [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DeepLabCut/DeepLabCut/blob/master/examples/COLAB/COLAB_DEMO_mouse_openfield.ipynb) -🐭🐭🐭 pose tracking of multiple animals demo [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/DeepLabCut/DeepLabCut/blob/master/examples/COLAB/COLAB_3miceDemo.ipynb) - -- See [more demos here](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/README.md). We provide data and several Jupyter Notebooks: one that walks you through a demo dataset to test your installation, and another Notebook to run DeepLabCut from the beginning on your own data. We also show you how to use the code in Docker, and on Google Colab. +See [more demos here](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/README.md). We provide data and several Jupyter Notebooks: one that walks you through a demo dataset to test your installation, and another Notebook to run DeepLabCut from the beginning on your own data. We also show you how to use the code in Docker, and on Google Colab. # Why use DeepLabCut? diff --git a/_config.yml b/_config.yml index c55ef8cebb..a24fcaf718 100644 --- a/_config.yml +++ b/_config.yml @@ -5,7 +5,7 @@ only_build_toc_files: true sphinx: config: - autodoc_mock_imports: ["wx"] + autodoc_mock_imports: ["wx", "matplotlib", "qtpy", "PySide6", "napari", "shiboken6"] mermaid_output_format: raw extra_extensions: - numpydoc diff --git a/_toc.yml b/_toc.yml index 471655454f..8935c8ff8e 100644 --- a/_toc.yml +++ b/_toc.yml @@ -46,10 +46,8 @@ parts: chapters: - file: docs/ModelZoo - file: docs/recipes/UsingModelZooPupil - - file: docs/recipes/MegaDetectorDLCLive - caption: 🧑‍🍳 Cookbook (detailed helper guides) chapters: - - file: docs/tutorial - file: docs/convert_maDLC - file: docs/recipes/OtherData - file: docs/recipes/io diff --git a/conda-environments/DEEPLABCUT.yaml b/conda-environments/DEEPLABCUT.yaml index 66edc47212..5b5c77d4c6 100644 --- a/conda-environments/DEEPLABCUT.yaml +++ b/conda-environments/DEEPLABCUT.yaml @@ -9,9 +9,13 @@ #Licensed under GNU Lesser General Public License v3.0 # # DeepLabCut environment -# FIRST: INSTALL CORRECT DRIVER for GPU, see https://stackoverflow.com/questions/30820513/what-is-the-correct-version-of-cuda-for-my-nvidia-driver/30820690 # -# AFTER THIS FILE IS INSTALLED, if you have a GPU be sure to install cudnn from conda-forge: conda install cudnn -c conda-forge +# FIRST: If you have an NVIDIA GPU and want to use it, check that you have drivers installed! +# To check if your GPUs are visible to PyTorch (and thus DeepLabCut), run: +# >>> python -c "import torch; print(torch.cuda.is_available())" +# +# If "False" is printed, PyTorch (and thus DeepLabCut) cannot access your GPU. For +# more information, see: https://pytorch.org/get-started/locally/ # # install: conda env create -f DEEPLABCUT.yaml # update: conda env update -f DEEPLABCUT.yaml @@ -29,4 +33,6 @@ dependencies: - ffmpeg - pytables==3.8.0 - pip: + - torch + - torchvision - "git+https://github.com/DeepLabCut/DeepLabCut.git@pytorch_dlc#egg=deeplabcut[gui,modelzoo,wandb]" diff --git a/conda-environments/DEEPLABCUT_M1.yaml b/conda-environments/DEEPLABCUT_M1.yaml deleted file mode 100644 index 47e8c02572..0000000000 --- a/conda-environments/DEEPLABCUT_M1.yaml +++ /dev/null @@ -1,43 +0,0 @@ -# DEEPLABCUT_M1.yaml - -#DeepLabCut2.0 Toolbox (deeplabcut.org) -#© A. & M.W. Mathis Labs -#https://github.com/DeepLabCut/DeepLabCut -#Please see AUTHORS for contributors. - -#https://github.com/DeepLabCut/DeepLabCut/blob/master/AUTHORS -#Licensed under GNU Lesser General Public License v3.0 -# -# DeepLabCut M1/M2 (Apple Silicon) environment instructions -# -# We'll get the miniconda M1 bash installer, as explained in -# https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html -# -# In the Terminal, run the following commands: -# wget https://repo.anaconda.com/miniconda/Miniconda3-py39_4.12.0-MacOSX-arm64.sh -O ~/miniconda.sh -# bash ~/miniconda.sh -b -p $HOME/miniconda -# source ~/miniconda/bin/activate -# conda init zsh -# -# Then, `git clone DeepLabCut`, and run: -# -# conda env create -f conda-environments/DEEPLABCUT_M1.yaml -# -# Next, activate the environment, and launch DLC with pythonw -m deeplabcut - -name: DEEPLABCUT_M1 -channels: - - conda-forge - - defaults -dependencies: - - python=3.10 - - pip - - ipython - - jupyter - - nb_conda - - notebook<7.0.0 - - python.app - - ffmpeg - - apple::tensorflow-deps - - pip: - - "deeplabcut[gui,apple_mchips]" diff --git a/deeplabcut/refine_training_dataset/outlier_frames.py b/deeplabcut/refine_training_dataset/outlier_frames.py index 533d10bfb0..2ec2465afa 100644 --- a/deeplabcut/refine_training_dataset/outlier_frames.py +++ b/deeplabcut/refine_training_dataset/outlier_frames.py @@ -235,7 +235,7 @@ def extract_outlier_frames( outlieralgorithm: str, optional, default="jump". String specifying the algorithm used to detect the outliers. - * ``'Fitting'`` fits a Auto Regressive Integrated Moving Average model to the + * ``'fitting'`` fits an Auto Regressive Integrated Moving Average model to the data and computes the distance to the estimated data. Larger distances than epsilon are then potentially identified as outliers * ``'jump'`` identifies larger jumps than 'epsilon' in any body part diff --git a/deeplabcut/utils/visualization.py b/deeplabcut/utils/visualization.py index 327ed7f759..cb6b4bfd6e 100644 --- a/deeplabcut/utils/visualization.py +++ b/deeplabcut/utils/visualization.py @@ -17,6 +17,7 @@ https://github.com/DeepLabCut/DeepLabCut/blob/master/AUTHORS Licensed under GNU Lesser General Public License v3.0 """ +from __future__ import annotations import os from pathlib import Path diff --git a/docs/Governance.md b/docs/Governance.md index 4fe068f0db..ff7477f28e 100644 --- a/docs/Governance.md +++ b/docs/Governance.md @@ -1,6 +1,6 @@ (governance-model)= # Governance Model of DeepLabCut -(adapted from https://napari.org/docs/_sources/developers/GOVERNANCE.md.txt) +(adapted from https://napari.org/stable/community/governance.html) ## Abstract @@ -30,7 +30,7 @@ project in concrete ways, such as: [GitHub pull request](https://github.com/DeepLabCut/DeepLabCut/pulls); - reporting issues on our [GitHub issues page](https://github.com/DeepLabCut/DeepLabCut/issues); -- proposing a change to the documentation (http://docs.deeplabcut.org) via a +- proposing a change to the [documentation](https://deeplabcut.github.io/DeepLabCut/README.html) via a GitHub pull request; - discussing the design of the `DeepLabCut` or its tutorials on in existing [issues](https://github.com/DeepLabCut/DeepLabCut/issues) and @@ -43,7 +43,7 @@ among other possibilities. Any community member can become a contributor, and all are encouraged to do so. By contributing to the project, community members can directly help to shape its future. -Contributors are encouraged to read the [contributing guide](https://github.com/DeepLabCut/DeepLabCut/CONTRIBUTING.md). +Contributors are encouraged to read the [contributing guide](https://github.com/DeepLabCut/DeepLabCut/blob/main/CONTRIBUTING.md). ### Core developers diff --git a/docs/MISSION_AND_VALUES.md b/docs/MISSION_AND_VALUES.md index 4677f07345..c82c4b7a39 100644 --- a/docs/MISSION_AND_VALUES.md +++ b/docs/MISSION_AND_VALUES.md @@ -3,14 +3,16 @@ This document is meant to help guide decisions about the future of `DeepLabCut`, be it in terms of whether to accept new functionality, changes to the styling of the code or graphical user interfaces (GUI), -or whether to take on new dependencies, when to break into other repos, among other things. It serves as a point of reference for core developers actively working on the project, and an introduction for +or whether to take on new dependencies, when to break into other repos, among other things. It serves as a point of +reference for core developers actively working on the project, and an introduction for newcomers who want to learn a little more about where the project is going and what the team's -values are. You can also learn more about how the project is managed by looking at our [governance model](governance-model). +values are. You can also learn more about how the project is managed by looking at our +[governance model](governance-model). ## Our founding principles -The founding DeepLabCut team came together around a shared vision for building the first open-source animal pose estimation framework -that is: +The founding DeepLabCut team came together around a shared vision for building the first open-source animal pose +estimation framework that is: - user defined pose estimation - i.e. species or object agnostic. - access to SOTA deep learning models that can be swiftly re-trained for customized applications @@ -18,39 +20,57 @@ that is: - scalable (project focused for ease of portability and sharability) -As the project has grown we've turned these original principles into the mission statement and set of values that we described below. +As the project has grown we've turned these original principles into the mission statement and set of values that we +described below. ## Our mission -DeepLabCut aims to be **the animal pose software package for Python** and to **provide access to deep learning-based pose estimation for people to use in their daily work** without the need to be able to program in a deep learning framework. -We hope to accomplish this by: +DeepLabCut aims to be **the animal pose software package for Python** and to **provide access to deep learning-based +pose estimation for people to use in their daily work** without the need to be able to program in a deep learning +framework. We hope to accomplish this by: -- being **easy to use and install**. We are careful in taking on new dependencies, sometimes making them optional, and aim support a fully (Python) packaged installation that works cross-platform. +- being **easy to use and install**. We are careful in taking on new dependencies, sometimes making them optional, and +aim support a fully (Python) packaged installation that works cross-platform. -- being **well-documented** with **comprehensive tutorials and examples**. All functions in our API have thorough docstrings clarifying expected inputs and outputs, and we maintain a separate [tutorials and information website](http://deeplabcut.org). +- being **well-documented** with **comprehensive tutorials and examples**. All functions in our API have thorough +docstrings clarifying expected inputs and outputs, and we maintain a separate +[tutorials and information website](http://deeplabcut.org). - providing **GUI access** to all critical functionality so DeepLabCut can be used by people without coding experience. - being **interactive** and **highly performant** in order to support large data pipelines. -- providing a **consistent and stable API** to enable plugin developers to build on top of DeepLabCut without their code constantly breaking and to enable advanced users to build out sophisticated Python workflows, if needed. +- providing a **consistent and stable API** to enable plugin developers to build on top of DeepLabCut without their +code constantly breaking and to enable advanced users to build out sophisticated Python workflows, if needed. -- **ensuring correctness**. We strive for complete test coverage of both the code and GUI, with all code reviewed by a core developer before being included in the repository. +- **ensuring correctness**. We strive for complete test coverage of both the code and GUI, with all code reviewed by a +core developer before being included in the repository. ## Our values -- We are **inclusive**. We welcome newcomers who are making their first contribution and strive to grow our most dedicated contributors into [core developers](https://github.com/orgs/DeepLabCut/teams/core-developers). We have a [Code of Conduct](https://github.com/DeepLabCut/DeepLabCut/CODE_OF_CONDUCT.md) to make DeepLabCut +- We are **inclusive**. We welcome newcomers who are making their first contribution and strive to grow our most +dedicated contributors into [core developers](https://github.com/orgs/DeepLabCut/teams/core-developers). +We have a [Code of Conduct](https://github.com/DeepLabCut/DeepLabCut/blob/main/CODE_OF_CONDUCT.md) to make DeepLabCut a welcoming place for all. -- We are **community-engaged**. We respond to feature requests and proposals on our [issue tracker](https://github.com/DeepLabCut/DeepLabCut/issues). +- We are **community-engaged**. We respond to feature requests and proposals on our +- [issue tracker](https://github.com/DeepLabCut/DeepLabCut/issues). -- We serve **scientific applications** primarily, over “consumer or commercial” pose estimation tools. This often means prioritizing core functionality support, and rejecting implementations of “flashy” features that have little scientific value. +- We serve **scientific applications** primarily, over “consumer or commercial” pose estimation tools. This often means +prioritizing core functionality support, and rejecting implementations of “flashy” features that have little +scientific value. -- We are **domain agnostic** within the sciences. Functionality that is highly specific to particular scientific domains belongs in plugins, whereas functionality that cuts across many domains and is likely to be widely used belongs inside DeepLabCut. +- We are **domain agnostic** within the sciences. Functionality that is highly specific to particular scientific +domains belongs in plugins, whereas functionality that cuts across many domains and is likely to be widely used belongs +inside DeepLabCut. -- We value **education and documentation**. All functions should have docstrings, preferably with examples, and major functionality should be explained in our [tutorials](http://deeplabcut.org). Core developers can take an active role in finishing documentation examples. +- We value **education and documentation**. All functions should have docstrings, preferably with examples, and major +functionality should be explained in our [tutorials](http://deeplabcut.org). Core developers can take an active role +in finishing documentation examples. ## Acknowledgements -We share a lot of our mission and values with [`napari`](https://napari.org/docs/developers/MISSION_AND_VALUES.html) and [`scikit-image`](https://scikit-image.org/docs/dev/values.html) and acknowledge the influence of their mission and values statements on this document. +We share a lot of our mission and values with [`napari`](https://napari.org/stable/community/mission_and_values.html) +and [`scikit-image`](https://scikit-image.org/docs/stable/about/values.html) and acknowledge the influence of their +mission and values statements on this document. diff --git a/docs/ModelZoo.md b/docs/ModelZoo.md index e91a71fde2..ca5d790659 100644 --- a/docs/ModelZoo.md +++ b/docs/ModelZoo.md @@ -6,13 +6,13 @@ ## 🏠 [Home page](http://modelzoo.deeplabcut.org/) -Started in 2020, expanded in 2022 with PhD student [Shaokai Ye et al.](https://arxiv.org/abs/2203.07436v1), and the first proper [SuperAnimal Foundation Models]() published in 2024 🔥, the Model Zoo is four things: - +Started in 2020, expanded in 2022 with PhD student [Shaokai Ye et al.](https://arxiv.org/abs/2203.07436v1), and the +first proper [SuperAnimal Foundation Models](#about-the-superanimal-models) published in 2024 🔥, the Model Zoo is four things: - (1) a collection of models that are trained on diverse data across (typically) large datasets, which means you do not need to train models yourself, rather you can use them in your research applications. - (2) a contribution website for community crowd sourcing of expertly labeled keypoints to improve models! You can get involved here: [contrib.deeplabcut.org](https://contrib.deeplabcut.org/). - (3) a no-install DeepLabCut that you can use on ♾[Google Colab](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/COLAB/COLAB_DEMO_SuperAnimal.ipynb), -test our models in 🕸[the browser](https://contrib.deeplabcut.org/), or on our 🤗[HuggingFace](https://huggingface.co/spaces/DeepLabCut/MegaDetector_DeepLabCut) app! +test our models in 🕸[the browser](https://contrib.deeplabcut.org/), or on our 🤗[HuggingFace](https://huggingface.co/spaces/DeepLabCut/DeepLabCutModelZoo-SuperAnimals) app! - (4) new methods to make SuperAnimal Foundation Models that combine data across different labs/datasets, keypoints, animals/species, and use on your data! ## Quick Start: @@ -35,13 +35,13 @@ To provide the community with easy access to such high performance models across - `superanimal_quadruped_x` models aim to work across a large range of quadruped animals, from horses, dogs, sheep, rodents, to elephants. The camera perspective is orthogonal to the animal ("side view"), and most of the data includes the animals face (thus the front and side of the animal). You will note we have several variants that differ in speed vs. performance, so please do test them out on your data to see which is best suited for your application. Also note we have a "video adaptation" feature, which lets you adapt your data to the model in a self-supervised way. No labeling needed! -- [PLEASE SEE THE FULL DATASHEET HERE](https://zenodo.org/records/10619173) -- [MORE DETAILS ON THE MODELS (detector, pose estimators)](https://huggingface.co/mwmathis/DeepLabCutModelZoo-SuperAnimal-Quadruped) +- [Please see the full datasheet here](https://zenodo.org/records/10619173) +- [More details on the models (detector, pose estimators)](https://huggingface.co/mwmathis/DeepLabCutModelZoo-SuperAnimal-Quadruped) - We provide several models: - `superanimal_quadruped_hrnetw32` (pytorch engine) - `superanimal_quadruped_hrnetw32` is a top-down model that is paired with a detector. That means it takes a cropped image from an object detector and predicts the keypoints. The object detector is currently a trained [ResNet50-based Faster-RCNN](https://pytorch.org/vision/stable/models/faster_rcnn.html). - `superanimal_quadruped_dlcrnet` (tensorflow engine) - - `superanimal_quadruped_dlcrnet` is a bottom-up model that predicts all keypoints then groups them into individuals. This can be faster, but more error prone. + - `superanimal_quadruped_dlcrnet` is a bottom-up model that predicts all keypoints, then groups them into individuals. This can be faster, but more error prone. - `superanimal_quadruped` -> This is the same as `superanimal_quadruped_dlcrnet`, this was the old naming and being depreciated. - For all models, they are automatically downloaded to modelzoo/checkpoints when used. @@ -54,8 +54,8 @@ To provide the community with easy access to such high performance models across - `superanimal_topviewmouse_x` aims to work across lab mice in different lab settings from a top-view perspective; this is very polar in many behavioral assays in freely moving mice. -- [PLEASE SEE THE FULL DATASHEET HERE](https://zenodo.org/records/10618947) -- [MORE DETAILS ON THE MODELS (detector, pose estimators)](https://huggingface.co/mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse) +- [Please see the full datasheet here](https://zenodo.org/records/10618947) +- [More details on the models (detector, pose estimators)](https://huggingface.co/mwmathis/DeepLabCutModelZoo-SuperAnimal-TopViewMouse) - We provide several models: - `superanimal_topviewmouse_hrnetw32` (pytorch engine) - `superanimal_topviewmouse_hrnetw32` is a top-down model that is paired with a detector. That means it takes a cropped image from an object detector and predicts the keypoints. The object detector is currently a trained [ResNet50-based Faster-RCNN](https://pytorch.org/vision/stable/models/faster_rcnn.html). @@ -115,39 +115,56 @@ Specifically: * `superanimal_topviewmouse_x` uses 27 keypoints ```python +import os +import deeplabcut +from deeplabcut.modelzoo import build_weight_init -superanimal_name = "superanimal_topviewmouse_hrnetw32" +superanimal_name = "superanimal_topviewmouse" config_path = os.path.join(os.getcwd(), "openfield-Pranav-2018-10-30", "config.yaml") -deeplabcut.create_training_dataset(config_path, superanimal_name = superanimal_name) +weight_init = build_weight_init( + cfg=config_path, + super_animal=superanimal_name, + model_name="hrnet_w32", + detector_name="fasterrcnn_resnet50_fpn_v2", + with_decoder=False, +) + +deeplabcut.create_training_dataset(config_path, weight_init = weight_init) deeplabcut.train_network(config_path, - maxiters=10, + epochs=10, superanimal_name = superanimal_name, superanimal_transfer_learning = True) ``` - - - ### Potential failure modes for SuperAnimal Models and how to fix it. -Spatial domain shift: typical DNN models suffer from the spatial resolution shift between training datasets and test videos. To help find the proper resolution for our model, please try a range of `scale_list` in the API (details in the API docs). For `superanimal_quadruped`, we empirically observe that if your video is larger than 1500 pixels, it is better to pass `scale_list` in the range within 1000. +Spatial domain shift: typical DNN models suffer from the spatial resolution shift between training datasets and test +videos. To help find the proper resolution for our model, please try a range of `scale_list` in the API (details in the +API docs). For `superanimal_quadruped`, we empirically observe that if your video is larger than 1500 pixels, it is +better to pass `scale_list` in the range within 1000. -Pixel statistics domain shift: The brightness of your video might look very different from our training datasets. This might either result in jittering predictions in the video or fail modes for lab mice videos (if the brightness of the mice is unusual compared to our training dataset). You can use our "video adaptation" model (released soon) to counter this. +Pixel statistics domain shift: The brightness of your video might look very different from our training datasets. +This might either result in jittering predictions in the video or fail modes for lab mice videos (if the brightness of +the mice is unusual compared to our training dataset). You can use our "video adaptation" model to counter this. ### Our longer term perspective ... -Via DeepLabCut Model Zoo, we aim to provide plug and play models that do not need any labeling and will just work decently on novel videos. If the predictions are not great enough due to failure modes described below, please give us feedback! We are rapidly improving our models and adaptation methods. We will also continue to expand this project to new model/data classes. Please do get in touch is you have data or ideas: modelzoo@deeplabcut.org +Via DeepLabCut Model Zoo, we aim to provide plug and play models that do not need any labeling and will just work +decently on novel videos. If the predictions are not great enough due to failure modes described below, please give us +feedback! We are rapidly improving our models and adaptation methods. We will also continue to expand this project to +new model/data classes. Please do get in touch is you have data or ideas: modelzoo@deeplabcut.org ## Publication: -To see the first preprint on the work, click [here](https://arxiv.org/abs/2203.07436v1). +To see the first preprint on the work, click [here](https://arxiv.org/abs/2203.07436v1). -Our first publication on this project is now published at Nature Communications: +Our first [publication](https://www.nature.com/articles/s41467-024-48792-2) on this project is now published at Nature +Communications: ```{hint} Here is the citation: diff --git a/docs/Overviewof3D.md b/docs/Overviewof3D.md index f7c8b63fa8..989ea484e9 100644 --- a/docs/Overviewof3D.md +++ b/docs/Overviewof3D.md @@ -1,14 +1,21 @@ (3D-overview)= # 3D DeepLabCut -In this repo we directly support 2-camera based 3D pose estimation. If you want n camera support, plus nicer optimization methods, please see our work that was published at [ICRA 2021 on strong baseline 3D models (and a 3D dataset)](https://github.com/African-Robotics-Unit/AcinoSet). In the link you will find how we optimize 6+ camera DLC output data for cheetahs (and see more below). +In this repo we directly support 2-camera based 3D pose estimation. If you want n camera support, plus nicer +optimization methods, please see our work that was published at +[ICRA 2021 on strong baseline 3D models (and a 3D dataset)](https://github.com/African-Robotics-Unit/AcinoSet). In the +link you will find how we optimize 6+ camera DLC output data for cheetahs (and see more below). DLC 3D ## **ATTENTION: Our code base in this repo assumes you:** -A. You have 2D videos and a DeepLabCut network to analyze them as described in the [main documentation](overview). This can be with multiple separate networks for each camera (less recommended), or one network trained on all views - recommended! (See [Nath*, Mathis* et al., 2019](https://www.biorxiv.org/content/10.1101/476531v1)). We also support multi-animal 3D with this code (please see [Lauer et al. 2022](https://doi.org/10.1038/s41592-022-01443-0)). +A. You have 2D videos and a DeepLabCut network to analyze them as described in the +[main documentation](overview). This can be with multiple +separate networks for each camera (less recommended), or one network trained on all views - recommended! (See +[Nath*, Mathis* et al., 2019](https://www.biorxiv.org/content/10.1101/476531v1)). We also support multi-animal 3D with this code (please see +[Lauer et al. 2022](https://doi.org/10.1038/s41592-022-01443-0)). B. You are using 2 cameras, in a [stereo configuration](https://github.com/DeepLabCut/DeepLabCut/blob/5ac4c8cb6bcf2314a3abfcf979b8dd170608e094/deeplabcut/pose_estimation_3d/camera_calibration.py#L223), for 3D*. @@ -20,12 +27,16 @@ Here are other excellent options for you to use that extend DeepLabCut: DLC 3D -- **[AcinoSet](https://github.com/African-Robotics-Unit/AcinoSet)**; **n**-camera support with triangulation, extended Kalman filtering, and trajectory optimization code (see video to the right for a min demo, courtesy of Prof. Patel), plus a GUI to visualize 3D data. It is built to work directly with DeepLabCut (but currently tailored to cheetah's, thus some coding skills are required at this time). +- **[AcinoSet](https://github.com/African-Robotics-Unit/AcinoSet)**; **n**-camera support with triangulation, extended Kalman filtering, and trajectory optimization +code (see video to the right for a min demo, courtesy of Prof. Patel), plus a GUI to visualize 3D data. It is built to +work directly with DeepLabCut (but currently tailored to cheetah's, thus some coding skills are required at this time). -- **[anipose.org](https://anipose.readthedocs.io/en/latest/)**; a wrapper for 3D deeplabcut that provides >3 camera support and is built to work directly with DeepLabCut. You can `pip install anipose` into your DLC conda environment. +- **[anipose.org](https://anipose.readthedocs.io/en/latest/)**; a wrapper for 3D deeplabcut that provides >3 camera support and is built to work directly with +DeepLabCut. You can `pip install anipose` into your DLC conda environment. -- **Argus, easywand or DLTdv** w/DeepLabCut see https://github.com/haliaetus13/DLCconverterDLT; this can be used with the the highly popular Argus or DLTdv tools for wand calibration. +- **Argus, easywand or DLTdv** w/DeepLabCut see https://github.com/haliaetus13/DLCconverterDLT; this can be used with +the the highly popular Argus or DLTdv tools for wand calibration. ## Jump in with direct DeepLabCut 2-camera support: @@ -39,83 +50,118 @@ Here are other excellent options for you to use that extend DeepLabCut: ### (1) Create a New 3D Project: -Watch a [DEMO VIDEO](https://youtu.be/Eh6oIGE4dwI) on how to use this code, and check out the Notebook [here](https://github.com/DeepLabCut/DeepLabCut/blob/master/examples/JUPYTER/Demo_3D_DeepLabCut.ipynb)! +Watch a [DEMO VIDEO](https://youtu.be/Eh6oIGE4dwI) on how to use this code, and check out the Notebook [here](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/JUPYTER/Demo_3D_DeepLabCut.ipynb)! -You will run this function **one** time per project; a project is defined as a given set of cameras and calibration images. You can always analyze new videos within this project. +You will run this function **one** time per project; a project is defined as a given set of cameras and calibration +images. You can always analyze new videos within this project. -The function **create\_new\_project\_3d** creates a new project directory specifically for converting the 2D pose to 3D pose, required subdirectories, and a basic 3D project configuration file. Each project is identified by the name of the project (e.g. Task1), name of the experimenter (e.g. YourName), as well as the date at creation. +The function **create\_new\_project\_3d** creates a new project directory specifically for converting the 2D pose to 3D +pose, required subdirectories, and a basic 3D project configuration file. Each project is identified by the name of the +project (e.g. Task1), name of the experimenter (e.g. YourName), as well as the date at creation. -Thus, this function requires the user to input the enter the name of the project, the name of the experimenter and number of cameras to be used. Currently, DeepLabCut supports triangulation using 2 cameras, but will expand to more than 2 cameras in a future version. +Thus, this function requires the user to enter the name of the project, the name of the experimenter and number of +cameras to be used. Currently, DeepLabCut supports triangulation using 2 cameras, but will expand to more than 2 cameras +in a future version. To start a 3D project type the following in ipython: ```python -deeplabcut.create_new_project_3d('ProjectName','NameofLabeler',num_cameras = 2) +deeplabcut.create_new_project_3d("ProjectName", "NameofLabeler", num_cameras=2) ``` -TIP 1: you can also pass ``working_directory=`Full path of the working directory'`` if you want to place this folder somewhere beside the current directory you are working in. If the optional argument ``working_directory`` is unspecified, the project directory is created in the current working directory. +TIP 1: you can also pass `working_directory="Full path of the working directory"` if you want to place this folder +somewhere beside the current directory you are working in. If the optional argument `working_directory` is unspecified, +the project directory is created in the current working directory. -TIP 2: you can also place ``config_path3d`` in front of ``deeplabcut.create_new_project_3d`` to create a variable that holds the path to the config.yaml file, i.e. ``config_path3d=deeplabcut.create_new_project_3d(...`` Or, set this variable for easy use. Please note that ``config_path3d='Full path of the 3D project configuration file'``. +TIP 2: you can also place `config_path3d` in front of `deeplabcut.create_new_project_3d` to create a variable that holds +the path to the config.yaml file, i.e. `config_path3d=deeplabcut.create_new_project_3d(...` Or, set this variable for +easy use. Please note that `config_path3d='Full path of the 3D project configuration file'`. - This function will create a project directory with the name **Name of the project+name of the experimenter+date of creation of the project+3d** in the **Working directory**. The project directory will have subdirectories: **calibration_images**, **camera_matrix**, **corners**, and **undistortion**. All the outputs generated during the course of a project will be stored in one of these subdirectories, thus allowing each project to be curated in separation from other projects. +This function will create a project directory with the name **Name of the project+name of the experimenter+date of +creation of the project+3d** in the **Working directory**. The project directory will have subdirectories: +**calibration_images**, **camera_matrix**, **corners**, and **undistortion**. All the outputs generated during the +course of a project will be stored in one of these subdirectories, thus allowing each project to be curated in +separation from other projects. - The purpose of the subdirectories is as follows: +The purpose of the subdirectories is as follows: - **calibration_images:** This directory will contain a set of calibration images acquired from the two cameras. A calibration image can be acquired using a printed checkerboard and its pair wise images are taken from both the cameras to consider as a set of calibration images. These pair of images are saved as ``.jpg`` with camera names as the prefix. e.g. ``camera-1-01.jpg`` and ``camera-2-01.jpg`` for the first pair of images. While taking the images: -- Keep the orientation of the chessboard same and do not rotate more than 30 degrees. Rotating the chessboard circular will change the origin across the frames and may result in incorrect order of detected corners. -- Cover several distances, and within each distance, cover all parts of the image view (all corners and center). -Use a chessboard as big as possible, ideally a chessboard with of at least 8x6 squares. -- Aim for taking at least 70 pair of images as after corner detection, some of the images might need to be discarded due to either incorrect corner detection or incorrect order of detected corners. +**calibration_images:** This directory will contain a set of calibration images acquired from the two cameras. A +calibration image can be acquired using a printed checkerboard and its pair wise images are taken from both the cameras +to consider as a set of calibration images. - **camera_matrix:** This directory will store the parameter for both the cameras as a pickle file. Specifically, these pickle files contain the intrinsic and extrinsic camera parameters. While the intrinsic parameters represent a transformation from 3-D camera's coordinates into the image coordinates, the extrinsic parameters represent a rigid transformation from world coordinate system to the 3-D camera's coordinate system. +**camera_matrix:** This directory will store the parameter for both the cameras as a pickle file. Specifically, these +pickle files contain the intrinsic and extrinsic camera parameters. While the intrinsic parameters represent a +transformation from 3-D camera's coordinates into the image coordinates, the extrinsic parameters represent a rigid +transformation from world coordinate system to the 3-D camera's coordinate system. - **corners:** As a part of camera calibration, the checkerboard pattern is detected in the calibration images and these patterns will be stored in this directory. Each row of the checkerboard grid is marked with a unique color. +**corners:** As a part of camera calibration, the checkerboard pattern is detected in the calibration images and these +patterns will be stored in this directory. Each row of the checkerboard grid is marked with a unique color. - **undistortion:** In order to check for calibration, the calibration images and the corresponding corner points are undistorted. These undistorted images are overlaid with undistorted points and will be stored in this directory. +**undistortion:** In order to check for calibration, the calibration images and the corresponding corner points are +undistorted. These undistorted images are overlaid with undistorted points and will be stored in this directory. - Here is an overview of the calibration and triangulation workflow that follows: +Here is an overview of the calibration and triangulation workflow that follows: -

+

### (2) Take and Process Camera Calibration Images: - (**CRITICAL!**) You must take images of a checkerboard to calibrate your images. Here are example boards you could print and use (mount it on a flat, hard surface!): https://markhedleyjones.com/projects/calibration-checkerboard-collection. +(**CRITICAL!**) You must take images of a checkerboard to calibrate your images. Here are example boards you could +print and use (mount it on a flat, hard surface!): +https://markhedleyjones.com/projects/calibration-checkerboard-collection. - You must save the image pairs as .jpg files. -- They should be named with the **camera-#** as the prefix, i.e. **camera-1-01.jpg** and **camera-2-01.jpg** for the first pair of images. Please note, this cannot be changed after the project is created. +- They should be named with the **camera-#** as the prefix, i.e. **camera-1-01.jpg** and **camera-2-01.jpg** for the +first pair of images. Please note, this cannot be changed after the project is created. -**TIP:** If you want to take a short video (vs. snapping pairs of frames) while you move the checkerboard around, you can use this command inside your conda environment (but outside of ipython!) to convert the video to **.jpg** frames (this will take the first 20 frames (set with ``-vframes``) and name them camera-1-001.jpg, etc; edit appropriately): +**TIP:** If you want to take a short video (vs. snapping pairs of frames) while you move the checkerboard around, you +can use this command inside your conda environment (but outside of ipython!) to convert the video to **.jpg** frames +(this will take the first 20 frames (set with `-vframes`) and name them camera-1-001.jpg, etc; edit appropriately): ```python ffmpeg -i videoname.mp4 -vframes 20 camera-1-%03d.jpg ``` - While taking the images: - - Keep the orientation of the checkerboard the same and do not rotate it more than 30 degrees. Rotating the checkerboard circular will change the origin across the frames and may result in incorrect order of detected corners. + - Keep the orientation of the checkerboard the same and do not rotate it more than 30 degrees. Rotating the + checkerboard circular will change the origin across the frames and may result in incorrect order of detected corners. - - Cover several distances, and within each distance, cover all parts of the image view (all corners and center). + - Cover several distances, and within each distance, cover all parts of the image view (all corners and center). - - Use a checkerboard as big as possible, ideally with at least 8x6 squares. + - Use a checkerboard as big as possible, ideally with at least 8x6 squares. - - Aim for taking at least 30-70 pair of images, as after corner detection, some of the images might need to be discarded due to either incorrect corner detection or incorrect order of detected corners. + - Aim for taking at least 30-70 pair of images, as after corner detection, some of the images might need to be + discarded due to either incorrect corner detection or incorrect order of detected corners. - - You can take the images as a series of .jpg images, or a video where you post-hoc pair sync'd frames (see tip above). + - You can take the images as a series of .jpg images, or a video where you post-hoc pair sync'd frames (see tip + above). -The camera calibration is an **iterative process**, where the user needs to select a set of calibration images where the grid pattern is correctly detected. The function:``deeplabcut.calibrate_cameras(config_path)`` -extracts the grid pattern from the calibration images and store them under the `corners` directory. The grid pattern could be 8x8 or 5x5 etc. We use a pattern of the 8x6 grid to find the internal corners of the checkerboard. +The camera calibration is an **iterative process**, where the user needs to select a set of calibration images where the +grid pattern is correctly detected. The function `deeplabcut.calibrate_cameras(config_path)` +extracts the grid pattern from the calibration images and store them under the `corners` directory. The grid pattern +could be 8x8 or 5x5 etc. We use a pattern of the 8x6 grid to find the internal corners of the checkerboard. -In some cases, it may happen that the corners are not detected correctly or the order of corners detected in the camera-1 image and camera-2 image is incorrect. You need to remove these pair of images from the **calibration_images** folder as they will reduce the calibration accuracy. +In some cases, it may happen that the corners are not detected correctly or the order of corners detected in the +camera-1 image and camera-2 image is incorrect. You need to remove these pair of images from the **calibration_images** +folder as they will reduce the calibration accuracy. To begin, please place your images into the **calibration_images** directory. - (**CRITICAL!**) Edit the **config.yaml** file to set the camera names; note that once this is set, **do not change the names!** +(**CRITICAL!**) Edit the **config.yaml** file to set the camera names; note that once this is set, **do not change the +names!** Then, run: ```python deeplabcut.calibrate_cameras(config_path3d, cbrow=8, cbcol=6, calibrate=False, alpha=0.9) ``` -NOTE: you need to specify how many rows (``cbrow``) and columns (``cbcol``) your checkerboard has. Also, first set the variable ``calibrate`` to **False**, so you can remove any faulty images. You need to visually inspect the output to check for the detected corners and select those pair of images where the corners are correctly detected. Please note, If the scaling parameter ``alpha=0``, it returns undistorted image with minimum unwanted pixels. So it may even remove some pixels at image corners. If ``alpha=1``, all pixels are retained with some extra black images. + +NOTE: you need to specify how many rows (`cbrow`) and columns (`cbcol`) your checkerboard has (beware, we count +edges between squares and not squares themselves, so for a 8 x 8 squares checkerboard set `cbrow=7` and `cbcol=7`). +Also, first set the variable `calibrate` to **False**, so you can remove any faulty images. You need to visually +inspect the output to check for the detected corners and select those pair of images where the corners are correctly +detected. Please note, If the scaling parameter `alpha=0`, it returns undistorted image with minimum unwanted pixels. +So it may even remove some pixels at image corners. If `alpha=1`, all pixels are retained with some extra black images. Here is what they might look like: @@ -125,55 +171,85 @@ Here is what they might look like:

-Once all the set of images are selected (namely, delete from the folder any bad pairs!) where the corners and their orders are detected correctly, then the two cameras can be calibrated using: +Once all the set of images has been selected (namely, delete from the folder any bad pairs!) where the corners and their +orders are detected correctly, then the two cameras can be calibrated using: ```python deeplabcut.calibrate_cameras(config_path3d, cbrow=8, cbcol=6, calibrate=True, alpha=0.9) ``` -This computes the intrinsic and extrinsic parameters for each camera. A re-projection error is also computed using the intrinsic and extrinsic parameters which provide an estimate of how good the parameters are. The transformation between the two cameras are estimated and the cameras are stereo calibrated. Furthermore, the above function brings both the camera image plane to the same plane by computing the stereo rectification. These parameters are stored as a pickle file named as `stereo_params.pickle` under the directory `camera_matrix`. +This computes the intrinsic and extrinsic parameters for each camera. A re-projection error is also computed using the +intrinsic and extrinsic parameters which provide an estimate of how good the parameters are. The transformation between +the two cameras is estimated and the cameras are stereo calibrated. Furthermore, the above function brings both the +camera image plane to the same plane by computing the stereo rectification. These parameters are stored as a pickle file +named as `stereo_params.pickle` under the directory `camera_matrix`. -Once you have run this for the project, you do not need to do so again (unless you want to re-calibrate your cameras); be advised, if you do re-calibrate, you may want to clearly mark which videos are analyzed with "old" vs. "new" calibration images. +Once you have run this for the project, you do not need to do so again (unless you want to re-calibrate your cameras); +be advised, if you do re-calibrate, you may want to clearly mark which videos are analyzed with "old" vs. "new" +calibration images. ### (3) Check for Undistortion: -In order to check how well the stereo calibration is, it is recommended to undistort the calibration images and the corner points using camera matrices and project these undistorted points on the undistorted images to check if they align correctly. This can be done in deeplabcut as: +In order to check how well the stereo calibration is, it is recommended to undistort the calibration images and the +corner points using camera matrices and project these undistorted points on the undistorted images to check if they +align correctly. This can be done in deeplabcut as: ```python deeplabcut.check_undistortion(config_path3d, cbrow=8, cbcol=6) ``` -Each calibration image is undistorted and saved under the directory ``undistortion``. A plot with a pair of undistorted camera images with its undistorted corner points overlaid is also stored. Please visually inspect this image. All the undistorted corner points from all the calibration images are triangulated and plotted for the user to visualize for any undistortion related errors. If they are not correct, go check and revise the calibration images (then repeat the calibration and this step)! +Each calibration image is undistorted and saved under the directory `undistortion`. A plot with a pair of undistorted +camera images with its undistorted corner points overlaid is also stored. Please visually inspect this image. All the +undistorted corner points from all the calibration images are triangulated and plotted for the user to visualize for any +undistortion related errors. If they are not correct, go check and revise the calibration images (then repeat the +calibration and this step)! ### (4) Triangulation --> Take your 2D to 3D! -If there are no errors in the undistortion, then the pose from the 2 cameras can be triangulated to get the 3D DeepLabCut coordinates! +If there are no errors in the undistortion, then the pose from the 2 cameras can be triangulated to get the 3D +DeepLabCut coordinates! - (**CRITICAL!**) Name the video files in such a way that the file name **contains the name of the cameras** as specified in the ``config file``. e.g. if the cameras as named as ``camera-1`` and ``camera-2`` (or ``cam-1``, ``cam-2`` etc.) then the video filename must contain this naming, i.e. this could be named as ``rig-1-mouse-day1-camera-1.avi`` and ``rig-1-mouse-day1-camera-2.avi`` or could be ``rig-1-mouse-day1-camera-1-date.avi`` and ``rig-1-mouse-day1-camera-2-date.avi``. +(**CRITICAL!**) Name the video files in such a way that the file name **contains the name of the cameras** as specified +in the `config file`. e.g. if the cameras as named as `camera-1` and `camera-2` (or `cam-1`, `cam-2` etc.) then the +video filename must contain this naming, i.e. this could be named as `rig-1-mouse-day1-camera-1.avi` and +`rig-1-mouse-day1-camera-2.avi` or could be `rig-1-mouse-day1-camera-1-date.avi` and +`rig-1-mouse-day1-camera-2-date.avi`. - **Note** that to correctly pair the videos, the file names otherwise need to be the same! - If helpful, [here is the software we use to record videos](https://github.com/AdaptiveMotorControlLab/Camera_Control). -- **Note** that the videos do not need to be the same pixel size, but be sure they are similar in size to the calibration images (and they must be the same cameras used for calibration). - (**CRITICAL!**) You must also edit the **3D project config.yaml** file to denote which DeepLabCut projects have the information for the 2D views. +(**CRITICAL!**) You must also edit the **3D project config.yaml** file to denote which DeepLabCut projects have the +information for the 2D views. - - Of critical importance is that you need to input the **same** body part names as in the config.yaml file of the 2D project. -- You must set the snapshot to use inside the 2D config file (default is -1, namely the last training snapshot of the network). +- Of critical importance is that you need to input the **same** body part names as in the config.yaml file of the 2D +project. +- You must set the snapshot to use inside the 2D config file (default is -1, namely the last training snapshot of the +network). - You need to set a "scorer 3D" name; this will point to the project file and be set in future 3D output file names. -- You should define a "skeleton" here as well (note, this is not rigid, it just connects the points in the plotting step). Not every point needs to be "skeletonized", i.e. these points can be a subset of the full body parts list. The other points will just be plotted into the 3D space. Here is how the config.yaml looks with some example inputs: +- You should define a "skeleton" here as well (note, this is not rigid, it just connects the points in the plotting +step). Not every point needs to be "skeletonized", i.e. these points can be a subset of the full body parts list. The +other points will just be plotted into the 3D space. Here is how the config.yaml looks with some example inputs:

-(**CRITICAL!**) This step will also run the equivalent of ``analyze_videos`` (in 2D) for you and then apply a median filter to the 2D data (``filterpredictions=True`` is by default)! If you already ran the 2D analysis and there is a filtered output file, it will take this by default (otherwise it will take your unfiltered 2D analysis files)! +(**CRITICAL!**) This step will also run the equivalent of `analyze_videos` (in 2D) for you and then apply a median +filter to the 2D data (`filterpredictions=True`)! If you already ran the 2D analysis and there is a filtered output +file, it will take this by default (otherwise it will take your unfiltered 2D analysis files)! -Next, pass the ``config_path3d`` and now the video folder path, which is the path to the **folder** where all the videos from two cameras are stored. The triangulation can be done in deeplabcut by typing: +Next, pass the `config_path3d` and now the video folder path, which is the path to the **folder** where all the videos +from two cameras are stored. The triangulation can be done in deeplabcut by typing: ```python -deeplabcut.triangulate(config_path3d, '/yourcomputer/fullpath/videofolder', filterpredictions=True/False) +deeplabcut.triangulate( + config_path3d, + "/yourcomputer/fullpath/videofolder", + filterpredictions=True/False +) ``` -NOTE: Windows users, you must input paths as: ``r`C:\Users\computername\videofolder' `` or ``C:\\Users\\computername\\videofolder'``. +NOTE: Windows users, you must input paths as: ``r`C:\Users\computername\videofolder'`` or +``C:\\Users\\computername\\videofolder'``. **TIP:** Here are all the parameters you can pass: @@ -204,8 +280,15 @@ destfolder: string, optional save_as_csv: bool, optional Saves the predictions in a .csv file. The default is ``False``; if provided it must be either ``True`` or ``False`` + +track_method: str, optional + Method used for tracking: "box" or "ellipse" ``` -The **triangulated file** is now saved under the same directory where the video files reside (or the destination folder you set)! This can be used for future analysis. This step can be run at anytime as you collect new videos, and easily added to your automated analysis pipeline, i.e. such as **replacing** ``deeplabcut.triangulate(config_path3d, video_path)`` with ``deeplabcut.analyze_videos`` (as if it's not analyzed in 2D already, this function will take care of it ;): +The **triangulated file** is now saved under the same directory where the video files reside (or the destination folder +you set)! This can be used for future analysis. This step can be run at anytime as you collect new videos, and easily +added to your automated analysis pipeline, i.e. such as **replacing** +`deeplabcut.triangulate(config_path3d, video_path)` with `deeplabcut.analyze_videos` (as if it's not analyzed in 2D +already, this function will take care of it ;):

@@ -213,22 +296,36 @@ The **triangulated file** is now saved under the same directory where the video ### (5) Visualize your 3D DeepLabCut Videos: -In order to visualize both the 2D videos with tracked points plut the pose in 3D, the user can create a 3D video for certain frames (these are large files, so we advise just looking at a subset of frames). The user can specify the config file, the **path of the triangulated file folder**, and specify the start and end frame indices to create a 3D labeled video. Note that the ``triangulated_file_folder`` is where the newly created file that ends with ``yourDLC_3D_scorername.h5`` is located. This can be done using: +In order to visualize both the 2D videos with tracked points plut the pose in 3D, the user can create a 3D video for +certain frames (these are large files, so we advise just looking at a subset of frames). The user can specify the config +file, the **path of the triangulated file folder**, and specify the start and end frame indices to create a 3D labeled +video. Note that the `triangulated_file_folder` is where the newly created file that ends with +`yourDLC_3D_scorername.h5` is located. This can be done using: ```python -deeplabcut.create_labeled_video_3d(config_path, ['triangulated_file_folder'], start=50, end=250) +deeplabcut.create_labeled_video_3d( + config_path, + ["triangulated_file_folder"], + start=50, + end=250 +) ``` -**TIP:** (see more parameters below) You can set how the axis of the 3D plot on the far right looks by changing the variables ``xlim``, ``ylim``, ``zlim`` and ``view``. Your checkerboard_3d.png image which was created above will show you the axis ranges. Here is an example: +**TIP:** (see more parameters below) You can set how the axis of the 3D plot on the far right looks by changing the +variables `xlim`, `ylim`, `zlim` and `view`. Your checkerboard_3d.png image which was created above will show you the +axis ranges. Here is an example:

-``View`` is used to set the elevation and azimuth of the axes (defaults are [113, 270], and you should play around to find the view-point you like!). Also note that the video is created from a set of .png files in a "temp" directory, so as soon as you run this command you can open the first image, and if you don't like the view, hit ``CNTRL+C`` to stop, edit the values, and start again! +`View` is used to set the elevation and azimuth of the axes (defaults are [113, 270], and you should play around to find +the view-point you like!). Also note that the video is created from a set of .png files in a "temp" directory, so as +soon as you run this command you can open the first image, and if you don't like the view, hit `CNTRL+C` to stop, edit +the values, and start again! **Other optional parameters include:** - +here ```python videofolder: string Full path of the folder where the videos are stored. Use this if the vidoes are stored in a different location other than where the triangulation files are stored. By default is ``None`` and therefore looks for video files in the directory where the triangulation file is stored. @@ -251,8 +348,25 @@ ylim: list zlim: list A list of integers specifying the limits for zaxis of 3d view. By default it is set to [None,None], where the z limit is set by taking the minimum and maximum value of the z coordinates for all the bodyparts. + +draw_skeleton: bool + If True adds a line connecting the body parts making a skeleton on on each frame. The body parts to be connected and the color of these connecting lines are specified in the config file. By default: True + +color_by : string, optional (default='bodypart') + Coloring rule. By default, each bodypart is colored differently. + If set to 'individual', points belonging to a single individual are colored the same. + +figsize: tuple[int, int], optional, default=(80, 8) + Size of the figure + +fps: int, optional, default=30 + Frames per second + +dpi: int, optional, default=300 + Dots per inch (resplution) ``` ### If you use this code: -We kindly ask that you cite [Mathis et al, 2018](https://www.nature.com/articles/s41593-018-0209-y) **&** [Nath*, Mathis*, et al., 2019](https://doi.org/10.1038/s41596-019-0176-0). If you use 3D multi-animal: [Lauer et al. 2022](https://doi.org/10.1038/s41592-022-01443-0). +We kindly ask that you cite [Mathis et al, 2018](https://www.nature.com/articles/s41593-018-0209-y) **&** [Nath*, Mathis*, et al., 2019](https://doi.org/10.1038/s41596-019-0176-0). If you use 3D +multi-animal: [Lauer et al. 2022](https://doi.org/10.1038/s41592-022-01443-0). diff --git a/docs/UseOverviewGuide.md b/docs/UseOverviewGuide.md index 778b5d2c0e..7e7ec1a137 100644 --- a/docs/UseOverviewGuide.md +++ b/docs/UseOverviewGuide.md @@ -18,11 +18,11 @@ We are primarily a package that enables deep learning-based pose estimation. We - Decide on your needs: there are **two main modes, standard DeepLabCut or multi-animal DeepLabCut**. We highly recommend carefully considering which one is best for your needs. For example, a white mouse + black mouse would call for standard, while two black mice would use multi-animal. **[Important Information on how to use DLC in different scenarios (single vs multi animal)](important-info-regd-usage)** Then pick a user guide: -- (1) [How to use standard DeepLabCut](single-animal-userguide) -- (2) [How to use multi-animal DeepLabCut](multi-animal-userguide) + - (1) [How to use standard DeepLabCut](single-animal-userguide) + - (2) [How to use multi-animal DeepLabCut](multi-animal-userguide) - To note, as of DLC3+ the single and multi-animal code bases are more integrated and we support **top-down**, **bottom-up**, and a new "hybrid" approach that is state-of-the-art, called **BUCTD** (bottom-up conditional top down), models. - - If these terms are new to you, check out our [Primer on Motion Capture with Deep Learning!](https://www.sciencedirect.com/science/article/pii/S0896627320307170). In brief, both work for single or multiple animals and each method can be better or worse on your data. + - If these terms are new to you, check out our [Primer on Motion Capture with Deep Learning!](https://www.sciencedirect.com/science/article/pii/S0896627320307170). In brief, both work for single or multiple animals and each method can be better or worse on your data.

@@ -67,15 +67,19 @@ Getting Started: [a video tutorial on navigating the documentation!](https://www

- +

### Overview of the workflow: This page contains a list of the essential functions of DeepLabCut as well as demos. There are many optional parameters with each described function, which you can find [here](functionDetails.md). For additional assistance, you can use the [help](UseOverviewGuide.md#help) function to better understand what each function does. -

- - +

+ +
+ + View in full screen + +

You can have as many projects on your computer as you wish. You can have DeepLabCut installed in an [environment](/conda-environments) and always exit and return to this environment to run the code. You just need to point to the correct ``config.yaml`` file to [jump back in](/docs/UseOverviewGuide.md#tips-for-daily-use)! The documentation below will take you through the individual steps. @@ -136,7 +140,7 @@ with the terminal interface you get the most versatility and options. ## Option 1: Demo Notebooks: [VIDEO TUTORIAL AVAILABLE!](https://www.youtube.com/watch?v=DRT-Cq2vdWs) -We provide Jupyter and COLAB notebooks for using DeepLabCut on both a pre-labeled dataset, and on the end user’s +We provide Jupyter and COLAB notebooks for using DeepLabCut on both a pre-labeled dataset, and on the end user's own dataset. See all the demo's [here!](/examples) Please note that GUIs are not easily supported in Jupyter in MacOS, as you need a framework build of python. While it's possible to launch them with a few tweaks, we recommend using the Project Manager GUI or terminal, so please follow the instructions below. (using-project-manager-gui)= diff --git a/docs/api/deeplabcut.analyze_videos.rst b/docs/api/deeplabcut.analyze_videos.rst index d43e39ea4c..274a801f02 100644 --- a/docs/api/deeplabcut.analyze_videos.rst +++ b/docs/api/deeplabcut.analyze_videos.rst @@ -1 +1 @@ -.. autofunction:: deeplabcut.pose_estimation_tensorflow.predict_videos.analyze_videos +.. autofunction:: deeplabcut.compat.analyze_videos diff --git a/docs/api/deeplabcut.convert_detections2tracklets.rst b/docs/api/deeplabcut.convert_detections2tracklets.rst new file mode 100644 index 0000000000..f69f721d84 --- /dev/null +++ b/docs/api/deeplabcut.convert_detections2tracklets.rst @@ -0,0 +1 @@ +.. autofunction:: deeplabcut.compat.convert_detections2tracklets diff --git a/docs/api/deeplabcut.create_training_dataset_from_existing_split.rst b/docs/api/deeplabcut.create_training_dataset_from_existing_split.rst new file mode 100644 index 0000000000..0b59472d91 --- /dev/null +++ b/docs/api/deeplabcut.create_training_dataset_from_existing_split.rst @@ -0,0 +1 @@ +.. autofunction:: deeplabcut.generate_training_dataset.trainingsetmanipulation.create_training_dataset_from_existing_split diff --git a/docs/api/deeplabcut.evaluate_network.rst b/docs/api/deeplabcut.evaluate_network.rst index f24ee4c481..56914774fe 100644 --- a/docs/api/deeplabcut.evaluate_network.rst +++ b/docs/api/deeplabcut.evaluate_network.rst @@ -1 +1 @@ -.. autofunction:: deeplabcut.pose_estimation_tensorflow.core.evaluate.evaluate_network +.. autofunction:: deeplabcut.compat.evaluate_network diff --git a/docs/api/deeplabcut.label_frames.rst b/docs/api/deeplabcut.label_frames.rst index b1a810d284..4de3a1054c 100644 --- a/docs/api/deeplabcut.label_frames.rst +++ b/docs/api/deeplabcut.label_frames.rst @@ -1 +1 @@ -.. autofunction:: deeplabcut.gui.label_frames.label_frames +.. autofunction:: deeplabcut.gui.tabs.label_frames.label_frames diff --git a/docs/api/deeplabcut.refine_labels.rst b/docs/api/deeplabcut.refine_labels.rst index 7c61d5586c..b54b640e46 100644 --- a/docs/api/deeplabcut.refine_labels.rst +++ b/docs/api/deeplabcut.refine_labels.rst @@ -1 +1 @@ -.. autofunction:: deeplabcut.gui.refine_labels.refine_labels +.. autofunction:: deeplabcut.gui.tabs.label_frames.refine_labels diff --git a/docs/api/deeplabcut.stitch_tracklets.rst b/docs/api/deeplabcut.stitch_tracklets.rst new file mode 100644 index 0000000000..96677d31a9 --- /dev/null +++ b/docs/api/deeplabcut.stitch_tracklets.rst @@ -0,0 +1 @@ +.. autofunction:: deeplabcut.refine_training_dataset.stitch.stitch_tracklets diff --git a/docs/api/deeplabcut.train_network.rst b/docs/api/deeplabcut.train_network.rst index e724591d21..cd32c85295 100644 --- a/docs/api/deeplabcut.train_network.rst +++ b/docs/api/deeplabcut.train_network.rst @@ -1 +1 @@ -.. autofunction:: deeplabcut.pose_estimation_tensorflow.training.train_network +.. autofunction:: deeplabcut.compat.train_network diff --git a/docs/beginner-guides/Training-Evaluation.md b/docs/beginner-guides/Training-Evaluation.md index de67d92198..18ab3bf736 100644 --- a/docs/beginner-guides/Training-Evaluation.md +++ b/docs/beginner-guides/Training-Evaluation.md @@ -40,7 +40,9 @@ After training, it's time to see how well your model performs. ### Understanding the Evaluation Results -- **Performance Metrics:** DLC will assess the latest snapshot of your model, generating a `.CSV` file with performance metrics. This file is stored in the **`evaluate network`** folder within your project. +- **Performance Metrics:** DLC will assess the latest snapshot of your model, generating a `.CSV` file with performance +metrics. This file is stored in the **`evaluation-results`** (for TensorFlow models) or the +**`evaluation-results-pytorch`** (for PyTorch models) folder within your project. ![Combined Evaluation Results in DeepLabCut](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/1717779617667-0RLTM9DVRALN9YIKSHJZ/combined-evaluation-results.png?format=750w)) diff --git a/docs/beginner-guides/beginners-guide.md b/docs/beginner-guides/beginners-guide.md index 0c8d323647..eeef2d8c4f 100644 --- a/docs/beginner-guides/beginners-guide.md +++ b/docs/beginner-guides/beginners-guide.md @@ -1,3 +1,4 @@ +(beginners-guide)= # Using DeepLabCut DLC LIVE! @@ -38,13 +39,13 @@ Now, we are going to install the core dependencies. The way this works is that t `PyTorch` is the backend deep-learning language we wrote DLC3 in. To select the right version, head to the [“Install PyTorch”](https://pytorch.org/get-started/locally/) instructions in the official PyTorch Docs. Select your desired PyTorch build, operating system, select conda as your package manager 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 CUDA 11.8** +- **GPU version of pytorch for CUDA 12.4** ``` -conda install pytorch cudatoolkit=11.8 -c pytorch +pip install torch torchvision --index-url https://download.pytorch.org/whl/cu124 ``` - **CPU only version of pytorch, using the latest version** ``` -conda install pytorch cpuonly -c pytorch +pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu ``` **(2) Install DeepLabCut** @@ -61,7 +62,7 @@ pip install "git+https://github.com/DeepLabCut/DeepLabCut.git@pytorch_dlc#egg=de ``` - OR run for the **Stable release:** ``` -pip install "deeplabcut[gui,modelzoo,tf]" +pip install "deeplabcut[gui,modelzoo,wandb]" ``` - This gives you DeepLabCut, the DLC GUI (gui), our latest neural networks (modelzoo) and a cool data logger (wandb) if you choose to use it later on! diff --git a/docs/beginner-guides/labeling.md b/docs/beginner-guides/labeling.md index 7d944137a6..e5c7492722 100644 --- a/docs/beginner-guides/labeling.md +++ b/docs/beginner-guides/labeling.md @@ -1,3 +1,4 @@ +(labeling)= # Labeling GUI ## Selecting Frames to Label @@ -45,7 +46,9 @@ Alright, you've got your extracted frames ready. Now comes the labeling! - **Navigate Through Frames:** Use the slider to go from one frame to the next after you're done labeling. - **Save Progress:** Remember to save your work as you go with **`Command and S`** (or **`Ctrl and S`** on Windows). -> 💡 **Note:** For a detailed walkthrough on using the Napari labeling GUI, have a look at the [DeepLabCut Napari Guide](https://deeplabcut.github.io/DeepLabCut/docs/napari_GUI.html). Additionally, you can watch our instructional [YouTube video](https://www.youtube.com/watch?v=hsA9IB5r73E) for more insights and tips. +> 💡 **Note:** For a detailed walkthrough on using the Napari labeling GUI, have a look at the +[DeepLabCut Napari Guide](napari-gui). Additionally, you can watch our instructional +[YouTube video](https://www.youtube.com/watch?v=hsA9IB5r73E) for more insights and tips. ### Completing the Set diff --git a/docs/beginner-guides/manage-project.md b/docs/beginner-guides/manage-project.md index a53789360f..3f26589ae2 100644 --- a/docs/beginner-guides/manage-project.md +++ b/docs/beginner-guides/manage-project.md @@ -41,4 +41,4 @@ A **`Configuration Editor`** window will open, displaying all the configuration - **Save the Configuration:** Once you're satisfied with the modifications, click **`Save`**. This will store your changes and return you to the main GUI window. -## Next, head over the beginner guide for [Labeling your data](https://deeplabcut.github.io/DeepLabCut/docs/labelling) +## Next, head over the beginner guide for [Labeling your data](labeling) diff --git a/docs/beginner-guides/video-analysis.md b/docs/beginner-guides/video-analysis.md index 59c976360d..849d8b6638 100644 --- a/docs/beginner-guides/video-analysis.md +++ b/docs/beginner-guides/video-analysis.md @@ -16,7 +16,7 @@ After training and evaluating your model, the next step is to apply it to your v - **Find Results in Your Project Folder:** After analysis, go to your project's video folder. - **Analysis Files:** Look also for a `.metapickle`, an `.h5`, and possibly a `.csv` file for detailed analysis data. -- **Review the Plot Poses Subfolder:** This contains visual outputs of the video analysis. +- **Review the "plot-poses" subfolder:** This contains visual outputs of the video analysis. ![Plot poses](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/1717779600836-YOWM5T2MBY0JN1LB537B/plot-poses.png?format=500w) diff --git a/docs/benchmark.md b/docs/benchmark.md index 114568decd..612e307d0f 100644 --- a/docs/benchmark.md +++ b/docs/benchmark.md @@ -32,4 +32,4 @@ benchmarks. For an example of how to implement a benchmark submission, refer to .. automodule:: deeplabcut.benchmark.metrics :members: :show-inheritance: -``` \ No newline at end of file +``` diff --git a/docs/convert_maDLC.md b/docs/convert_maDLC.md index 6671afb1ef..19dd017692 100644 --- a/docs/convert_maDLC.md +++ b/docs/convert_maDLC.md @@ -1,10 +1,12 @@ (convert-maDLC)= -# How to convert a pre-2.2 project for use with DeepLabCut 2.2 +# How to convert a pre-2.2 project for use with DeepLabCut 2.2 or later DLC! -If you have a pre-2.2 project (`labeled-data`) with a **single animal** that you want to use with a multianimal project in DLC 2.2, i.e. use your older data to now train the new multi-task deep neural network, here is what you need to do. +If you have a pre-2.2 project (`labeled-data`) with a **single animal** that you want to use with a multianimal project +in DLC 2.2 or later, i.e. use your older data to now train the new multi-task deep neural network, here is what you +need to do. (1) We recommend you make a back-up of your project folder. @@ -14,7 +16,8 @@ If you have a pre-2.2 project (`labeled-data`) with a **single animal** that you

-- After `task, scorer, date, project_path` please add the following (i.e. in the image above, you would start adding below line 6) Note, the ordering isn't important but useful to keep consistent with the template: +- After `task, scorer, date, project_path` please add the following (i.e. in the image above, you would start adding +below line 6) Note, the ordering isn't important but useful to keep consistent with the template: ```python multianimalproject: true @@ -29,9 +32,12 @@ individuals: - mouse1 ``` -- `"uniquebodyparts: []` can stay blank, unless you have other items labeled you want to estimate (consider these as similar to bodyparts in pre-2.2); i.e. corners of a box, etc. All unique bodyparts should not be connected to the multianimal bodyparts in the skeleton you will eventually make. But see "advanced option" below. +- `"uniquebodyparts: []` can stay blank, unless you have other items labeled you want to estimate (consider these as +similar to bodyparts in pre-2.2); i.e. corners of a box, etc. All unique bodyparts should not be connected to the +multianimal bodyparts in the skeleton you will eventually make. See "advanced option" below. -- Please move your "bodyparts:" to "multianimalbodyparts:" (bodypart names must stay the same!) These are the parts that will always be interconnected fully! +- Please move your "bodyparts:" to "multianimalbodyparts:" (bodypart names must stay the same!) These are the parts +that will always be interconnected fully! ```python multianimalbodyparts: - snout @@ -46,20 +52,25 @@ then you can set `bodyparts: MULTI!` deeplabcut.convert2_maDLC(path_config_file, userfeedback=True) ``` -Now you will see that your data within `labeled-data` are converted to a new format, and the single animal format was saved for you under a new file named `CollectedData_ ...singleanimal.h5` and `.csv` as a back-up! +Now you will see that your data within `labeled-data` are converted to a new format, and the single animal format was +saved for you under a new file named `CollectedData_ ...singleanimal.h5` and `.csv` as a back-up! -(4) We strongly recommend to first run check_labels and verify that the conversion was as expected before creating a multianimal training dataset. For instance, you can load this project `config.yaml` in the Project Manager GUI and check labels then create a multi-animal training set with +(4) We strongly recommend to first run check_labels and verify that the conversion was as expected before creating a +multianimal training dataset. For instance, you can load this project `config.yaml` in the Project Manager GUI and +check labels then create a multi-animal training set with ```python deeplabcut.create_multianimaltraining_dataset(path_config_file) ``` to begin training. -**Advanced option:** You can also assign former `bodyparts` to either `uniquebodyparts` or `multianimalbodyparts` (you can even leave some unassigned, which means they will be dropped in the conversion). +**Advanced option:** You can also assign former `bodyparts` to either `uniquebodyparts` or `multianimalbodyparts` +(you can even leave some unassigned, which means they will be dropped in the conversion). Example: Imagine you had a project with the moon and a rocket with two parts labeled: `bodyparts: [moon, rocket_tip,rocket_bottom]` -Now you want to use this former project (labeled-data) and work on a new dataset (videos) with one moon but multiple (3) rockets. Then convert it as follows: +Now you want to use this former project (labeled-data) and work on a new dataset (videos) with one moon but multiple +(3) rockets. Then convert it as follows: ``` individuals: [rocket1, rocket2, rocket3] uniquebodyparts: [moon] diff --git a/docs/course.md b/docs/course.md index 2ad096995f..b71438cb4f 100644 --- a/docs/course.md +++ b/docs/course.md @@ -1,5 +1,10 @@ ## DeepLabCut Self-paced Course +::::{warning} +This course was designed for DLC 2. +An updated version for DLC 3 is in the works. +:::: + Do you have video of animal behaviors? Step 1: Get Poses ... DLC LIVE! @@ -17,7 +22,7 @@ We expect it to take *roughly* 1-2 weeks to get through if you do it rigorously. ## Installation: You need Python and DeepLabCut installed! -- [See these "beginner docs" for help!](https://deeplabcut.github.io/DeepLabCut/docs/beginners-guide.html) +- [See these "beginner docs" for help!](beginners-guide) - **WATCH:** overview of conda: [Python Tutorial: Anaconda - Installation and Using Conda](https://www.youtube.com/watch?v=YJC6ldI3hWk) @@ -32,10 +37,8 @@ You need Python and DeepLabCut installed! - **Learning:** learning and teaching signal processing, and overview from Prof. Demba Ba [talk at JupyterCon](https://www.youtube.com/watch?v=ywz-LLYwkQQ) -- **Learning:** Watch a talk from Alexander Mathis (a lead DeepLabCut developer) [talk about DeepLabCut!](https://www.youtube.com/watch?v=ZjWPHM0sL4E) - - **DEMO:** Can I DEMO DEEPLABCUT (DLC) quickly? - - Yes: [you can click through this DEMO notebook](https://github.com/DeepLabCut/DeepLabCut/blob/master/examples/COLAB/COLAB_DEMO_mouse_openfield.ipynb) + - Yes: [you can click through this DEMO notebook](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/COLAB/COLAB_DEMO_mouse_openfield.ipynb) - AND follow along with me: [Video Tutorial!](https://www.youtube.com/watch?v=DRT-Cq2vdWs) @@ -55,9 +58,9 @@ You need Python and DeepLabCut installed! **What you need:** any videos where you can see the animals/objects, etc. You can use our demo videos, grab some from the internet, or use whatever older data you have. Any camera, color/monochrome, etc will work. Find diverse videos, and label what you want to track well :) -- IF YOU ARE PART OF THE COURSE: you will be contributing to the DLC Model Zoo :smile: +- IF YOU ARE PART OF THE COURSE: you will be contributing to the DLC Model Zoo 😊 - - **Slides:** [Overview of starting new projects](https://github.com/DeepLabCut/DeepLabCut-Workshop-Materials/blob/master/part1-labeling.pdf) + - **Slides:** [Overview of starting new projects](https://github.com/DeepLabCut/DeepLabCut-Workshop-Materials/blob/main/part1-labeling.pdf) - **READ ME PLEASE:** [DeepLabCut, the science](https://rdcu.be/4Rep) - **READ ME PLEASE:** [DeepLabCut, the user guide](https://rdcu.be/bHpHN) - **WATCH:** Video tutorial 1: [using the Project Manager GUI](https://www.youtube.com/watch?v=KcXogR-p5Ak) @@ -71,12 +74,12 @@ You can use our demo videos, grab some from the internet, or use whatever older ### **Module 2: Neural Networks** - - **Slides:** [Overview of creating training and test data, and training networks](https://github.com/DeepLabCut/DeepLabCut-Workshop-Materials/blob/master/part2-network.pdf) + - **Slides:** [Overview of creating training and test data, and training networks](https://github.com/DeepLabCut/DeepLabCut-Workshop-Materials/blob/main/part2-network.pdf) - **READ ME PLEASE:** [What are convolutional neural networks?](https://towardsdatascience.com/a-comprehensive-guide-to-convolutional-neural-networks-the-eli5-way-3bd2b1164a53) - **READ ME PLEASE:** Here is a new paper from us describing challenges in robust pose estimation, why PRE-TRAINING really matters - which was our major scientific contribution to low-data input pose-estimation - and it describes new networks that are available to you. [Pretraining boosts out-of-domain robustness for pose estimation](https://paperswithcode.com/paper/pretraining-boosts-out-of-domain-robustness) - - **MORE DETAILS:** ImageNet: check out the original paper and dataset: http://www.image-net.org/ (link to [ppt from Dr. Fei-Fei Li](http://www.image-net.org/papers/ImageNet_2010.ppt)) + - **MORE DETAILS:** ImageNet: check out the original paper and dataset: http://www.image-net.org/ - **REVIEW PAPER:** [A Primer on Motion Capture with Deep Learning: Principles, Pitfalls and Perspectives](https://www.sciencedirect.com/science/article/pii/S0896627320307170) @@ -84,18 +87,18 @@ You can use our demo videos, grab some from the internet, or use whatever older review! Before you create a training/test set, please read/watch: - - **More information:** [Which types neural networks are available, and what should I use?](https://github.com/AlexEMG/DeepLabCut/wiki/What-neural-network-should-I-use%3F) + - **More information:** [Which types neural networks are available, and what should I use?](https://github.com/DeepLabCut/DeepLabCut/wiki/What-neural-network-should-I-use%3F-(Trade-offs,-speed-performance,-and-considerations)) - **WATCH:** Video tutorial 1: [How to test different networks in a controlled way](https://www.youtube.com/watch?v=WXCVr6xAcCA) - Now, decide what model(s) you want to test. - IF you want to train on your CPU, then run the step `create_training_dataset`, in the GUI etc. on your own computer. - - IF you want to use GPUs on google colab, [**(1)** watch this FIRST/follow along here!](https://www.youtube.com/watch?v=qJGs8nxx80A) **(2)** move your whole project folder to Google Drive, and then [**use this notebook**](https://github.com/DeepLabCut/DeepLabCut/blob/master/examples/COLAB/COLAB_YOURDATA_TrainNetwork_VideoAnalysis.ipynb) + - IF you want to use GPUs on google colab, [**(1)** watch this FIRST/follow along here!](https://www.youtube.com/watch?v=qJGs8nxx80A) **(2)** move your whole project folder to Google Drive, and then [**use this notebook**](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/COLAB/COLAB_YOURDATA_TrainNetwork_VideoAnalysis.ipynb) **MODULE 2 webinar**: https://youtu.be/ILsuC4icBU0 ### **Module 3: Evalution of network performance** - - **Slides** [Evalute your network](https://github.com/DeepLabCut/DeepLabCut-Workshop-Materials/blob/master/part3-analysis.pdf) + - **Slides** [Evalute your network](https://github.com/DeepLabCut/DeepLabCut-Workshop-Materials/blob/main/part3-analysis.pdf) - **WATCH:** [Evaluate the network in ipython](https://www.youtube.com/watch?v=bgfnz1wtlpo) - why evaluation matters; how to benchmark; analyzing a video and using scoremaps, conf. readouts, etc. diff --git a/docs/deeplabcutlive.md b/docs/deeplabcutlive.md index f8784b651d..f1edbff3cc 100644 --- a/docs/deeplabcutlive.md +++ b/docs/deeplabcutlive.md @@ -1,3 +1,4 @@ +(deeplabcut-live)= # DeepLabCut-Live! We provide two additional pip packages that allow you to record and stream camera data and run DeeplabCut models in real-time. diff --git a/docs/gui/PROJECT_GUI.md b/docs/gui/PROJECT_GUI.md index 106e7eca5c..e0883c324e 100644 --- a/docs/gui/PROJECT_GUI.md +++ b/docs/gui/PROJECT_GUI.md @@ -10,7 +10,7 @@ As some users may be more comfortable working with an interactive interface, we (1) Install DeepLabCut using the simple-install with Anaconda found [here!](how-to-install)*. Now you have DeepLabCut installed, but if you want to update it, either follow the prompt in the GUI which will ask you to upgrade when a new version is available, or just go into your env (activate DEEPLABCUT) then run: -` pip install 'deeplabcut[gui,modelzoo]'` *but please see [full install guide](https://deeplabcut.github.io/DeepLabCut/docs/installation.html)! +` pip install 'deeplabcut[gui,modelzoo]'` *but please see [full install guide](how-to-install)! (2) Open the terminal and run: `python -m deeplabcut` @@ -23,15 +23,14 @@ Now you have DeepLabCut installed, but if you want to update it, either follow t Start at the Project Management Tab and work your way through the tabs to built your customized model and deploy it on new data. We recommend to keep the terminal visible (as well as the GUI) so you can see the ongoing processes as you step through your project, or any errors that might arise. -- For specific napari-based labeling features, see the ["napari gui" docs](https://deeplabcut.github.io/DeepLabCut/docs/napari_GUI.html#usage). +- For specific napari-based labeling features, see the ["napari gui" docs](napari-gui-usage). - To change from dark to light mode, set appearance at the top:

- -## VIDEO DEMOS: How to launch and run the Project Manager GUI: +## Video Demos: How to launch and run the Project Manager GUI: **Click on the images!** @@ -39,17 +38,17 @@ Note that currently the video demo is the wxPython version, but the logic is the [![Watch the video](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/1572824438905-QY9XQKZ8LAJZG6BLPWOQ/ke17ZwdGBToddI8pDm48kIIa76w436aRzIF_cdFnEbEUqsxRUqqbr1mOJYKfIPR7LoDQ9mXPOjoJoqy81S2I8N_N4V1vUb5AoIIIbLZhVYxCRW4BPu10St3TBAUQYVKcLthF_aOEGVRewCT7qiippiAuU5PSJ9SSYal26FEts0MmqyMIhpMOn8vJAUvOV4MI/guilaunch.jpg?format=1000w)](https://youtu.be/KcXogR-p5Ak) -### Using the Project Manager GUI with the latest DLC code (single animals, plus objects): :arrow_down: +### Using the Project Manager GUI with the latest DLC code (single animals, plus objects): ⬇️ [![Watch the video](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/1589046800303-OV1CCNZINWDMF1PZWCWE/ke17ZwdGBToddI8pDm48kB4PVlRPKDmSlQNbUD3wvXgUqsxRUqqbr1mOJYKfIPR7LoDQ9mXPOjoJoqy81S2I8N_N4V1vUb5AoIIIbLZhVYxCRW4BPu10St3TBAUQYVKcaja1QZ1SznGf7WzFOi-J6zLusnaF2VdeZcKivwxvFiDfGDqVYuwbAlftad9hfoui/dlc_gui_22.png?format=1000w)](https://www.youtube.com/watch?v=JDsa8R5J0nQ) -[READ MORE HERE](important-info-regd-usage) +[Read more here](important-info-regd-usage) ### Using the Project Manager GUI with the latest DLC code (multiple identical-looking animals, plus objects): [![Watch the video](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/1589047147498-G1KTFA5BXR4PVHOOR7OG/ke17ZwdGBToddI8pDm48kJDij24pM2COisBTLIGjR1pZw-zPPgdn4jUwVcJE1ZvWQUxwkmyExglNqGp0IvTJZamWLI2zvYWH8K3-s_4yszcp2ryTI0HqTOaaUohrI8PIel60EThn7SDFlTiSprUhmjQQHn9bhdY9dnQSKs8bCCo/Untitled.png?format=1000w)](https://www.youtube.com/watch?v=Kp-stcTm77g) -[READ MORE HERE](important-info-regd-usage) +[Read more here](important-info-regd-usage) ## VIDEO DEMO: How to benchmark your data with the new networks and data augmentation pipelines: diff --git a/docs/gui/napari_GUI.md b/docs/gui/napari_GUI.md index 5fe4fb84c5..4875b23237 100644 --- a/docs/gui/napari_GUI.md +++ b/docs/gui/napari_GUI.md @@ -1,3 +1,4 @@ +(napari-gui)= # napari labeling GUI We replaced wxPython with PySide6 + as of version 2.3. Here is how to use the napari-aspects of the new GUI. It is available in napari-hub as a stand alone GUI as well as integrated into our main GUI, [please see docs here](https://deeplabcut.github.io/DeepLabCut/docs/PROJECT_GUI.html). @@ -30,6 +31,7 @@ To install latest development version: ` pip install git+https://github.com/DeepLabCut/napari-deeplabcut.git ` +(napari-gui-usage)= ## Usage To use the full GUI, please run: @@ -46,8 +48,7 @@ All accepted files (`config.yaml`, images, `.h5` data files) can be loaded eithe The easiest way to get started is to drop a folder (typically a folder from within a DeepLabCut's `labeled-data` directory), and, if labeling from scratch, drop the corresponding `config.yaml` to automatically add a `Points layer` and populate the dropdown menus. -[🎥 DEMO -](https://youtu.be/hsA9IB5r73E) +[🎥 DEMO](https://youtu.be/hsA9IB5r73E) **Tools & shortcuts are:** diff --git a/docs/images/box1-multi.png b/docs/images/box1-multi.png new file mode 100644 index 0000000000..2da7320667 Binary files /dev/null and b/docs/images/box1-multi.png differ diff --git a/docs/images/box1-single.png b/docs/images/box1-single.png new file mode 100644 index 0000000000..c5c802d486 Binary files /dev/null and b/docs/images/box1-single.png differ diff --git a/docs/installation.md b/docs/installation.md index 6872255a83..04bc2e40ca 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -6,6 +6,41 @@ - We recommend for most users to use our supplied CONDA environment. - Please note, you will get the best performance with using a GPU! Please see the section on [GPU support](https://deeplabcut.github.io/DeepLabCut/docs/installation.html#gpu-support) to install your GPU driver and CUDA. +````{admonition} Familiar with python packages and conda? Quick install here. +:class: dropdown + +This assumes you have `conda`/`mamba` installed and installs DeepLabCut in a fresh +environment. If you have an NVIDIA GPU, install PyTorch according to [their instructions +](https://pytorch.org/get-started/locally/) (with your desired CUDA version) - you just +need your GPU drivers installed. + +```bash +conda create -n DEEPLABCUT python=3.10 +conda activate DEEPLABCUT +conda install -c conda-forge pytables==3.8.0 + +# install torch with your desired CUDA version (or CPU) - check their website +# for the exact command +pip install torch torchvision + +# install the latest version of DeepLabCut +pip install --pre deeplabcut +# or if you want to use the GUI +pip install deeplabcut[gui] + +# ONLY IF YOU HAVE A CUDA GPU - check that PyTorch can access your GPU; this +# should print `True` +python -c "import torch; print(torch.cuda.is_available())" +``` + +Why do we install [pytables](https://www.pytables.org/usersguide/installation.html) with +`conda` and not `pip`? Because it requires some libraries that not all users will have +installed, and conda will ensure that they are installed as well. + +If you're familiar with the command line and want TensorFlow support, look [below]( +deeplabcut-with-tf-install) for a fresh installation that has worked for us (on Linux) +and makes it possible to use the GPU with both PyTorch and TensorFlow. +```` ## CONDA: The installation process is as easy as this figure! --> @@ -24,15 +59,15 @@ - **Apple M-chip GPU?** Be sure to install miniconda3, and your GPU will be used by default. ```` - ### Step 1: Install Python via Anaconda -#### Install [anaconda](https://www.anaconda.com/distribution/), or use miniconda3 for MacOS users (see below) +#### Install [anaconda](https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html#), or use miniconda3 for MacOS users (see below) - Anaconda is an easy way to install Python and additional packages across various operating systems. With Anaconda you create all the dependencies in an [environment](https://conda.io/docs/user-guide/tasks/manage-environments.html) on your machine. ```{Hint} -Download anaconda for your operating system: https://www.anaconda.com/distribution/. +Download anaconda for your operating system: [anaconda.com/download/ +](https://www.anaconda.com/download/) ``` - IF you use a M1 or M2 chip in your MacBook with v12.5+ (typically 2020 or newer machines), we recommend **miniconda3,** which operates with the same principles as anaconda. This is straight forward and explained in detail here: https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html. But in short, open the program "terminal" and copy/paste and run the code that is supplied below. @@ -83,11 +118,46 @@ Be sure you are in the folder that has the `.yaml` file, then run: Now you should see (`nameofenv`) on the left of your terminal screen, i.e. ``(DEEPLABCUT) YourName-MacBook...`` NOTE: no need to run pip install deeplabcut, as it is already installed!!! :) +(deeplabcut-with-tf-install)= #### 💡 Notice: PyTorch and TensorFlow Support within DeepLabCut -```{Hint} + +````{admonition} DeepLabCut TensorFlow Support :class: dropdown -As of June 2024 we have a PyTorch Engine backend and we will be depreciating the TensorFlow backend by the end of 2024. Currently, if you want to use TensorFlow, you need to run `pip install deeplabcut[tf]` in order to install the correct version of TensorFlow in your conda env. Please note, we will be providing bug fixes, but we will not be supporting new TensorFlow versions beyond 2.10 (Windows), and 2.12 for other OS. +As of June 2024 we have a PyTorch Engine backend and we will be depreciating the +TensorFlow backend by the end of 2024. Currently, if you want to use TensorFlow, you +need to run `pip install deeplabcut[tf]` in order to install the correct version of +TensorFlow in your conda env. Please note, we will be providing bug fixes, but we will +not be supporting new TensorFlow versions beyond 2.10 (Windows), and 2.12 for other OS. + +Installing TensorFlow and getting it to have access to the GPU can be a bit tricky. +Check TensorFlow's [compatibility matrix](https://www.tensorflow.org/install/source#gpu) +to know which version of CUDA and cuDNN you should install. + +We have found that installing DeepLabCut with the following commands works well for +Linux users to install PyTorch 2.3.1, TensorFlow 2.12, CUDA 11.8 and cuDNN 8 in a Conda +environment: + +```bash +conda create -n deeplabcut-with-tf "python=3.10" +conda activate deeplabcut-with-tf + +# Install the desired TensorFlow version, built for CUDA 11.8 and cuDNN 8 +pip install "tensorflow==2.12" "tensorpack>=0.11" "tf_slim>=1.1.0" + +# Install PyTorch with a version using CUDA 11.8 and cuDNN 8 +pip install "torch==2.3.1" torchvision --index-url https://download.pytorch.org/whl/cu118 + +# Create symbolic links to NVIDIA shared libraries for TensorFlow +# -> as described in their installation docs: +# https://www.tensorflow.org/install/pip#step-by-step_instructions + +pushd $(dirname $(python -c 'print(__import__("tensorflow").__file__)')) +ln -svf ../nvidia/*/lib/*.so* . +popd + +pip install --pre deeplabcut ``` +```` **Great, that's it! DeepLabCut is installed!** 🎉💜 @@ -109,7 +179,7 @@ To git clone type: ``git clone https://github.com/DeepLabCut/DeepLabCut.git``). ### PIP: -- Everything you need to build custom models within DeepLabCut (i.e., use our source code and our dependencies) can be installed with `pip install 'deeplabcut[gui]'` (for GUI support w/tensorflow) or without the gui: `pip install 'deeplabcut'`. +- Everything you need to build custom models within DeepLabCut (i.e., use our source code and our dependencies) can be installed with `pip install 'deeplabcut[gui]'` (for GUI support w/PyTorch) or without the gui: `pip install 'deeplabcut'`. - If you want to use the SuperAnimal models, then please use `pip install 'deeplabcut[gui,modelzoo]'`. ## DOCKER: @@ -118,13 +188,29 @@ To git clone type: ``git clone https://github.com/DeepLabCut/DeepLabCut.git``). ## Pro Tips: -More [installation ProTips](installTips) are also available. +More [installation ProTips](installation-tips) are also available. -If you ever want to update your DLC, just run `pip install --upgrade deeplabcut` once you are inside your env. If you want to use a specific release, then you need to specify the version you want, such as `pip install deeplabcut==2.2`. Once installed, you can check the version by running `import deeplabcut` `deeplabcut.__version__`. Don't be afraid to update, DLC is backwards compatible with your 2.0+ projects and performance continues to get better and new features are added nearly monthly. +If you ever want to update your DLC, just run `pip install --upgrade deeplabcut` once +you are inside your env. If you want to use a specific release, then you need to specify +the version you want, such as `pip install deeplabcut==3.0`. Once installed, you can +check the version by running `import deeplabcut` `deeplabcut.__version__`. Don't be +afraid to update, DLC is backwards compatible with your 2.0+ projects and performance +continues to get better and new features are added nearly monthly. -Here are some conda environment management tips: https://kapeli.com/cheat_sheets/Conda.docset/Contents/Resources/Documents/index +**All of the data you labelled in version 2.X is also compatible with version 3+ and the +PyTorch engine**! There is no change in the workflow or the way labels are handled: the +big changes happen under-the-hood! If you've been working with DeepLabCut 2.X and want +to learn more about moving to the PyTorch engine, checkout our docs on [moving from +TensorFlow to PyTorch](dlc3-user-guide) -**Pro Tip:** If you want to modify code and then test it, you can use our provided testscripts. This would mean you need to be up-to-date with the latest GitHub-based code though! Please see [here](installTips) on how to get the latest GitHub code, and how to test your installation by following this video: https://www.youtube.com/watch?v=IOWtKn3l33s. +Here are some conda environment management tips: [kapeli.com: Conda Cheat Sheet]( +https://kapeli.com/cheat_sheets/Conda.docset/Contents/Resources/Documents/index) + +**Pro Tip:** If you want to modify code and then test it, you can use our provided +testscripts. This would mean you need to be up-to-date with the latest GitHub-based code +though! Please see [here](installation-tips) on how to get the latest GitHub code, and +how to test your installation by following this video: +https://www.youtube.com/watch?v=IOWtKn3l33s. ## Creating your own customized conda env (recommended route for Linux: Ubuntu, CentOS, Mint, etc.) @@ -136,7 +222,9 @@ In the terminal type: `conda create -n DLC python=3.10` -**Current version:** The only thing you then need to add to the env is deeplabcut (`pip install deeplabcut`) or `pip install 'deeplabcut[gui]'` which has a napari based GUI. +**Current version:** The only thing you then need to add to the env is deeplabcut ( +`pip install deeplabcut`) or `pip install 'deeplabcut[gui]'` which has a napari based +GUI. ## **GPU Support:** @@ -145,36 +233,51 @@ The ONLY thing you need to do **first** if you have an NVIDIA GPU and the matchi - CUDA: https://developer.nvidia.com/cuda-downloads (just follow the prompts here!) - DRIVERS: https://www.nvidia.com/Download/index.aspx -#### The most common "new user" hurdle is installing and using your GPU, so don't get discouraged! +### The most common "new user" hurdle is installing and using your GPU, so don't get discouraged! -**CRITICAL:** If you have a GPU, you should FIRST **install the NVIDIA CUDA package and an appropriate driver for your specific GPU**, then you can use the supplied conda file. Please follow the instructions found here https://www.tensorflow.org/install/gpu, and more tips below, to install the correct version of CUDA and your graphic card driver. The order of operations matters. +**CRITICAL:** If you have a GPU, you should FIRST **install an appropriate driver for +your specific GPU**, then you can use the supplied conda file. You'll need an NVIDIA GPU +which is compatible with CUDA. To see a list of CUDA-enabled NVIDIA GPUs, please [see +their website](https://developer.nvidia.com/cuda-gpus). -- Here we provide notes on how to install and check your GPU use with TensorFlow (which is used by DeepLabCut and already installed with the Anaconda files above). Thus, you do not need to independently install tensorflow. +- Here we provide notes on how to install and check your GPU use with TensorFlow (which +is used by DeepLabCut and already installed with the Anaconda files above). Thus, you do +not need to independently install tensorflow. +**FIRST**, install a driver for your GPU. Find DRIVER HERE: +https://www.nvidia.com/download/index.aspx -**FIRST**, install a driver for your GPU. Find DRIVER HERE: https://www.nvidia.com/download/index.aspx -- check which driver is installed by typing this into the terminal: ``nvidia-smi``. +- Check which driver is installed by typing this into the terminal: ``nvidia-smi``. **SECOND**, install CUDA: https://developer.nvidia.com/ (Note that cuDNN, https://developer.nvidia.com/cudnn, is supplied inside the anaconda environment files, so you don't need to install it again). **THIRD:** Follow the steps above to get the `DEEPLABCUT` conda file and install it! -##### Notes: - - - **As of verion 3.0+ we moved to PyTorch. The Last supported vesion of TensorFlow is 2.10 (window users) and 2.12 for others (we have not tested beyond this).** - - Please be mindful different versions of TensorFlow require different CUDA versions. - - As the combination of TensorFlow and CUDA matters, we strongly encourage you to **check your driver/cuDNN/CUDA/TensorFlow versions** [on this StackOverflow post](https://stackoverflow.com/questions/30820513/what-is-version-of-cuda-for-nvidia-304-125/30820690#30820690). - - To check your GPU is working, in the terminal, run: - - `nvcc -V` to check your installed version(s). - -- The best practice is to then run the supplied `testscript.py` (this is inside the examples folder you acquired when you git cloned the repo). Here is more information/a short [video on running the testscript](https://www.youtube.com/watch?v=IOWtKn3l33s). - -- Additionally, if you want to use the bleeding edge, with yout git clone you also get the latest code. While inside the main DeepLabCut folder, you can run `./reinstall.sh` to be sure it's installed (more here: https://github.com/DeepLabCut/DeepLabCut/wiki/How-to-use-the-latest-GitHub-code) - -- You can test that your GPU is being properly engaged with these additional [tips](https://www.tensorflow.org/programmers_guide/using_gpu). - -- Ubuntu users might find this [installation guide](https://deeplabcut.github.io/DeepLabCut/docs/recipes/installTips.html#installation-on-ubuntu-20-04-lts) for a fresh ubuntu install useful as well. +#### Notes: + +- **As of verion 3.0+ we moved to PyTorch. The Last supported vesion of TensorFlow is +2.10 (window users) and 2.12 for others (we have not tested beyond this).** +- Please be mindful different versions of TensorFlow require different CUDA versions. +- As the combination of TensorFlow and CUDA matters, we strongly encourage you to +**check your driver/cuDNN/CUDA/TensorFlow versions** [on this StackOverflow post]( +https://stackoverflow.com/questions/30820513/what-is-version-of-cuda-for-nvidia-304-125/30820690#30820690 +). +- To check your GPU is working, in the terminal, run: + +`nvcc -V` to check your installed version(s). + +- The best practice is to then run the supplied `testscript_pytorch_single_animal.py` +(or `testscript.py` for the TensorFlow engine); this is inside the examples folder you +acquired when you git cloned the repo. Here is more information/a short +[video on running the testscript](https://www.youtube.com/watch?v=IOWtKn3l33s). +- Additionally, if you want to use the bleeding edge, with yout git clone you also get +the latest code. While inside the main DeepLabCut folder, you can run `./reinstall.sh` +to be sure it's installed (more [here](installation-tips)) +- You can test that your GPU is being properly engaged with these additional [tips]( +https://www.tensorflow.org/programmers_guide/using_gpu). +- Ubuntu users might find this [installation guide]( +https://deeplabcut.github.io/DeepLabCut/docs/recipes/installTips.html#installation-on-ubuntu-20-04-lts +) for a fresh ubuntu install useful as well. ## Troubleshooting: @@ -230,7 +333,3 @@ If you perform the system-wide installation, and the computer has other Python p - If you want to use a pre3.0 version, you will need [TensorFlow](https://www.tensorflow.org/) (we used version 1.0 in the Nature Neuroscience paper, later versions also work with the provided code (we tested **TensorFlow versions 1.0 to 1.15, and 2.0 to 2.10**; we recommend TF2.10 now) for Python 3.8, 3.9, 3.10 with GPU support. - To note, is it possible to run DeepLabCut on your CPU, but it will be VERY slow (see: [Mathis & Warren](https://www.biorxiv.org/content/early/2018/10/30/457242)). However, this is the preferred path if you want to test DeepLabCut on your own computer/data before purchasing a GPU, with the added benefit of a straightforward installation! Otherwise, use our COLAB notebooks for GPU access for testing. - Docker: We highly recommend advanced users use the supplied [Docker container](docker-containers) - - - -Return to [readme](readme). diff --git a/docs/maDLC_UserGuide.md b/docs/maDLC_UserGuide.md index 8f6ca7ab39..8f5b5a964e 100644 --- a/docs/maDLC_UserGuide.md +++ b/docs/maDLC_UserGuide.md @@ -4,9 +4,16 @@ This document should serve as the user guide for maDLC, and it is here to support the scientific advances presented in [Lauer et al. 2022](https://doi.org/10.1038/s41592-022-01443-0). - Note, we strongly encourage you to use the [Project Manager GUI](project-manager-gui) when you first start using multi-animal mode. Each tab is customized for multi-animal when you create or load a multi-animal project. As long as you follow the recommendations within the GUI, you should be good to go! +````{versionadded} 3.0.0 +PyTorch is now available as a deep learning engine for pose estimation models, along +with new model architectures! For more information about moving from TensorFlow to +PyTorch (if you're already familiar with DeepLabCut & the TensorFlow engine), +check out [the PyTorch user guide](dlc3-user-guide). If you're just starting +out with DeepLabCut, we suggest you use the PyTorch backend. +```` + ## How to think about using maDLC: You should think of maDLC being **four** parts. @@ -21,9 +28,9 @@ Thus, you should always label, train, and evaluate the pose estimation performan ## Install: -**Quick start:** If you are using DeepLabCut on the cloud, or otherwise cannot use the GUIs and you should install with: `pip install 'deeplabcut'`; if you need GUI support, please use: `pip install 'deeplabcut[gui]'`. +**Quick start:** If you are using DeepLabCut on the cloud, or otherwise cannot use the GUIs and you should install with: `pip install 'deeplabcut'`; if you need GUI support, please use: `pip install 'deeplabcut[gui]'`. Check the [installation page](how-to-install) for more information, including GPU support. -IF you want to use the bleeding edge version to make edits to the code, see here on how to install it and test it (https://deeplabcut.github.io/DeepLabCut/docs/recipes/installTips.html#how-to-use-the-latest-updates-directly-from-github). +IF you want to use the bleeding edge version to make edits to the code, see [here on how to install it and test it](https://deeplabcut.github.io/DeepLabCut/docs/recipes/installTips.html#how-to-use-the-latest-updates-directly-from-github). ## Get started in the terminal or Project GUI: @@ -35,7 +42,7 @@ Then follow the tabs! It might be useful to read the following, however, so you ```{Hint} 🚨 If you use Windows, please always open the terminal with administrator privileges! Right click, and "run as administrator". ``` - Please read more [here](https://github.com/DeepLabCut/Docker4DeepLabCut2.0), and in our Nature Protocols paper [here](https://www.nature.com/articles/s41596-019-0176-0). And, see our [troubleshooting wiki](https://github.com/DeepLabCut/DeepLabCut/wiki/Troubleshooting-Tips). + Please read more [here](https://deeplabcut.github.io/DeepLabCut/docs/docker.html), and in our Nature Protocols paper [here](https://www.nature.com/articles/s41596-019-0176-0). And, see our [troubleshooting wiki](https://github.com/DeepLabCut/DeepLabCut/wiki/Troubleshooting-Tips). Open an ``ipython`` session and import the package by typing in the terminal: ```python @@ -43,20 +50,25 @@ ipython import deeplabcut ``` -```{TIP:} +```{TIP} for every function there is a associated help document that can be viewed by adding a **?** after the function name; i.e. ``deeplabcut.create_new_project?``. To exit this help screen, type ``:q``. ``` -### Create a New Project: +### (A) Create a New Project ```python -deeplabcut.create_new_project('ProjectName','YourName', ['/usr/FullPath/OfVideo1.avi', '/usr/FullPath/OfVideo2.avi', '/usr/FullPath/OfVideo1.avi'], - copy_videos=True, multianimal=True) +deeplabcut.create_new_project( + "ProjectName", + "YourName", + ["/usr/FullPath/OfVideo1.avi", "/usr/FullPath/OfVideo2.avi", "/usr/FullPath/OfVideo1.avi"], + copy_videos=True, + multianimal=True, +) ``` -Tip: if you want to place the project folder somewhere specific, please also pass : ``working_directory = 'FullPathOftheworkingDirectory'`` +Tip: if you want to place the project folder somewhere specific, please also pass : ``working_directory = "FullPathOftheworkingDirectory"`` -- Note, if you are a linux/macos user the path should look like: ``['/home/username/yourFolder/video1.mp4']``; if you are a Windows user, it should look like: ``[r'C:\username\yourFolder\video1.mp4']`` +- Note, if you are a linux/macOS user the path should look like: ``["/home/username/yourFolder/video1.mp4"]``; if you are a Windows user, it should look like: ``[r"C:\username\yourFolder\video1.mp4"]`` - Note, you can also put ``config_path = `` in front of the above line to create the path to the config.yaml that is used in the next step, i.e. ``config_path=deeplabcut.create_project(...)``) - If you do not, we recommend setting a variable so this can be easily used! Once you run this step, the config_path is printed for you once you run this line, so set a variable for ease of use, i.e. something like: ```python @@ -64,9 +76,20 @@ config_path = '/thefulloutputpath/config.yaml' ``` - just be mindful of the formatting for Windows vs. Unix, see above. -This set of arguments will create a project directory with the name **Name of the project+name of the experimenter+date of creation of the project** in the **Working directory** and creates the symbolic links to videos in the **videos** directory. The project directory will have subdirectories: **dlc-models**, **labeled-data**, **training-datasets**, and **videos**. All the outputs generated during the course of a project will be stored in one of these subdirectories, thus allowing each project to be curated in separation from other projects. The purpose of the subdirectories is as follows: - -**dlc-models:** This directory contains the subdirectories *test* and *train*, each of which holds the meta information with regard to the parameters of the feature detectors in configuration files. The configuration files are YAML files, a common human-readable data serialization language. These files can be opened and edited with standard text editors. The subdirectory *train* will store checkpoints (called snapshots in TensorFlow) during training of the model. These snapshots allow the user to reload the trained model without re-training it, or to pick-up training from a particular saved checkpoint, in case the training was interrupted. +This set of arguments will create a project directory with the name **Name of the project+name of the experimenter+date of creation of the project** in the **Working directory** and creates the symbolic links to videos in the **videos** directory. The project directory will have subdirectories: **dlc-models**, **dlc-models-pytorch**, **labeled-data**, **training-datasets**, and **videos**. All the outputs generated during the course of a project will be stored in one of these subdirectories, thus allowing each project to be curated in separation from other projects. The purpose of the subdirectories is as follows: + +**dlc-models** and **dlc-models-pytorch** have a similar structure: the first contains +files for the TensorFlow engine while the second contains files for the PyTorch engine. +At the top level in these directories, there are +directories referring to different iterations of labels refinement (see below): **iteration-0**, **iteration-1**, etc. +The refinement iterations directories store shuffle directories, each shuffle directory stores model data related to a +particular experiment: trained and tested on a particular training and testing sets, and with a particular model +architecture. Each shuffle directory contains the subdirectories *test* and *train*, each of which holds the meta +information with regard to the parameters of the feature detectors in configuration files. The configuration files are +YAML files, a common human-readable data serialization language. These files can be opened and edited with standard text +editors. The subdirectory *train* will store checkpoints (called snapshots) during training of the model. These +snapshots allow the user to reload the trained model without re-training it, or to pick-up training from a particular +saved checkpoint, in case the training was interrupted. **labeled-data:** This directory will store the frames used to create the training dataset. Frames from different videos are stored in separate subdirectories. Each frame has a filename related to the temporal index within the corresponding video, which allows the user to trace every frame back to its origin. @@ -75,33 +98,55 @@ This set of arguments will create a project directory with the name **Name of th **videos:** Directory of video links or videos. When **copy\_videos** is set to ``False``, this directory contains symbolic links to the videos. If it is set to ``True`` then the videos will be copied to this directory. The default is ``False``. Additionally, if the user wants to add new videos to the project at any stage, the function **add\_new\_videos** can be used. This will update the list of videos in the project's configuration file. Note: you neither need to use this folder for videos, nor is it required for analyzing videos (they can be anywhere). ```python -deeplabcut.add_new_videos('Full path of the project configuration file*', ['full path of video 4', 'full path of video 5'], copy_videos=True/False) +deeplabcut.add_new_videos( + "Full path of the project configuration file*", + ["full path of video 4", "full path of video 5"], + copy_videos=True/False, +) ``` *Please note, *Full path of the project configuration file* will be referenced as ``config_path`` throughout this protocol. -You can also use annotated data from singe-animal projects, by converting those files. There are docs for this: [convert single to multianimal annotation data](convert-maDLC) +You can also use annotated data from single-animal projects, by converting those files. +There are docs for this: [convert single to multianimal annotation data](convert-maDLC) + +![Box 1 - Multi Animal Project Configuration File Glossary](images/box1-multi.png) + +#### API Docs +````{admonition} Click the button to see API Docs +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.create_new_project.rst +``` +```` -### Configure the Project: +### (B) Configure the Project -- open the **config.yaml** file (in a text editor (like atom, gedit, vim etc.)), which can be found in the subfolder created when you set your project name, to change parameters and identify label names! This is a crucial step. +Next, open the **config.yaml** file, which was created during **create\_new\_project**. +You can edit this file in any text editor. Familiarize yourself with the meaning of the +parameters (Box 1). You can edit various parameters, in particular you **must add the list of *individuals* and *bodyparts* (or points of interest)**. DLC Utils -Next, open the **config.yaml** file, which was created during **create\_new\_project**. You can edit this file in any text editor. Familiarize yourself with the meaning of the parameters (Box 1). You can edit various parameters, in particular you **must add the list of *bodyparts* (or points of interest)** that you want to track. You can also set the *colormap* here that is used for all downstream steps (can also be edited at anytime), like labeling GUIs, videos, etc. Here any [matplotlib colormaps](https://matplotlib.org/tutorials/colors/colormaps.html) will do! +You can also set the *colormap* here that is used for all downstream steps (can also be edited at anytime), like labeling GUIs, videos, etc. Here any [matplotlib colormaps](https://matplotlib.org/tutorials/colors/colormaps.html) will do! An easy way to programmatically edit the config file at any time is to use the function **edit\_config**, which takes the full path of the config file to edit and a dictionary of key–value pairs to overwrite. -````python -edits = {'colormap': 'summer', - 'individuals': ['mickey', 'minnie', 'bianca'], - 'skeleton': [['snout', 'tailbase'], ['snout', 'rightear']]} +```python +import deeplabcut + +config_path = "/path/to/project-dlc-2025-01-01/config.yaml" +edits = { + "colormap": "summer", + "individuals": ["mickey", "minnie", "bianca"], + "skeleton": [["snout", "tailbase"], ["snout", "rightear"]] +} deeplabcut.auxiliaryfunctions.edit_config(config_path, edits) -```` +``` Please DO NOT have spaces in the names of bodyparts, uniquebodyparts, individuals, etc. -**ATTENTIONt:** You need to edit the config.yaml file to **modify the following items** which specify the animal ID, body parts, and any unique labels. Note, we also highly recommend that you use **more bodypoints** that you might be interested in for your experiment, i.e., labeling along the spine/tail for 8 bodypoints would be better than four. This will help the performance. +**ATTENTION:** You need to edit the config.yaml file to **modify the following items** which specify the animal ID, bodyparts, and any unique labels. Note, we also highly recommend that you use **more bodyparts** that you might be interested in for your experiment, i.e., labeling along the spine/tail for 8 bodyparts would be better than four. This will help the performance. Modifying the `config.yaml` is crucial: @@ -123,6 +168,7 @@ multianimalbodyparts: identity: True/False ``` + **Individuals:** are names of "individuals" in the annotation dataset. These should/can be generic (e.g. mouse1, mouse2, etc.). These individuals are comprised of the same bodyparts defined by `multianimalbodyparts`. For annotation in the GUI and training, it is important that all individuals in each frame are labeled. Thus, keep in mind that you need to set individuals to the maximum number in your labeled-data set, .i.e., if there is (even just one frame) with 17 animals then the list should be `- indv1` to `- indv17`. Note, once trained if you have a video with more or less animals, that is fine - you can have more or less animals during video analysis! **Identity:** If you can tell the animals apart, i.e., one might have a collar, or a black marker on the tail of a mouse, then you should label these individuals consistently (i.e., always label the mouse with the black marker as "indv1", etc). If you have this scenario, please set `identity: True` in your `config.yaml` file. If you have 4 black mice, and you truly cannot tell them apart, then leave this as `false`. @@ -131,14 +177,22 @@ identity: True/False **Uniquebodyparts:** are points that you want to track, but that appear only once within each frame, i.e. they are "unique". Typically these are things like unique objects, landmarks, tools, etc. They can also be animals, e.g. in the case where one German shepherd is attending to many sheep the sheep bodyparts would be multianimalbodyparts, the shepherd parts would be uniquebodyparts and the individuals would be the list of sheep (e.g. Polly, Molly, Dolly, ...). -### Select Frames to Label: +### (C) Select Frames to Label **CRITICAL:** A good training dataset should consist of a sufficient number of frames that capture the breadth of the behavior. This ideally implies to select the frames from different (behavioral) sessions, different lighting and different animals, if those vary substantially (to train an invariant, robust feature detector). Thus for creating a robust network that you can reuse in the laboratory, a good training dataset should reflect the diversity of the behavior with respect to postures, luminance conditions, background conditions, animal identities, etc. of the data that will be analyzed. For the simple lab behaviors comprising mouse reaching, open-field behavior and fly behavior, 100−200 frames gave good results [Mathis et al, 2018](https://www.nature.com/articles/s41593-018-0209-y). However, depending on the required accuracy, the nature of behavior, the video quality (e.g. motion blur, bad lighting) and the context, more or less frames might be necessary to create a good network. Ultimately, in order to scale up the analysis to large collections of videos with perhaps unexpected conditions, one can also refine the data set in an adaptive way (see refinement below). **For maDLC, be sure you have labeled frames with closely interacting animals!** The function `extract_frames` extracts frames from all the videos in the project configuration file in order to create a training dataset. The extracted frames from all the videos are stored in a separate subdirectory named after the video file’s name under the ‘labeled-data’. This function also has various parameters that might be useful based on the user’s need. + ```python -deeplabcut.extract_frames(config_path, mode='automatic/manual', algo='uniform/kmeans', userfeedback=False, crop=True/False) +deeplabcut.extract_frames( + config_path, + mode='automatic/manual', + algo='uniform/kmeans', + userfeedback=False, + crop=True/False, +) ``` + **CRITICAL POINT:** It is advisable to keep the frame size small, as large frames increase the training and inference time, or you might not have a large enough GPU for this. When running the function `extract_frames`, if the parameter crop=True, then you will be asked to draw a box within the GUI (and this is written to the config.yaml file). @@ -160,63 +214,99 @@ behaviors, and not extract the frames across the whole video. This can be achiev parameters in the config.yaml file. Also, the user can change the number of frames to extract from each video using the numframes2extract in the config.yaml file. - **For maDLC, be sure you have labeled frames with closely interacting animals!** Therefore, manually selecting some frames is a good idea if interactions are not highly frequent in the video. +```{TIP} +For maDLC, **be sure you have labeled frames with closely interacting animals**! +Therefore, manually selecting some frames is a good idea if interactions are not highly +frequent in the video. +``` -However, picking frames is highly dependent on the data and the behavior being studied. Therefore, it is hard to -provide all purpose code that extracts frames to create a good training dataset for every behavior and animal. If the user feels specific frames are lacking, they can extract hand selected frames of interest using the interactive GUI +However, picking frames is highly dependent on the data and the behavior being studied. +Therefore, it is hard to provide all purpose code that extracts frames to create a good +training dataset for every behavior and animal. If the user feels specific frames are +lacking, they can extract hand selected frames of interest using the interactive GUI provided along with the toolbox. This can be launched by using: + ```python deeplabcut.extract_frames(config_path, 'manual') ``` -The user can use the *Load Video* button to load one of the videos in the project configuration file, use the scroll -bar to navigate across the video and *Grab a Frame* (or a range of frames, as of version 2.0.5) to extract the frame(s). The user can also look at the extracted frames and e.g. delete frames (from the directory) that are too similar before reloading the set and then manually annotating them. +// FIXME(niels) - add a napari frame extractor description. +The user can use the *Load Video* button to load one of the videos in the project +configuration file, use the scroll bar to navigate across the video and *Grab a Frame*. +The user can also look at the extracted frames and e.g. delete frames (from the +directory) that are too similar before reloading the set and then manually annotating +them. + +````{admonition} Click the button to see API Docs +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.extract_frames.rst +``` +```` -### Label Frames: +### (D) Label Frames ```python deeplabcut.label_frames(config_path) ``` -As of 2.2 there is a new multi-animal labeling GUI (as long as in your `config.yaml` says `multianimalproject: true` at the top, this will automatically launch). +The toolbox provides a function **label_frames** which helps the user to easily label +all the extracted frames using an interactive graphical user interface (GUI). The user +should have already named the bodyparts to label (points of interest) in the +project’s configuration file by providing a list. The following command invokes the +napari-deeplabcut labelling GUI. -The toolbox provides a function **label_frames** which helps the user to easily label all the extracted frames using -an interactive graphical user interface (GUI). The user should have already named the body parts to label (points of -interest) in the project’s configuration file by providing a list. The following command invokes the labeling toolbox. +[🎥 DEMO](https://youtu.be/hsA9IB5r73E) -The user needs to use the *Load Frames* button to select the directory which stores the extracted frames from one of -the videos. Subsequently, the user can use one of the radio buttons (top right) to select a body part to label. **RIGHT** click to add the label. Left click to drag the label, if needed. If you label a part accidentally, you can use the middle button on your mouse to delete (or hit the delete key while you hover over the point)! If you cannot see a body part in the frame, skip over the label! Please see the ``HELP`` button for more user instructions. This auto-advances once you labeled the first body part. You can also advance to the next frame by clicking on the RIGHT arrow on your keyboard (and go to a previous frame with LEFT arrow). -Each label will be plotted as a dot in a unique color. +HOT KEYS IN THE Labeling GUI (also see "help" in GUI): -The user is free to move around the body part and once satisfied with its position, can select another radio button -(in the top right) to switch to the respective body part (it otherwise auto-advances). The user can skip a body part if it is not visible. Once all the visible body parts are labeled, then the user can use ‘Next Frame’ to load the following frame. The user needs to save the labels after all the frames from one of the videos are labeled by clicking the save button at the bottom right. Saving the labels will create a labeled dataset for each video in a hierarchical data file format (HDF) in the -subdirectory corresponding to the particular video in **labeled-data**. You can save at any intermediate step (even without closing the GUI, just hit save) and you return to labeling a dataset by reloading it! +``` +Ctrl + C: Copy labels from previous frame. +Keyboard arrows: advance frames. +Delete key: delete label. +``` -**CRITICAL POINT:** It is advisable to **consistently label similar spots** (e.g., on a wrist that is very large, try -to label the same location). In general, invisible or occluded points should not be labeled by the user, unless you want to teach the network to "guess" - this is possible, but could affect accuracy. If you don't want/or don't see a bodypart, they can simply be skipped by not applying the label anywhere on the frame. +![hot keys](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/192345a5-e411-4d56-b718-ef52f91e195e/Qwerty.png?format=2500w) -OPTIONAL: In the event of adding more labels to the existing labeled dataset, the user need to append the new -labels to the bodyparts in the config.yaml file. Thereafter, the user can call the function **label_frames**. A box will pop up and ask the user if they wish to display all parts, or only add in the new labels. Saving the labels after all the images are labelled will append the new labels to the existing labeled dataset. +**CRITICAL POINT:** It is advisable to **consistently label similar spots** (e.g., on a +wrist that is very large, try to label the same location). In general, invisible or +occluded points should not be labeled by the user, unless you want to teach the network +to "guess" - this is possible, but could affect accuracy. If you don't want/or don't see +a bodypart, they can simply be skipped by not applying the label anywhere on the frame. -**maDeepLabCut CRITICAL POINT:** For multi-animal labeling, unless you can tell apart the animals, you do not need to worry about the "ID" of each animal. For example: if you have a white and black mouse label the white mouse as animal 1, and black as animal 2 across all frames. If two black mice, then the ID label 1 or 2 can switch between frames - no need for you to try to identify them (but always label consistently within a frame). If you have 2 black mice but one always has an optical fiber (for example), then DO label them consistently as animal1 and animal_fiber (for example). The point of multi-animal DLC is to train models that can first group the correct bodyparts to individuals, then associate those points in a given video to a specific individual, which then also uses temporal information to link across the video frames. +OPTIONAL: In the event of adding more labels to the existing labeled dataset, the user +needs to append the new labels to the bodyparts in the config.yaml file. Thereafter, the +user can call the function **label_frames**. A box will pop up and ask the user if they +wish to display all parts, or only add in the new labels. Saving the labels after all +the images are labelled will append the new labels to the existing labeled dataset. -Note, we also highly recommend that you use more bodypoints that you might otherwise have (see the example below). +**maDeepLabCut CRITICAL POINT:** For multi-animal labeling, unless you can tell apart +the animals, you do not need to worry about the "ID" of each animal. For example: if you +have a white and black mouse label the white mouse as animal 1, and black as animal 2 +across all frames. If two black mice, then the ID label 1 or 2 can switch between +frames - no need for you to try to identify them (but always label consistently within a +frame). If you have 2 black mice but one always has an optical fiber (for example), then +DO label them consistently as animal1 and animal_fiber (for example). The point of +multi-animal DLC is to train models that can first group the correct bodyparts to +individuals, then associate those points in a given video to a specific individual, +which then also uses temporal information to link across the video frames. -**Example Labeling with maDeepLabCut:** -- note you should within an animal be consistent, i.e., all bodyparts on mouse1 should be on mouse1, but across frames "mouse1" can be any of the black mice (as here it is nearly impossible to tell them apart visually). IF you can tell them apart, do label consistently! +Note, we also highly recommend that you use more bodyparts that you might otherwise have +(see the example below). -

- -

+For more information, checkout the [napari-deeplabcut docs](napari-gui) for +more information about the labelling workflow. -### Check Annotated Frames: +### (E) Check Annotated Frames Checking if the labels were created and stored correctly is beneficial for training, since labeling is one of the most critical parts for creating the training dataset. The DeepLabCut toolbox provides a function -‘check_labels’ to do so. It is used as follows: +`check_labels` to do so. It is used as follows: + ```python deeplabcut.check_labels(config_path, visualizeindividuals=True/False) ``` + **maDeepLabCut:** you can check and plot colors per individual or per body part, just set the flag `visualizeindividuals=True/False`. Note, you can run this twice in both states to see both images.

@@ -225,13 +315,33 @@ deeplabcut.check_labels(config_path, visualizeindividuals=True/False) For each video directory in labeled-data this function creates a subdirectory with **labeled** as a suffix. Those directories contain the frames plotted with the annotated body parts. The user can double check if the body parts are labeled correctly. If they are not correct, the user can reload the frames (i.e. `deeplabcut.label_frames`), move them around, and click save again. +````{admonition} Click the button to see API Docs +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.check_labels.rst +``` +```` + +### (F) Create Training Dataset + +At this point, you'll need to select your neural network type. + +For the **PyTorch engine**, please see [the PyTorch Model Architectures]( +dlc3-architectures) for options. -### Create Training Dataset: +For the **TensorFlow engine**, please see Lauer et al. 2021 for options. Multi-animal +models will use `imgaug`, ADAM optimization, our new DLCRNet, and batch training. We +suggest keeping these defaults at this time. At this step, the ImageNet pre-trained +networks (i.e. ResNet-50) weights will be downloaded. If they do not download (you will +see this downloading in the terminal, then you may not have permission to do so ( +something we have seen with some Windows users - see the **[ +WIKI troubleshooting for more help!]( +https://github.com/DeepLabCut/DeepLabCut/wiki/Troubleshooting-Tips)**). -At this point you also select your neural network type. Please see Lauer et al. 2021 for options. For **create_multianimaltraining_dataset** we already changed this such that by default you will use imgaug, ADAM optimization, our new DLCRNet, and batch training. We suggest these defaults at this time. Then run: +Then run: ```python -deeplabcut.create_multianimaltraining_dataset(config_path) +deeplabcut.create_training_dataset(config_path) ``` - The set of arguments in the function will shuffle the combined labeled dataset and split it to create train and test @@ -242,47 +352,33 @@ keeps track of how often the dataset was refined). - OPTIONAL: If the user wishes to benchmark the performance of the DeepLabCut, they can create multiple training datasets by specifying an integer value to the `num_shuffles`; see the docstring for more details. -- Each iteration of the creation of a training dataset will create several files, which is used by the feature detectors, -and a ``.pickle`` file that contains the meta information about the training dataset. This also creates two subdirectories -within **dlc-models** called ``test`` and ``train``, and these each have a configuration file called pose_cfg.yaml. -Specifically, the user can edit the **pose_cfg.yaml** within the **train** subdirectory before starting the training. These -configuration files contain meta information with regard to the parameters of the feature detectors. Key parameters -are listed in Box 2. - -- At this step, the ImageNet pre-trained networks (i.e. ResNet-50) weights will be downloaded. If they do not download (you will see this downloading in the terminal, then you may not have permission to do so (something we have seen with some Windows users - see the **[WIKI troubleshooting for more help!](https://github.com/DeepLabCut/DeepLabCut/wiki/Troubleshooting-Tips)**). - -**OPTIONAL POINTS:** - -With the data-driven skeleton selection introduced in 2.2rc1+, DLC networks are trained by default -on complete skeletons (i.e., they learn all possible redundant connections), before being optimally pruned -at model evaluation. Although this procedure is by far superior to manually defining a graph, -we leave manually-defining a skeleton as an option for the advanced user: - -```python -my_better_graph = [[0, 1], [1, 2], [2, 3]] # These are indices in the list of multianimalbodyparts -deeplabcut.create_multianimaltraining_dataset(config_path, paf_graph=my_better_graph) -``` - -Alternatively, the `skeleton` defined in the `config.yaml` file can also be used: - -```python -deeplabcut.create_multianimaltraining_dataset(config_path, paf_graph='config') -``` - -Importantly, if a user-defined graph is used it still is required to cover all multianimalbodyparts at least once. - -**DATA AUGMENTATION:** At this stage you can also decide what type of augmentation to use. The default loaders work well for most all tasks (as shown on www.deeplabcut.org), but there are many options, more data augmentation, intermediate supervision, etc. Please look at the [**pose_cfg.yaml**](https://github.com/DeepLabCut/DeepLabCut/blob/master/deeplabcut/pose_cfg.yaml) file for a full list of parameters **you might want to change before running this step.** There are several data loaders that can be used. For example, you can use the default loader (introduced and described in the Nature Protocols paper), [TensorPack](https://github.com/tensorpack/tensorpack) for data augmentation (currently this is easiest on Linux only), or [imgaug](https://imgaug.readthedocs.io/en/latest/). We recommend `imgaug` (which is default now!). You can set this by passing:``` deeplabcut.create_training_dataset(config_path, augmenter_type='imgaug') ``` - -The differences of the loaders are as follows: -- `default`: our standard DLC 2.0 introduced in Nature Protocols variant (scaling, auto-crop augmentation) *will be renamed to `crop_scale` in a future release!* -- `imgaug`: a lot of augmentation possibilities, efficient code for target map creation & batch sizes >1 supported. You can set the parameters such as the `batch_size` in the `pose_cfg.yaml` file for the model you are training. -- `tensorpack`: a lot of augmentation possibilities, multi CPU support for fast processing, target maps are created less efficiently than in imgaug, does not allow batch size>1 -- `deterministic`: only useful for testing, freezes numpy seed; otherwise like default. - -Our recent [A Primer on Motion Capture with Deep Learning: Principles, Pitfalls, and Perspectives](https://www.cell.com/neuron/pdf/S0896-6273(20)30717-0.pdf), details the advantage of augmentation for a worked example (see Fig 7). TL;DR: use imgaug and use the symmetries of your data! - - -Alternatively, you can set the loader (as well as other training parameters) in the **pose_cfg.yaml** file of the model that you want to train. Note, to get details on the options, look at the default file: [**pose_cfg.yaml**](https://github.com/DeepLabCut/DeepLabCut/blob/master/deeplabcut/pose_cfg.yaml). +- Each iteration of the creation of a training dataset will create several files, which +is used by the feature detectors, and a ``.pickle`` file that contains the meta +information about the training dataset. This also creates two subdirectories within +**dlc-models-pytorch** (**dlc-models** for the TensorFlow engine) called ``test`` and +``train``, and these each have a configuration file called pose_cfg.yaml. Specifically, +the user can edit the **pytorch_config.yaml** (**pose_cfg.yaml** for TensorFlow engine) +within the **train** subdirectory before starting the training. These configuration +files contain meta information with regard to the parameters of the feature detectors. +Key parameters are listed in Box 2. + +**DATA AUGMENTATION:** At this stage you can also decide what type of augmentation to +use. Once you've called `create_training_dataset`, you can edit the +[**pytorch_config.yaml**](dlc3-pytorch-config) file that was created (or for the +TensorFlow engine, the [**pose_cfg.yaml**]( +https://github.com/DeepLabCut/DeepLabCut/blob/master/deeplabcut/pose_cfg.yaml) file). + +- PyTorch Engine: [Albumentations](https://albumentations.ai/docs/) is used for data +augmentation. Look at the [**pytorch_config.yaml**](dlc3-pytorch-config) for more +information about image augmentation options. +- TensorFlow Engine: The default augmentation works well for most tasks (as shown on +www.deeplabcut.org), but there are many options, more data augmentation, intermediate +supervision, etc. Only `imgaug` augmentation is available for multi-animal projects. + +[A Primer on Motion Capture with Deep Learning: Principles, Pitfalls, and Perspectives]( +https://www.cell.com/neuron/pdf/S0896-6273(20)30717-0.pdf), details the advantage of +augmentation for a worked example (see Fig 8). TL;DR: use imgaug and use the symmetries +of your data! Importantly, image cropping as previously done with `deeplabcut.cropimagesandlabels` in multi-animal projects is now part of the augmentation pipeline. In other words, image crops are no longer stored in labeled-data/..._cropped @@ -292,118 +388,254 @@ In addition, one can specify a crop sampling strategy: crop centers can either b As a reminder, cropping images into smaller patches is a form of data augmentation that simultaneously allows the use of batch processing even on small GPUs that could not otherwise accommodate larger images + larger batchsizes (this usually increases performance and decreasing training time). +**MODEL COMPARISON**: You can also test several models by creating the same train/test +split for different networks. +You can easily do this in the Project Manager GUI (by selecting the "Use an existing +data split" option), which also lets you compare PyTorch and TensorFlow models. -### Train The Network: - -```python -deeplabcut.train_network(config_path, allow_growth=True) -``` +````{versionadded} 3.0.0 +You can now create new shuffles using the same train/test split as +existing shuffles with `create_training_dataset_from_existing_split`. This allows you to +compare model performance (between different architectures or when using different +training hyper-parameters) as the shuffles were trained on the same data, and evaluated +on the same test data! -The set of arguments in the function starts training the network for the dataset created for one specific shuffle. Note that you can change the loader (imgaug/default/etc) as well as other training parameters in the **pose_cfg.yaml** file of the model that you want to train (before you start training). +Example usage - creating 3 new shuffles (with indices 10, 11 and 12) for a ResNet 50 +pose estimation model, using the same data split as was used for shuffle 0: -Example parameters that one can call: ```python -deeplabcut.train_network(config_path, shuffle=1, trainingsetindex=0, gputouse=None, max_snapshots_to_keep=5, autotune=False, displayiters=100, saveiters=15000, maxiters=30000, allow_growth=True) +deeplabcut.create_training_dataset_from_existing_split( + config_path, + from_shuffle=0, + shuffles=[10, 11, 12], + net_type="resnet_50", +) ``` +```` -By default, the pretrained networks are not in the DeepLabCut toolbox (as they are around 100MB each), but they get downloaded before you train. However, if not previously downloaded from the TensorFlow model weights, it will be downloaded and stored in a subdirectory *pre-trained* under the subdirectory *models* in *Pose_Estimation_Tensorflow*. -At user specified iterations during training checkpoints are stored in the subdirectory *train* under the respective iteration directory. - -If the user wishes to restart the training at a specific checkpoint they can specify the full path of the checkpoint to -the variable ``init_weights`` in the **pose_cfg.yaml** file under the *train* subdirectory (see Box 2). - -**CRITICAL POINT:** It is recommended to train the networks for thousands of iterations until the loss plateaus (typically around **500,000**) if you use batch size 1, and **50-100K** if you use batchsize 8 (the default). +````{admonition} Click the button to see API Docs for deeplabcut.create_training_dataset +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.create_training_dataset.rst +``` +```` -If you use **maDeepLabCut** the recommended training iterations is **20K-100K** (it automatically stops at 200K!), as we use Adam and batchsize 8; if you have to reduce the batchsize for memory reasons then the number of iterations needs to be increased. +````{admonition} Click the button to see API Docs for deeplabcut.create_training_model_comparison +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.create_training_model_comparison.rst +``` +```` -The variables ``display_iters`` and ``save_iters`` in the **pose_cfg.yaml** file allows the user to alter how often the loss is displayed and how often the weights are stored. +````{admonition} Click the button to see API Docs for deeplabcut.create_training_dataset_from_existing_split +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.create_training_dataset_from_existing_split.rst +``` +```` -**maDeepLabCut CRITICAL POINT:** For multi-animal projects we are using not only different and new output layers, but also new data augmentation, optimization, learning rates, and batch training defaults. Thus, please use a lower ``save_iters`` and ``maxiters``. I.e. we suggest saving every 10K-15K iterations, and only training until 50K-100K iterations. We recommend you look closely at the loss to not overfit on your data. The bonus, training time is much less!!! +### (G) Train The Network -**Parameters:** +```python +deeplabcut.train_network(config_path, shuffle=1) ``` -config : string - Full path of the config.yaml file as a string. -shuffle: int, optional - Integer value specifying the shuffle index to select for training. Default is set to 1 +The set of arguments in the function starts training the network for the dataset created +for one specific shuffle. Note that you can change training parameters in the +[**pytorch_config.yaml**](dlc3-pytorch-config) file (or **pose_cfg.yaml** for TensorFlow +models) of the model that you want to train (before you start training). + +At user specified iterations during training checkpoints are stored in the subdirectory +*train* under the respective iteration & shuffle directory. -trainingsetindex: int, optional - Integer specifying which TrainingsetFraction to use. By default the first (note that TrainingFraction is a list in config.yaml). +````{admonition} Tips on training models with the PyTorch Engine +:class: dropdown -gputouse: int, optional. Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU, put None. -See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries +Example parameters that one can call: -max_snapshots_to_keep: int, or None. Sets how many snapshots are kept, i.e. states of the trained network. For every saving iteration a snapshot is stored, however, only the last max_snapshots_to_keep many are kept! If you change this to None, then all are kept. -See: https://github.com/DeepLabCut/DeepLabCut/issues/8#issuecomment-387404835 +```python +deeplabcut.train_network( + config_path, + shuffle=1, + trainingsetindex=0, + device="cuda:0", + max_snapshots_to_keep=5, + displayiters=100, + save_epochs=5, + epochs=200, +) +``` + +Pytorch models in DeepLabCut 3.0 are trained for a set number of epochs, instead of a +maximum number of iterations (which is what was used for TensorFlow models). An epoch +is a single pass through the training dataset, which means your model has seen each +training image exactly once. So if you have 64 training images for your network, an +epoch is 64 iterations with batch size 1 (or 32 iterations with batch size 2, 16 with +batch size 4, etc.). + +By default, the pretrained networks are not in the DeepLabCut toolbox (as they can be +more than 100MB), but they get downloaded automatically before you train. + +If the user wishes to restart the training at a specific checkpoint they can specify the +full path of the checkpoint to the variable ``resume_training_from`` in the [ +**pytorch_config.yaml**]( +dlc3-pytorch-config) file (checkout the "Restarting Training at a Specific Checkpoint" +section of the docs) under the *train* subdirectory. + +**CRITICAL POINT:** It is recommended to train the networks **until the loss plateaus** +(depending on the dataset, model architecture and training hyper-parameters this happens +after 100 to 250 epochs of training). + +The variables ``display_iters`` and ``save_epochs`` in the [**pytorch_config.yaml**]( +dlc3-pytorch-config) file allows the user to alter how often the loss is displayed +and how often the weights are stored. We suggest saving every 5 to 25 epochs. +```` -autotune: property of TensorFlow, somehow faster if 'false' (as Eldar found out, see https://github.com/tensorflow/tensorflow/issues/13317). Default: False +````{admonition} Tips on training models with the TensorFlow Engine +:class: dropdown -displayiters: this variable is actually set in pose_config.yaml. However, you can overwrite it with this hack. Don't use this regularly, just if you are too lazy to dig out -the pose_config.yaml file for the corresponding project. If None, the value from there is used, otherwise it is overwritten! Default: None +Example parameters that one can call: -saveiters: this variable is actually set in pose_config.yaml. However, you can overwrite it with this hack. Don't use this regularly, just if you are too lazy to dig out -the pose_config.yaml file for the corresponding project. If None, the value from there is used, otherwise it is overwritten! Default: None +```python +deeplabcut.train_network( + config_path, + shuffle=1, + trainingsetindex=0, + gputouse=None, + max_snapshots_to_keep=5, + autotune=False, + displayiters=100, + saveiters=15000, + maxiters=30000, + allow_growth=True, +) +``` + +By default, the pretrained networks are not in the DeepLabCut toolbox (as they are +around 100MB each), but they get downloaded before you train. However, if not previously +downloaded from the TensorFlow model weights, it will be downloaded and stored in a +subdirectory *pre-trained* under the subdirectory *models* in +*Pose_Estimation_Tensorflow*. At user specified iterations during training checkpoints +are stored in the subdirectory *train* under the respective iteration directory. + +If the user wishes to restart the training at a specific checkpoint they can specify the +full path of the checkpoint to the variable ``init_weights`` in the **pose_cfg.yaml** +file under the *train* subdirectory (see Box 2). + +**CRITICAL POINT:** It is recommended to train the networks for thousands of iterations +until the loss plateaus (typically around **500,000**) if you use batch size 1, and +**50-100K** if you use batchsize 8 (the default). + +If you use **maDeepLabCut** the recommended training iterations is **20K-100K** +(it automatically stops at 200K!), as we use Adam and batchsize 8; if you have to reduce + the batchsize for memory reasons then the number of iterations needs to be increased. + +The variables ``display_iters`` and ``save_iters`` in the **pose_cfg.yaml** file allows +the user to alter how often the loss is displayed and how often the weights are stored. + +**maDeepLabCut CRITICAL POINT:** For multi-animal projects we are using not only +different and new output layers, but also new data augmentation, optimization, learning +rates, and batch training defaults. Thus, please use a lower ``save_iters`` and +``maxiters``. I.e. we suggest saving every 10K-15K iterations, and only training until +50K-100K iterations. We recommend you look closely at the loss to not overfit on your +data. The bonus, training time is much less!!! +```` -maxiters: This sets how many iterations to train. This variable is set in pose_config.yaml. However, you can overwrite it with this. If None, the value from there is used, otherwise it is overwritten! Default: None +````{admonition} Click the button to see API Docs for train_network +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.train_network.rst ``` +```` -### Evaluate the Trained Network: +### (H) Evaluate the Trained Network + +It is important to evaluate the performance of the trained network. This performance is +measured by computing two metrics: + +- **Average root mean square error** (RMSE) between the manual labels and the ones +predicted by your trained DeepLabCut model. The RMSE is proportional to the mean average +Euclidean error (MAE) between the manual labels and the ones predicted by DeepLabCut. +The MAE is displayed for all pairs and only likely pairs (>p-cutoff). This helps to +exclude, for example, occluded body parts. One of the strengths of DeepLabCut is that +due to the probabilistic output of the scoremap, it can, if sufficiently trained, also +reliably report if a body part is visible in a given frame. (see discussions of finger +tips in reaching and the Drosophila legs during 3D behavior in [Mathis et al, 2018]). +- **Mean Average Precision** (mAP) and **Mean Average Recall** (mAR) for the individuals +predicted by your trained DeepLabCut model. This metric describes the precision of your +model, based on a considered definition of what a correct detection of an individual is. +It isn't as useful for single-animal models, as RMSE does a great job of evaluating your +model in that case. + +```{admonition} A more detailed description of mAP and mAR +:class: dropdown + +For multi-animal pose estimation, multiple predictions can be made for each image. +We want to get some idea of the proportion of correct predictions among all predictions +that are made. +However, the notion of "correct prediction" for pose estimation is not straightforward: +is a prediction correct if all predicted keypoints are within 5 pixels of the ground +truth? Within 2 pixels of the ground truth? What if all pixels but one match the ground +truth perfectly, but the wrong prediction is 50 pixels away? Mean average precision ( +and mean average recall) estimate the precision/recall of your models by setting +different "thresholds of correctness" and averaging results. How "correct" a +prediction is can be evaluated through [object-keypoint similarity]( +https://cocodataset.org/#keypoints-eval). + +A good resource to get a deeper understanding of mAP is the [Stanford CS230 course]( +https://cs230.stanford.edu/section/8/#object-detection-iou-ap-and-map). While it +describes mAP for object detection (where bounding boxes are predicted instead of +keypoints), the same metric can be computed for pose estimation, where similarity +between predictions and ground truth is computed through [object-keypoint similarity]( +https://cocodataset.org/#keypoints-eval) instead of intersection-over-union (IoU). +``` + +It's also important to visually inspect predictions on individual frames to assess the +performance of your model. You can do this by setting `plotting=True` when you call +`evaluate_network`. The evaluation results are computed by typing: -Here, for traditional projects you will get a pixel distance metric and you should inspect the individual frames: ```python -deeplabcut.evaluate_network(config_path, plotting=True) +deeplabcut.evaluate_network(config_path, Shuffles=[1], plotting=True) ``` -:movie_camera:[VIDEO TUTORIAL AVAILABLE!](https://www.youtube.com/watch?v=bgfnz1wtlpo) -It is important to evaluate the performance of the trained network. This performance is measured by computing -the mean average Euclidean error (MAE; which is proportional to the average root mean square error) between the -manual labels and the ones predicted by DeepLabCut. The MAE is saved as a comma separated file and displayed -for all pairs and only likely pairs (>p-cutoff). This helps to exclude, for example, occluded body parts. One of the -strengths of DeepLabCut is that due to the probabilistic output of the scoremap, it can, if sufficiently trained, also -reliably report if a body part is visible in a given frame. (see discussions of finger tips in reaching and the Drosophila -legs during 3D behavior in [Mathis et al, 2018]). The evaluation results are computed by typing: +🎥 [VIDEO TUTORIAL AVAILABLE!](https://www.youtube.com/watch?v=bgfnz1wtlpo) Setting ``plotting`` to True plots all the testing and training frames with the manual and predicted labels; these will -be colored by body part type by default. They can alternatively be colored by individual by passing `plotting`=`individual`. +be colored by body part type by default. They can alternatively be colored by individual by passing `plotting="individual"`. The user should visually check the labeled test (and training) images that are created in the ‘evaluation-results’ directory. Ideally, DeepLabCut labeled unseen (test images) according to the user’s required accuracy, and the average train and test errors are comparable (good generalization). What (numerically) comprises an acceptable MAE depends on many factors (including the size of the tracked body parts, the labeling variability, etc.). Note that the test error can also be larger than the training error due to human variability (in labeling, see Figure 2 in Mathis et al, Nature Neuroscience 2018). -**Optional parameters:** -``` - Shuffles: list, optional -List of integers specifying the shuffle indices of the training dataset. The default is [1] - - plotting: bool, optional -Plots the predictions on the train and test images. The default is `False`; if provided it must be either `True` or `False` - - show_errors: bool, optional -Display train and test errors. The default is `True` - - comparisonbodyparts: list of bodyparts, Default is all -The average error will be computed for those body parts only (Has to be a subset of the body parts). - - gputouse: int, optional -Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU, put None. See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries -``` - The plots can be customized by editing the **config.yaml** file (i.e., the colormap, scale, marker size (dotsize), and -transparency of labels (alphavalue) can be modified). By default each body part is plotted in a different color +transparency of labels (alpha-value) can be modified). By default each body part is plotted in a different color (governed by the colormap) and the plot labels indicate their source. Note that by default the human labels are -plotted as plus (‘+’), DeepLabCut’s predictions either as ‘.’ (for confident predictions with likelihood > p-cutoff) and +plotted as plus (‘+’), DeepLabCut’s predictions either as ‘.’ (for confident predictions with likelihood > `pcutoff`) and ’x’ for (likelihood <= `pcutoff`). -The evaluation results for each shuffle of the training dataset are stored in a unique subdirectory in a newly created -directory ‘evaluation-results’ in the project directory. The user can visually inspect if the distance between the labeled -and the predicted body parts are acceptable. In the event of benchmarking with different shuffles of same training -dataset, the user can provide multiple shuffle indices to evaluate the corresponding network. If the generalization is -not sufficient, the user might want to: +The evaluation results for each shuffle of the training dataset are stored in a unique +subdirectory in a newly created directory ‘evaluation-results-pytorch’ (or +‘evaluation-results’ for TensorFlow models) in the project directory. +The user can visually inspect if the distance between the labeled and the predicted body +parts are acceptable. In the event of benchmarking with different shuffles of same training +dataset, the user can provide multiple shuffle indices to evaluate the corresponding +network. If the generalization is not sufficient, the user might want to: -• check if the labels were imported correctly; i.e., invisible points are not labeled and the points of interest are -labeled accurately +• check if the labels were imported correctly; i.e., invisible points are not labeled +and the points of interest are labeled accurately • make sure that the loss has already converged • consider labeling additional images and make another iteration of the training data set +````{admonition} Click the button to see API Docs for evaluate_network +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.evaluate_network.rst +``` +```` + **maDeepLabCut: (or on normal projects!)** In multi-animal projects, model evaluation is crucial as this is when @@ -416,9 +648,10 @@ You should also plot the scoremaps, locref layers, and PAFs to assess performanc ```python deeplabcut.extract_save_all_maps(config_path, shuffle=shuffle, Indices=[0, 5]) ``` -you can drop "Indices" to run this on all training/testing images (this is very slow!) -### Evaluating your network on videos +You can drop "Indices" to run this on all training/testing images (this is very slow!) + +### (I) Analyze new Videos **-------------------- DECISION POINT -------------------** @@ -428,12 +661,28 @@ you can drop "Indices" to run this on all training/testing images (this is very Please run: ```python -scorername = deeplabcut.analyze_videos(config_path,['/fullpath/project/videos/testVideo.mp4'], videotype='.mp4') -deeplabcut.create_video_with_all_detections(config_path, ['/fullpath/project/videos/testVideo.mp4'], videotype='.mp4') +videos_to_analyze = ['/fullpath/project/videos/testVideo.mp4'] +scorername = deeplabcut.analyze_videos(config_path, videos_to_analyze, videotype='.mp4') +deeplabcut.create_video_with_all_detections(config_path, videos_to_analyze, videotype='.mp4') ``` -Please note that you do **not** get the .h5/csv file you might be used to getting (this comes after tracking). You will get a `pickle` file that is used in `create_video_with_all_detections`. -Another sanity check may be to examine the distributions of edge affinity costs using `deeplabcut.utils.plot_edge_affinity_distributions`. Easily separable distributions indicate that the model has learned strong links to group keypoints into distinct individuals — likely a necessary feature for the assembly stage (note that the amount of overlap will also depend on the amount of interactions between your animals in the daset). -IF you have good clean out video, ending in `....full.mp4` (and the evaluation metrics look good, scoremaps look good, plotted evaluation images, and affinity distributions are far apart for most edges), then go forward!!! + +Please note that you do **not** get the .h5/csv file you might be used to getting (this +comes after tracking). You will get a `pickle` file that is used in +`create_video_with_all_detections`. + +For models predicting part-affinity fields, another sanity check may be to +examine the distributions of edge affinity costs using `deeplabcut.utils.plot_edge_affinity_distributions`. Easily separable distributions +indicate that the model has learned strong links to group keypoints into distinct +individuals — likely a necessary feature for the assembly stage (note that the amount of +overlap will also depend on the amount of interactions between your animals in the +dataset). All TensorFlow multi-animal models use part-affinity fields and PyTorch models +consisting of just a backbone name (e.g. `resnet_50`, `resnet_101`) use part-affinity +fields. If you're unsure whether your PyTorch model has a one, check +the **pytorch_config.yaml** for a `DLCRNetHead`. + +IF you have good clean out video, ending in `....full.mp4` (and the evaluation metrics +look good, scoremaps look good, plotted evaluation images, and affinity distributions +are far apart for most edges), then go forward!!! If this does not look good, we recommend extracting and labeling more frames (even from more videos). Try to label close interactions of animals for best performance. Once you label more, you can create a new training set and train. @@ -441,30 +690,53 @@ You can either: 1. extract more frames manually from existing or new videos and label as when initially building the training data set, or 2. let DeepLabCut find frames where keypoints were poorly detected and automatically extract those for you. All you need is to run: + ```python deeplabcut.find_outliers_in_raw_data(config_path, pickle_file, video_file) ``` + where pickle_file is the `_full.pickle` one obtains after video analysis. Flagged frames will be added to your collection of images in the corresponding labeled-data folders for you to label. -## Animal Assembly and Tracking across frames +### Animal Assembly and Tracking across frames -After pose estimation, now you perform assembly and tracking. *NEW* in 2.2 is a novel data-driven way to set the optimal skeleton and assembly metrics, so this no longer requires user input. The metrics, in case you do want to edit them, can be found in the `inference_cfg.yaml` file. +After pose estimation, now you perform assembly and tracking. + +````{versionadded} v2.2.0 +*NEW* in 2.2 is a novel data-driven way to set the optimal skeleton and assembly +metrics, so this no longer requires user input. The metrics, in case you do want to edit +them, can be found in the `inference_cfg.yaml` file. +```` ### Optimized Animal Assembly + Video Analysis: -- Please note that **novel videos DO NOT need to be added to the config.yaml file**. You can simply have a folder elsewhere on your computer and pass the video folder (then it will analyze all videos of the specified type (i.e. ``videotype='.mp4'``), or pass the path to the **folder** or exact video(s) you wish to analyze: +Please note that **novel videos DO NOT need to be added to the config.yaml file**. You +can simply have a folder elsewhere on your computer and pass the video folder (then it +will analyze all videos of the specified type (i.e. ``videotype='.mp4'``), or pass the +path to the **folder** or exact video(s) you wish to analyze: ```python deeplabcut.analyze_videos(config_path, ['/fullpath/project/videos/'], videotype='.mp4', auto_track=True) ``` + #### IF auto_track = True: -- *NEW* in 2.2.0.3+: `deeplabcut.analyze_videos` has a new argument `auto_track=True`, chaining pose estimation, tracking, and stitching in a single function call with defaults we found to work well. Thus, you'll now get the `.h5` file you might be used to getting in standard DLC. If `auto_track=False`, one must run `convert_detections2tracklets` and `stitch_tracklets` manually (see below), granting more control over the last steps of the workflow (ideal for advanced users). +```{versionadded} v2.2.0.3 +A new argument `auto_track=True`, was added to `deeplabcut.analyze_videos` chaining pose +estimation, tracking, and stitching in a single function call with defaults we found to +work well. Thus, you'll now get the `.h5` file you might be used to getting in standard +DLC. If `auto_track=False`, one must run `convert_detections2tracklets` and +`stitch_tracklets` manually (see below), granting more control over the last steps of +the workflow (ideal for advanced users). +``` #### IF auto_track = False: - - You can validate the tracking parameters. Namely, you can iteratively change the parameters, run `convert_detections2tracklets` then load them in the GUI (`refine_tracklets`) if you want to look at the performance. If you want to edit these, you will need to open the `inference_cfg.yaml` file (or click button in GUI). The options are: +You can validate the tracking parameters. Namely, you can iteratively change the +parameters, run `convert_detections2tracklets` then load them in the GUI +(`refine_tracklets`) if you want to look at the performance. If you want to edit these, +you will need to open the `inference_cfg.yaml` file (or click button in GUI). The +options are: ```python # Tracking: @@ -503,8 +775,13 @@ from the final h5 file as was customary in single animal projects. **Next, tracklets are stitched to form complete tracks with: ```python -deeplabcut.stitch_tracklets(config_path, ['videofile_path'], videotype='mp4', - shuffle=1, trainingsetindex=0) +deeplabcut.stitch_tracklets( + config_path, + ['videofile_path'], + videotype='mp4', + shuffle=1, + trainingsetindex=0, +) ``` Note that the base signature of the function is identical to `analyze_videos` and `convert_detections2tracklets`. @@ -515,15 +792,42 @@ can be directly specified as follows: ```python deeplabcut.stitch_tracklets(..., n_tracks=n) ``` + In such cases, file columns will default to dummy animal names (ind1, ind2, ..., up to indn). +#### API Docs + +````{admonition} Click the button to see API Docs for analyze_videos +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.analyze_videos.rst +``` +```` + +````{admonition} Click the button to see API Docs for convert_detections2tracklets +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.convert_detections2tracklets.rst +``` +```` + +````{admonition} Click the button to see API Docs for stitch_tracklets +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.stitch_tracklets.rst +``` +```` + ### Using Unsupervised Identity Tracking: -In Lauer et al. 2022 we introduced a new method to do unsupervised reID of animals. Here, you can use the tracklets to learn the identity of animals to enhance your tracking performance. To use the code: +In Lauer et al. 2022 we introduced a new method to do unsupervised reID of animals. +Here, you can use the tracklets to learn the identity of animals to enhance your +tracking performance. To use the code: ```python deeplabcut.transformer_reID(config, videos_to_analyze, n_tracks=None, videotype="mp4") ``` + Note you should pass the n_tracks (number of animals) you expect to see in the video. ### Refine Tracklets: @@ -549,7 +853,7 @@ Short demo:

-### Once you have analyzed video data (and refined your maDeepLabCut tracklets): +### (J) Filter Pose Data Firstly, Here are some tips for scaling up your video analysis, including looping over many folders for batch processing: https://github.com/DeepLabCut/DeepLabCut/wiki/Batch-Processing-your-Analysis @@ -559,7 +863,14 @@ deeplabcut.filterpredictions(config_path,['/fullpath/project/videos/reachingvide ``` Note, this creates a file with the ending filtered.h5 that you can use for further analysis. This filtering step has many parameters, so please see the full docstring by typing: ``deeplabcut.filterpredictions?`` -### Plotting Results: +````{admonition} Click the button to see API Docs +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.filterpredictions.rst +``` +```` + +### (K) Plot Trajectories , (L) Create Labeled Videos - **NOTE :bulb::mega::** Before you create a video, you should set what threshold to use for plotting. This is set in the `config.yaml` file as `pcutoff` - if you have a well trained network, this should be high, i.e. set it to `0.8` or higher! IF YOU FILLED IN GAPS, you need to set this to `0` to "see" the filled in parts. @@ -579,6 +890,20 @@ Create videos: (more details [here](functionDetails.md#i-video-analysis-and-plotting-results)) +````{admonition} Click the button to see API Docs for plot_trajectories +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.plot_trajectories.rst +``` +```` + +````{admonition} Click the button to see API Docs for create_labeled_video +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.create_labeled_video.rst +``` +```` + ### HELP: In ipython/Jupyter notebook: diff --git a/docs/pytorch/architectures.md b/docs/pytorch/architectures.md index 4e41be29a1..53dfaca621 100644 --- a/docs/pytorch/architectures.md +++ b/docs/pytorch/architectures.md @@ -10,22 +10,32 @@ from deeplabcut.pose_estimation_pytorch import available_models print(available_models()) ``` -## Backbones (neural networks) +You can see a list of supported object detection architectures/variants by using: -Several backbones are currently implemented in DeepLabCut PyTorch (more will come, and you can add more easily in our new model registry). +```python +from deeplabcut.pose_estimation_pytorch import available_detectors +print(available_detectors()) +``` + +## Neural Networks Architectures + +Several architectures are currently implemented in DeepLabCut PyTorch (more will come, +and you can add more easily in our new model registry). **ResNets** - Adapted from [He, Kaiming, et al. "Deep residual learning for image recognition." Proceedings of the IEEE conference on Computer Vision and Pattern Recognition. 2016.](https://openaccess.thecvf.com/content_cvpr_2016/html/He_Deep_Residual_Learning_CVPR_2016_paper.html) and [Insafutdinov, Eldar et al. "DeeperCut: A Deeper, Stronger, and Faster Multi-Person Pose Estimation Model". European Conference on Computer Vision (ECCV) 2016.] -- Current variants are `resnet_50`, `resnet_101`, `top_down_resnet_101`, `top_down_resnet_50` +- Current bottom-up variants are `resnet_50`, `resnet_101` +- Current top-down variants are `top_down_resnet_101`, `top_down_resnet_50` **HRNet** - Adapted from [Wang, Jingdong, et al. "Deep high-resolution representation learning for visual recognition." IEEE transactions on pattern analysis and machine intelligence 43.10 (2020): 3349-3364.](https://arxiv.org/abs/1908.07919) -- Current variants are `hrnet_w18`, `hrnet_w32`, `hrnet_w48`, `top_down_hrnet_w18`, `top_down_hrnet_w32`, `top_down_hrnet_w48` +- Current variants are `hrnet_w18`, `hrnet_w32`, `hrnet_w48`, +- Current top-down variants are `top_down_hrnet_w18`, `top_down_hrnet_w32`, `top_down_hrnet_w48` - Slower but typically more powerful than ResNets **DEKR** - Adapted from [Geng, Zigang et al. "Bottom-Up Human Pose Estimation Via Disentangled Keypoint Regression." Proceedings of the IEEE conference on Computer Vision and Pattern Recognition. 2021.](https://openaccess.thecvf.com/content/CVPR2021/papers/Geng_Bottom-Up_Human_Pose_Estimation_via_Disentangled_Keypoint_Regression_CVPR_2021_paper.pdf) -- This model uses HRNet as a backbone. It learns to predict the center of each animal, and predicts the offset between each animal center and their keypoints +- This model is a bottom-up model using HRNet as a backbone. It learns to predict the center of each animal, and predicts the offset between each animal center and their keypoints - Current variants that are implemented (from smallest to largest): `dekr_w18`, `dekr_w32`, `dekr_w48` - Note, this is a powerful multi-animal model but very heavy (slow) @@ -40,6 +50,10 @@ Several backbones are currently implemented in DeepLabCut PyTorch (more will co - This model uses a multi-scale variant of a ResNet as a backbone, and part-affinity fields to assemble individuals - Variants: `dlcrnet_stride16_ms5`, `dlcrnet_stride32_ms5` +**RTMPose** +- From [Jiang, Tao et al. "RTMPose: Real-Time Multi-Person Pose Estimation based on MMPose"](https://arxiv.org/abs/2303.07399) +- Top-down pose estimation model using a fast CSPNeXt backbone with a SimCC-style head +- Variants: `rtmpose_s`, `rtmpose_m`, `rtmpose_x` **AnimalTokenPose** - Adapted from [Li, Yanjie, et al. "Tokenpose: Learning keypoint tokens for human pose estimation." Proceedings of the IEEE/CVF International conference on computer vision. 2021.](https://arxiv.org/abs/2104.03516) as in Ye et al. "SuperAnimal pretrained pose estimation models for behavioral analysis." Nature Communications. 2024](https://arxiv.org/abs/2203.07436) @@ -78,11 +92,12 @@ individual). As localization of individuals is handled by the detector, this sim the pose task to single-animal pose estimation! Hence any single-animal model can be transformed into a top-down, multi-animal model. To -do so, simply prefix `top_down` to your single-animal model name. Currently only a -single FasterRCNN variant is available as a detector. Other variants will be added soon! +do so, simply prefix `top_down` to your single-animal model name. Currently, the +following detectors are available: `ssdlite`, `fasterrcnn_mobilenet_v3_large_fpn`, +`fasterrcnn_resnet50_fpn_v2`. Other variants will be added soon! The pose model for top-down nets is simply the backbone followed by a single convolution for pose estimation. It's also possible to add deconvolutional layers to top-down model heads. -Example top-down models would be `top_down_resnet_50` and `top_down_hrnet_w32`. \ No newline at end of file +Example top-down models would be `top_down_resnet_50` and `top_down_hrnet_w32`. diff --git a/docs/pytorch/pytorch_config.md b/docs/pytorch/pytorch_config.md index 35af20158c..13e6244ea0 100644 --- a/docs/pytorch/pytorch_config.md +++ b/docs/pytorch/pytorch_config.md @@ -26,6 +26,7 @@ runner: # configuring the runner used for training train_settings: # generic training settings, such as batch size and maximum number of epochs ... logger: # optional: the configuration for a logger if you want one +resume_training_from: # optional: restart the training at the specific checkpoint ``` ## Sections @@ -60,60 +61,21 @@ data: normalize_images: true # this should always be set to true train: affine: - p: 0.9 + p: 0.5 rotation: 30 - translation: 40 - collate: # rescales the images when putting them in a batch - type: ResizeFromDataSizeCollate - min_scale: 0.4 - max_scale: 1.0 - min_short_side: 128 - max_short_side: 1152 - multiple_of: 32 - to_square: false + scaling: [0.5, 1.25] + translation: 0 covering: true + crop_sampling: + width: 448 # if your images are very small or very large, you may need to edit! + height: 448 # see below for more information about crop_sampling! + max_shift: 0.1 + method: hybrid gaussian_noise: 12.75 - hist_eq: true motion_blur: true normalize_images: true # this should always be set to true ``` -One of the most important elements is the `collate` configuration. If all images in your -dataset have the same size, then it doesn't necessarily need to be added (but might -still be beneficial). But if you have images of different sizes, then you'll need to -define a way of "combining" these images into a single tensor of shape `(B, 3, H, W)`. -The default way to do this is to use the `ResizeFromDataSizeCollate` collate function -(other collate functions are defined in -`deeplabcut/pose_estimation_pytorch/data/collate.py`). For each batch to collate, this -implementation: -1. Selects the target width & height all images will be resized to by getting the size -of the first image in the batch, and multiplying it by a scale sampled uniformly at -random from `(min_scale, max_scale)`. -2. Resizes all images in the batch (while preserving their aspect ratio) such that they -are the smallest size such that the target size fits entirely in the image. -3. Crops each resulting image into the target size with a random crop. - -**Collate**: Defines how images are collated into batches. - -```yaml -collate: # rescales the images when putting them in a batch - type: ResizeFromDataSizeCollate # You can also use `ResizeFromListCollate` - max_shift: 10 # the maximum shift, in pixels, to add to the random crop (this means - # there can be a slight border around the image) - max_size: 1024 # the maximum size of the long edge of the image when resized. If the - # longest side will be greater than this value, resizes such that the longest side - # is this size, and the shortest side is smaller than the desired size. This is - # useful to keep some information from images with extreme aspect ratios. - min_scale: 0.4 # the minimum scale to resize the image with - max_scale: 1.0 # the maximum scale to resize the image with - min_short_side: 128 # the minimum size of the target short side - max_short_side: 1152 # the maximum size of the target short side - multiple_of: 32 # pads the target height, width such that they are multiples of 32 - to_square: false # instead of using the aspect ratio of the first image, only the - # short side of the first image will be used to sample a "side", and the images will - # be cropped in squares -``` - The following transformations are available for the `train` and `inference` keys. **Affine**: Applies an affine (rotation, translation, scaling) transformation to the @@ -165,16 +127,21 @@ the noise will be set as 12.75). gaussian_noise: 12.75 # bool, float: add gaussian noise ``` - **Horizontal Flips**: This flips the image horizontally around the y-axis. As the resulting image is mirrored, it does not preserve labels (the left hand would become the -right hand, and vice-versa). This augmentation should not be used for pose models if you -have symmetric keypoints! However, it is safe to use it to train detectors. +right hand, and vice versa). This augmentation should not be used for pose models if you +have symmetric keypoints! However, it is safe to use it to train detectors. If you want +to use horizontal flips with symmetric keypoints, you need to specify them through the +`symmetries` parameter! ```yaml -# if float > 0, the probability of applying a horizontal flip -# if true, applies a horizontal flip with probability 0.5 -hflip: true # bool, float +# augmentation for object detectors or when no symmetric (left/right) keypoints exist: +hflip: true + +# augmentation if your bodyparts are [snout, eye_L, eye_R, ear_L, ear_R] +hflip: + p: 0.5 # apply a horizontal flip with 50% probability + symmetries: [[1, 2], [3, 4]] # the indices of symmetric keypoints ``` **Histogram Equalization**: Applies histogram equalization with probability 50%. @@ -189,33 +156,92 @@ hist_eq: true # bool: whether to apply histogram equalization motion_blur: true # bool: whether to apply motion blur ``` -**Normalization** +**Normalization**: This should always be set to `true`. ```yaml normalize_images: true # normalizes images ``` -**Resizing**: Resizes the images while preserving the aspect ratio (first resizes to the -maximum possible size, then adds padding for the missing pixels). +#### Dealing with Variable Image Sizes -```yaml -resize: - height: 640 # int: the height to which all images will be resized - width: 480 # int: the width to which all images will be resized - keep_ratio: true # bool: the +```{NOTE} +When training with batch size 1 (or if all images in your dataset have the same size), +you don't need to worry about any of this! However, you can still use `crop_sampling` +which may help your model generalize. ``` +When training with a batch size greater than 1, all images in a batch **must** have the +same size. PyTorch **collates** all images into one tensor of shape `[b, c, h, w]`, +where `b` is the batch size, `c` the number of channels in the image, `h` and `w` the +height and width of images in the batches. There are a few different ways to ensure that +all images in a batch have the same size: + +1. **Crop sampling**. This is the default behavior for the PyTorch engine in DeepLabCut. +A part of each image (of a fixed size) is cropped and given to the model to train. See +below for more information. +2. **A custom collate function**. Collate functions define a way that images of different +sizes can be combined into one tensor. This involves resizing and padding images to the +same size and aspect ratio. Available collate functions are defined in +`deeplabcut/pose_estimation_pytorch/data/collate.py`. +3. **Resizing all images**. All images can simply be resized to the same size. This +usually doesn't lead to the best performance. + **Resizing - Crop Sampling**: An alternative way to ensure all images have the same size is through cropping. The `crop_sampling` crops images down to a maximum width and height, with options to sample the center of the crop according to the positions of the -keypoints. +keypoints. The methods to sample the center of the crop are as follows: + +- `uniform`: randomly over the image +- `keypoints`: randomly over the annotated keypoints +- `density`: weighing preferentially dense regions of keypoints +- `hybrid`: alternating randomly between `uniform` and `density` ```yaml crop_sampling: height: 400 # int: the height of the crop width: 400 # int: the height of the crop max_shift: 0.4 # float: maximum allowed shift of the cropping center position as a fraction of the crop size. - method: hybrid # str: how to sample the center of crops (one of 'uniform', 'keypoints', 'density', 'hybrid') + method: hybrid # str: the center sampling method (one of 'uniform', 'keypoints', 'density', 'hybrid') +``` + +**Collate**: Defines how images are collated into batches. The default way collate +function to use is `ResizeFromDataSizeCollate` (other collate functions are defined in +`deeplabcut/pose_estimation_pytorch/data/collate.py`). For each batch to collate, this +implementation: +1. Selects the target width & height all images will be resized to by getting the size +of the first image in the batch, and multiplying it by a scale sampled uniformly at +random from `(min_scale, max_scale)`. +2. Resizes all images in the batch (while preserving their aspect ratio) such that they +are the smallest size such that the target size fits entirely in the image. +3. Crops each resulting image into the target size with a random crop. + +```yaml +collate: # rescales the images when putting them in a batch + type: ResizeFromDataSizeCollate # You can also use `ResizeFromListCollate` + max_shift: 10 # the maximum shift, in pixels, to add to the random crop (this means + # there can be a slight border around the image) + max_size: 1024 # the maximum size of the long edge of the image when resized. If the + # longest side will be greater than this value, resizes such that the longest side + # is this size, and the shortest side is smaller than the desired size. This is + # useful to keep some information from images with extreme aspect ratios. + min_scale: 0.4 # the minimum scale to resize the image with + max_scale: 1.0 # the maximum scale to resize the image with + min_short_side: 128 # the minimum size of the target short side + max_short_side: 1152 # the maximum size of the target short side + multiple_of: 32 # pads the target height, width such that they are multiples of 32 + to_square: false # instead of using the aspect ratio of the first image, only the + # short side of the first image will be used to sample a "side", and the images will + # be cropped in squares +``` + +**Resizing**: Resizes the images while preserving the aspect ratio (first resizes to the +maximum possible size, then adds padding for the missing pixels). + +```yaml +resize: + height: 640 # int: the height to which all images will be resized + width: 480 # int: the width to which all images will be resized + keep_ratio: true # bool: whether the aspect ratio should be preserved when resizing ``` ### Model @@ -293,6 +319,9 @@ runner: max_snapshots: 5 # the maximum number of snapshots to save (the "best" model does not count as one of them) save_epochs: 25 # the interval between each snapshot save save_optimizer_state: false # whether the optimizer state should be saved with the model snapshots (very little reason to set to true) + gpus: # GPUs to use to train the network + - 0 + - 1 ``` **Key metric**: Every time the model is evaluated on the test set, metrics are computed @@ -389,7 +418,6 @@ warmup epochs, and a second scheduler later. An example usage would be: - 5 ``` - ### Train Settings The `train_settings` key contains parameters that are specific to training. For more @@ -495,6 +523,7 @@ detector: ... train_settings: # detector train settings (same keys as for the pose model) ... + resume_training_from: # optional: restart the training at the specific checkpoint ``` Currently, the only detectors available are `FasterRCNN` and `SSDLite`. However, multiple variants of diff --git a/docs/pytorch/user_guide.md b/docs/pytorch/user_guide.md index 1a01cdfdeb..6bbb4f289d 100644 --- a/docs/pytorch/user_guide.md +++ b/docs/pytorch/user_guide.md @@ -4,40 +4,31 @@ ## Using DeepLabCut 3.0 **DeepLabCut 3.0 keeps the same high-level API that you know, but has a full new PyTorch backend. -Moreover, it is a rewrite that more developer friendly, more powerful, and built for modern deep +Moreover, it is a rewrite that is more developer friendly, more powerful, and built for modern deep learning-based computer vision applications.** -**NOTE**🔥: We suggest that if you're just starting with DeepLabCut you start with the PyTorch backend. -You will easily know which "engine" you are using by looking at the main `config.yaml` file, or top right corner in the GUI. -If you have DeepLabCut projects in TensorFlow, we've got you covered too: you can seamlessly switch to train your already labeled data -by simply switching the engine (and thereby also compare performance). In short, expect a boost 🔥. +**NOTE**🔥: We suggest that if you're just starting with DeepLabCut you start with the +PyTorch backend. You will easily know which "engine" you are using by looking at the +main `config.yaml` file, or top right corner in the GUI. If you have DeepLabCut projects +in TensorFlow, we've got you covered too: you can seamlessly switch to train your +already labeled data by simply switching the engine (and thereby also compare +performance). In short, expect a boost 🔥. -In short, PyTorch models can be trained in any DeepLabCut project. If you have a project already made, simply add a new key to your -project `config.yaml` file specifying `engine: pytorch`. Then any new training dataset -that will be created will be a PyTorch model (see -[Creating Shuffles and Model Configuration](#Creating-Shuffles-and-Model-Configuration)) -to learn more about training PyTorch models. To train Tensorflow models again, you can -set `engine: tensorflow`. +In short, PyTorch models can be trained in any DeepLabCut project. If you have a project +already made, simply add a new key to your project `config.yaml` file specifying +`engine: pytorch`. Then any new training dataset that will be created will be a PyTorch +model (see [Creating Shuffles and Model Configuration]( +#Creating-Shuffles-and-Model-Configuration)) to learn more about training PyTorch +models. To train Tensorflow models again, you can set `engine: tensorflow`. ### Installation -During the alpha phase, you can use the `yaml` we provide, or create a new `env`. -- If you are a beginner user, [please see these docs!](https://deeplabcut.github.io/DeepLabCut/docs/beginner-guides/beginners-guide.html) -- If you are an advanced user, here is a quick start. [“Install PyTorch”](https://pytorch.org/get-started/locally/), then: - -``` -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] -``` +To see the DeepLabCut 3.0 installation guide, check the [installation docs](how-to-install). ### Using the GUI -You can use the GUI to train DeepLabCut projects. However, you cannot switch between -PyTorch and Tensorflow models while using the GUI. If you have set your engine to -`pytorch`, then the GUI will only offer the creation of PyTorch shuffles. - -You can create `tensorflow` shuffles and train them again by setting the -`engine: tensorflow` in the top right corner of the GUI. +You can use the GUI to train DeepLabCut projects. You can switch between the PyTorch +and TensorFlow engine through the drop-down menu in the top right corner. ## Major changes diff --git a/docs/quick-start/single_animal_quick_guide.md b/docs/quick-start/single_animal_quick_guide.md index c34268f2dc..6e4090d854 100644 --- a/docs/quick-start/single_animal_quick_guide.md +++ b/docs/quick-start/single_animal_quick_guide.md @@ -4,17 +4,21 @@ Open ipython in the terminal: ``` ipython +``` + +Import DeepLabCut: +``` import deeplabcut ``` Create a new project: ``` -deeplabcut.create_new_project(‘project_name’,‘experimenter’,[‘path of video 1’,‘path of video2’,..]) +deeplabcut.create_new_project("project_name", "experimenter", ["path of video 1", "path of video2", ..]) ``` Set a config_path variable for ease of use + go edit this file!: ``` -config_path = ‘yourdirectory/project_name/config.yaml’ +config_path = "yourdirectory/project_name/config.yaml" ``` Extract frames: @@ -49,20 +53,20 @@ deeplabcut.evaluate_network(config_path) Video analysis: ``` -deeplabcut.analyze_videos(config_path, [‘path of video 1’,‘path of video2’, ...]) +deeplabcut.analyze_videos(config_path, ["path of video 1", "path of video2", ..]) ``` Filter predictions [OPTIONAL]: ``` -deeplabcut.filterpredictions(config_path, [‘path of video 1’,‘path of video2’,..]) +deeplabcut.filterpredictions(config_path, ["path of video 1", "path of video2", ..]) ``` Plot results (trajectories): ``` -deeplabcut.plot_trajectories(config_path, [‘path of video 1’,‘path of video2’,..], filtered=True) +deeplabcut.plot_trajectories(config_path, ["path of video 1", "path of video2", ..], filtered=True) ``` Create a video: ``` -deeplabcut.create_labeled_video(config_path, [‘path of video 1’,‘path of video2’,..]filtered=True) +deeplabcut.create_labeled_video(config_path, ["path of video 1", "path of video2", ..], filtered=True) ``` diff --git a/docs/quick-start/tutorial_maDLC.md b/docs/quick-start/tutorial_maDLC.md index 947e036513..507ad559d8 100644 --- a/docs/quick-start/tutorial_maDLC.md +++ b/docs/quick-start/tutorial_maDLC.md @@ -69,7 +69,17 @@ deeplabcut.create_multianimaltraining_dataset( ``` **(7) Train the network** + ```python +# PyTorch Engine +deeplabcut.train_network( + config_path, + device="cuda", + save_epochs=5, + epochs=200, +) + +# TensorFlow Engine deeplabcut.train_network( config_path, saveiters=10000, diff --git a/docs/recipes/BatchProcessing.md b/docs/recipes/BatchProcessing.md index abac0bf27d..b0f91bbee6 100644 --- a/docs/recipes/BatchProcessing.md +++ b/docs/recipes/BatchProcessing.md @@ -3,10 +3,12 @@ ## Tips for working with DLC networks: -- Now you have a DLC network and are happy with the performance on selected videos, you may want to run it on all your videos without hassle. If all your videos are in one folder this is easy, simply pass the foldername to `deeplabcut.analyze_videos(config,[folder])` and you are fine. What if the videos are scattered? +Now you have a DLC network and are happy with the performance on selected videos, you may want to run it on all your +videos without hassle. If all your videos are in one folder this is easy, simply pass the foldername to +`deeplabcut.analyze_videos(config,[folder])` and you are fine. What if the videos are scattered? - -You could create a simply script that runs over all your video folders with the network of choice. Your "key" to this network is your config.yaml file. +You can create a simple script that runs over all your video folders with the network of choice. Your "key" to this +network is your config.yaml file. ![](https://static1.squarespace.com/static/57f6d51c9f74566f55ecf271/t/5ccc5abe0d9297405a428522/1556896461304/howtouseDLC-01.png?format=1000w) @@ -33,18 +35,18 @@ import deeplabcut def getsubfolders(folder): ''' returns list of subfolders ''' - return [os.path.join(folder,p) for p in os.listdir(folder) if os.path.isdir(os.path.join(folder,p))] + return [os.path.join(folder, p) for p in os.listdir(folder) if os.path.isdir(os.path.join(folder, p))] -project='ComplexWheelD3-12-Fumi-2019-01-28' +project = "ComplexWheelD3-12-Fumi-2019-01-28" -shuffle=1 +shuffle = 1 -prefix='/home/alex/DLC-workshopRowland' +prefix = "/home/alex/DLC-workshopRowland" -projectpath=os.path.join(prefix,project) -config=os.path.join(projectpath,'config.yaml') +projectpath = os.path.join(prefix, project) +config = os.path.join(projectpath, "config.yaml") -basepath='/home/alex/BenchmarkingExperimentsJan2019' #data' +basepath = "/home/alex/BenchmarkingExperimentsJan2019" ''' @@ -57,13 +59,13 @@ Imagine that the data (here: videos of 3 different types) are in subfolders: ''' -subfolders=getsubfolders(basepath) +subfolders = getsubfolders(basepath) for subfolder in subfolders: #this would be January, February etc. in the upper example - print("Starting analyze data in:", subfolder) - subsubfolders=getsubfolders(subfolder) + print("Starting analyze data in: ", subfolder) + subsubfolders = getsubfolders(subfolder) for subsubfolder in subsubfolders: #this would be Febuary1, etc. in the upper example... - print("Starting analyze data in:", subsubfolder) - for vtype in ['.mp4','.m4v','.mpg']: + print("Starting analyze data in: ", subsubfolder) + for vtype in [".mp4", ".m4v", ".mpg"]: deeplabcut.analyze_videos(config,[subsubfolder],shuffle=shuffle,videotype=vtype,save_as_csv=True) ``` @@ -90,25 +92,25 @@ import os import deeplabcut -Maxiter=int(1.5*10**5) +epochs = 200 model=int(sys.argv[1]) -Projects=[['project1-phoenix-2019-01-28'],['ComplexWheelD3-12-Fumi-2019-01-28', 'maze-ariel-2019-01-28'], ['TBI-BvA-2019-01-28','group-eli-2019-01-28']] +Projects=[["project1-phoenix-2019-01-28"], ["ComplexWheelD3-12-Fumi-2019-01-28", "maze-ariel-2019-01-28"], ["TBI-BvA-2019-01-28", "group-eli-2019-01-28"]] shuffle=1 -prefix='/home/alex/DLC-workshopRowland' +prefix = "/home/alex/DLC-workshopRowland" for project in Projects[model]: - projectpath=os.path.join(prefix,project) - config=os.path.join(projectpath,'config.yaml') + projectpath = os.path.join(prefix, project) + config = os.path.join(projectpath, "config.yaml") - cfg=deeplabcut.auxiliaryfunctions.read_config(config) - previous_path=cfg['project_path'] + cfg = deeplabcut.auxiliaryfunctions.read_config(config) + previous_path = cfg["project_path"] - cfg['project_path']=projectpath - deeplabcut.auxiliaryfunctions.write_config(config,cfg) + cfg["project_path"]=projectpath + deeplabcut.auxiliaryfunctions.write_config(config, cfg) print("This is the name of the script: ", sys.argv[0]) print("Shuffle: ", shuffle) @@ -116,22 +118,18 @@ for project in Projects[model]: deeplabcut.create_training_dataset(config, Shuffles=[shuffle]) - deeplabcut.train_network(config, shuffle=shuffle, max_snapshots_to_keep=5, maxiters=Maxiter) + deeplabcut.train_network(config, shuffle=shuffle, max_snapshots_to_keep=5, epochs=epochs) print("Evaluating...") - deeplabcut.evaluate_network(config, Shuffles=[shuffle],plotting=True) + deeplabcut.evaluate_network(config, Shuffles=[shuffle], plotting=True) print("Analyzing videos..., switching to last snapshot...") - #cfg=deeplabcut.auxiliaryfunctions.read_config(config) - #cfg['snapshotindex']=-1 - #deeplabcut.auxiliaryfunctions.write_config(config,cfg) - for vtype in ['.mp4','.m4v','.mpg']: try: - deeplabcut.analyze_videos(config,[str(os.path.join(projectpath,'videos'))],shuffle=shuffle,videotype=vtype,save_as_csv=True) + deeplabcut.analyze_videos(config, [str(os.path.join(projectpath, "videos"))], shuffle=shuffle, videotype=vtype, save_as_csv=True) except: pass print("DONE WITH ", project," resetting to original path") - cfg['project_path']=previous_path - deeplabcut.auxiliaryfunctions.write_config(config,cfg) + cfg["project_path"] = previous_path + deeplabcut.auxiliaryfunctions.write_config(config, cfg) ``` diff --git a/docs/recipes/ClusteringNapari.md b/docs/recipes/ClusteringNapari.md index 4d309d74e5..00a3b9f514 100644 --- a/docs/recipes/ClusteringNapari.md +++ b/docs/recipes/ClusteringNapari.md @@ -1,62 +1,88 @@ # Clustering in the napari-DeepLabCut GUI -To increase model performance, one can find the errors in the user-defined label (or in output H5 files after video inference). You can correct the errors and add them back into the training dataset, a process called active learning. +To increase model performance, one can find the errors in the user-defined label (or in output H5 files after video +inference). You can correct the errors and add them back into the training dataset, a process called active learning. -User errors can be detrimental to model performance, so beyond just `check_labels`, this tool allows you to find your mistakes. If you are curious about how errors affect performance, read the paper: [A Primer on Motion Capture with Deep Learning: Principles, Pitfalls, and Perspectives](https://www.sciencedirect.com/science/article/pii/S0896627320307170). +User errors can be detrimental to model performance, so beyond just `check_labels`, this tool allows you to find your +mistakes. If you are curious about how errors affect performance, read the paper: +[A Primer on Motion Capture with Deep Learning: Principles, Pitfalls, and Perspectives](https://www.sciencedirect.com/science/article/pii/S0896627320307170). **TL;DR: your data quality matters!** - DLC +DLC ```{Hint} **Labeling Pitfalls: How Corruptions Affect Performance** -(A) Illustration of two types of labeling errors. Top is ground truth, middle is missing a label at the tailbase, and bottom is if the labeler swapped the ear identity (left to right, etc.). (B) Using a small training dataset of 106 frames, how do the corruptions in (A) affect the percent of correct keypoints (PCK) on the test set as the distance to ground truth increases from 0 pixels (perfect prediction) to 20 pixels (larger error)? The x axis denotes the difference in the ground truth to the predicted location (RMSE in pixels), whereas the y axis is the fraction of frames considered accurate (e.g., z80% of frames fall within 9 pixels, even on this small training dataset, for points that are not corrupted, whereas for swapped points this falls to z65%). The fraction of the dataset that is corrupted affects this value. Shown is when missing the tailbase label (top) or swapping the ears in 1%, 5%, 10%, and 20% of frames (of 106 labeled training images). Swapping versus missing labels has a more notable adverse effect on network performance. +(A) Illustration of two types of labeling errors. Top is ground truth, middle is missing a label at the tailbase, and +bottom is if the labeler swapped the ear identity (left to right, etc.). (B) Using a small training dataset of 106 +frames, how do the corruptions in (A) affect the percent of correct keypoints (PCK) on the test set as the distance +to ground truth increases from 0 pixels (perfect prediction) to 20 pixels (larger error)? The x axis denotes the +difference in the ground truth to the predicted location (RMSE in pixels), whereas the y axis is the fraction of +frames considered accurate (e.g., z80% of frames fall within 9 pixels, even on this small training dataset, for +points that are not corrupted, whereas for swapped points this falls to z65%). The fraction of the dataset that is +corrupted affects this value. Shown is when missing the tailbase label (top) or swapping the ears in 1%, 5%, 10%, +and 20% of frames (of 106 labeled training images). Swapping versus missing labels has a more notable adverse effect +on network performance. ``` -The DeepLabCut toolbox supports **active learning** by extracting outlier frames be several methods and allowing the user to correct the frames, then retrain the model. See the [Nature Protocols paper](https://www.nature.com/articles/s41596-019-0176-0) for the detailed steps, or in the docs, [here](https://deeplabcut.github.io/DeepLabCut/docs/standardDeepLabCut_UserGuide.html#m-optional-active-learning-network-refinement-extract-outlier-frames). +The DeepLabCut toolbox supports **active learning** by extracting outlier frames be several methods and allowing the +user to correct the frames, then retrain the model. See the +[Nature Protocols paper](https://www.nature.com/articles/s41596-019-0176-0) for the detailed steps, or in the docs, +[here](active-learning). -To facilitate this process, here we propose a new way to detect 'outlier frames', which is planned to be released in ~Sept 2022. Your contributions and suggestions are welcomed, so test the [PR](https://github.com/DeepLabCut/napari-deeplabcut/pull/38) and give us feedback! +To facilitate this process, here we propose a new way to detect 'outlier frames'. +Your contributions and suggestions are welcomed, so test the +[PR](https://github.com/DeepLabCut/napari-deeplabcut/pull/38) and give us feedback! -This #cookbook recipe aims to show a usecase of **clustering in napari** and is contributed by 2022 DLC AI Resident [Sabrina Benas](https://twitter.com/Sabrineiitor) 💜. +This #cookbook recipe aims to show a usecase of **clustering in napari** and is contributed by 2022 DLC AI Resident +[Sabrina Benas](https://twitter.com/Sabrineiitor) 💜. ## Detect Outliers to Refine Labels ### Open `napari` and the `DeepLabCut plugin` - - Then open your `CollectedData_.h5` file. We used the Horse-30 dataset, presented in [Mathis, Biasi et al. WACV 2022](http://horse10.deeplabcut.org/), as our demo and development set. Here is an example of what it should look like: +Then open your `CollectedData_.h5` file. We used the Horse-30 dataset, presented in +[Mathis, Biasi et al. WACV 2022](http://horse10.deeplabcut.org/), as our demo and development set. Here is an example of what it should look like: - DLC +DLC ### Clustering -- Click on the button `cluster` and wait a few seconds until it displays a new layer with the cluster: +Click on the button `cluster` and wait a few seconds until it displays a new layer with the cluster: - DLC +DLC You can click on a point and see the image on the right with the keypoints: - DLC +DLC ### Visualize & refine -If you decided to refine that frame (we moved the points to make outliers obvious), click `show img` and refine them using the plugin features and instructions: +If you decided to refine that frame (we moved the points to make outliers obvious), click `show img` and refine them +using the plugin features and instructions: - DLC +DLC - ```{Attention} - When you're done, you need to click `ctl-s` to save it. +```{Attention} +When you're done, you need to click `ctl-s` to save it. ``` -- You can go back to the cluster layer by clicking on `close img` and refine another image. Reminder, when you're done editing you need to click `ctl-s` to save your work. And now you can take the updated `CollectedData` file, create and **new training shuffle**, and train the network! Read more about how to [create a training dataset](https://deeplabcut.github.io/DeepLabCut/docs/standardDeepLabCut_UserGuide.html#f-create-training-dataset-s). +You can go back to the cluster layer by clicking on `close img` and refine another image. Reminder, when you're done +editing you need to click `ctl-s` to save your work. And now you can take the updated `CollectedData` file, create +and **new training shuffle**, and train the network! Read more about how to +[create a training dataset](create-training-dataset). ```{hint} -If you want to change the clustering method, you can modify the file [kmeans.py](https://github.com/DeepLabCutAIResidency/napari-deeplabcut/blob/cluster1/src/napari_deeplabcut/kmeans.py) +If you want to change the clustering method, you can modify the file +[kmeans.py](https://github.com/DeepLabCutAIResidency/napari-deeplabcut/blob/cluster1/src/napari_deeplabcut/kmeans.py) +``` ::::{important} -You have to keep the way the file is opened (pandas dataframe) and the output has to be the cluster points, the points colors in the cluster colors and the frame names (in this order). +You have to keep the way the file is opened (pandas dataframe) and the output has to be the cluster points, the points +colors in the cluster colors and the frame names (in this order). :::: ``` diff --git a/docs/recipes/DLCMethods.md b/docs/recipes/DLCMethods.md index b2f7a5a7af..74d5ec4c65 100644 --- a/docs/recipes/DLCMethods.md +++ b/docs/recipes/DLCMethods.md @@ -2,7 +2,13 @@ **Pose estimation using DeepLabCut** -For body part tracking we used DeepLabCut (version 2.X.X) [Mathis et al, 2018, Nath et al, 2019]. Specifically, we labeled X number of frames taken from X videos/animals (then X% was used for training (default is 95%). We used a X-based neural network (i.e., X = ResNet-50, ResNet-101, MobileNetV2-0.35, MobileNetV2-0.5, MobileNetV2-0.75, MobileNetV2-1, EfficientNet ..X, dlcrnet_ms5, etc.)*** with default parameters* for X number of training iterations. We validated with X number of shuffles, and found the test error was: X pixels, train: X pixels (image size was X by X). We then used a p-cutoff of X (i.e. 0.9) to condition the X,Y coordinates for future analysis. This network was then used to analyze videos from similar experimental settings. +For body part tracking we used DeepLabCut (version 3.X.X) [Mathis et al, 2018, Nath et al, 2019]. Specifically, we +labeled X number of frames taken from X videos/animals (then X% was used for training (default is 95%). We used a +X-based neural network (i.e., X = ResNet-50, ResNet-101, MobileNetV2-0.35, MobileNetV2-0.5, MobileNetV2-0.75, +MobileNetV2-1, EfficientNet ..X, dlcrnet_ms5, cspnext_s, dekr_w32, rtmpose_s, etc.)*** with default parameters* for X +number of training iterations. We validated with X number of shuffles, and found the test error was: X pixels, train: +X pixels (image size was X by X). We then used a p-cutoff of X (i.e. 0.9) to condition the X,Y coordinates for future +analysis. This network was then used to analyze videos from similar experimental settings. *If any defaults were changed in *`pose_config.yaml`*, mention them. @@ -43,4 +49,5 @@ If you use ResNets, consider citing Insafutdinov et al 2016 & He et al 2016. If > 770–778 (2016). URL https://arxiv.org/abs/ > 1512.03385. -We also have the network graphic freely available on SciDraw.io if you'd like to use it! https://scidraw.io/drawing/290. If you use our DLC logo, please include the TM symbol, thank you! +We also have the network graphic freely available on SciDraw.io if you'd like to use it! https://scidraw.io/drawing/290. +If you use our DLC logo, please include the TM symbol, thank you! diff --git a/docs/recipes/MegaDetectorDLCLive.md b/docs/recipes/MegaDetectorDLCLive.md index 20e35a38fc..8af6f59d1b 100644 --- a/docs/recipes/MegaDetectorDLCLive.md +++ b/docs/recipes/MegaDetectorDLCLive.md @@ -14,7 +14,9 @@ MegaDetector detects an animal and generates a bounding box around the animal. T ## DeepLabCut-Live -DeepLabCut-Live! is a real-time package for running DeepLabCut. However, you can also use it as a lighter-weight package for running DeeplabCut even if you don't need real-time. It's very useful to use in HPC or servers, or in Apps, as we do here. To read more, check out the [docs](https://deeplabcut.github.io/DeepLabCut/docs/deeplabcutlive.html). +DeepLabCut-Live! is a real-time package for running DeepLabCut. However, you can also use it as a lighter-weight +package for running DeeplabCut even if you don't need real-time. It's very useful to use in HPC or servers, or in Apps, +as we do here. To read more, check out the [docs](deeplabcut-live). ### MegaDetector meets DeepLabCut diff --git a/docs/recipes/OpenVINO.md b/docs/recipes/OpenVINO.md index 2033045ce8..78ea18e82f 100644 --- a/docs/recipes/OpenVINO.md +++ b/docs/recipes/OpenVINO.md @@ -1,10 +1,15 @@ # Intel OpenVINO backend +::::{warning} +This feature is currently implemented for TensorFlow-based models only. +:::: + DeepLabCut provides an option to run deep learning model with [OpenVINO](https://github.com/openvinotoolkit/openvino) backend. -To enable OpenVINO in your pipeline, use `use_openvino` flag of `analyze_videos` method with one of string values indicating device: -* "CPU" - Use CPU. This is a default value. -* "GPU" - Use iGPU (requires OpenCL to be installed). First launch might take some time for kernels initialization. -* "MULTI:CPU,GPU" - Use CPU and GPU simultaneously. In most cases this option provides the best efficiency. +To enable OpenVINO in your pipeline, use `use_openvino` flag of `analyze_videos` method with one of string values +indicating device: +* ```"CPU"``` - Use CPU. This is a default value. +* ```"GPU"``` - Use GPU (requires OpenCL to be installed). First launch might take some time for kernels initialization. +* ```"MULTI:CPU,GPU"``` - Use CPU and GPU simultaneously. In most cases this option provides the best efficiency. ```python def analyze_videos( diff --git a/docs/recipes/OtherData.md b/docs/recipes/OtherData.md index 1d9e648d70..73343284b3 100644 --- a/docs/recipes/OtherData.md +++ b/docs/recipes/OtherData.md @@ -9,12 +9,12 @@ Some users may have annotation data in different formats, yet want to use the DL Here is a guide to do this via the ".csv" route: (the pandas array route is identical, just format the pandas array in the same way). -**Step 1**: create a project as describe in the user guide: https://github.com/AlexEMG/DeepLabCut/blob/master/docs/UseOverviewGuide.md#create-a-new-project +**Step 1**: create a project as describe in the user guide: https://github.com/DeepLabCut/DeepLabCut/blob/main/docs/UseOverviewGuide.md#create-a-new-project **Step 2**: edit the ``config.yaml`` file to include the body part names, please take care that spelling, spacing, and capitalization are IDENTICAL to the "labeled data body part names". -**Step 3**: Please inspect the excel formatted sheet (.csv) from our [demo project](https://github.com/AlexEMG/DeepLabCut/tree/master/examples/Reaching-Mackenzie-2018-08-30/labeled-data/reachingvideo1) -- i.e. this file: https://github.com/AlexEMG/DeepLabCut/blob/master/examples/Reaching-Mackenzie-2018-08-30/labeled-data/reachingvideo1/CollectedData_Mackenzie.csv +**Step 3**: Please inspect the excel formatted sheet (.csv) from our [demo project](https://github.com/DeepLabCut/DeepLabCut/tree/main/examples/Reaching-Mackenzie-2018-08-30/labeled-data/reachingvideo1) +- i.e. this file: https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/Reaching-Mackenzie-2018-08-30/labeled-data/reachingvideo1/CollectedData_Mackenzie.csv **Step 4**: Edit the .csv file such that it contains the X, Y pixel coordinates, the body part names, the scorer name as well as the relative path to the image: e.g. /labeled-data/somefolder/img017.jpg Then make sure the scorer name, and body parts are the same in the config.yaml file. diff --git a/docs/recipes/TechHardware.md b/docs/recipes/TechHardware.md index f4610f5442..879fc67dfb 100644 --- a/docs/recipes/TechHardware.md +++ b/docs/recipes/TechHardware.md @@ -23,10 +23,20 @@ The software is very robust to track data from any camera (cell phone cameras, g **Anaconda/Python3:** Anaconda: a free and open source distribution of the Python programming language (download from https://www.anaconda.com/). DeepLabCut is written in Python 3 (https://www.python.org/) and not compatible with Python 2. - -**TensorFlow** You will need [TensorFlow](https://www.tensorflow.org/) (we used version 1.0 in the paper, later versions also work with the provided code (we tested **TensorFlow versions 1.0 to 1.15, and 2.0 to 2.5**; we recommend TF2.5 now) for Python 3.7, 3.8, or 3.9 with GPU support. - -To note, is it possible to run DeepLabCut on your CPU, but it will be VERY slow (see: [Mathis & Warren](https://www.biorxiv.org/content/early/2018/10/30/457242)). However, this is the preferred path if you want to test DeepLabCut on your own computer/data before purchasing a GPU, with the added benefit of a straightforward installation! Otherwise, use our COLAB notebooks for GPU access for testing. - -Docker: We highly recommend advaced users use the supplied [Docker container](https://github.com/MMathisLab/Docker4DeepLabCut2.0). -NOTE: [this container does not work on windows hosts!](https://github.com/NVIDIA/nvidia-docker/issues/43) +**For the TensorFlow Engine:** You will need [TensorFlow](https://www.tensorflow.org/). +We used version 1.0 in the paper, later versions also work with the provided code (we +tested **TensorFlow versions 1.0 to 1.15, and 2.0 to 2.12 (2.10 for Windows)**; we +recommend TF2.12 for MacOS/Ubuntu and 2.10 for Windows) for Python 3.10 with GPU +support. + +To note, is it possible to run DeepLabCut on your CPU, but it will be VERY slow (see: +[Mathis & Warren](https://www.biorxiv.org/content/early/2018/10/30/457242)). However, this is the preferred path if you want to test +DeepLabCut on your own computer/data before purchasing a GPU, with the added benefit of +a straightforward installation! Otherwise, use our COLAB notebooks for GPU access for +testing. + +Docker: We highly recommend advanced users use the supplied [Docker container]( +docker-containers). + +NOTE: [Currently GPU support in Docker Desktop is only available on Windows with the +WSL2 backend.](https://docs.docker.com/desktop/features/gpu/) diff --git a/docs/recipes/UsingModelZooPupil.md b/docs/recipes/UsingModelZooPupil.md index 05d6a76d86..d73a1acbd7 100644 --- a/docs/recipes/UsingModelZooPupil.md +++ b/docs/recipes/UsingModelZooPupil.md @@ -1,17 +1,25 @@ # Using ModelZoo models on your own datasets -

Animal behavior has to be analyzed with painstaking accuracy. Therefore, animal pose estimation has been an important tool to study animal behavior precisely. +

Animal behavior has to be analyzed with painstaking accuracy. Therefore, animal pose estimation has been +an important tool to study animal behavior precisely. -Beside providing an open source toolbox for researchers to develop customized deep neural networks for markerless pose estimation, we at DeepLabCut also aim to build robust, generalizable models. Part of this effort is via the [DeeplabCut ModelZoo](http://www.mackenziemathislab.org/dlc-modelzoo). +Beside providing an open source toolbox for researchers to develop customized deep neural networks for markerless pose +estimation, we at DeepLabCut also aim to build robust, generalizable models. Part of this effort is via the +[DeeplabCut ModelZoo](http://modelzoo.deeplabcut.org/). -The Zoo hosts user-contributed and #teamDLC developed models that are trained on specific animals and scenarios. You can analyze your videos directly with these models without training. The models have strong zero-shot performance on unseen out-of-domain data which can be further improved via pseudo-labeling. Please check the first [ModelZoo manuscript](https://arxiv.org/abs/2203.07436v1) for further details. +The Zoo hosts user-contributed and DLC-team developed models that are trained on specific animals and scenarios. You can +analyze your videos directly with these models without training. The models have strong zero-shot performance on unseen +out-of-domain data which can be further improved via pseudo-labeling. Please check the first +[ModelZoo manuscript](https://arxiv.org/abs/2203.07436v1) for further details. -This recipe aims to show a usecase of the **mouse_pupil_vclose** and is contributed by 2022 DLC AI Resident [Neslihan Wittek](https://github.com/neslihanedes) 💜. +This recipe aims to show a usecase of the **mouse_pupil_vclose** and is contributed by 2022 DLC AI Resident +[Neslihan Wittek](https://github.com/neslihanedes) 💜. ## `mouse_pupil_vclose` model This model was contributed by Jim McBurney-Lin at University of California Riverside, USA. -The model was trained on images of C57/B6J mice eyes, and also then augmented with mouse eye data from the Mathis Lab at EPFL. +The model was trained on images of C57/B6J mice eyes, and also then augmented with mouse eye data from the Mathis Lab at +EPFL. DLC @@ -30,23 +38,37 @@ The model was trained on images of C57/B6J mice eyes, and also then augmented wi | 8 | VLpupil | Ventral/left aspect of pupil | -Since we would like to evaluate the models performance on out-of-domain data, we will analyze pigeon pupils. For more discussions and work on so-called out-of-domain data, see [Mathis, Biasi 2020](http://www.mackenziemathislab.org/horse10). +Since we would like to evaluate the models performance on out-of-domain data, we will analyze pigeon pupils. For more +discussions and work on so-called out-of-domain data, see +[Mathis, Biasi 2020](https://paperswithcode.com/dataset/horse-10). ## Pigeon Pupil -The eye pupil admits and regulates the amount of light entering the retina in order to enable image perception. Beside this curicial role, the pupil also reflects the state of the brain. The systemic behavior of the pupil has not been vastly studied in birds, although researchers from Max Planck Institute for Ornithology in Seewiesen have shed light on pupil behaviors in pigeons. +The eye pupil admits and regulates the amount of light entering the retina in order to enable image perception. Beside +this curicial role, the pupil also reflects the state of the brain. The systemic behavior of the pupil has not been +vastly studied in birds, although researchers from +Max Planck Institute for Ornithology in Seewiesen +have shed light on pupil behaviors in pigeons. -The pupils of male pigeons get smaller during courtship behavior. This is in contrast to mammals, for which the pupil size dilates in response to an increase in arousal. In addition, the pupil size of pigeons dilates during non-REM sleep, while they rapidly constrict during REM sleep. Examining these differences and the reason behind them, might be helpful to understand the pupillary behavior in general. +The pupils of male pigeons get smaller during courtship behavior. This is in contrast to mammals, for which the pupil +size dilates in response to an increase in arousal. In addition, the pupil size of pigeons dilates during non-REM sleep, +while they rapidly constrict during REM sleep. Examining these differences and the reason behind them, might be helpful +to understand the pupillary behavior in general. -In light of these findings, we wanted to show whether the **mouse_pupil_vclose** model give us an accurate tracking performance for the pigeon pupil as well. +In light of these findings, we wanted to show whether the **mouse_pupil_vclose** model give us an accurate tracking +performance for the pigeon pupil as well. ### Jupyter & Google Colab Notebook -DeepLabCut provides a Google Colab Notebook to analyze your video with a pretrained networks from the ModelZoo. No need for local installation of DeepLabCut! +DeepLabCut provides a Google Colab Notebook to analyze your video with a pretrained networks from the ModelZoo. No need +for local installation of DeepLabCut! -Since we are interested in the accuracy of the **mouse_pupil_vclose** on pigeon pupil data, we will use a video which consists of 7 recordings of pigeon pupils. +Since we are interested in the accuracy of the **mouse_pupil_vclose** on pigeon pupil data, we will use a video which +consists of 7 recordings of pigeon pupils. -Check ModelZoo Colab page and a video tutorial on how to use the ModelZoo on Google Colab. +Check the +[ModelZoo Colab page](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/COLAB/COLAB_DLC_ModelZoo.ipynb) +and a video tutorial on how to use the ModelZoo on Google Colab.

IMAGE ALT TEXT @@ -63,21 +85,23 @@ files.download("/content/file.zip") ### Analyze Videos at Your Local Machine -DeepLabCut host models from the DeepLabCut ModelZoo Project. +DeepLabCut host models from the [DeepLabCut ModelZoo Project](http://modelzoo.deeplabcut.org/). The `create_pretrained_project` function will create a new project directory with the necessary sub-directories and a basic configuration file. It will also initialize your project with a pre-trained model from the DeepLabCut ModelZoo. The rest of the code should be run within your DeepLabCut environment. -Check here for the instructions for the DeepLabCut installation. +Check [here](how-to-install) for the instructions for the DeepLabCut installation. +To initialize a new project directory with a pre-trained model from the DeepLabCut ModelZoo, run the code below. + +::::{warning} +This method is currently implemented for Tensorflow only, Pytorch compatibility is coming soon. +:::: ```python import deeplabcut -``` -To initialize a new project directory with a pre-trained model from the DeepLabCut ModelZoo, run the code below. -```python deeplabcut.create_pretrained_project( "projectname", "experimenter", @@ -93,6 +117,7 @@ deeplabcut.create_pretrained_project( engine=deeplabcut.Engine.TF, ) ``` + ::::{important} Your videos should be cropped around the eye for better model accuracy! 👁🐭 :::: @@ -101,13 +126,12 @@ Excitingly, 6 out of the 7 pigeon pupils were tracked nicely: DLC - -When we further evaluate the model accuracy by checking the likelihood of tracked points, we see that the tracking is low confidience when the pigeons close their eyelid (which is of course expected, and can be leveraged to measure blinking 👁). - +When we further evaluate the model accuracy by checking the likelihood of tracked points, we see that the tracking is +low confidience when the pigeons close their eyelid (which is of course expected, and can be leveraged to measure +blinking 👁). 6 pigeion eyes tracked with deeplabcut - But you also might encounter larger problems than small tracking glitches: 1eye @@ -118,12 +142,26 @@ The more problems you encounter, the higher the number of frames you might want You should also add the path of the video(s) into the `config.yaml` file, or run the following command to add the videos to your project: ```python -deeplabcut.add_new_videos('/pathofproject/config.yaml', ['/pathofvideos/pigeon.mp4'], copy_videos=False, coords=None, extract_frames=False) +deeplabcut.add_new_videos( + "/pathofproject/config.yaml", + ["/pathofvideos/pigeon.mp4"], + copy_videos=False, + coords=None, + extract_frames=False +) ``` The `deeplabcut.extract_outlier_frames` function will check for outliers and ask your feedback on whether to extract these outliers frames. ```python -deeplabcut.extract_outlier_frames('/pathofproject/config.yaml', ['/pathofvideos/pigeon.mp4'], automatic=True) +deeplabcut.analyze_videos( + "/pathofproject/config.yaml", + ["/pathofvideos/pigeon.mp4"] +) +deeplabcut.extract_outlier_frames( + "/pathofproject/config.yaml", + ["/pathofvideos/pigeon.mp4"], + automatic=True +) ``` The `deeplabcut.refine_labels` function starts the GUI which allows you to refine the outlier frames manually. You should load the outlier frames directory and corresponding `.h5` file from the previous model. @@ -131,9 +169,9 @@ It will ask you to define the `likelihood` threshold: labels under the threshold After refining, you should combine these data with your previous model's data set and create a new training data set. ```python -deeplabcut.refine_labels('/pathofproject/config.yaml') -deeplabcut.merge_datasets('/pathofproject/config.yaml') -deeplabcut.create_training_dataset('/pathofproject/config.yaml') +deeplabcut.refine_labels("/pathofproject/config.yaml") +deeplabcut.merge_datasets("/pathofproject/config.yaml") +deeplabcut.create_training_dataset("/pathofproject/config.yaml") ``` Before starting the training of your model, there is one last step left: editing the `init_weights` parameter in your `pose_cfg.yaml` file. Go to your project and check the latest snapshot (e.g., `snapshot-610000`) of your model in `dlc-models/train` directory. @@ -143,7 +181,7 @@ Edit the value of the `init_weights` key in the `pose_cfg.yaml` file and start t `init_weights: pathofyourproject\dlc-models\iteration-0\DLCFeb31-trainset95shuffle1\train\snapshot-610000` ```python -deeplabcut.train_network('/pathofproject/config.yaml', shuffle=1, saveiters=25000) +deeplabcut.train_network("/pathofproject/config.yaml", shuffle=1, saveiters=25000) ``` ```{hint} Check this video for model refining! diff --git a/docs/recipes/installTips.md b/docs/recipes/installTips.md index 3864af7815..ab4565a880 100644 --- a/docs/recipes/installTips.md +++ b/docs/recipes/installTips.md @@ -3,24 +3,24 @@ ## How to use the latest updates directly from GitHub -We often update the master deeplabcut code base on github, and then ~1 a month we push out a stable release on pypi. This is what most users turn to on a daily basis (i.e. pypi is where you get your `pip install deeplabcut` code from! But, sometimes we add things to the repo that are not yet integrated, or you might want to edit the code yourself. Here, we show you how to do this. +We often update the master deeplabcut code base on GitHub, and then ~1 a month we push out a stable release on pypi. This is what most users turn to on a daily basis (i.e. pypi is where you get your `pip install deeplabcut` code from! But, sometimes we add things to the repo that are not yet integrated, or you might want to edit the code yourself. Here, we show you how to do this. ### Method 1: -If you want to *use* the latest, you can use pip and add the specific tags, such as `tf` or `gui`, etc. by modifying and running: +If you want to *use* the latest, you can use pip and add the specific tags, such as `gui`, etc. by modifying and running: ``` -pip install --upgrade 'git+https://github.com/deeplabcut/deeplabcut.git#egg=deeplabcut[tf]' +pip install --upgrade 'git+https://github.com/deeplabcut/deeplabcut.git#egg=deeplabcut[gui]' ``` which will download and update deeplabcut, and any dependencies that don't match the new version. If you want to force upgrade all of the dependencies to the latest available versions, too, then use the additional `--upgrade-strategy eager`, i.e.: ``` -pip install --upgrade --upgrade-strategy eager 'git+https://github.com/deeplabcut/deeplabcut.git#egg=deeplabcut[tf,gui]' +pip install --upgrade --upgrade-strategy eager 'git+https://github.com/deeplabcut/deeplabcut.git#egg=deeplabcut[gui]' ``` ### Method 2: -If you want to be able to *edi* the source code of DeepLabCut, i.e., maybe add a feature or fix a 🐛, then you need to "clone" the source code: +If you want to be able to *edit* the source code of DeepLabCut, i.e., maybe add a feature or fix a 🐛, then you need to "clone" the source code: **Step 1:** @@ -54,6 +54,10 @@ If you make changes, you can also then utilize our test scripts. Run the desired i.e., for example: ``` +# Testing with the PyTorch engine +python testscript_pytorch_multi_animal.py + +# Testing with the TensorFlow engine python testscript_multianimal.py ``` @@ -243,6 +247,7 @@ Share images, automate workflows, and more with a free Docker ID: For more examples and ideas, visit: https://docs.docker.com/get-started/ ``` + ### Next, Anaconda! Click here to get the ubuntu/linux package: https://www.anaconda.com/products/individual#linux @@ -283,6 +288,11 @@ Follow prompts! ## Troubleshooting: Note, if you get a failed build due to wxPython (note, this does not happen on Ubuntu 18, 16, etc), i.e.: +```{warning} +DeepLabCut no longer uses `wxpython` for its GUI - if you're getting such an error, +you're likely installing an old version of DeepLabCut. +``` + ```python ERROR: Command errored out with exit status 1: /home/mackenzie/anaconda3/envs/DLC-GPU/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-0jsmkrr1/wxpython_aeff462b2060421a9cf65df55f63a126/setup.py'"'"'; __file__='"'"'/tmp/pip-install-0jsmkrr1/wxpython_aeff462b2060421a9cf65df55f63a126/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-pzy9q5u2/install-record.txt --single-version-externally-managed --compile --install-headers /home/mackenzie/anaconda3/envs/DLC-GPU/include/python3.7m/wxpython Check the logs for full command output. @@ -313,11 +323,11 @@ Activate! `conda activate DEEPLABCUT` and then run: `conda install -c conda-forg Then run `python -m deeplabcut` which launches the DLC GUI. -## DeepLabCut MacOS M1 and M2 chip installation environment instructions: - -This only assumes you have anaconda installed. +## DeepLabCut MacOS M-chip installation environment instructions: -Use the `DEEPLABCUT_M1.yaml` conda file if you have an Macbok with an M1 or M2 chip, and follow these steps: +This only assumes you have anaconda installed. Use the `DEEPLABCUT_M1.yaml` conda file +if you have a newer MacBook (with an M1, M2, M3, M4 chip or more later), and follow +these steps: (1) git clone the deeplabcut cut repo: @@ -330,17 +340,24 @@ git clone https://github.com/DeepLabCut/DeepLabCut.git (3) Then, run: ```bash -conda env create -f DEEPLABCUT_M1.yaml +conda env create -f DEEPLABCUT.yaml ``` (4) Finally, activate your environment and to launch DLC with the GUI ```bash -conda activate DEEPLABCUT_M1 +conda activate DEEPLABCUT python -m deeplabcut ``` -The GUI will open. Of course, you can also run DeepLabCut in headless mode. +The GUI will open. Of course, you can also run DeepLabCut in headless mode. + +If **you want to use the TensorFlow engine**, you'll need to install the `apple_mchips` +extra with DeepLabCut. You can do so by running: + +```bash +pip install deeplabcut[apple_mchips] +``` ## How to confirm that your GPU is being used by DeepLabCut @@ -364,7 +381,8 @@ During training and analysis steps, DeepLabCut does not use the GPU processor he (5) If you don't see activity there during training, then your GPU is likely not installed correctly for DeepLabCut. Return to the installation instructions, and be sure you installed CUDA 11+, and ran `conda install cudnn -c conda-forge` after installing DeepLabCut. -## How to install DeepLabCut for Intel and AMD GPUs on Windows +## How to install DeepLabCut for Intel and AMD GPUs on Windows for the TensorFlow engine + If you are on Windows 10/11 and have a DirectX 12 compatible GPU from any vendor (AMD, Intel, or Nvidia), you utilise GPU acceleration for inference, with an installation that is consistent between devices. This method uses [Tensorflow-directml](https://github.com/microsoft/tensorflow-directml) which uses DirectML instead of Cuda for ML training and inference. To check the DirectX version of your installed GPU, type in dxdiag into windows search and select the run command. In system information, the bottom item of the list shows your DirectX version. In addition to this ensure your standard GPU drivers are up-to-date. Updating drivers by any official means (Nvidia Geforce experience, AMD radeon software, direct from the vendor website) is fine. diff --git a/docs/recipes/nn.md b/docs/recipes/nn.md index 75b44e315f..39ff354938 100644 --- a/docs/recipes/nn.md +++ b/docs/recipes/nn.md @@ -1,15 +1,16 @@ +(tf-training-tips-and-tricks)= # Model training tips & tricks -## Limiting a GPU's memory consumption +## TensorFlow Engine: Limiting a GPU's memory consumption -All GPU memory is allocated to training by default, preventing +With TensorFlow, all GPU memory is allocated to training by default, preventing other Tensorflow processes from being run on the same machine. -A flexible solution to limiting memory usage is to call `deeplabcut.train(..., allow_growth=True)`, -which dynamically grows the GPU memory region as it is needed. -Another, stricter option is to explicitly cap GPU usage to only a fraction -of the available memory. For example, allocating a maximum of 1/4 of the total -memory could be done as follows: +A flexible solution to limiting memory usage is to call +`deeplabcut.train(..., allow_growth=True)`, which dynamically grows the GPU memory +region as it is needed. Another, stricter option is to explicitly cap GPU usage to only +a fraction of the available memory. For example, allocating a maximum of 1/4 of the +total memory could be done as follows: ```python import tensorflow as tf @@ -18,6 +19,7 @@ gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) ``` +(tf-custom-image-augmentation)= ## Using custom image augmentation Image augmentation is the process of artificially expanding the training set @@ -25,89 +27,104 @@ by applying various transformations to images (e.g., rotation or rescaling) in order to make models more robust and more accurate (read our [primer](https://www.sciencedirect.com/science/article/pii/S0896627320307170) for more information). Although data augmentation is automatically accomplished -by DeepLabCut, default values (see the augmentation variables in the -[default pose_cfg.yaml](https://github.com/DeepLabCut/DeepLabCut/blob/master/deeplabcut/pose_cfg.yaml#L23-L74) file) -can be readily overwritten prior to training. +by DeepLabCut, default values can be readily overwritten prior to training. See the +augmentation variables defined in the: -Another option we discuss is a different data-efficient approach based on a method called active learning. See this [this blog post](https://github.com/DeepLabCut/DeepLabCut/blob/master/docs/recipes/nn.md#using-custom-image-augmentation) for further details. +- PyTorch Engine: [docs for the `pytorch_config.yaml` file](dlc3-pytorch-config) +- TensorFlow Engine: [default pose_cfg.yaml file]( +https://github.com/DeepLabCut/DeepLabCut/blob/main/deeplabcut/pose_cfg.yaml#L23-L74) -When you `create_training_dataset` [you have several options](https://github.com/DeepLabCut/DeepLabCut/wiki/DOCSTRINGS#create_training_dataset) on what types of augmentation to use. -```python -deeplabcut.create_training_dataset(configpath, augmenter_type='imgaug') -``` - -When you do this (i.e. pass `augmenter_type`) what underlying files you are calling are these: -https://github.com/DeepLabCut/DeepLabCut/tree/master/deeplabcut/pose_estimation_tensorflow/datasets -You can look at what types of augmentation are available to you (or edit those files to add more). Moreover, you can add more options to the pose_cfg.yaml file. Here is a simple script you can modify and run to automatically edit the correct pose_cfg.yaml to add more augmentation to the `imgaug` loader (or open it and edit yourself). - -But, you can add more: +For the single-animal TensorFlow models, [you have several options]( +https://deeplabcut.github.io/DeepLabCut/docs/standardDeepLabCut_UserGuide.html#f-create-training-dataset-s-and-selection-of-your-neural-network) +for image augmentation when calling `create_training_dataset` -```python -import deeplabcut - -train_pose_config, _ = deeplabcut.return_train_network_path(config_path) -augs = { - "gaussian_noise": True, - "elastic_transform": True, - "rotation": 180, - "covering": True, - "motion_blur": True, -} -deeplabcut.auxiliaryfunctions.edit_config( - train_pose_config, - augs, -) -``` -An in-depth tutorial on image augmentation and training hyperparameters can be found [here](https://deeplabcut.github.io/DeepLabCut/docs/recipes/pose_cfg_file_breakdown.html). +An in-depth tutorial on image augmentation and training hyperparameters can be found [ +here]( +https://deeplabcut.github.io/DeepLabCut/docs/recipes/pose_cfg_file_breakdown.html). ## Evaluating intermediate (and all) snapshots -The latest snapshot stored during training may not necessarily be the one that yields the highest performance. Therefore, you should analyze ALL snapshots, and select the best. Put 'all' in the snapshots section of the config.yaml to do this. +The latest snapshot stored during training may not necessarily be the one that yields +the highest performance. Therefore, you should analyze ALL snapshots, and select the +best. Put 'all' in the snapshots section of the `config.yaml` to do this. +(what-neural-network-should-i-use)= ## What neural network should I use? (Trade offs, speed performance, and considerations) -### With the release of even more network options, you now have to decide what to use! This additionally flexibility is hopefully helpful, but we want to give you some guidance on where to start. +You always select the network type when you create a training data set: i.e., standard +dlc: `deeplabcut.create_training_dataset(config, net_type=resnet_50)` , or maDLC: +`deeplabcut.create_multianimaltraining_dataset(config, net_type=dlcrnet_ms5)`. There is +nothing else you should change. +### PyTorch Engine -**TL;DR - your best performance for most everything is ResNet-50; MobileNetV2-1 is much faster, needs less memory on your GPU to train and nearly as accurate.** +The different architectures available are described in the [PyTorch model architectures +](dlc3-architectures) page. -You always select the network type when you create a training data set: i.e., standard dlc: `deeplabcut.create_training_dataset(config, net_type=resnet_50)` , or maDLC: `deeplabcut.create_multianimaltraining_dataset(config, net_type=dlcrnet_ms5)`. There is nothing else you should change. +### TensorFlow Engine +With the release of even more network options, you now have to decide what to use! This +additionally flexibility is hopefully helpful, but we want to give you some guidance on +where to start. + +**TL;DR - your best performance for most everything is ResNet-50; MobileNetV2-1 is much +faster, needs less memory on your GPU to train and nearly as accurate.** *** -## ResNets: +#### ResNets: -In Mathis et al. 2018 we benchmarked three networks: **ResNet-50, ResNet-101, and ResNet-101ws**. For ALL lab applications, ResNet-50 was enough. For all the demo videos on [www.deeplabcut.org](http://www.mousemotorlab.org/deeplabcut) the backbones are ResNet-50's. Thus, we recommend making this your go-to workhorse for data analysis. Here is a figure from the paper, see panel "B" (they are all within a few pixels of each other on the open-field dataset): +In Mathis et al. 2018 we benchmarked three networks: **ResNet-50, ResNet-101, and +ResNet-101ws**. For ALL lab applications, ResNet-50 was enough. For all the demo videos +on [www.deeplabcut.org](http://www.mousemotorlab.org/deeplabcut) the backbones are +ResNet-50's. Thus, we recommend making this your go-to workhorse for data analysis. Here +is a figure from the paper, see panel "B" (they are all within a few pixels of each +other on the open-field dataset):

-This is also one of the main result figures, generated with ResNet-50. BLUE is training - RED is testing - BLACK is our best human-level performance, and 10 pixels is the width of the mouse nose -so anything under that is good performance for us on this task! +This is also one of the main result figures, generated with ResNet-50. BLUE is +training - RED is testing - BLACK is our best human-level performance, and 10 pixels is +the width - of the mouse nose -so anything under that is good performance for us on this +task! +

-Here are also some speed stats for analyzing videos with ResNet-50, see https://www.biorxiv.org/content/early/2018/10/30/457242 for more details: +Here are also some speed stats for analyzing videos with ResNet-50, see +https://www.biorxiv.org/content/early/2018/10/30/457242 for more details:

-**So, why use a ResNet-101 or even 152?** if you have a much more challenging problem, like multiple humans dancing, this is a good option. You should then also set `intermediate_supervision=True` for best performance in the pose_config.yaml of that shuffle folder ( before you train). Note, for ResNet-50 this does NOT help, and can hurt. +**So, why use a ResNet-101 or even 152?** if you have a much more challenging problem, +like multiple humans dancing, this is a good option. You should then also set +`intermediate_supervision=True` for best performance in the `pose_config.yaml` of that +shuffle folder (before you train). Note, for ResNet-50 this does NOT help, and can +hurt. -## When should I use a MobileNet? +#### When should I use a MobileNet? -MobileNets are fast to run, fast to train, more memory efficient, and faster for analysis (inference) - e.g. on CPUs they are 4 times faster, on GPUs up to 2x! So, if you don't have a GPU (or a GPU with little memory), and don't want to use Google COLAB, etc, then these are a great starting point. +MobileNets are fast to run, fast to train, more memory efficient, and faster for +analysis (inference) - e.g. on CPUs they are 4 times faster, on GPUs up to 2x! So, if +you don't have a GPU (or a GPU with little memory), and don't want to use Google COLAB, +etc, then these are a great starting point. -They are smaller/shallower networks though, so you don't want to be pushing in very large images. So, be sure to use `deeplabcut.DownSampleVideo` on your data (which is frankly never a bad idea). +They are smaller/shallower networks though, so you don't want to be pushing in very +large images. So, be sure to use `deeplabcut.DownSampleVideo` on your data (which is +frankly never a bad idea). -Additionally, these are good options for running on "live" videos, i.e. if you want to give real-time feedback in an experiment, you can run a video around a smaller cropped area, and run this rather fast! +Additionally, these are good options for running on "live" videos, i.e. if you want to +give real-time feedback in an experiment, you can run a video around a smaller cropped +area, and run this rather fast! **So, how fast are they?** -Here are comparisons of 4 MobileNetV2 variants to ResNet-50 and ResNet-101 (darkest red): -read more here: https://arxiv.org/abs/1909.11229 +Here are comparisons of 4 MobileNetV2 variants to ResNet-50 and ResNet-101 (darkest +red - read more here: https://arxiv.org/abs/1909.11229)

@@ -117,14 +134,26 @@ read more here: https://arxiv.org/abs/1909.11229

-## When should I use an EfficientNet? +#### When should I use an EfficientNet? -Built with inverse residual blocks like MobileNets, but more powerful than ResNets, due to optimal depth/width/resolution scaling, [EfficientNet](https://arxiv.org/abs/1905.11946) are an excellent choice if you want speed and performance. They do require more careful handling though! Especially for small datasets, you will need to tune the batch size and learning rates. So, we suggest these for more advanced users, or those willing to run experiments to find the best settings. Here is the speed comparison, and for performance see our latest work at: http://horse10.deeplabcut.org +Built with inverse residual blocks like MobileNets, but more powerful than ResNets, due +to optimal depth/width/resolution scaling, [EfficientNet]( +https://arxiv.org/abs/1905.11946) are an excellent choice if you want speed and +performance. They do require more careful handling though! Especially for small +datasets, you will need to tune the batch size and learning rates. So, we suggest these +for more advanced users, or those willing to run experiments to find the best settings. +Here is the speed comparison, and for performance see our latest work at: +http://horse10.deeplabcut.org

-## How can I compare them? +#### How can I compare them? -Great question! So, the best way to do this is to use the **same** test/train split (that is generated in create_training_dataset) with different models. Here, as of 2.1+, we have a **new** function that lets you do this easily. Instead of using `create_training_dataset` you will run `create_training_model_comparison` (see the docstrings by `deeplabcut.create_training_model_comparison?` or run the Project Manager GUI - `deeplabcut.launch_dlc()`- for assistance. +Great question! So, the best way to do this is to use the **same** test/train split ( +that is generated in create_training_dataset) with different models. Here, as of 2.1+, +we have a **new** function that lets you do this easily. Instead of using +`create_training_dataset` you will run `create_training_model_comparison` (see the +docstrings by `deeplabcut.create_training_model_comparison?` or run the Project Manager +GUI - `deeplabcut.launch_dlc()`- for assistance. diff --git a/docs/recipes/pose_cfg_file_breakdown.md b/docs/recipes/pose_cfg_file_breakdown.md index 2e980edc25..2f79ac28d3 100644 --- a/docs/recipes/pose_cfg_file_breakdown.md +++ b/docs/recipes/pose_cfg_file_breakdown.md @@ -1,5 +1,10 @@ # The `pose_cfg.yaml` Guideline Handbook +::::{warning} +The following is specific to Tensorflow-based models. To read the equivalent explanations for Pytorch-based models, +click [here](dlc3-pytorch-config) +:::: + 👋 Hello! Mabuhay! Hola! This recipe was written by the [2023 DLC AI Residents](https://www.deeplabcutairesidency.org/)! When you train, evaluate, and run inference with a neural network there are hyperparatmeters you must consider. While DLC attempts to set the "globally good for everyone" parameters, you might want to change them. Therefore, in this recipe we will review the pose config parameters related to neural network models' and the related data augmentation! diff --git a/docs/recipes/publishing_notebooks_into_the_DLC_main_cookbook.md b/docs/recipes/publishing_notebooks_into_the_DLC_main_cookbook.md index 31710c3a2d..4a7e47b856 100644 --- a/docs/recipes/publishing_notebooks_into_the_DLC_main_cookbook.md +++ b/docs/recipes/publishing_notebooks_into_the_DLC_main_cookbook.md @@ -133,7 +133,7 @@ This command installs DeepLabCut along with the dependencies required to build t 14. **🎉PR Approval:🎉** Once your PR is approved, the maintainers will merge it into the main repository. Your notebook will then be a part of the DeepLabCut Jupyter book! Yay! -Remember to always check the [DLC CONTRIBUTING guidelines](https://github.com/DeepLabCut/DeepLabCut/blob/main/CONTRIBUTING.md). +Remember to always check the [DLC contributing guidelines](https://github.com/DeepLabCut/DeepLabCut/blob/main/CONTRIBUTING.md). ## Wrap-Up 🎉 diff --git a/docs/roadmap.md b/docs/roadmap.md index 905a95ae6f..b303754e37 100644 --- a/docs/roadmap.md +++ b/docs/roadmap.md @@ -2,7 +2,7 @@ ## A development roadmap for DeepLabCut -:loudspeaker: :hourglass_flowing_sand: :construction: +📢 ⏳ 🚧 **General Enhancements:** - [ ] DeepLabCut PyTorch & Model Zoo --> DLC 3.0 🔥 @@ -34,8 +34,8 @@ - [X] DeepLabCut-live! published in eLife **DeepLabCut Model Zoo: a collection of pretrained models for plug-in-play DLC and community crowd-sourcing.** -- [X] BETA release with 2.1.8b0: http://www.mousemotorlab.org/dlc-modelzoo -- [X] full release with 2.1.8.1 http://www.mousemotorlab.org/dlc-modelzoo +- [X] BETA release with 2.1.8b0: https://www.mackenziemathislab.org/deeplabcut +- [X] full release with 2.1.8.1 https://www.mackenziemathislab.org/deeplabcut - [X] Manuscript forthcoming! --> see arXiv https://arxiv.org/abs/2203.07436 - [X] new models added; horse, cheetah - [X] TopView_Mouse model diff --git a/docs/standardDeepLabCut_UserGuide.md b/docs/standardDeepLabCut_UserGuide.md index 39188b3d48..0b32ccec65 100644 --- a/docs/standardDeepLabCut_UserGuide.md +++ b/docs/standardDeepLabCut_UserGuide.md @@ -1,7 +1,8 @@ (single-animal-userguide)= # DeepLabCut User Guide (for single animal projects) -This document covers single/standard DeepLabCut use. If you have a complicated multi-animal scenario (i.e., they look the same), then please see our [maDLC user guide](multi-animal-userguide). +This document covers single/standard DeepLabCut use. If you have a complicated multi-animal scenario (i.e., they look +the same), then please see our [maDLC user guide](multi-animal-userguide). To get started, you can use the GUI, or the terminal. See below. @@ -11,7 +12,12 @@ To get started, you can use the GUI, or the terminal. See below. **GUI:** -To begin, navigate to Aanaconda Prompt Terminal and right-click to "open as admin "(Windows), or simply launch "Terminal" (unix/MacOS) on your computer. We assume you have DeepLabCut installed (if not, see Install docs!). Next, launch your conda env (i.e., for example `conda activate DEEPLABCUT`). Then, simply run ``python -m deeplabcut``. The below functions are available to you in an easy-to-use graphical user interface. While most functionality is available, advanced users might want the additional flexibility that command line interface offers. Read more below. +To begin, navigate to Anaconda Prompt Terminal and right-click to "open as admin "(Windows), or simply launch +"Terminal" (unix/MacOS) on your computer. We assume you have DeepLabCut installed (if not, see +[install docs](how-to-install)!). Next, launch your conda env (i.e., for example `conda activate DEEPLABCUT`). Then, +simply run `python -m deeplabcut`. The below functions are available to you in an easy-to-use graphical user interface. +While most functionality is available, advanced users might want the additional flexibility that command line interface +offers. Read more below. ```{Hint} 🚨 If you use Windows, please always open the terminal with administrator privileges! Right click, and "run as administrator". ``` @@ -20,11 +26,19 @@ To begin, navigate to Aanaconda Prompt Terminal and right-click to "open as admi

-As a reminder, the core functions are described in our [Nature Protocols](https://www.nature.com/articles/s41596-019-0176-0) paper (published at the time of 2.0.6). Additional functions and features are continually added to the package. Thus, we recommend you read over the protocol and then please look at the following documentation and the doctrings. Thanks for using DeepLabCut! +As a reminder, the core functions are described in our +[Nature Protocols paper](https://www.nature.com/articles/s41596-019-0176-0) (published at the time of 2.0.6). +Additional functions and features are continually added to the package. Thus, we recommend you read over the protocol +and then please look at the following documentation and the doctrings. Thanks for using DeepLabCut! ## DeepLabCut in the Terminal/Command line interface: -To begin, navigate to Aanaconda Prompt Terminal and right-click to "open as admin "(Windows), or simply launch "Terminal" (unix/MacOS) on your computer. We assume you have DeepLabCut installed (if not, see Install docs!). Next, launch your conda env (i.e., for example `conda activate DEEPLABCUT`) and then type `ipython`. Then type `import deeplabcut`. +To begin, navigate to Anaconda Prompt Terminal and right-click to "open as admin "(Windows), or simply launch +"Terminal" (unix/MacOS) on your computer. We assume you have DeepLabCut installed (if not, see Install docs!). Next, +launch your conda env (i.e., for example `conda activate DEEPLABCUT`) and then type `ipython`. Then type: +```python +import deeplabcut +``` ```{Hint} 🚨 If you use Windows, please always open the terminal with administrator privileges! Right click, and "run as administrator". @@ -32,48 +46,89 @@ To begin, navigate to Aanaconda Prompt Terminal and right-click to "open as admi ### (A) Create a New Project -The function **create\_new\_project** creates a new project directory, required subdirectories, and a basic project configuration file. Each project is identified by the name of the project (e.g. Reaching), name of the experimenter (e.g. YourName), as well as the date at creation. +The function `create_new_project` creates a new project directory, required subdirectories, and a basic project +configuration file. Each project is identified by the name of the project (e.g. Reaching), name of the experimenter +(e.g. YourName), as well as the date at creation. -Thus, this function requires the user to input the name of the project, the name of the experimenter, and the full path of the videos that are (initially) used to create the training dataset. +Thus, this function requires the user to input the name of the project, the name of the experimenter, and the full +path of the videos that are (initially) used to create the training dataset. -Optional arguments specify the working directory, where the project directory will be created, and if the user wants to copy the videos (to the project directory). If the optional argument working\_directory is unspecified, the project directory is created in the current working directory, and if copy\_videos is unspecified symbolic links for the videos are created in the videos directory. Each symbolic link creates a reference to a video and thus eliminates the need to copy the entire video to the video directory (if the videos remain at the original location). +Optional arguments specify the working directory, where the project directory will be created, and if the user wants +to copy the videos (to the project directory). If the optional argument `working_directory` is unspecified, the +project directory is created in the current working directory, and if `copy_videos` is unspecified symbolic links +for the videos are created in the videos directory. Each symbolic link creates a reference to a video and thus +eliminates the need to copy the entire video to the video directory (if the videos remain at the original location). ```python -deeplabcut.create_new_project('Name of the project', 'Name of the experimenter', ['Full path of video 1', 'Full path of video2', 'Full path of video3'], working_directory='Full path of the working directory', copy_videos=True/False, multianimal=True/False) +deeplabcut.create_new_project( + "Name of the project", + "Name of the experimenter", + ["Full path of video 1", "Full path of video2", "Full path of video3"], + working_directory="Full path of the working directory", + copy_videos=True/False, + multianimal=False +) ``` **Important path formatting note** -Windows users, you must input paths as: ``r'C:\Users\computername\Videos\reachingvideo1.avi' `` or - -`` 'C:\\Users\\computername\\Videos\\reachingvideo1.avi'`` - - (TIP: you can also place ``config_path`` in front of ``deeplabcut.create_new_project`` to create a variable that holds the path to the config.yaml file, i.e. ``config_path=deeplabcut.create_new_project(...)``) - - -This set of arguments will create a project directory with the name **Name of the project+name of the experimenter+date of creation of the project** in the **Working directory** and creates the symbolic links to videos in the **videos** directory. The project directory will have subdirectories: **dlc-models**, **labeled-data**, **training-datasets**, and **videos**. All the outputs generated during the course of a project will be stored in one of these subdirectories, thus allowing each project to be curated in separation from other projects. The purpose of the subdirectories is as follows: - -**dlc-models:** This directory contains the subdirectories *test* and *train*, each of which holds the meta information with regard to the parameters of the feature detectors in configuration files. The configuration files are YAML files, a common human-readable data serialization language. These files can be opened and edited with standard text editors. The subdirectory *train* will store checkpoints (called snapshots in TensorFlow) during training of the model. These snapshots allow the user to reload the trained model without re-training it, or to pick-up training from a particular saved checkpoint, in case the training was interrupted. - -**labeled-data:** This directory will store the frames used to create the training dataset. Frames from different videos are stored in separate subdirectories. Each frame has a filename related to the temporal index within the corresponding video, which allows the user to trace every frame back to its origin. - -**training-datasets:** This directory will contain the training dataset used to train the network and metadata, which contains information about how the training dataset was created. - -**videos:** Directory of video links or videos. When **copy\_videos** is set to ``False``, this directory contains symbolic links to the videos. If it is set to ``True`` then the videos will be copied to this directory. The default is ``False``. Additionally, if the user wants to add new videos to the project at any stage, the function **add\_new\_videos** can be used. This will update the list of videos in the project's configuration file. +Windows users, you must input paths as: `r'C:\Users\computername\Videos\reachingvideo1.avi'` or +` 'C:\\Users\\computername\\Videos\\reachingvideo1.avi'` + +TIP: you can also place `config_path` in front of `deeplabcut.create_new_project` to create a variable that holds +the path to the config.yaml file, i.e. `config_path=deeplabcut.create_new_project(...)` + +This set of arguments will create a project directory with the name +**++** in the **Working directory** and +creates the symbolic links to videos in the **videos** directory. The project directory will have subdirectories: +**dlc-models**, **dlc-models-pytorch**, **labeled-data**, **training-datasets**, and **videos**. All the outputs +generated during the course of a project will be stored in one of these subdirectories, thus allowing each project to be +curated in separation from other projects. The purpose of the subdirectories is as follows: + +**dlc-models** and **dlc-models-pytorch** have a similar structure; the first contains files for the TensorFlow engine +while the second contains files for the PyTorch engine. At the top level in these directories, there are directories +referring to different iterations of label refinement (see below): **iteration-0**, **iteration-1**, etc. +The iteration directories store shuffle directories, where each shuffle directory stores model data related to a +particular experiment: trained and tested on a particular training and testing sets, and with a particular model +architecture. Each shuffle directory contains the subdirectories *test* and *train*, each of which holds the meta +information with regard to the parameters of the feature detectors in configuration files. The configuration files are +YAML files, a common human-readable data serialization language. These files can be opened and edited with standard text +editors. The subdirectory *train* will store checkpoints (called snapshots) during training of the model. These +snapshots allow the user to reload the trained model without re-training it, or to pick-up training from a particular +saved checkpoint, in case the training was interrupted. + +**labeled-data:** This directory will store the frames used to create the training dataset. Frames from different videos +are stored in separate subdirectories. Each frame has a filename related to the temporal index within the corresponding +video, which allows the user to trace every frame back to its origin. + +**training-datasets:** This directory will contain the training dataset used to train the network and metadata, which +contains information about how the training dataset was created. + +**videos:** Directory of video links or videos. When **copy\_videos** is set to `False`, this directory contains +symbolic links to the videos. If it is set to `True` then the videos will be copied to this directory. The default is +`False`. Additionally, if the user wants to add new videos to the project at any stage, the function +**add\_new\_videos** can be used. This will update the list of videos in the project's configuration file. ```python -deeplabcut.add_new_videos('Full path of the project configuration file*', ['full path of video 4', 'full path of video 5'], copy_videos=True/False) +deeplabcut.add_new_videos( + "Full path of the project configuration file*", + ["full path of video 4", "full path of video 5"], + copy_videos=True/False +) ``` -*Please note, *Full path of the project configuration file* will be referenced as ``config_path`` throughout this protocol. +*Please note, *Full path of the project configuration file* will be referenced as `config_path` throughout this +protocol. -The project directory also contains the main configuration file called *config.yaml*. The *config.yaml* file contains many important parameters of the project. A complete list of parameters including their description can be found in Box1. +The project directory also contains the main configuration file called *config.yaml*. The *config.yaml* file contains +many important parameters of the project. A complete list of parameters including their description can be found in +Box1. -The ``create_new_project`` step writes the following parameters to the configuration file: *Task*, *scorer*, *date*, *project\_path* as well as a list of videos *video\_sets*. The first three parameters should **not** be changed. The list of videos can be changed by adding new videos or manually removing videos. +The `create_new_project` step writes the following parameters to the configuration file: *Task*, *scorer*, *date*, +*project\_path* as well as a list of videos *video\_sets*. The first three parameters should **not** be changed. The +list of videos can be changed by adding new videos or manually removing videos. -

- -

+![Box 1 - Single Animal Project Configuration File Glossary](images/box1-single.png) #### API Docs ````{admonition} Click the button to see API Docs @@ -87,34 +142,61 @@ The ``create_new_project`` step writes the following parameters to the configura DLC Utils -Next, open the **config.yaml** file, which was created during **create\_new\_project**. You can edit this file in any text editor. Familiarize yourself with the meaning of the parameters (Box 1). You can edit various parameters, in particular you **must add the list of *bodyparts* (or points of interest)** that you want to track. You can also set the *colormap* here that is used for all downstream steps (can also be edited at anytime), like labeling GUIs, videos, etc. Here any [matplotlib colormaps](https://matplotlib.org/tutorials/colors/colormaps.html) will do! +Next, open the **config.yaml** file, which was created during **create\_new\_project**. You can edit this file in any +text editor. Familiarize yourself with the meaning of the parameters (Box 1). You can edit various parameters, in +particular you **must add the list of *bodyparts* (or points of interest)** that you want to track. You can also set the +*colormap* here that is used for all downstream steps (can also be edited at anytime), like labeling GUIs, videos, etc. +Here any [matplotlib colormaps](https://matplotlib.org/tutorials/colors/colormaps.html) will do! Please DO NOT have spaces in the names of bodyparts. **bodyparts:** are the bodyparts of each individual (in the above list). - ### (C) Data Selection (extract frames) - -**CRITICAL:** A good training dataset should consist of a sufficient number of frames that capture the breadth of the behavior. This ideally implies to select the frames from different (behavioral) sessions, different lighting and different animals, if those vary substantially (to train an invariant, robust feature detector). Thus for creating a robust network that you can reuse in the laboratory, a good training dataset should reflect the diversity of the behavior with respect to postures, luminance conditions, background conditions, animal identities,etc. of the data that will be analyzed. For the simple lab behaviors comprising mouse reaching, open-field behavior and fly behavior, 100−200 frames gave good results [Mathis et al, 2018](https://www.nature.com/articles/s41593-018-0209-y). However, depending on the required accuracy, the nature of behavior, the video quality (e.g. motion blur, bad lighting) and the context, more or less frames might be necessary to create a good network. Ultimately, in order to scale up the analysis to large collections of videos with perhaps unexpected conditions, one can also refine the data set in an adaptive way (see refinement below). - -The function `extract_frames` extracts frames from all the videos in the project configuration file in order to create a training dataset. The extracted frames from all the videos are stored in a separate subdirectory named after the video file’s name under the ‘labeled-data’. This function also has various parameters that might be useful based on the user’s need. + ### (C) Select Frames to Label + +**CRITICAL:** A good training dataset should consist of a sufficient number of frames that capture the breadth of the +behavior. This ideally implies to select the frames from different (behavioral) sessions, different lighting and +different animals, if those vary substantially (to train an invariant, robust feature detector). Thus for creating a +robust network that you can reuse in the laboratory, a good training dataset should reflect the diversity of the +behavior with respect to postures, luminance conditions, background conditions, animal identities,etc. of the data that +will be analyzed. For the simple lab behaviors comprising mouse reaching, open-field behavior and fly behavior, 100−200 +frames gave good results [Mathis et al, 2018](https://www.nature.com/articles/s41593-018-0209-y). However, depending on +the required accuracy, the nature of behavior, the video quality (e.g. motion blur, bad lighting) and the context, more +or less frames might be necessary to create a good network. Ultimately, in order to scale up the analysis to large +collections of videos with perhaps unexpected conditions, one can also refine the data set in an adaptive way (see +refinement below). + +The function `extract_frames` extracts frames from all the videos in the project configuration file in order to create +a training dataset. The extracted frames from all the videos are stored in a separate subdirectory named after the video +file’s name under the ‘labeled-data’. This function also has various parameters that might be useful based on the user’s +need. ```python -deeplabcut.extract_frames(config_path, mode='automatic/manual', algo='uniform/kmeans', userfeedback=False, crop=True/False) +deeplabcut.extract_frames( + config_path, + mode="automatic/manual", + algo="uniform/kmeans", + crop=True/False, + userfeedback=False +) ``` **CRITICAL POINT:** It is advisable to keep the frame size small, as large frames increase the training and inference time. The cropping parameters for each video can be provided in the config.yaml file (and see below). -When running the function extract_frames, if the parameter crop=True, then you will be asked to draw a box within the GUI (and this is written to the config.yaml file). - -`userfeedback` allows the user to check which videos they wish to extract frames from. In this way, if you added more videos to the config.yaml file it does not, by default, extract frames (again) from every video. If you wish to disable this question, set `userfeedback = True`. - -The provided function either selects frames from the videos that are randomly sampled from a uniform distribution (uniform), by clustering based on visual appearance (k-means), or by manual selection. Random -selection of frames works best for behaviors where the postures vary across the whole video. However, some behaviors -might be sparse, as in the case of reaching where the reach and pull are very fast and the mouse is not moving much -between trials (thus, we have the default set to True, as this is best for most use-cases we encounter). In such a case, the function that allows selecting frames based on k-means derived quantization would -be useful. If the user chooses to use k-means as a method to cluster the frames, then this function downsamples the -video and clusters the frames using k-means, where each frame is treated as a vector. Frames from different clusters -are then selected. This procedure makes sure that the frames look different. However, on large and long videos, this -code is slow due to computational complexity. +When running the function extract_frames, if the parameter crop=True, then you will be asked to draw a box within the +GUI (and this is written to the config.yaml file). + +`userfeedback` allows the user to specify which videos they wish to extract frames from. When set to `"True"`, a dialog +will be initiated, where the user is asked for each video if (additional/any) frames from this video should be +extracted. Use this, e.g. if you have already labeled some folders and want to extract data for new videos. + +The provided function either selects frames from the videos that are randomly sampled from a uniform distribution +(uniform), by clustering based on visual appearance (k-means), or by manual selection. Random uniform selection of +frames works best for behaviors where the postures vary across the whole video. However, some behaviors might be sparse, +as in the case of reaching where the reach and pull are very fast and the mouse is not moving much between trials. In +such a case, the function that allows selecting frames based on k-means derived quantization would be useful. If the +user chooses to use k-means as a method to cluster the frames, then this function downsamples the video and clusters the +frames using k-means, where each frame is treated as a vector. Frames from different clusters are then selected. This +procedure makes sure that the frames look different. However, on large and long videos, this code is slow due to +computational complexity. **CRITICAL POINT:** It is advisable to extract frames from a period of the video that contains interesting behaviors, and not extract the frames across the whole video. This can be achieved by using the start and stop @@ -122,13 +204,16 @@ parameters in the config.yaml file. Also, the user can change the number of fram the numframes2extract in the config.yaml file. However, picking frames is highly dependent on the data and the behavior being studied. Therefore, it is hard to -provide all purpose code that extracts frames to create a good training dataset for every behavior and animal. If the user feels specific frames are lacking, they can extract hand selected frames of interest using the interactive GUI +provide all purpose code that extracts frames to create a good training dataset for every behavior and animal. If the +user feels specific frames are lacking, they can extract hand selected frames of interest using the interactive GUI provided along with the toolbox. This can be launched by using: ```python -deeplabcut.extract_frames(config_path, 'manual') +deeplabcut.extract_frames(config_path, "manual") ``` The user can use the *Load Video* button to load one of the videos in the project configuration file, use the scroll -bar to navigate across the video and *Grab a Frame* (or a range of frames, as of version 2.0.5) to extract the frame(s). The user can also look at the extracted frames and e.g. delete frames (from the directory) that are too similar before reloading the set and then manually annotating them. +bar to navigate across the video and *Grab a Frame* (or a range of frames, as of version 2.0.5) to extract the frame(s). +The user can also look at the extracted frames and e.g. delete frames (from the directory) that are too similar before +reloading the set and then manually annotating them.

@@ -144,38 +229,42 @@ bar to navigate across the video and *Grab a Frame* (or a range of frames, as of ### (D) Label Frames -The toolbox provides a function **label_frames** which helps the user to easily label all the extracted frames using -an interactive graphical user interface (GUI). The user should have already named the body parts to label (points of -interest) in the project’s configuration file by providing a list. The following command invokes the labeling toolbox. +The toolbox provides a function **label_frames** which helps the user to easily label +all the extracted frames using an interactive graphical user interface (GUI). The user +should have already named the bodyparts to label (points of interest) in the +project’s configuration file by providing a list. The following command invokes the +napari-deeplabcut labelling GUI. Checkout the [napari-deeplabcut docs](napari-gui) for +more information about the labelling workflow. + ```python deeplabcut.label_frames(config_path) ``` -The user needs to use the *Load Frames* button to select the directory which stores the extracted frames from one of -the videos. Subsequently, the user can use one of the radio buttons (top right) to select a body part to label. RIGHT click to add the label. Left click to drag the label, if needed. If you label a part accidentally, you can use the middle button on your mouse to delete! If you cannot see a body part in the frame, skip over the label! Please see the ``HELP`` button for more user instructions. This auto-advances once you labeled the first body part. You can also advance to the next frame by clicking on the RIGHT arrow on your keyboard (and go to a previous frame with LEFT arrow). -Each label will be plotted as a dot in a unique color. - -The user is free to move around the body part and once satisfied with its position, can select another radio button -(in the top right) to switch to the respective body part (it otherwise auto-advances). The user can skip a body part if it is not visible. Once all the visible body parts are labeled, then the user can use ‘Next Frame’ to load the following frame. The user needs to save the labels after all the frames from one of the videos are labeled by clicking the save button at the bottom right. Saving the labels will create a labeled dataset for each video in a hierarchical data file format (HDF) in the -subdirectory corresponding to the particular video in **labeled-data**. You can save at any intermediate step (even without closing the GUI, just hit save) and you return to labeling a dataset by reloading it! - -**CRITICAL POINT:** It is advisable to **consistently label similar spots** (e.g., on a wrist that is very large, try -to label the same location). In general, invisible or occluded points should not be labeled by the user. They can -simply be skipped by not applying the label anywhere on the frame. -OPTIONAL: In the event of adding more labels to the existing labeled dataset, the user need to append the new -labels to the bodyparts in the config.yaml file. Thereafter, the user can call the function **label_frames**. As of 2.0.5+: then a box will pop up and ask the user if they wish to display all parts, or only add in the new labels. Saving the labels after all the images are labelled will append the new labels to the existing labeled dataset. +[🎥 DEMO](https://youtu.be/hsA9IB5r73E) HOT KEYS IN THE Labeling GUI (also see "help" in GUI): + ``` Ctrl + C: Copy labels from previous frame. Keyboard arrows: advance frames. Delete key: delete label. ``` + ![hot keys](https://images.squarespace-cdn.com/content/v1/57f6d51c9f74566f55ecf271/192345a5-e411-4d56-b718-ef52f91e195e/Qwerty.png?format=2500w) +**CRITICAL POINT:** It is advisable to **consistently label similar spots** (e.g., on a wrist that is very large, try +to label the same location). In general, invisible or occluded points should not be labeled by the user. They can +simply be skipped by not applying the label anywhere on the frame. +OPTIONAL: In the event of adding more labels to the existing labeled dataset, the user need to append the new +labels to the bodyparts in the config.yaml file. Thereafter, the user can call the function **label_frames**. As of +2.0.5+: then a box will pop up and ask the user if they wish to display all parts, or only add in the new labels. +Saving the labels after all the images are labelled will append the new labels to the existing labeled dataset. -### (E) Check Annotated Frames +For more information, checkout the [napari-deeplabcut docs](napari-gui) for +more information about the labelling workflow. + +### (E) Check Annotated Frames OPTIONAL: Checking if the labels were created and stored correctly is beneficial for training, since labeling is one of the most critical parts for creating the training dataset. The DeepLabCut toolbox provides a function @@ -184,7 +273,10 @@ is one of the most critical parts for creating the training dataset. The DeepLab deeplabcut.check_labels(config_path, visualizeindividuals=True/False) ``` -For each video directory in labeled-data this function creates a subdirectory with **labeled** as a suffix. Those directories contain the frames plotted with the annotated body parts. The user can double check if the body parts are labeled correctly. If they are not correct, the user can reload the frames (i.e. `deeplabcut.label_frames`), move them around, and click save again. +For each video directory in labeled-data this function creates a subdirectory with **labeled** as a suffix. Those +directories contain the frames plotted with the annotated body parts. The user can double check if the body parts are +labeled correctly. If they are not correct, the user can reload the frames (i.e. `deeplabcut.label_frames`), move them +around, and click save again. #### API Docs ````{admonition} Click the button to see API Docs @@ -194,99 +286,228 @@ For each video directory in labeled-data this function creates a subdirectory wi ``` ```` -### (F) Create Training Dataset(s) and selection of your neural network +(create-training-dataset)= +### (F) Create Training Dataset -**CRITICAL POINT:** Only run this step **where** you are going to train the network. If you label on your laptop but move your project folder to Google Colab or AWS, lab server, etc, then run the step below on that platform! If you labeled on a Windows machine but train on Linux, this is fine as of 2.0.4 onwards it will be done automatically (it saves file sets as both Linux and Windows for you). +**CRITICAL POINT:** Only run this step **where** you are going to train the network. If you label on your laptop but +move your project folder to Google Colab or AWS, lab server, etc, then run the step below on that platform! If you +labeled on a Windows machine but train on Linux, this is fine as of 2.0.4 onwards it will be done automatically (it +saves file sets as both Linux and Windows for you). -- If you move your project folder, you must only change the `project_path` (which is done automatically) in the main config.yaml file - that's it - no need to change the video paths, etc! Your project is fully portable. +- If you move your project folder, you must only change the `project_path` (which is done automatically) in the main +config.yaml file - that's it - no need to change the video paths, etc! Your project is fully portable. -- Be aware you select your neural network backbone at this stage. As of DLC3+ we support PyTorch (and TensorFlow, but this will be phased out). +- Be aware you select your neural network backbone at this stage. As of DLC3+ we support PyTorch (and TensorFlow, but +this will be phased out). -**OVERVIEW:** This function combines the labeled datasets from all the videos and splits them to create train and test datasets. The training data will be used to train the network, while the test data set will be used for evaluating the network. The function **create_training_dataset** performs those steps. +**OVERVIEW:** This function combines the labeled datasets from all the videos and splits them to create train and test +datasets. The training data will be used to train the network, while the test data set will be used for evaluating the +network. ```python deeplabcut.create_training_dataset(config_path) ``` -- OPTIONAL: If the user wishes to benchmark the performance of the DeepLabCut, they can create multiple training datasets by specifying an integer value to the `num_shuffles`; see the docstring for more details. - -within **dlc-models** called ``test`` and ``train``, and these each have a configuration file called pose_cfg.yaml. -Specifically, the user can edit the **pose_cfg.yaml** within the **train** subdirectory before starting the training. These configuration files contain meta information with regard to the parameters of the feature detectors. Key parameters are listed in Box 2. +- OPTIONAL: If the user wishes to benchmark the performance of the DeepLabCut, they can create multiple training +datasets by specifying an integer value to the `num_shuffles`; see the docstring for more details. -**CRITICAL POINT:** At this step, for **create_training_dataset** you select the network you want to use, and any additional data augmentation (beyond our defaults). You can set ``net_type`` and ``augmenter_type`` when you call the function. +The function creates a new shuffle(s) directory in the **dlc-models-pytorch** directory +(**dlc-models** if using Tensorflow), in the current "iteration" directory. +The `train` and `test` directories each have a configuration file +(**pytorch_config.yaml** in **train** and **pose_cfg.yaml** in **test** for Pytorch models, +**pose_cfg.yaml** in **train** and **test** for Tensorflow models). +Specifically, the user can edit the **pytorch_config.yaml** (or **pose_cfg.yaml**) within the **train** subdirectory +before starting the training. These configuration files contain meta information with regard to the parameters +of the feature detectors. For more information about the **pytorch_config.yaml** file, see [here](dlc3-pytorch-config) +(for TensorFlow-based models, see key parameters +[here](https://github.com/DeepLabCut/DeepLabCut/blob/main/deeplabcut/pose_cfg.yaml)). -- Networks: ImageNet pre-trained networks OR SuperAnimal pre-trained networks weights will be downloaded, as you select. You can decide to do transfer-learning (recommended) or "fine-tune" both the backbone and the decoder head. We suggest seeing our [dedicated documentation on models](https://deeplabcut.github.io/DeepLabCut/docs/pytorch/architectures.html) for more information. +**CRITICAL POINT:** At this step, for **create_training_dataset** you select the network you want to use, and any +additional data augmentation (beyond our defaults). You can set `net_type`, `detector_type` (if using a detector) +and `augmenter_type` when you call the function. +- Networks: ImageNet pre-trained networks OR SuperAnimal pre-trained networks weights will be downloaded, as you +select. You can decide to do transfer-learning (recommended) or "fine-tune" both the backbone and the decoder head. We +suggest seeing our [dedicated documentation on models](dlc3-architectures) for more information ( +or the [this page on selecting models](what-neural-network-should-i-use) for the TensorFlow engine). ```{Hint} -🚨 If they do not download (you will see this downloading in the terminal), then you may not have permission to do so - be sure to open your terminal "as an admin" (This is only something we have seen with some Windows users - see the **[docs for more help!](https://deeplabcut.github.io/DeepLabCut/docs/recipes/nn.html)**). -``` - -**DATA AUGMENTATION:** At this stage you can also decide what type of augmentation to use. The default loaders work well for most all tasks (as shown on www.deeplabcut.org), but there are many options, more data augmentation, intermediate supervision, etc. Please look at the [**pose_cfg.yaml**](https://github.com/DeepLabCut/DeepLabCut/blob/master/deeplabcut/pose_cfg.yaml) file for a full list of parameters **you might want to change before running this step.** There are several data loaders that can be used. For example, you can use the default loader (introduced and described in the Nature Protocols paper), [TensorPack](https://github.com/tensorpack/tensorpack) for data augmentation (currently this is easiest on Linux only), or [imgaug](https://imgaug.readthedocs.io/en/latest/). We recommend `imgaug`. You can set this by passing:``` deeplabcut.create_training_dataset(config_path, augmenter_type='imgaug') ``` - -**For TensorFlow Models:** the differences of the loaders are as follows: -- `imgaug`: a lot of augmentation possibilities, efficient code for target map creation & batch sizes >1 supported. You can set the parameters such as the `batch_size` in the `pose_cfg.yaml` file for the model you are training. This is the recommended DEFAULT! -- `crop_scale`: our standard DLC 2.0 introduced in Nature Protocols variant (scaling, auto-crop augmentation) -- `tensorpack`: a lot of augmentation possibilities, multi CPU support for fast processing, target maps are created less efficiently than in imgaug, does not allow batch size>1 -- `deterministic`: only useful for testing, freezes numpy seed; otherwise like default. - -**For PyTorch Models:** -- #TODO: more information coming soon; in the meantime see the docstrings! - - -**MODEL COMPARISON:** You can also test several models by creating the same test/train split for different networks. You can easily do this in the Project Manager GUI, which also lets you compare PyTorch and TensorFlow models. - -Please also consult the [following page on selecting models]( https://deeplabcut.github.io/DeepLabCut/docs/recipes/nn.html#what-neural-network-should-i-use-trade-offs-speed-performance-and-considerations) +🚨 If they do not download (you will see this downloading in the terminal), then you may not have permission to do +so - be sure to open your terminal "as an admin" (This is only something we have seen with some Windows users - see +the **[docs for more help!](tf-training-tips-and-tricks)**). +``` + +**DATA AUGMENTATION:** At this stage you can also decide what type of augmentation to +use. Once you've called `create_training_dataset`, you can edit the +[**pytorch_config.yaml**](dlc3-pytorch-config) file that was created (or for the +TensorFlow engine, the [**pose_cfg.yaml**]( +https://github.com/DeepLabCut/DeepLabCut/blob/main/deeplabcut/pose_cfg.yaml) file). + +- PyTorch Engine: [Albumentations](https://albumentations.ai/docs/) is used for data +augmentation. Look at the [**pytorch_config.yaml**](dlc3-pytorch-config) for more +information about image augmentation options. +- TensorFlow Engine: The default augmentation works well for most tasks (as shown on +www.deeplabcut.org), but there are many options, more data augmentation, intermediate +supervision, etc. Here are the available loaders: + - `imgaug`: a lot of augmentation possibilities, efficient code for target map creation & batch sizes >1 supported. + You can set the parameters such as the `batch_size` in the `pose_cfg.yaml` file for the model you are training. This + is the recommended default! + - `crop_scale`: our standard DLC 2.0 introduced in Nature Protocols variant (scaling, auto-crop augmentation) + - `tensorpack`: a lot of augmentation possibilities, multi CPU support for fast processing, target maps are created + less efficiently than in imgaug, does not allow batch size>1 + - `deterministic`: only useful for testing, freezes numpy seed; otherwise like default. + +**MODEL COMPARISON**: You can also test several models by creating the same train/test +split for different networks. +You can easily do this in the Project Manager GUI (by selecting the "Use an existing +data split" option), which also lets you compare PyTorch and TensorFlow models. + +````{versionadded} 3.0.0 +You can now create new shuffles using the same train/test split as +existing shuffles with `create_training_dataset_from_existing_split`. This allows you to +compare model performance (between different architectures or when using different +training hyper-parameters) as the shuffles were trained on the same data, and evaluated +on the same test data! + +Example usage - creating 3 new shuffles (with indices 10, 11 and 12) for a ResNet 50 +pose estimation model, using the same data split as was used for shuffle 0: - See Box 2 on how to specify **which network is loaded for training (including your own network, etc):** - -

- -

+```python +deeplabcut.create_training_dataset_from_existing_split( + config_path, + from_shuffle=0, + shuffles=[10, 11, 12], + net_type="resnet_50", +) +``` +```` -#### API Docs for deeplabcut.create_training_dataset -````{admonition} Click the button to see API Docs +````{admonition} Click the button to see API Docs for deeplabcut.create_training_dataset :class: dropdown ```{eval-rst} .. include:: ./api/deeplabcut.create_training_dataset.rst ``` ```` -#### API Docs for deeplabcut.create_training_model_comparison -````{admonition} Click the button to see API Docs +````{admonition} Click the button to see API Docs for deeplabcut.create_training_model_comparison :class: dropdown ```{eval-rst} .. include:: ./api/deeplabcut.create_training_model_comparison.rst ``` ```` +````{admonition} Click the button to see API Docs for deeplabcut.create_training_dataset_from_existing_split +:class: dropdown +```{eval-rst} +.. include:: ./api/deeplabcut.create_training_dataset_from_existing_split.rst +``` +```` + ### (G) Train The Network The function ‘train_network’ helps the user in training the network. It is used as follows: ```python deeplabcut.train_network(config_path) ``` -The set of arguments in the function starts training the network for the dataset created for one specific shuffle. Note that you can change the loader (imgaug/default/etc) as well as other training parameters in the **pose_cfg.yaml** file of the model that you want to train (before you start training). +The set of arguments in the function starts training the network for the dataset created +for one specific shuffle. Note that you can change training parameters in the +[**pytorch_config.yaml**](dlc3-pytorch-config) file (or **pose_cfg.yaml** for TensorFlow +models) of the model that you want to train (before you start training). -Example parameters that one can call: -```python -deeplabcut.train_network(config_path, shuffle=1, trainingsetindex=0, gputouse=None, max_snapshots_to_keep=5, autotune=False, displayiters=100, saveiters=15000, maxiters=30000, allow_growth=True) -``` +At user specified iterations during training checkpoints are stored in the subdirectory +*train* under the respective iteration & shuffle directory. + +````{admonition} Tips on training models with the PyTorch Engine +:class: dropdown -By default, the pretrained networks are not in the DeepLabCut toolbox (as they are around 100MB each), but they get downloaded before you train. However, if not previously downloaded, it will be downloaded and stored in a subdirectory *pre-trained* under the subdirectory *models* in *Pose_Estimation_Tensorflow* or *Pose_Estimation_PyTorch*. -At user specified iterations during training checkpoints are stored in the subdirectory *train* under the respective iteration directory. +Example parameters that one can call: -If the user wishes to restart the training at a specific checkpoint they can specify the full path of the checkpoint to -the variable ``init_weights`` in the **pose_cfg.yaml** file under the *train* subdirectory (see Box 2). +```python +deeplabcut.train_network( + config_path, + shuffle=1, + trainingsetindex=0, + device="cuda:0", + max_snapshots_to_keep=5, + displayiters=100, + save_epochs=5, + epochs=200, +) +``` + +Pytorch models in DeepLabCut 3.0 are trained for a set number of epochs, instead of a +maximum number of iterations (which is what was used for TensorFlow models). An epoch +is a single pass through the training dataset, which means your model has seen each +training image exactly once. So if you have 64 training images for your network, an +epoch is 64 iterations with batch size 1 (or 32 iterations with batch size 2, 16 with +batch size 4, etc.). + +By default, the pretrained networks are not in the DeepLabCut toolbox (as they can be +more than 100MB), but they get downloaded automatically before you train. + +If the user wishes to restart the training at a specific checkpoint they can specify the +full path of the checkpoint to the variable ``resume_training_from`` in the [ +**pytorch_config.yaml**]( +dlc3-pytorch-config) file (checkout the "Restarting Training at a Specific Checkpoint" +section of the docs) under the *train* subdirectory. + +**CRITICAL POINT:** It is recommended to train the networks **until the loss plateaus** +(depending on the dataset, model architecture and training hyper-parameters this happens +after 100 to 250 epochs of training). + +The variables ``display_iters`` and ``save_epochs`` in the [**pytorch_config.yaml**]( +dlc3-pytorch-config) file allows the user to alter how often the loss is displayed +and how often the weights are stored. We suggest saving every 5 to 25 epochs. +```` -**CRITICAL POINT, For TensorFlow models:** it is recommended to train the ResNets or MobileNets for thousands of iterations until the loss plateaus (typically around **500,000**) if you use batch size 1. If you want to batch train, [we recommend using Adam, see more here](https://deeplabcut.github.io/DeepLabCut/docs/recipes/nn.html#using-custom-image-augmentation). +````{admonition} Tips on training models with the TensorFlow Engine +:class: dropdown -**CRITICAL POINT, For PyTorch models:** PyTorch uses "epochs" not iterations. Please see our dedicated documentation that [explains how best to set the number of epochs here](https://deeplabcut.github.io/DeepLabCut/docs/pytorch/user_guide.html). When in doubt, stick to the default! A bonus, training time is much less! +Example parameters that one can call: -**maDeepLabCut CRITICAL POINT:** For multi-animal projects we are using not only different and new output layers, but also new data augmentation, optimization, learning rates, and batch training defaults. Thus, please use a lower ``save_iters`` and ``maxiters``. I.e., we suggest saving every 10K-15K iterations, and only training until 50K-100K iterations. We recommend you look closely at the loss to not overfit on your data. The bonus, training time is much less! +```python +deeplabcut.train_network( + config_path, + shuffle=1, + trainingsetindex=0, + gputouse=None, + max_snapshots_to_keep=5, + autotune=False, + displayiters=100, + saveiters=25000, + maxiters=300000, + allow_growth=True, +) +``` + +By default, the pretrained networks are not in the DeepLabCut toolbox (as they are +around 100MB each), but they get downloaded before you train. However, if not previously +downloaded from the TensorFlow model weights, it will be downloaded and stored in a +subdirectory *pre-trained* under the subdirectory *models* in +*Pose_Estimation_Tensorflow*. At user specified iterations during training checkpoints +are stored in the subdirectory *train* under the respective iteration directory. + +If the user wishes to restart the training at a specific checkpoint they can specify the +full path of the checkpoint to the variable ``init_weights`` in the **pose_cfg.yaml** +file under the *train* subdirectory (see Box 2). + +**CRITICAL POINT:** It is recommended to train the networks for thousands of iterations +until the loss plateaus (typically around **500,000**) if you use batch size 1. If you +want to batch train, we recommend using Adam, +[see more here](tf-custom-image-augmentation). + +The variables ``display_iters`` and ``save_iters`` in the **pose_cfg.yaml** file allows +the user to alter how often the loss is displayed and how often the weights are stored. + +**maDeepLabCut CRITICAL POINT:** For multi-animal projects we are using not only +different and new output layers, but also new data augmentation, optimization, learning +rates, and batch training defaults. Thus, please use a lower ``save_iters`` and +``maxiters``. I.e. we suggest saving every 10K-15K iterations, and only training until +50K-100K iterations. We recommend you look closely at the loss to not overfit on your +data. The bonus, training time is much less!!! +```` -#### API Docs -````{admonition} Click the button to see API Docs +````{admonition} Click the button to see API Docs for train_network :class: dropdown ```{eval-rst} .. include:: ./api/deeplabcut.train_network.rst @@ -296,36 +517,40 @@ the variable ``init_weights`` in the **pose_cfg.yaml** file under the *train* su ### (H) Evaluate the Trained Network It is important to evaluate the performance of the trained network. This performance is measured by computing -the mean average Euclidean error (MAE; which is proportional to the average root mean square error) between the -manual labels and the ones predicted by DeepLabCut. The MAE is saved as a comma separated file and displayed -for all pairs and only likely pairs (>p-cutoff). This helps to exclude, for example, occluded body parts. One of the -strengths of DeepLabCut is that due to the probabilistic output of the scoremap, it can, if sufficiently trained, also -reliably report if a body part is visible in a given frame. (see discussions of finger tips in reaching and the Drosophila -legs during 3D behavior in [Mathis et al, 2018]). The evaluation results are computed by typing: +the average root mean square error (RMSE) between the manual labels and the ones predicted by DeepLabCut. +The RMSE is saved as a comma separated file and displayed for all pairs and only likely pairs (>p-cutoff). +This helps to exclude, for example, occluded body parts. One of the strengths of DeepLabCut is that due to the +probabilistic output of the scoremap, it can, if sufficiently trained, also reliably report if a body part is visible +in a given frame. (see discussions of finger tips in reaching and the Drosophila legs during 3D behavior in +[Mathis et al, 2018]). The evaluation results are computed by typing: ```python deeplabcut.evaluate_network(config_path, Shuffles=[1], plotting=True) ``` -Setting ``plotting`` to true plots all the testing and training frames with the manual and predicted labels. The user +Setting `plotting` to true plots all the testing and training frames with the manual and predicted labels. The user should visually check the labeled test (and training) images that are created in the ‘evaluation-results’ directory. Ideally, DeepLabCut labeled unseen (test images) according to the user’s required accuracy, and the average train -and test errors are comparable (good generalization). What (numerically) comprises an acceptable MAE depends on -many factors (including the size of the tracked body parts, the labeling variability, etc.). Note that the test error can -also be larger than the training error due to human variability (in labeling, see Figure 2 in Mathis et al, Nature Neuroscience 2018). +and test errors are comparable (good generalization). What (numerically) comprises an acceptable RMSE depends on +many factors (including the size of the tracked body parts, the labeling variability, etc.). Note that the test error +can also be larger than the training error due to human variability (in labeling, see Figure 2 in Mathis et al, Nature +Neuroscience 2018). **Optional parameters:** -``` - Shuffles: list, optional -List of integers specifying the shuffle indices of the training dataset. The default is [1] - plotting: bool, optional -Plots the predictions on the train and test images. The default is `False`; if provided it must be either `True` or `False` +- `Shuffles: list, optional` - List of integers specifying the shuffle indices of the training dataset. +The default is [1] - show_errors: bool, optional -Display train and test errors. The default is `True` +- `plotting: bool, optional` - Plots the predictions on the train and test images. The default is `False`; +if provided it must be either `True` or `False` - comparisonbodyparts: list of bodyparts, Default is all -The average error will be computed for those body parts only (Has to be a subset of the body parts). +- `show_errors: bool, optional` - Display train and test errors. The default is `True` - gputouse: int, optional -Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not have a GPU, put None. See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries -``` +- `comparisonbodyparts: list of bodyparts, Default is all` - The average error will be computed for those body parts +only (Has to be a subset of the body parts). + +- `gputouse: int, optional` - Natural number indicating the number of your GPU (see number in nvidia-smi). If you do not +have a GPU, put None. See: https://nvidia.custhelp.com/app/answers/detail/a_id/3751/~/useful-nvidia-smi-queries The plots can be customized by editing the **config.yaml** file (i.e., the colormap, scale, marker size (dotsize), and transparency of labels (alphavalue) can be modified). By default each body part is plotted in a different color @@ -334,9 +559,10 @@ plotted as plus (‘+’), DeepLabCut’s predictions either as ‘.’ (for con ’x’ for (likelihood <= `pcutoff`). The evaluation results for each shuffle of the training dataset are stored in a unique subdirectory in a newly created -directory ‘evaluation-results’ in the project directory. The user can visually inspect if the distance between the labeled -and the predicted body parts are acceptable. In the event of benchmarking with different shuffles of same training -dataset, the user can provide multiple shuffle indices to evaluate the corresponding network. +directory ‘evaluation-results-pytorch’ (‘evaluation-results’ for tensorflow models) in the project directory. +The user can visually inspect if the distance between the labeled and the predicted body parts are acceptable. +In the event of benchmarking with different shuffles of same training dataset, the user can provide multiple shuffle +indices to evaluate the corresponding network. Note that with multi-animal projects additional distance statistics aggregated over animals or bodyparts are also stored in that directory. This aims at providing a finer quantitative evaluation of multi-animal prediction performance before animal tracking. If the generalization is not sufficient, the user might want to: @@ -363,24 +589,40 @@ you can drop "Indices" to run this on all training/testing images (this is slow! ``` ```` -### (I) Novel Video Analysis: +### (I) Analyze new Videos -The trained network can be used to analyze new videos. The user needs to first choose a checkpoint with the best -evaluation results for analyzing the videos. In this case, the user can enter the corresponding index of the checkpoint -to the variable snapshotindex in the config.yaml file. By default, the most recent checkpoint (i.e. last) is used for -analyzing the video. Novel/new videos **DO NOT have to be in the config file!** You can analyze new videos anytime by simply using the following line of code: +The trained network can be used to analyze new videos. Novel/new videos **DO NOT have to be in the config file!**. +You can analyze new videos anytime by simply using the following line of code: ```python -deeplabcut.analyze_videos(config_path, ['fullpath/analysis/project/videos/reachingvideo1.avi'], save_as_csv=True) +deeplabcut.analyze_videos( + config_path, ["fullpath/analysis/project/videos/reachingvideo1.avi"], + save_as_csv=True +) ``` There are several other optional inputs, such as: ```python -deeplabcut.analyze_videos(config_path, videos, videotype='avi', shuffle=1, trainingsetindex=0, gputouse=None, save_as_csv=False, destfolder=None, dynamic=(True, .5, 10)) -``` -The labels are stored in a [MultiIndex Pandas Array](http://pandas.pydata.org), which contains the name of the network, body part name, (x, y) label position in pixels, and the likelihood for each frame per body part. These -arrays are stored in an efficient Hierarchical Data Format (HDF) in the same directory, where the video is stored. -However, if the flag ``save_as_csv`` is set to ``True``, the data can also be exported in comma-separated values format -(.csv), which in turn can be imported in many programs, such as MATLAB, R, Prism, etc.; This flag is set to ``False`` -by default. You can also set a destination folder (``destfolder``) for the output files by passing a path of the folder you wish to write to. +deeplabcut.analyze_videos( + config_path, + videos, + videotype="avi", + shuffle=1, + trainingsetindex=0, + gputouse=None, + save_as_csv=False, + destfolder=None, + dynamic=(True, .5, 10) +) +``` +The user can choose a checkpoint for analyzing the videos. For this, the user can enter the corresponding index of the +checkpoint to the variable snapshotindex in the config.yaml file. By default, the most recent checkpoint (i.e. last) is +used for analyzing the video. +The labels are stored in a MultiIndex [Pandas](http://pandas.pydata.org) Array, which contains the name of the network, +body part name, (x, y) label position in pixels, and the likelihood for each frame per body part. These arrays are +stored in an efficient Hierarchical Data Format (HDF) in the same directory, where the video is stored. +However, if the flag `save_as_csv` is set to `True`, the data can also be exported in comma-separated values format +(.csv), which in turn can be imported in many programs, such as MATLAB, R, Prism, etc.; This flag is set to `False` +by default. You can also set a destination folder (`destfolder`) for the output files by passing a path of the folder +you wish to write to. #### API Docs ````{admonition} Click the button to see API Docs @@ -394,26 +636,56 @@ by default. You can also set a destination folder (``destfolder``) for the outpu #### Dynamic-cropping of videos: -As of 2.1+ we have a dynamic cropping option. Namely, if you have large frames and the animal/object occupies a smaller fraction, you can crop around your animal/object to make processing speeds faster. For example, if you have a large open field experiment but only track the mouse, this will speed up your analysis (also helpful for real-time applications). To use this simply add ``dynamic=(True,.5,10)`` when you call ``analyze_videos``. +As of 2.1+ we have a dynamic cropping option. Namely, if you have large frames and the animal/object occupies a smaller +fraction, you can crop around your animal/object to make processing speeds faster. For example, if you have a large open +field experiment but only track the mouse, this will speed up your analysis (also helpful for real-time applications). +To use this simply add `dynamic=(True,.5,10)` when you call `analyze_videos`. ```python dynamic: triple containing (state, detectiontreshold, margin) - If the state is true, then dynamic cropping will be performed. That means that if an object is detected (i.e., any body part > detectiontreshold), 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., detectiontreshold), + 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., < detectiontreshold). 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). ``` -### (J) Filter pose data (RECOMMENDED!): +### (J) Filter Pose Data You can also filter the predictions with a median filter (default) or with a [SARIMAX model](https://www.statsmodels.org/dev/generated/statsmodels.tsa.statespace.sarimax.SARIMAX.html), if you wish. This creates a new .h5 file with the ending *_filtered* that you can use in create_labeled_data and/or plot trajectories. ```python -deeplabcut.filterpredictions(config_path, ['fullpath/analysis/project/videos/reachingvideo1.avi']) +deeplabcut.filterpredictions( + config_path, + ["fullpath/analysis/project/videos/reachingvideo1.avi"] +) ``` An example call: - ```python -deeplabcut.filterpredictions(config_path,['fullpath/analysis/project/videos'], videotype='.mp4',filtertype= 'arima',ARdegree=5,MAdegree=2) - ``` +```python +deeplabcut.filterpredictions( + config_path, + ["fullpath/analysis/project/videos"], + videotype=".mp4", + filtertype="arima", + ARdegree=5, + MAdegree=2 +) +``` Here are parameters you can modify and pass: ```python -deeplabcut.filterpredictions(config_path, ['fullpath/analysis/project/videos/reachingvideo1.avi'], shuffle=1, trainingsetindex=0, comparisonbodyparts='all', filtertype='arima', p_bound=0.01, ARdegree=3, MAdegree=1, alpha=0.01) +deeplabcut.filterpredictions( + config_path, + ["fullpath/analysis/project/videos/reachingvideo1.avi"], + shuffle=1, + trainingsetindex=0, + filtertype="arima", + p_bound=0.01, + ARdegree=3, + MAdegree=1, + alpha=0.01 +) ``` Here is an example of how this can be applied to a video: @@ -429,7 +701,7 @@ deeplabcut.filterpredictions(config_path, ['fullpath/analysis/project/videos/rea ``` ```` -### (K) Plot Trajectories: +### (K) Plot Trajectories The plotting components of this toolbox utilizes matplotlib. Therefore, these plots can easily be customized by the end user. We also provide a function to plot the trajectory of the extracted poses across the analyzed video, which @@ -439,7 +711,11 @@ can be called by typing: deeplabcut.plot_trajectories(config_path, [‘fullpath/analysis/project/videos/reachingvideo1.avi’]) ``` -It creates a folder called ``plot-poses`` (in the directory of the video). The plots display the coordinates of body parts vs. time, likelihoods vs time, the x- vs. y- coordinate of the body parts, as well as histograms of consecutive coordinate differences. These plots help the user to quickly assess the tracking performance for a video. Ideally, the likelihood stays high and the histogram of consecutive coordinate differences has values close to zero (i.e. no jumps in body part detections across frames). Here are example plot outputs on a demo video (left): +It creates a folder called `plot-poses` (in the directory of the video). The plots display the coordinates of body parts +vs. time, likelihoods vs time, the x- vs. y- coordinate of the body parts, as well as histograms of consecutive +coordinate differences. These plots help the user to quickly assess the tracking performance for a video. Ideally, the +likelihood stays high and the histogram of consecutive coordinate differences has values close to zero (i.e. no jumps in +body part detections across frames). Here are example plot outputs on a demo video (left):

@@ -454,51 +730,88 @@ It creates a folder called ``plot-poses`` (in the directory of the video). The p ``` ```` -### (L) Create Labeled Videos: +### (L) Create Labeled Videos Additionally, the toolbox provides a function to create labeled videos based on the extracted poses by plotting the -labels on top of the frame and creating a video. There are two modes to create videos: FAST and SLOW (but higher quality!). If you want to create high-quality videos, please add ``save_frames=True``. One can use the command as follows to create multiple labeled videos: +labels on top of the frame and creating a video. There are two modes to create videos: FAST and SLOW (but higher +quality!). One can use the command as follows to create multiple labeled videos: ```python -deeplabcut.create_labeled_video(config_path, ['fullpath/analysis/project/videos/reachingvideo1.avi','fullpath/analysis/project/videos/reachingvideo2.avi'], save_frames = True/False) -``` - Optionally, if you want to use the filtered data for a video or directory of filtered videos pass ``filtered=True``, i.e.: +deeplabcut.create_labeled_video( + config_path, + ["fullpath/analysis/project/videos/reachingvideo1.avi", + "fullpath/analysis/project/videos/reachingvideo2.avi"], + save_frames = True/False +) +``` + Optionally, if you want to use the filtered data for a video or directory of filtered videos pass `filtered=True`, + i.e.: ```python -deeplabcut.create_labeled_video(config_path, ['fullpath/afolderofvideos'], videotype='.mp4', filtered=True) -``` -You can also optionally add a skeleton to connect points and/or add a history of points for visualization. To set the "trailing points" you need to pass ``trailpoints``: +deeplabcut.create_labeled_video( + config_path, + ["fullpath/afolderofvideos"], + videotype=".mp4", + filtered=True +) +``` +You can also optionally add a skeleton to connect points and/or add a history of points for visualization. To set the +"trailing points" you need to pass `trailpoints`: ```python -deeplabcut.create_labeled_video(config_path, ['fullpath/afolderofvideos'], videotype='.mp4', trailpoints=10) -``` -To draw a skeleton, you need to first define the pairs of connected nodes (in the ``config.yaml`` file) and set the skeleton color (in the ``config.yaml`` file). There is also a GUI to help you do this, use by calling `deeplabcut.SkeletonBuilder(config+path)`! - -Here is how the ``config.yaml`` additions/edits should look (for example, on the Openfield demo data we provide): +deeplabcut.create_labeled_video( + config_path, + ["fullpath/afolderofvideos"], + videotype=".mp4", + trailpoints=10 +) +``` +To draw a skeleton, you need to first define the pairs of connected nodes (in the `config.yaml` file) and set the +skeleton color (in the `config.yaml` file). There is also a GUI to help you do this, use by calling +`deeplabcut.SkeletonBuilder(configpath)`! + +Here is how the `config.yaml` additions/edits should look (for example, on the Openfield demo data we provide): ```python # Plotting configuration -skeleton: [['snout', 'leftear'], ['snout', 'rightear'], ['leftear', 'tailbase'], ['leftear', 'rightear'], ['rightear','tailbase']] +skeleton: + - ["snout", "leftear"] + - ["snout", "rightear"] + - ["leftear", "tailbase"] + - ["leftear", "rightear"] + - ["rightear", "tailbase"] skeleton_color: white pcutoff: 0.4 dotsize: 4 alphavalue: 0.5 colormap: jet ``` -Then pass ``draw_skeleton=True`` with the command: +Then pass `draw_skeleton=True` with the command: ```python -deeplabcut.create_labeled_video(config_path,['fullpath/afolderofvideos'], videotype='.mp4', draw_skeleton = True) +deeplabcut.create_labeled_video( + config_path, + ["fullpath/afolderofvideos"], + videotype=".mp4", + draw_skeleton=True +) ``` -**NEW** as of 2.2b8: You can create a video with only the "dots" plotted, i.e., in the [style of Johansson](https://link.springer.com/article/10.1007/BF00309043), by passing `keypoints_only=True`: +**NEW** as of 2.2b8: You can create a video with only the "dots" plotted, i.e., in the +[style of Johansson](https://link.springer.com/article/10.1007/BF00309043), by passing `keypoints_only=True`: ```python -deeplabcut.create_labeled_video(config_path,['fullpath/afolderofvideos'], videotype='.mp4', keypoints_only=True) +deeplabcut.create_labeled_video( + config_path,["fullpath/afolderofvideos"], + videotype=".mp4", + keypoints_only=True +) ``` -**PRO TIP:** that the **best quality videos** are created when ``save_frames=True`` is passed. Therefore, when ``trailpoints`` and ``draw_skeleton`` are used, we **highly** recommend you also pass ``save_frames=True``! +**PRO TIP:** that the **best quality videos** are created when `fastmode=False` is passed. Therefore, when +`trailpoints` and `draw_skeleton` are used, we **highly** recommend you also pass `fastmode=False`!

-This function has various other parameters, in particular the user can set the ``colormap``, the ``dotsize``, and ``alphavalue`` of the labels in **config.yaml** file. +This function has various other parameters, in particular the user can set the `colormap`, the `dotsize`, and +`alphavalue` of the labels in **config.yaml** file. #### API Docs ````{admonition} Click the button to see API Docs @@ -510,10 +823,20 @@ This function has various other parameters, in particular the user can set the ` #### Extract "Skeleton" Features: -NEW, as of 2.0.7+: You can save the "skeleton" that was applied in ``create_labeled_videos`` for more computations. Namely, it extracts length and orientation of each "bone" of the skeleton as defined in the **config.yaml** file. You can use the function by: +NEW, as of 2.0.7+: You can save the "skeleton" that was applied in `create_labeled_videos` for more computations. +Namely, it extracts length and orientation of each "bone" of the skeleton as defined in the **config.yaml** file. You +can use the function by: ```python -deeplabcut.analyzeskeleton(config, video, videotype='avi', shuffle=1, trainingsetindex=0, save_as_csv=False, destfolder=None) +deeplabcut.analyzeskeleton( + config, + video, + videotype="avi", + shuffle=1, + trainingsetindex=0, + save_as_csv=False, + destfolder=None +) ``` #### API Docs @@ -524,6 +847,7 @@ deeplabcut.analyzeskeleton(config, video, videotype='avi', shuffle=1, trainingse ``` ```` +(active-learning)= ### (M) Optional Active Learning -> Network Refinement: Extract Outlier Frames While DeepLabCut typically generalizes well across datasets, one might want to optimize its performance in various, @@ -539,37 +863,46 @@ where the decoder might make large errors. All this can be done for a specific video by typing (see other optional inputs below): ```python -deeplabcut.extract_outlier_frames(config_path, ['videofile_path']) +deeplabcut.extract_outlier_frames(config_path, ["videofile_path"]) ``` We provide various frame-selection methods for this purpose. In particular the user can set: ``` -outlieralgorithm: 'fitting', 'jump', or 'uncertain'`` +outlieralgorithm: "fitting", "jump", or "uncertain" ``` -• select frames if the likelihood of a particular or all body parts lies below *pbound* (note this could also be due to -occlusions rather than errors); (``outlieralgorithm='uncertain'``), but also set ``p_bound``. +• `outlieralgorithm="uncertain"`: select frames if the likelihood of a particular or all body parts lies below `p_bound` +(note this could also be due to occlusions rather than errors). -• select frames where a particular body part or all body parts jumped more than *\uf* pixels from the last frame (``outlieralgorithm='jump'``). +• `outlieralgorithm="jump"`: select frames where a particular body part or all body parts jumped more than `epsilon` +pixels from the last frame. -• select frames if the predicted body part location deviates from a state-space model fit to the time series -of individual body parts. Specifically, this method fits an Auto Regressive Integrated Moving Average (ARIMA) -model to the time series for each body part. Thereby each body part detection with a likelihood smaller than -pbound is treated as missing data. Putative outlier frames are then identified as time points, where the average body part estimates are at least *\uf* pixel away from the fits. The parameters of this method are *\uf*, *pbound*, the ARIMA parameters as well as the list of body parts to average over (can also be ``all``). +• `outlieralgorithm="fitting"`: select frames if the predicted body part location deviates from a state-space model fit +to the time series of individual body parts. Specifically, this method fits an Auto Regressive Integrated Moving Average +(ARIMA) model to the time series for each body part. Thereby each body part detection with a likelihood smaller than +`p_bound` is treated as missing data. Putative outlier frames are then identified as time points, where the average +body part estimates are at least `epsilon` pixels away from the fits. The parameters of this method are `epsilon`, +`p_bound`, the ARIMA parameters as well as the list of body parts to average over (can also be `all`). -• manually select outlier frames based on visual inspection from the user (``outlieralgorithm='manual'``). +• `outlieralgorithm="manual"`: manually select outlier frames based on visual inspection from the user. As an example: ```python -deeplabcut.extract_outlier_frames(config_path, ['videofile_path'], outlieralgorithm='manual') +deeplabcut.extract_outlier_frames(config_path, ["videofile_path"], outlieralgorithm="manual") ``` In general, depending on the parameters, these methods might return much more frames than the user wants to -extract (``numframes2pick``). Thus, this list is then used to select outlier frames either by randomly sampling from this -list (``extractionalgorithm='uniform'``), by performing ``extractionalgorithm='k-means'`` clustering on the corresponding frames. +extract (`numframes2pick`). Thus, this list is then used to select outlier frames either by randomly sampling from +this list (`extractionalgorithm="uniform"`), by performing `extractionalgorithm="kmeans"` clustering on the +corresponding frames. -In the automatic configuration, before the frame selection happens, the user is informed about the amount of frames satisfying the criteria and asked if the selection should proceed. This step allows the user to perhaps change the parameters of the frame-selection heuristics first (i.e. to make sure that not too many frames are qualified). The user can run the extract_outlier_frames iteratively, and (even) extract additional frames from the same video. Once enough outlier frames are extracted the refinement GUI can be used to adjust the labels based on user feedback (see below). +In the automatic configuration, before the frame selection happens, the user is informed about the amount of frames +satisfying the criteria and asked if the selection should proceed. This step allows the user to perhaps change the +parameters of the frame-selection heuristics first (i.e. to make sure that not too many frames are qualified). The user +can run the `extract_outlier_frames` method iteratively, and (even) extract additional frames from the same video. +Once enough outlier frames are extracted the refinement GUI can be used to adjust the labels based on user feedback +(see below). #### API Docs ````{admonition} Click the button to see API Docs @@ -603,7 +936,7 @@ deeplabcut.refine_labels(config_path) ``` This will launch a GUI where the user can refine the labels. -Use the ‘Load Labels’ button to select one of the subdirectories, where the extracted frames are stored. Every label will be identified by a unique color. For better chances to identify the low-confidence labels, specify the threshold of the likelihood. This changes the body parts with likelihood below this threshold to appear as circles and the ones above as solid disks while retaining the same color scheme. Next, to adjust the position of the label, hover the mouse over the labels to identify the specific body part, left click and drag it to a different location. To delete a specific label, middle click on the label (once a label is deleted, it cannot be retrieved). +Please refer to the [napari-deeplabcut docs](napari-gui) for more information about the labelling workflow. After correcting the labels for all the frames in each of the subdirectories, the users should merge the data set to create a new dataset. In this step the iteration parameter in the config.yaml file is automatically updated. @@ -612,13 +945,16 @@ deeplabcut.merge_datasets(config_path) ``` Once the dataset is merged, the user can test if the merging process was successful by plotting all the labels (Step E). Next, with this expanded training set the user can now create a novel training set and train the network as described -in Steps F and G. The training dataset will be stored in the same place as before but under a different ``iteration #`` -subdirectory, where the ``#`` is the new value of ``iteration`` variable stored in the project’s configuration file (this is -automatically done). +in Steps F and G. The training dataset will be stored in the same place as before but under a different `iteration-#` +subdirectory, where the ``#`` is the new value of `iteration` variable stored in the project’s configuration file +(this is automatically done). -Now you can run ``create_training_dataset``, then ``train_network``, etc. If your original labels were adjusted at all, start from fresh weights (the typically recommended path anyhow), otherwise consider using your already trained network weights (see Box 2). +Now you can run `create_training_dataset`, then `train_network`, etc. If your original labels were adjusted at all, +start from fresh weights (the typically recommended path anyhow), otherwise consider using your already trained network +weights (see Box 2). -If after training the network generalizes well to the data, proceed to analyze new videos. Otherwise, consider labeling more data. +If after training the network generalizes well to the data, proceed to analyze new videos. Otherwise, consider labeling +more data. #### API Docs for deeplabcut.refine_labels ````{admonition} Click the button to see API Docs @@ -639,12 +975,15 @@ If after training the network generalizes well to the data, proceed to analyze n ### Jupyter Notebooks for Demonstration of the DeepLabCut Workflow We also provide two Jupyter notebooks for using DeepLabCut on both a pre-labeled dataset, and on the end user’s -own dataset. Firstly, we prepared an interactive Jupyter notebook called run_yourowndata.ipynb that can serve as a -template for the user to develop a project. Furthermore, we provide a notebook for an already started project with -labeled data. The example project, named as Reaching-Mackenzie-2018-08-30 consists of a project configuration file -with default parameters and 20 images, which are cropped around the region of interest as an example dataset. These -images are extracted from a video, which was recorded in a study of skilled motor control in mice. Some example -labels for these images are also provided. See more details [here](https://github.com/DeepLabCut/DeepLabCut/blob/master/examples). +own dataset. Firstly, we prepared an interactive Jupyter notebook called +[Demo_yourowndata.ipynb](https://github.com/DeepLabCut/DeepLabCut/blob/main/examples/JUPYTER/Demo_yourowndata.ipynb) +that can serve as a template for the user to develop a project. Furthermore, we provide a notebook for an already +started project with labeled data. The example project, named as +[Reaching-Mackenzie-2018-08-30](https://github.com/DeepLabCut/DeepLabCut/tree/main/examples/Reaching-Mackenzie-2018-08-30) +consists of a project configuration file with default parameters and 20 images, which are cropped around the region of +interest as an example dataset. These images are extracted from a video, which was recorded in a study of skilled motor +control in mice. Some example labels for these images are also provided. See more details +[here](https://github.com/DeepLabCut/DeepLabCut/tree/main/examples). ## 3D Toolbox