/co-tracker

CoTracker is a model for tracking any point (pixel) on a video.

Primary LanguageJupyter NotebookOtherNOASSERTION

CoTracker3: Simpler and Better Point Tracking by Pseudo-Labelling Real Videos

Meta AI Research, GenAI; University of Oxford, VGG

Nikita Karaev, Iurii Makarov, Jianyuan Wang, Ignacio Rocco, Benjamin Graham, Natalia Neverova, Andrea Vedaldi, Christian Rupprecht

Open In Colab Spaces

CoTracker is a fast transformer-based model that can track any point in a video. It brings to tracking some of the benefits of Optical Flow.

CoTracker can track:

  • Any pixel in a video
  • A quasi-dense set of pixels together
  • Points can be manually selected or sampled on a grid in any video frame

Try these tracking modes for yourself with our Colab demo or in the Hugging Face Space 🤗.

Updates:

  • [October 15, 2024] 📣 We're releasing CoTracker3! State-of-the-art point tracking with a lightweight architecture trained with 1000x less data than previous top-performing models. Code for baseline models and the pseudo-labeling pipeline are available in the repo, as well as model checkpoints. Check out our paper for more details.

  • [September 25, 2024] CoTracker2.1 is now available! This model has better performance on TAP-Vid benchmarks and follows the architecture of the original CoTracker. Try it out!

  • [June 14, 2024] We have released the code for VGGSfM, a model for recovering camera poses and 3D structure from any image sequences based on point tracking! VGGSfM is the first fully differentiable SfM framework that unlocks scalability and outperforms conventional SfM methods on standard benchmarks.

  • [December 27, 2023] CoTracker2 is now available! It can now track many more (up to 265*265!) points jointly and it has a cleaner and more memory-efficient implementation. It also supports online processing. See the updated paper for more details. The old version remains available here.

  • [September 5, 2023] You can now run our Gradio demo locally.

Quick start

The easiest way to use CoTracker is to load a pretrained model from torch.hub:

Offline mode:

pip install imageio[ffmpeg], then:

import torch
# Download the video
url = 'https://github.com/facebookresearch/co-tracker/raw/refs/heads/main/assets/apple.mp4'

import imageio.v3 as iio
frames = iio.imread(url, plugin="FFMPEG")  # plugin="pyav"

device = 'cuda'
grid_size = 10
video = torch.tensor(frames).permute(0, 3, 1, 2)[None].float().to(device)  # B T C H W

# Run Offline CoTracker:
cotracker = torch.hub.load("facebookresearch/co-tracker", "cotracker3_offline").to(device)
pred_tracks, pred_visibility = cotracker(video, grid_size=grid_size) # B T N 2,  B T N 1

Online mode:

cotracker = torch.hub.load("facebookresearch/co-tracker", "cotracker3_online").to(device)

# Run Online CoTracker, the same model with a different API:
# Initialize online processing
cotracker(video_chunk=video, is_first_step=True, grid_size=grid_size)  

# Process the video
for ind in range(0, video.shape[1] - cotracker.step, cotracker.step):
    pred_tracks, pred_visibility = cotracker(
        video_chunk=video[:, ind : ind + cotracker.step * 2]
    )  # B T N 2,  B T N 1

Online processing is more memory-efficient and allows for the processing of longer videos. However, in the example provided above, the video length is known! See the online demo for an example of tracking from an online stream with an unknown video length.

Visualize predicted tracks:

After installing CoTracker, you can visualize tracks with:

from cotracker.utils.visualizer import Visualizer

vis = Visualizer(save_dir="./saved_videos", pad_value=120, linewidth=3)
vis.visualize(video, pred_tracks, pred_visibility)

We offer a number of other ways to interact with CoTracker:

  1. Interactive Gradio demo:
  2. Jupyter notebook:
  3. You can install CoTracker locally and then:
    • Run an offline demo with 10 ⨉ 10 points sampled on a grid on the first frame of a video (results will be saved to ./saved_videos/demo.mp4)):

      python demo.py --grid_size 10
    • Run an online demo:

      python online_demo.py

A GPU is strongly recommended for using CoTracker locally.

Installation Instructions

You can use a Pretrained Model via PyTorch Hub, as described above, or install CoTracker from this GitHub repo. This is the best way if you need to run our local demo or evaluate/train CoTracker.

Ensure you have both PyTorch and TorchVision installed on your system. Follow the instructions here for the installation. We strongly recommend installing both PyTorch and TorchVision with CUDA support, although for small tasks CoTracker can be run on CPU.

Install a Development Version

git clone https://github.com/facebookresearch/co-tracker
cd co-tracker
pip install -e .
pip install matplotlib flow_vis tqdm tensorboard

You can manually download all CoTracker3 checkpoints (baseline and scaled models, as well as single and sliding window architectures) from the links below and place them in the checkpoints folder as follows:

mkdir -p checkpoints
cd checkpoints
# download the online (multi window) model
wget https://huggingface.co/facebook/cotracker3/resolve/main/scaled_online.pth
# download the offline (single window) model
wget https://huggingface.co/facebook/cotracker3/resolve/main/scaled_offline.pth
cd ..

You can also download CoTracker3 checkpoints trained only on Kubric:

# download the online (sliding window) model
wget https://huggingface.co/facebook/cotracker3/resolve/main/baseline_online.pth
# download the offline (single window) model
wget https://huggingface.co/facebook/cotracker3/resolve/main/baseline_offline.pth

For old checkpoints, see this section.

Evaluation

To reproduce the results presented in the paper, download the following datasets:

And install the necessary dependencies:

pip install hydra-core==1.1.0 mediapy

Then, execute the following command to evaluate the online model on TAP-Vid DAVIS:

python ./cotracker/evaluation/evaluate.py --config-name eval_tapvid_davis_first exp_dir=./eval_outputs dataset_root=your/tapvid/path

And the offline model:

python ./cotracker/evaluation/evaluate.py --config-name eval_tapvid_davis_first exp_dir=./eval_outputs dataset_root=/fsx-repligen/shared/datasets/tapvid offline_model=True window_len=60 checkpoint=./checkpoints/scaled_offline.pth

We run evaluations jointly on all the target points at a time for faster inference. With such evaluations, the numbers are similar to those presented in the paper. If you want to reproduce the exact numbers from the paper, add the flag single_point=True.

These are the numbers that you should be able to reproduce using the released checkpoint and the current version of the codebase:

Kinetics, $\delta_\text{avg}^\text{vis}$ DAVIS, $\delta_\text{avg}^\text{vis}$ RoboTAP, $\delta_\text{avg}^\text{vis}$ RGB-S, $\delta_\text{avg}^\text{vis}$
CoTracker2, 27.12.23 61.8 74.6 69.6 73.4
CoTracker2.1, 25.09.24 63 76.1 70.6 79.6
CoTracker3 offline, 15.10.24 67.8 76.9 78.0 85.0
CoTracker3 online, 15.10.24 68.3 76.7 78.8 82.7

Training

Baseline

To train the CoTracker as described in our paper, you first need to generate annotations for Google Kubric MOVI-f dataset. Instructions for annotation generation can be found here. You can also find a discussion on dataset generation in this issue.

Once you have the annotated dataset, you need to make sure you followed the steps for evaluation setup and install the training dependencies:

pip install pip==24.0
pip install pytorch_lightning==1.6.0 tensorboard opencv-python

Now you can launch training on Kubric. Our model was trained for 50000 iterations on 32 GPUs (4 nodes with 8 GPUs). Modify dataset_root and ckpt_path accordingly before running this command. For training on 4 nodes, add --num_nodes 4.

Here is an example of how to launch training of the online model on Kubric:

 python train_on_kubric.py --batch_size 1 --num_steps 50000 \
 --ckpt_path ./ --model_name cotracker_three --save_freq 200 --sequence_len 64 \
  --eval_datasets tapvid_davis_first tapvid_stacking --traj_per_sample 384 \
  --sliding_window_len 16 --train_datasets kubric --save_every_n_epoch 5 \
  --evaluate_every_n_epoch 5 --model_stride 4 --dataset_root ${path_to_your_dataset} \
   --num_nodes 4 --num_virtual_tracks 64 --mixed_precision --corr_radius 3 \ 
   --wdecay 0.0005 --linear_layer_for_vis_conf --validate_at_start --add_huber_loss

Training the offline model on Kubric:

python train_on_kubric.py --batch_size 1 --num_steps 50000 \
 --ckpt_path ./ --model_name cotracker_three --save_freq 200 --sequence_len 60 \
 --eval_datasets tapvid_davis_first tapvid_stacking --traj_per_sample 512 \
 --sliding_window_len 60 --train_datasets kubric --save_every_n_epoch 5 \
 --evaluate_every_n_epoch 5 --model_stride 4 --dataset_root ${path_to_your_dataset} \
 --num_nodes 4 --num_virtual_tracks 64 --mixed_precision --offline_model \
 --random_frame_rate --query_sampling_method random --corr_radius 3 \
 --wdecay 0.0005 --random_seq_len --linear_layer_for_vis_conf \
 --validate_at_start --add_huber_loss

Fine-tuning with pseudo labels

In order to launch training with pseudo-labelling, you need to collect your own dataset of real videos. There is a sample class available in cotracker/datasets/real_dataset.py with keyword-based filtering that we used for training. Your class should implement loading a video and storing it in the CoTrackerData class as a field, while pseudo labels will be generated in train_on_real_data.py.

You should have an existing Kubric-trained model for fine-tuning with pseudo labels. Here is an example of how you can launch fine-tuning of the online model:

python ./train_on_real_data.py --batch_size 1 --num_steps 15000 \
 --ckpt_path ./ --model_name cotracker_three --save_freq 200 --sequence_len 64 \
 --eval_datasets tapvid_stacking tapvid_davis_first --traj_per_sample 384 \
 --save_every_n_epoch 15 --evaluate_every_n_epoch 15 --model_stride 4 \
 --dataset_root ${path_to_your_dataset} --num_nodes 4 --real_data_splits 0 \
 --num_virtual_tracks 64 --mixed_precision --random_frame_rate \
 --restore_ckpt ./checkpoints/baseline_online.pth \
 --lr 0.00005 --real_data_filter_sift --validate_at_start \
 --sliding_window_len 16 --limit_samples 15000

And the offline model:

python train_on_real_data.py --batch_size 1 --num_steps 15000 \
 --ckpt_path ./ --model_name cotracker_three --save_freq 200 --sequence_len 80 \
 --eval_datasets tapvid_stacking tapvid_davis_first --traj_per_sample 384 --save_every_n_epoch 15 \
 --evaluate_every_n_epoch 15 --model_stride 4 --dataset_root ${path_to_your_dataset} \
  --num_nodes 4 --real_data_splits 0 --num_virtual_tracks 64 --mixed_precision \
  --random_frame_rate --restore_ckpt ./checkpoints/baseline_offline.pth --lr 0.00005 \
  --real_data_filter_sift --validate_at_start --offline_model --limit_samples 15000

Development

Building the documentation

To build CoTracker documentation, first install the dependencies:

pip install sphinx
pip install sphinxcontrib-bibtex

Then you can use this command to generate the documentation in the docs/_build/html folder:

make -C docs html

Previous versions

CoTracker v2

You could use CoTracker v2 with torch.hub in both offline and online modes.

Offline mode:

pip install imageio[ffmpeg], then:

import torch
# Download the video
url = 'https://github.com/facebookresearch/co-tracker/blob/main/assets/apple.mp4'

import imageio.v3 as iio
frames = iio.imread(url, plugin="FFMPEG")  # plugin="pyav"

device = 'cuda'
grid_size = 10
video = torch.tensor(frames).permute(0, 3, 1, 2)[None].float().to(device)  # B T C H W

# Run Offline CoTracker:
cotracker = torch.hub.load("facebookresearch/co-tracker", "cotracker2").to(device)
pred_tracks, pred_visibility = cotracker(video, grid_size=grid_size) # B T N 2,  B T N 1

Online mode:

cotracker = torch.hub.load("facebookresearch/co-tracker", "cotracker2_online").to(device)

# Run Online CoTracker, the same model with a different API:
# Initialize online processing
cotracker(video_chunk=video, is_first_step=True, grid_size=grid_size)  

# Process the video
for ind in range(0, video.shape[1] - cotracker.step, cotracker.step):
    pred_tracks, pred_visibility = cotracker(
        video_chunk=video[:, ind : ind + cotracker.step * 2]
    )  # B T N 2,  B T N 1

Checkpoint for v2 could be downloaded with the following command:

wget https://huggingface.co/facebook/cotracker/resolve/main/cotracker2.pth

CoTracker v1

It is directly available via pytorch hub:

import torch
import einops
import timm
import tqdm

cotracker = torch.hub.load("facebookresearch/co-tracker:v1.0", "cotracker_w8")

The old version of the code is available here. You can also download the corresponding checkpoints:

wget https://dl.fbaipublicfiles.com/cotracker/cotracker_stride_4_wind_8.pth
wget https://dl.fbaipublicfiles.com/cotracker/cotracker_stride_4_wind_12.pth
wget https://dl.fbaipublicfiles.com/cotracker/cotracker_stride_8_wind_16.pth

License

The majority of CoTracker is licensed under CC-BY-NC, however portions of the project are available under separate license terms: Particle Video Revisited is licensed under the MIT license, TAP-Vid and LocoTrack are licensed under the Apache 2.0 license.

Acknowledgments

We would like to thank PIPs, TAP-Vid, LocoTrack for publicly releasing their code and data. We also want to thank Luke Melas-Kyriazi for proofreading the paper, Jianyuan Wang, Roman Shapovalov and Adam W. Harley for the insightful discussions.

Citing CoTracker

If you find our repository useful, please consider giving it a star ⭐ and citing our research papers in your work:

@inproceedings{karaev23cotracker,
  title     = {CoTracker: It is Better to Track Together},
  author    = {Nikita Karaev and Ignacio Rocco and Benjamin Graham and Natalia Neverova and Andrea Vedaldi and Christian Rupprecht},
  booktitle = {Proc. {ECCV}},
  year      = {2024}
}
@inproceedings{karaev24cotracker3,
  title     = {CoTracker3: Simpler and Better Point Tracking by Pseudo-Labelling Real Videos},
  author    = {Nikita Karaev and Iurii Makarov and Jianyuan Wang and Natalia Neverova and Andrea Vedaldi and Christian Rupprecht},
  booktitle = {Proc. {arXiv:2410.11831}},
  year      = {2024}
}