Markerless tracking of user-defined features with deep learning

Alexander Mathis, Pranav Mamidanna, Taiga Abe, Kevin M. Cury, Venkatesh N. Murthy, Mackenzie W. Mathis, Matthias Bethge

I Introduction

Accurate quantification of behavior is essential for understanding the brain Tinbergen (1963); Bernstein (1967); Krakauer et al. (2017). Both within and beyond the field of neuroscience, there is a fruitful tradition of using cutting edge technology to study movement. Often, the application of new technology has the potential to reveal unforeseen features of the phenomena being studied, as in the case of Muybridge’s famous photography studies in the mid 19th century, or modern high-speed videography that has revealed previously unknown motor sequences, such as “tap dancing” in the songbird Bernstein (1967); Ota et al. (2015); Wade (2016). Historically, collected data was analyzed manually, which is a time-consuming, labor-intensive, and an error-prone process that is prohibitively inefficient at today’s high rates of acquisition. Conversely, advances in computer vision have persistently inspired methods of data analysis to reduce human labor Dell et al. (2014); Gomez-Marin et al. (2014); Anderson and Perona (2014).

We are particularly interested in extracting the pose of animals, i.e. the geometrical configuration of multiple body parts. The gold standard for pose estimation in the field of motor control is the combination of video recordings with easily recognizable reflective markers applied to locations of interest, which greatly simplifies subsequent analysis and allows for tracking of body parts with high accuracy Winter (2009); Vargas-Irwin et al. (2010); Wenger et al. (2014); Maghsoudi et al. (2017). However, such systems can be expensive, potentially distracting to animals Perez-Escudero et al. (2014); Nakamura et al. (2016), and markers need to be placed before recording, which pre-defines the features that can be tracked. This mitigates one of the benefits of video data, i.e. it’s low level of invasiveness. One alternative to physical markers is to fit skeleton/active contour models de Chaumont et al. (2012); Matsumoto et al. (2013); Perez-Escudero et al. (2014); Nakamura et al. (2016); Uhlmann et al. (2017). These methods can work quite well and are fast, but require sophisticated skeleton models, which are difficult to develop and to fit to data, limiting the flexibility of such methods Felzenszwalb and Huttenlocher (2005); Toshev and Szegedy (2014). Another alternative is training regressors based on various computationally derived features to track particular body parts in a supervised way Dollár et al. (2010); Perez-Escudero et al. (2014); Dell et al. (2014); Machado et al. (2015); Guo et al. (2015). Training predictors based on features from deep neural networks also falls in this category Krizhevsky et al. (2012); He et al. (2016). Indeed, the best algorithms for challenging benchmarks in pose estimation of humans from images use deep features Toshev and Szegedy (2014); Wei et al. (2016); Pishchulin et al. (2016); Insafutdinov et al. (2016); Feichtenhofer et al. (2017); Insafutdinov et al. . This suggests that deep learning architectures should also greatly improve the accuracy of pose estimation for lab applications. However, the labeled data sets for these benchmarks are rather large (e.g. ≈25,000\approx 25,000 in the MPII Human Pose data set Andriluka et al. (2014)), which may render deep learning approaches infeasible as efficient tools at the scale of interest to neuroscience labs. Yet, due to transfer learning Donahue et al. (2014); Yosinski et al. (2014); Goodfellow et al. (2016); Kümmerer et al. (2016) we will show that this need not be the case.

Here, we demonstrate that by capitalizing on state-of-the-art methods for detecting human limb configurations, we can achieve excellent performance on pose estimation problems in the laboratory setting with minimal training data. Specifically, we investigated the feature detector architecture from DeeperCut Insafutdinov et al. (2016); Pishchulin et al. (2016), one of the best performing pose estimation algorithms, and demonstrate that a small number of training images (≈200\approx 200) can be sufficient to train this network to within human-level labeling accuracy. This is possible due to transfer learning: the feature detectors are based on extremely deep neural networks, which were pre-trained on ImageNet, a massive data set for object recognition He et al. (2016). By labeling only a few hundred frames one can train tailored, robust feature detectors that are capable of localizing a variety of experimentally relevant body parts. We illustrate the power of this approach by tracking the snout, ears, and tail-base during an odor-guided navigation task, multiple body parts of a fruit fly behaving in a 3D chamber, as well as joints of individual digits during a reaching task.

II Results

DeeperCut achieves outstanding performance on multi-human pose detection benchmarks Insafutdinov et al. (2016). However, to achieve this performance its neural network architecture has been trained on thousands of labeled images. Here we focus on a subset of DeeperCut: its feature detectors, which are variations of Deep Residual Neural Networks (ResNet) He et al. (2016) with readout layers that predict the location of a body part (see Methods, Figure 1). To distinguish the feature detectors from the full DeeperCut, we refer to this autonomous portion as DeepLabCut. In this paper, we (1) evaluate the performance of DeepLabCut for posture tracking in various laboratory behaviors, (2) investigate the amount of required training data for good generalization, and (3) provide an open source toolbox based on DeeperCut that is broadly accessible to the neuroscience community (https://github.com/AlexEMG/DeepLabCut).

Analyzing videos taken in a dynamically changing environment can be challenging. Therefore, to test the utility of our toolbox, we first focused on an odor-guided navigation task for mice. Briefly, mice run freely on an ‘endless’ paper spool that includes an adapted ink-jet printer to deliver odor trails in real-time as a mouse runs and tracks trails (further details and results will be published elsewhere). The video captured during the behavior poses several key challenges: inhomogeneous illumination, transparent side walls that appear dark, shadows around the mouse from overhead lighting, distortions due to a wide-angle lens, the mouse often crosses the odor trail, and rewards are often directly in front of its snout ( which influences its appearance). Yet, accurately tracking the snout as a mouse samples the “odorscape” is crucial for studying odor-guided navigation.

First, we extracted 1,0801,080 distinct frames from multiple videos (across two cameras and 77 different mice, see Methods) and manually labeled the snout, left ear, right ear, and tail base in all frames (Figure 2A, Figure S1). In order to facilitate comparisons to ground truth and to quantify the robustness of predictors, we estimated variability (root mean square error; RMSE) within one human labeler by comparing two distinct label sets of the same data. We found the average variability for all body parts to be very small 2.69±0.12.69\pm 0.1 pixels (mean ±\pm s.e.m. n=4,320n=4,320 body part image pairs; Figure S1, see Methods), which is less than the width of the mouse’s snout in low resolution camera frames (Figure 2A). The RMSE across two trials of annotating the same images is referred to as ‘human variability’ (note that the variability differs slightly across body parts).

To quantify the feature detector’s performance, we randomly split the data into a training and test set (80%/20%80\%/20\%) and evaluated the performance of DeepLabCut on test images across all body parts (Figure 2B, C) and in a sub-set of body parts (snout/tail)(Figure 2D). Unless otherwise noted, we always trained (and tested) with the labels from the first set of human labels. The test RMSE for different training/test set splits achieved average human variability (Figure 2D). Thus, we found that when trained with 80%80\% of the data the algorithm achieved human-level accuracy on the test set for detection of the snout and the tail base (Figure 2D, E).

Next, we systematically varied the size of the training set and trained 3030 distinct networks (33 splits for 50%50\% and 80%80\% training set size; 66 splits for 11, 55, 1010 and 20%20\% training set fraction). As expected, the test error increases for decreasing number of training images (Figure 2F). Yet remarkably, the test RMSE only slowly attenuates from 80%80\% training set fraction to 10%10\%, where one still achieves an average pixel error of less than 55 pixels. Such average errors are on the order of the size of the snout in the low resolution camera (around 55 pixels) and much smaller than the size of the snout in higher resolution camera (around 3030 pixels). Thus, we found that even 100100 frames are enough to achieve excellent generalization.

Since the RMSE is computed as an average across images, we next checked if there were any systematic differences across images by comparing the human variability across the two splits vs. the model variability (trained with the first set of human labels; Figure 2G: data for 11 split with a 50%50\% training set size). We found that both the human and the algorithm produced only a few outliers, but no systematic error was detected (see Figure 2G for examples).

Thus far, we used a part detector based on the 5050 layer deep ResNet-5050 Insafutdinov et al. (2016); He et al. (2016). We also trained deeper networks with 101101 layers and found that both the training and testing errors decreased slightly, suggesting that the performance can be further improved if required (Average test RMSE for 33 identical splits of 50%50\% training set fraction: ResNet-5050: 3.09±0.043.09\pm 0.04, ResNet-101101: 2.90±0.092.90\pm 0.09 and ResNet-101101 with intermediate supervision: 2.88±0.062.88\pm 0.06, pixel mean ±\pm s.e.m.; see Figure S2A).

Overall, given the robustness and the low error rate of DeepLabCut even with small training sets, we find this to be a useful tool for studies such as odor-guided navigation. For example, Figure 3 recapitulates a salient signature of the tracking behavior, namely that rodents swing their snout across the trail Khan et al. (2012). Knowing the location of the ears as well as the tail is also important to computationally assess the orientation of the mouse (Figure 3). Furthermore, having an automated pose estimation algorithm as presented will be crucial for video-rich experiments (such as this one).

II.2 Generalization & transfer learning

We have demonstrated that DeepLabCut can accurately detect body parts across different mice, but how does it generalize to novel scenarios? Firstly, we found that DeepLabCut generalizes to novel mice during trail tracking (Figure 4A). Secondly, we tested if the trained network could identify multiple body parts across multiple mice within the same frame (transfer learning). Remarkably, although the network has only been trained with images containing a single mouse, it could detect all the body parts of each mouse in images with multiple interacting mice. Although not error-free, we found that the model performed remarkably well in a social task (three mice interacting in an unevenly illuminated open field, Figure 4B). The performance of the body part detectors could be improved by training the network with training images that include multiple mice with occlusions, and/or by training image-conditioned pairwise terms between body parts to harness the power of multi human pose estimation models Insafutdinov et al. (2016) (see discussion). Nonetheless, this example of multi-mice tracking illustrates that even the single-mouse trained feature detectors alone can readily transfer to extensions, as would be useful in studies of social behaviors Dell et al. (2014); Li et al. (2017); Robie et al. (2017).

II.3 The power of end-to-end training

Due to the architecture of DeeperCut, the deconvolution layers are specific to each body part, but the deep pre-processing network (ResNet) is shared (Figure 1). We hypothesized that this architecture can facilitate the localization of one body part based on other labeled body parts. To test this hypothesis, we examined the performance of networks trained with only the snout/tail data while using the identical 33 splits of 50%50\% training data as in Figure 2B. We found that the network that was trained with all body part labels simultaneously outperforms the specialized networks nearly twofold (Figure 5). This result also demonstrates that training the weights throughout the whole network in an end-to-end fashion rather than just the readout weights substantially improves the performance. This further highlights the advantage of such deep learning based models over approaches with fixed feature representations, which cannot be refined during training.

II.4 Drosophila in 3D behavioral chamber

To further demonstrate the flexibility of the DeepLabCut toolbox, we tracked the bodies of freely behaving fruit flies (Drosophila) exploring a small cubical environment in which one surface contained an agar-based substrate for egg laying. Freely behaving flies readily exhibit many orientations, and also frequent the walls and ceiling. When viewed from a fixed perspective, these changes in orientation dramatically alter the appearance of flies as the spatial relationship of body features change, or as different body parts come into or out of view. Moreover, reliably tracking features across an entire egg-laying behavioral session could potentially be challenging to DeepLabCut owing to significant changes in the background (the accumulation of new eggs or changes in the agar substrate appearance due to evaporation).

To build towards an understanding of the behavioral patterns that surround egg-laying in an efficient way, we chose 1212 distinct points on the body of the fly and labeled 589589 frames of diverse orientation and posture from six different animals, labeling only those features that were visible within a given frame. We labeled four points on the head (the dorsal tip of each compound eye, the ocellus, and the tip of the proboscis), the posterior tip of the scutellum on the thorax, the joint between the femur and tibia on each metathoracic leg, the abdominal stripes on the four most posterior abdominal segments (A33-A66), and the ovipositor.

We trained DeepLabCut with 95%95\% of the data and found a test error of 4.174.17 ±\pm 0.320.32 pixels (mean ±\pm s.e.m.; corresponding to an average training error of 1.391.39 ±\pm 0.010.01 pixels, n=33 splits, mean ±\pm s.e.m.). Figure 6A depicts some example test frames with human and network applied labels. Generalization to flies not used in the training set was excellent and the feature detectors are robust to changes in orientation (Figure 6B) and background (Figure 6C). Although fly bodies are relatively rigid, which simplifies tracking, there are exceptions. For instance, the proboscis dramatically changes its visual appearance during feeding behaviors. Yet, the feature detectors can resolve fast motor movements such as the extension and retraction of the proboscis (Figure 6D). Thus, DeepLabCut allows accurate extraction of low-dimensional pose information from videos of freely behaving Drosophila.

II.5 Digit Tracking During Reaching

To further illustrate the versatility and capabilities of DeepLabCut we tracked segments of individual digits of a mouse hand (Figure 1 and 7A). We recently established a head-fixed, skilled reaching paradigm in mice Mathis et al. (2017), where mice grab a 2-degree of freedom joystick and pull it from a virtual start location to a target location. While the joystick allows for spatially accurate measurement of the joystick (hand) position during the pull with high temporal precision, it neither constrains the hand position on the joystick, nor provide position during the reaches or between pulls (when the mice might or might not hold the joystick). Placing markers is difficult as the mouse hand is a small, and a highly complex structure with multiple bones, joints, and muscles. Moreover, it is intrusive to the mice and can disrupt performance. Therefore, in principle, markerless tracking is a promising approach for analyzing reaching dynamics. However, tracking is challenging due to the complexity of possible hand articulations, as well as the presence of the other hand in the background. The images are also subject to very different image statistics, making this task well posed to highlight the generality of our DeepLabCut toolbox.

We labeled 1313 points per frame: 33 points per visible digit (44 digits): the digit tip, one mid digit joint, the base of the digit joint (these roughly correspond to the proximal interphalangeal joint (PIP) and the metacarpophalangeal joint (MCP), respectively), as well as 11 point at the base of the hand (wrist). Remarkably, we found that by using just 141141 training frames we achieved an average test error of 5.21±0.285.21\pm 0.28 pixels (mean ±\pm s.e.m.; corresponding to average training error 1.16±0.031.16\pm 0.03 pixels, n=3n=3 splits, mean ±\pm s.e.m.). For reference, the width of a single digit is ≈15\approx 15 pixels. Figure 7A depicts some example test frames. We believe that this application of hand pose estimation highlights the excellent generalization performance of DeepLabCut despite training with only a few images.

So far we have shown that the body part estimates derived from DeeperCut are highly accurate. But, in general, especially when sieving through massive data sets, the end user would like to have each point estimate accompanied by a confidence measure of the label location. The location predictions in DeeperCut are obtained by extracting the most likely region, based on a scalar field that represents the probability that a particular body part is in a particular region. In DeeperCut these probability maps are called “score-maps”, and predictions are generated by finding the point with the highest probability value (see Methods). The amplitude of the maximum can be used as a confidence read-out to examine the strength of evidence for individual localizations of the individual parts to be detected. For instance, the peak probability of the digit tip is low when the mouse holds the joystick (which causes occlusions of the digit tips). Similarly, when the features cannot be disambiguated, the likelihood becomes small (Figure 7A). This confidence readout also works in other contexts, for instance in the drosophila example frames we only depicted the predicted body parts when the probability was larger than 10%10\% (Figure 6B-D). Using this threshold, the point estimate for the left leg can be automatically excluded in Figure 6C, D. Indeed, all occluded body parts are also omitted in Figure 6B and Figure 7F.

Lastly, once a network is trained on the hand posture frames, the body part locations can be extracted from videos and utilized in many ways. Here we illustrate a few examples: digit positions during a reach across time (Figure 7B; note that this trajectory comes from frame by frame prediction without any temporal filtering), comparison of movement patterns across body parts (Figure 7C, D), dimensionality reduction to reveal the richness of mouse hand postures during reaching (Figure 7E) and creating ‘skeletons’ based on semantic meaning of the labels (Figure 7F).

III Discussion

Detecting postures from monocular images is a challenging problem. Traditionally, postures are modeled as a graph of parts, where each node encodes the local visual properties of the part in question, and then these parts are connected by spring-like links. This graph is then fit to images by minimizing some appearance cost function Felzenszwalb and Huttenlocher (2005). This minimization is hard to solve, but designing the model topology together with the visual appearance model is even more challenging Felzenszwalb and Huttenlocher (2005); Toshev and Szegedy (2014); this can be illustrated by considering the diversity of fruit fly (Figure 6) and hand postures we examined (Figure 7). In contrast, casting this problem as a minimization with deep residual neural networks allows each joint predictor to have more than just local access to the image Toshev and Szegedy (2014); Wei et al. (2016); Insafutdinov et al. (2016); Pishchulin et al. (2016); Feichtenhofer et al. (2017). Due to the extreme depth of ResNets, architectures like DeeperCut have large receptive fields, which can learn to extract postures in a robust way Insafutdinov et al. (2016).

Here we demonstrated that cutting-edge deep learning models can be efficiently utilized in the laboratory. Specifically, we leveraged the fact that adapting pre-trained models to new tasks can dramatically reduce the amount of training data required, a phenomena known as transfer learning Donahue et al. (2014); Yosinski et al. (2014); Goodfellow et al. (2016); Insafutdinov et al. (2016); Kümmerer et al. (2016). We first estimated the accuracy of a human labeler who could readily identify the body parts we were interested in for odor guided navigation, and then demonstrated that a deep architecture can achieve similar performance on detection of body parts such as the snout, or the tail after training on only a few hundred images. Moreover, this solution requires no computational body-model, stick figure, time-information, or sophisticated inference algorithm. Thus, it can also be quickly applied to completely different behaviors that pose qualitatively distinct challenges to computer vision, like skilled reaching or egg-laying in drosophila.

We believe that DeepLabCut will supplement the rich literature of computational methods for video analysis Drai and Golani (2001); Sousa et al. (2006); Gomez-Marin et al. (2012); Matsumoto et al. (2013); Machado et al. (2015); Guo et al. (2015); Dollár et al. (2010); Ben-Shaul (2017); Dell et al. (2014); Anderson and Perona (2014), where powerful feature detectors of user-defined body parts need to be learned for a specific situation, or where regressors based on standard image features and thresholding heuristics Gomez-Marin et al. (2012); Robie et al. (2017); Dell et al. (2014) fail to provide satisfying solutions. This is particularly the case in dynamic visual environments (e.g. with varying background and reflective walls (Figures 2 and 6) or when tracking highly articulated objects like the hand (Figure 7).

Deep learning algorithms are extremely powerful and can learn to associate arbitrary categories to images Zhang et al. (2016); Goodfellow et al. (2016). This is consistent with our own observation that the training set should be free of errors (Figure 2G) and approximate the diversity of visual appearances. Thus, in order to train DeepLabCut for specific applications we recommend labeling maximally diverse images (i.e. different poses, different individuals, luminance conditions, data collected with different cameras, etc.) in a consistent way and curate the labeled data well. Even for a extremely small training set, the typical errors can be small, but large errors for test images that are quite distinct from the training set can start to dominate the average error. One limitation for generalizing to novel situations comes from stochasticity in training set selection. Given that we only select a small number of training samples (i.e. a few hundred frames), and do so randomly from the data, it is plausible that images representing behaviors that are especially sparse or noisy (i.e. due to motion blur) could be sub-optimally sampled or entirely excluded from the training data, resulting in difficulties at test time.

Therefore, a user can expand the initial training dataset in an iterative fashion using the score-maps as a guide. Specifically, errors can be addressed via post-hoc “fine tuning” of the network weights, taking advantage of the fact that the network outputs confidence estimates for its own generated labels (Figure 7A, see Methods). By using these confidence estimates to select sequences of frames containing a sparse behavior (by sampling around points of high probability), or to find frames where reliably captured behaviors are largely corrupted with noise (by sampling points of low probability), a user can then selectively label frames based on these confidence criteria to generate a minimal yet additional training set for fine tuning the network. This selectively improves model performance on edge cases, thereby extending the architecture’s capacity for generalization in an efficient way. Thus, for generalization to large data sets, one can use features of the score-maps such as the amplitude or width, or use heuristics such as the continuity of body part trajectories, to identify images where the decoder might make large errors. Images with insufficient automatic labeling performance that are identified in this way can then be manually labeled to increase the training set and iteratively improve the feature detectors. Such an active learning framework can be used to achieve a predefined level of confidence for all images with minimal labeling cost. Then, due to the large capacity of the neural network that underlies the feature detectors, one can continue training the network with these additional examples.

We note however, that not every low probability score-map value necessarily reflects erroneous detection. As we showed, low probabilities can also be indicative of occlusions, as in the case of the digit tips when the mouse is holding the joystick (Figure 7). Here, multiple camera angles can be used to fully capture a behavior of interest, or heuristics can be used (such as a body-model) to approximate occluded body parts based on temporal and spatial information.

Another important feature of DeepLabCut is that it can accurately transform large videos into low-dimensional time sequence data with semantic meaning, as the experimenter pre-selects the parts that will presumably provide the most information about the behavior being studied. Such low-dimensional time sequence data is also highly amenable to behavioral clustering and analysis due to its computational tractability Berman (2018); Dell et al. (2014); Anderson and Perona (2014); Gomez-Marin et al. (2014). On modern hardware pose extraction is also fast. For instance, one can process the 682×540682\times 540 sized frames of the drosophila behavior at around 3030 frames per second on a NVIDIA 10801080-Ti GPU. Such fast pose extraction can make this tool potentially amenable for real-time feedback Mathis et al. (2017) based on video-based posture estimates. This processing speed can be further improved by cropping input frames in an adaptive way around the animal, and/or adapting the network architecture to speed up processing times.

As presented, DeepLabCut extracts the posture data frame-by-frame, but one can add temporal Kalman filtering to improve performance (as for other approaches) Stauffer and Grimson (1999); Ristic et al. (2003); Dell et al. (2014). Here we omitted such methods due to the high precision of the model without these additional steps, and to focus on the prediction based on single frames solely driven by within-frame visual information in a variety of contexts.

While temporal information could indeed be beneficial in certain contexts, challenges remain to using end-to-end trained deep architectures for video data to extract postures. Due to the curse of dimensionality, deep architectures on videos must rely on lower spatial resolution input images, and thus, the best performing action recognition algorithms still rely on frame-by-frame analysis with deep networks pre-trained on ImageNet due to hardware limitations Feichtenhofer et al. (2017); Carreira and Zisserman (2017); Insafutdinov et al. . As this is an active area of research, we believe this situation will likely change due to improvements in hardware (and in deep learning algorithms), and this should have a strong influence on pose estimation in the future. Therefore currently, in situations where occlusions are very common, such as in social behaviors, pairwise interactions could also be added to improve performance Felzenszwalb and Huttenlocher (2005); de Chaumont et al. (2012); Matsumoto et al. (2013); Perez-Escudero et al. (2014); Nakamura et al. (2016); Uhlmann et al. (2017); Dell et al. (2014); Insafutdinov et al. ; Insafutdinov et al. (2016). Here we focused on the deep feature detectors alone to demonstrate remarkable transfer learning for laboratory tasks without the need for such extensions.

Together with this paper we provide an open source software package called DeepLabCut. The toolbox uses the feature detectors from DeeperCut and provides routines to a) extract distinct frames from videos for labeling, b) generate training data based on labels, c) train networks to the desired feature sets, and d) extract these feature locations from unlabeled data (Figure 1). The typical use case would be that an experimenter extracts distinct frames from videos and labels the body-parts of interest to create tailored part detectors. Then, after only a few hours of labeling and a day of training the network, they can apply DeepLabCut to novel videos. While we demonstrate the utility of this toolbox on mice and Drosophila, there is no inherent limitation of this framework, and our toolbox can be applied to other model, or non-model, organisms in a diverse range of behaviors.

IV Methods

The trail-tracking behavior is part of an investigation into odor guided navigation, where one or multiple wild type (C57BL/6J) mice are running on a paper spool and following odor trails. These experiments were carried out in the Murthy lab at Harvard University and will be published elsewhere. For trail-tracking we extracted 1 0801\,080 random, distinct frames from multiple experimental sessions observing 77 different mice. Data was recorded by two different cameras (640×480640\times 480 pixels, and at approximately 1 700×1 2001\,700\times 1\,200) at 3030 Hz. The latter images are prohibitively large to process by this deep neural network without down-sampling, therefore we cropped around mice to generate images that are approximately 800×800800\times 800. One human annotator was instructed to localize the snout, the tip of the left and right ear as well as the base of the tail in the example images on two different occasions, generating two distinct label sets (>1>1 month apart to reduce memory bias, see Figure S1).

Experimental procedures for the training of the joystick behavior and the construction of the behavioral set-up can be found in Mathis et al. (2017). In brief, head-fixed mice were trained to reach, grab, and pull a joystick for a liquid reward. To generate a train/test set of images, we labeled 159159 frames at the digit tip, the joint in the middle and at the base of the digit for four digits (which roughly correspond to the proximal interphalangeal joint (PIP) and the metacarpophalangeal joint (MCP), respectively), as well as the base of the hand (wrist). The data was collected across 55 different mice (C57BL/6J, male and female) and were recorded at 2 048×1 0882\,048\times 1\,088 resolution with a frame rate of 100−320100-320 Hz. For tracking the digits we used the supplied toolbox code to crop the data to only extract regions of interest containing the movement of the arm to limit the size of the input image to the network.

All surgical and experimental procedures for mice were in accordance with the National Institutes of Health Guide for the Care and Use of Laboratory Animals and approved by the Harvard Institutional Animal Care and Use Committee.

Experiments were carried out in the laboratory of Richard Axel at Columbia University and will be published elsewhere. In brief, egg-laying behavior was observed in custom designed 3D-printed chambers (Protolabs). Individual chambers were 4.1 mm deep and tapered from top to bottom, with the top dimensions 7.3 mm ×\times 5.8 mm and the bottom dimensions 6.7mm ×\times 4.3mm. One side of the chamber opened to a reservoir within which 1% agar was poured and allowed to set. Small acrylic windows were slid into place within grooves at the top and bottom to enclose the fly within the chamber and to allow for viewing. The chamber was illuminated by a 2 inch off-axis ring light (Metaphase) and video recording was performed from above the chamber using an IR-sensitive CMOS camera (Basler) with a 0.5x telecentric lens (Edmund Optics) at 2020 Hz (682×540682\times 540 pixels). We identified 12 distinct points of interest to quantify the behavior of interest on the body of the fly. One human annotator manually extracted 589 distinct and informative frames from six different animals, labeling only those features that were visible within a given frame. The 12 points comprise: four points on the head (the dorsal tip of each compound eye, the ocellus, and the tip of the proboscis), the posterior 10 tip of the scutellum on the thorax, the joint between the femur and tibia on each metathoracic leg, the abdominal stripes on the four most posterior abdominal segments (A3-A6), and the ovipositor.

We employ strong body part detectors, which are part of state-of-the art algorithms for human pose estimation called DeeperCut Insafutdinov et al. (2016); Pishchulin et al. (2016); Insafutdinov et al. . Those part detectors build upon state-of-the-art object recognition architectures, namely extremely deep Residual Networks (ResNet) He et al. (2016). Specifically, we use a variant of the ResNet with 50 layers, which achieved outstanding performance in object recognition competitions He et al. (2016). In the DeeperCut implementation, the ResNets were adapted to represent the images with higher spatial resolution, and the softmax layer used in the original architecture after the conv5 bank (as would be appropriate for object classification) was replaced by “deconvolutional layers” that produce a scalar field of activation values corresponding to regions in original image. This output is also connected to the conv3 bank to make use of finer features generated earlier in the ResNet architecture Insafutdinov et al. (2016). For each body part, there is a corresponding output layer whose activity represents probability “score-maps”. These score-maps represent the probability that a body part is at a particular pixel Insafutdinov et al. (2016); Pishchulin et al. (2016). During training, a score-map with positive label 11 (unity probability) is generated for all locations up to ϵ\epsilon pixels away from the ground truth per body part (distance variable). The ResNet architecture used to generate features is initialized with weights trained on ImageNet He et al. (2016) and the cross-entropy loss between the predicted score-map and the ground-truth score-map is minimized by stochastic gradient descent Insafutdinov et al. (2016). Around five hundred thousand training steps were enough for convergence and training takes around 24-36h on a GPU (NVIDIA GTX 1080 Ti; see Figure 2B). We used a batch size of 11, which allows to have images of different size, decreased the learning rate over training and performed data augmentation during training by rescaling the images (all like DeeperCut, but we used a range of 50%50\% to 150%150\%). Unless otherwise noted we used a distance variable ϵ=17\epsilon=17 (pixel radius) and scale factor 0.80.8 (which affects the ratio of the input image to output score-map). We cross validated the choice of ϵ\epsilon for a higher resolution output (scale factor = 1) and found that the test performance is not improved when varying ϵ\epsilon widely, but the rate of performance improvement was strongly decreased for small ϵ\epsilon (Figure S2). We also compared deeper networks with 101 layers, ResNet-101, as well as ResNet-101ws (with intermediate supervision, Figure S2A; more technical details for this architecture can be found in Insafutdinov et al. (2016)).

The trained network can be used to predict body part locations. At any state of training the network can be presented with novel frames, for which the prediction of the location of a particular body part is given by the peak of the corresponding score-map. This estimate is further refined based on learned correspondences between the score-map grid and ground truth joint positions Insafutdinov et al. . In the case of multiple mice the local maxima of the score-map are extracted as predictions of the body part locations (Figure 4).

To compare between datasets generated by the human scorer, as well as with/between model-generated labellings, we used the Euclidean distance (root mean square error, abbreviated: RMSE) calculated pairwise per body-part. Depending on the context this metric is either shown for a specific body-part, averaged over all body-parts, or averaged over a set of images. To quantify the error across learning, we stored snapshots of the weights in TensorFlow (usually every 50 00050\,000 iterations) and evaluated the RMSE for predictions generated by these frozen networks post-hoc. Note that the RMSE is not the loss function minimized during training. However, the RMSE is the relevant performance metric for pose estimation for assessing labeling precision.

The RMSE between the first and second annotation is referred to as human variability. In figures we also depict the 95%95\% confidence interval for this RMSE, whose limits are given as mean ±1.96\pm 1.96 times the standard error of the mean (Figure 2C, D, F; Figure 4 Figure 5; Figure S2A-D). Depending on the figure, the RMSE is averaged over all or just a subset of body parts.

In (Figure 7) we extracted cropped images of the hand from full frames by centering it using the predicted wrist position. We then performed dimensionality reduction by t-SNE embedding of those images Pedregosa et al. (2011) and randomly selected certain and sufficiently distant points to illustrate the corresponding hand postures.

This publication is accompanied by open source Python code for selecting training frames, checking human annotator labels, generating training data in the required format, as well as evaluating the performance on test frames. The toolbox also contains code to extract postures from novel videos with trained feature detectors. Thus, this toolbox allows one to train a tailored network based on labeled images and can then perform automatic labeling for novel data. See https://github.com/AlexEMG/DeepLabCut for details.

References

V Supplementary Materials

Materials related to main figures of Mathis et al. Please also see http://www.mousemotorlab.org/deeplabcut for video demonstrations of the tracking.