V-JEPA 2

repository·main·Indexed 25 days ago

https://github.com/facebookresearch/vjepa2

A collection of self-supervised video models developed by Meta FAIR, including V-JEPA 2, V-JEPA 2.1, and V-JEPA 2-AC. These PyTorch implementations provide video encoders for motion understanding, human action anticipation, and robotic manipulation tasks. V-JEPA 2.1 introduces Dense Predictive Loss and Multi-Modal Tokenizers, while V-JEPA 2-AC is a latent action-conditioned world model for robot planning. Models are available via PyTorch Hub and Hugging Face Transformers.

Tokens
10.9K
Snippets
23
Records
66
Agent score
89%

What's inside V-JEPA 2

  1. Overview of V-JEPA 2, V-JEPA 2.1, and V-JEPA 2-AC

    main

    V-JEPA 2 is an official PyTorch codebase for self-supervised video models designed for understanding, prediction, and planning. It includes three main variants:

    1. V-JEPA 2: A self-supervised approach for training video encoders using internet-scale video data. It excels at motion understanding and human action anticipation.
    2. V-JEPA 2.1: An improved version focusing on high-quality and temporally consistent dense features. It utilizes a Dense Predictive Loss (masking-based self-supervision where all tokens contribute to the loss), Deep Self-Supervision (applying loss at multiple intermediate representations), and Multi-Modal Tokenizers for images and videos.
    3. V-JEPA 2-AC: A latent action-conditioned world model post-trained from V-JEPA 2 using robot trajectory interaction data. It is designed for robot manipulation tasks (like reaching, grasping, and pick-and-place) via planning from image goals, without requiring environment-specific data or task-specific calibration.
  2. Post-train Action-Conditioned Models

    main
    Post-train a model starting from a pretrained V-JEPA 2 backbone (e.g., starting from the ViT-g/16 backbone). Use the app.main or app.main_distributed entrypoints with the appropriate config (e.g., configs/train/vitg16/droid-256px-8f.yaml).
  3. Train Attentive Probes

    main
    Train an attentive probe on top of frozen V-JEPA 2 features. You can run evaluations locally or via a distributed SLURM cluster. Use the provided configs in configs/eval/ to specify tasks like Something-Something v2 classification. Ensure you update filepaths for folder, checkpoint, dataset_train, and dataset_val in your config files.
  4. Load V-JEPA 2.1 models via Hugging Face Transformers

    main

    V-JEPA 2.1 models are available on Hugging Face. You can use the transformers library to load the AutoModel and AutoVideoProcessor using specific repository IDs.

    Supported repository IDs include:

    • facebook/vjepa2-vitl-fpc64-256
    • facebook/vjepa2-vith-fpc64-256
    • facebook/vjepa2-vitg-fpc64-256
    • facebook/vjepa2-vitg-fpc64-384
    from transformers import AutoVideoProcessor, AutoModel
    
    hf_repo = "facebook/vjepa2-vitg-fpc64-256"
    
    model = AutoModel.from_pretrained(hf_repo)
    processor = AutoVideoProcessor.from_pretrained(hf_repo)
  5. Run Inference from Existing Probes

    main

    Perform inference using pre-trained probes.

    1. Download the corresponding checkpoint.
    2. Rename the checkpoint to latest.pt.
    3. Place it in a folder following this structure: [folder]/[eval_name]/[tag]/latest.pt.
    4. Run the inference command using configs from configs/inference.
  6. Install V-JEPA 2

    main

    Set up the environment using Conda and install the package via pip.

    Note for macOS users: V-JEPA 2 requires decord, which does not support macOS. You must use an alternative implementation such as eva-decord or decord2 to run the code on macOS.

    conda create -n vjepa2-312 python=3.12
    conda activate vjepa2-312
    pip install .
    # or `pip install -e .` for development mode
  7. Load V-JEPA 2 backbones via PyTorch Hub

    main

    You can load pretrained V-JEPA 2 and V-JEPA 2.1 models directly using torch.hub. Ensure you have torch, timm, and einops installed locally. It is strongly recommended to use a PyTorch installation with CUDA support.

    Available symbols include the preprocessor and various model sizes for both V-JEPA 2 and V-JEPA 2.1.

    import torch
    
    # preprocessor
    processor = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_preprocessor')
    
    # V-JEPA 2 models
    vjepa2_vit_large = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_vit_large')
    vjepa2_vit_huge = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_vit_huge')
    vjepa2_vit_giant = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_vit_giant')
    vjepa2_vit_giant_384 = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_vit_giant_384')
    
    # V-JEPA 2.1 models
    vjepa2_1_vit_base_384 = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_1_vit_base_384')
    vjepa2_1_vit_large_384 = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_1_vit_large_384')
    vjepa2_1_vit_giant_384 = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_1_vit_giant_384')
    vjepa2_1_vit_gigantic_384 = torch.hub.load('facebookresearch/vjepa2', 'vjepa2_1_vit_gigantic_384')
  8. Load V-JEPA 2 weights in vanilla PyTorch

    main

    To load V-JEPA 2 encoder weights into a PyTorch model, use the load_pretrained_vjepa_pt_weights function. This function handles the preprocessing of the state_dict by removing module. and backbone. prefixes to ensure compatibility with your model instance.

    Weights can be downloaded via wget from the official Facebook repositories (e.g., https://dl.fbaipublicfiles.com/vjepa2/vitg-384.pt).

    def load_pretrained_vjepa_pt_weights(model, pretrained_weights):
        # Load weights of the VJEPA2 encoder
        pretrained_dict = torch.load(pretrained_weights, weights_only=True, map_location="cpu")["encoder"]
        pretrained_dict = {k.replace("module.", ""): v for k, v in pretrained_dict.items()}
        pretrained_dict = {k.replace("backbone.", ""): v for k, v in pretrained_dict.items()}
        msg = model.load_state_dict(pretrained_dict, strict=False)
        print("Pretrained weights found at {} and loaded with msg: {}".format(pretrained_weights, msg))
  9. Perform video action classification

    main

    After extracting patch-wise features from the encoder, you can pass them to an AttentiveClassifier to predict action classes (e.g., for the Something-Something V2 dataset).

    1. Initialize AttentiveClassifier with the encoder's embed_dim.
    2. Load pretrained probe weights.
    3. Pass the patch features to the classifier.
    4. Map the output indices to class names using a JSON mapping file (e.g., ssv2_classes.json).
    from src.models.attentive_pooler import AttentiveClassifier
    
    # Initialize the classifier
    classifier = AttentiveClassifier(
        embed_dim=model_pt.embed_dim, 
        num_heads=16, 
        depth=4, 
        num_classes=174
    ).cuda().eval()
    
    # Load weights and predict
    load_pretrained_vjepa_classifier_weights(classifier, classifier_model_path)
    
    with torch.inference_mode():
        out_classifier = classifier(out_patch_features_pt)
        # out_classifier contains logits for top-k prediction
  10. Initialize V-JEPA 2 via HuggingFace

    main

    V-JEPA 2 models are available on HuggingFace. You can initialize the model and its corresponding video processor using AutoModel and AutoVideoProcessor. This method automatically handles weight downloading via from_pretrained().

    from transformers import AutoVideoProcessor, AutoModel
    
    # HuggingFace model repo name
    hf_model_name = "facebook/vjepa2-vitg-fpc64-384"
    
    # Initialize the HuggingFace model, load pretrained weights
    model_hf = AutoModel.from_pretrained(hf_model_name)
    model_hf.cuda().eval()
    
    # Build HuggingFace preprocessing transform
    hf_transform = AutoVideoProcessor.from_pretrained(hf_model_name)
  11. Run evaluation via evals/main.py

    main
    The evals/main.py script is the entrypoint for running evaluation tasks. It supports distributed execution across multiple GPUs, loading configurations from YAML files, and overriding specific parameters like checkpoints, model names, and batch sizes via CLI flags. By default, it uses multiprocessing to spawn processes for each device specified in --devices.