TorchMultimodal Documentation

repository·main·Indexed 23 days ago

https://github.com/facebookresearch/multimodal

A PyTorch-based library providing modular building blocks and pretrained models for large-scale multimodal research, including generative and content understanding models. The library includes implementations and examples for ALBEF (retrieval and VQA), FLAVA scaling studies, MDETR phrase grounding, and MUGEN text-to-video generation.

Tokens
17.1K
Snippets
50
Records
72
Agent score
83%

What's inside TorchMultimodal

  1. Overview of TorchMultimodal

    main

    TorchMultimodal is a PyTorch library designed for training large-scale multimodal multi-task models, covering both content understanding and generative tasks. It provides:

    • Modular Building Blocks: Composable fusion layers, loss functions, datasets, and utilities.
    • Pretrained Model Classes: A collection of common multimodal models built from the library's building blocks.
    • Research Baselines: Example scripts that combine these components with the PyTorch ecosystem to replicate state-of-the-art research.
  2. Explore MUGEN multimodal tasks

    main

    The MUGEN playground provides examples for two primary multimodal tasks:

    1. Text-video retrieval: Finding videos that match specific text descriptions.
    2. Text-to-video generation: Generating video content based on text prompts.

    Specific implementation details and scripts for these tasks are located in the retrieval and generation subdirectories respectively.

  3. Understand the MUGEN video-text retrieval architecture

    main

    The MUGEN retrieval model is based on the VideoCLIP architecture, which is a contrastive model designed to learn a joint embedding space for video and text. It optimizes a scaled cosine similarity function between video and text embedding vectors.

    Core Components:

    • Video Encoder: Uses a Separable 3D CNN (S3D).
    • Text Encoder: Uses DistilBERT (a lightweight transformer).

    This architecture allows for retrieval tasks such as Text2video (finding videos given a text query) and Video2text (finding text descriptions given a video).

  4. How MUGEN multimodal generation works

    main

    MUGEN follows a two-stage generative process inspired by DALL-E but adapted for video using VideoGPT components:

    1. Discrete Latent Representation: Each modality is converted into discrete tokens.
      • Text: Obtained via tokenization (e.g., BPE).
      • Video/Image: A VQ-VAE model is used to learn downsampled discrete embedding vectors via nearest-neighbor lookups from a "codebook". The resulting indices are referred to as token IDs.
    2. Joint Prior Learning: A GPT transformer decoder is used to learn a joint prior for both modalities in the latent space, allowing for generation in one modality given inputs from another.

    For the video component specifically, the architecture uses 3D-convolutions and self-axial-attention within the VQ-VAE encoder/decoder and the GPT transformer decoder.

  5. Core concepts of diffusion_labs

    main

    The diffusion_labs module provides components for building and training diffusion models (e.g., Dalle2, LDM). The architecture is built around several key abstractions:

    • Models: Definitions for diffusion models (like LDM) and their internal components (like U-Net or Transformers). For example, the ADM U-Net is available at diffusion_labs/models/adm_unet.
    • Adapters: Wrappers around models that adapt the architecture to handle various conditional inputs. Because all Adapters share the same forward signature, they can be stacked to handle multiple input types.
    • Predictor: Defines the target of the model's training (e.g., predicting added noise vs. a clean image) and converts model outputs into denoised data points.
    • Schedule: Defines the diffusion process, including the type and amount of noise applied at each step. It manages noise values and related computations.
    • Sampler: A wrapper that uses the Model, Predictor, and Schedule to denoise data. In train mode, it calls the model for a single step; in eval mode, it executes the entire diffusion schedule.
    • Transform: Specialized diffusion helper transforms implemented as nn.Module. They accept a data dictionary and return an updated dictionary, allowing them to be stacked using nn.Sequential and compatible with compilation.
  6. Explore the TorchMultimodal code structure

    main

    The repository is organized into several functional directories:

    • torchmultimodal/diffusion_labs: Components for building diffusion models.
    • torchmultimodal/models: Specific model architectures (e.g., blip2, albef).
    • torchmultimodal/modules: Generic building blocks:
      • layers: Codebooks, patch embeddings, transformers.
      • losses: Contrastive loss, reconstruction loss.
      • encoders: ViT, BERT.
      • fusions: Fusion modules like Deep Set fusion.
    • torchmultimodal/transforms: Model-specific data transforms (e.g., clip_transform, flava_transform, mae_transform).
  7. Build TorchMultimodal from Source

    main

    To build from source and run the included examples, clone the repository recursively to ensure submodules are included, then install the package in editable mode.

    git clone --recursive https://github.com/facebookresearch/multimodal.git multimodal
    cd multimodal
    
    pip install -e .
  8. Install Omnivore dependencies

    main

    To run training and evaluation for Omnivore, you must install the nightly PyTorch core, the TorchMultimodal package, and specific dependencies like scipy and av.

    1. Install nightly PyTorch core and domains using conda.
    2. Install the TorchMultimodal package in editable mode from your local root folder.
    3. Install additional dependencies scipy and av via pip.
    # Install nightly pytorch core and domains
    conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch-nightly
    
    # Install pytorch MultiModal
    cd <torchmultimodal root folder>
    pip install -e .
    
    # Other dependency
    pip install scipy==1.8.1 av==9.2.0
  9. Evaluate the MUGEN video-text retrieval model

    main

    To evaluate the model, use the eval.py script. Configuration is managed via configs/eval.yaml.

    By default, the evaluation script uses the MUGEN authors' published weights. To evaluate your own trained model, replace the checkpoint_path key in configs/eval.yaml with the path to your specific checkpoint file.

    python eval.py config=configs/eval.yaml
  10. Organize datasets for Omnivore

    main

    Omnivore expects specific folder structures for ImageNet1K, Kinetics400, and SunRGBD datasets to support multi-modal training (image, video, and RGB-D).

    ImageNet1K

    Organize by class subdirectories under train and val folders:

    root
        train
            class_1
                image_1.jpeg
                ...
        val
            class_1
                image_1.jpeg
                ...

    Kinetics400

    Organize by class subdirectories containing .mp4 files:

    root
        train
            class_1
                video_1.mp4
                ...

    SunRGBD

    Requires a specific structure containing image directories, depth files, and metadata files (intrinsics.txt, scene.txt), along with the SUNRGBDtoolbox containing allsplit.mat.

  11. Set up ALBEF for Retrieval tasks

    main

    To run retrieval tasks (image-text or text-image), follow these steps:

    1. Download Data: Download and extract COCO 2014 train/val splits and the custom ALBEF annotations.
    # Download and extract train and val splits from COCO 2014 dataset
    wget http://images.cocodataset.org/zips/train2014.zip
    wget http://images.cocodataset.org/zips/val2014.zip
    unzip train2014.zip
    unzip val2014.zip
    
    # Download and extract annotations
    wget https://storage.googleapis.com/sfr-pcl-data-research/ALBEF/data.tar.gz
    tar -xvzf data.tar.gz
    1. Configure Paths: Update examples/albef/configs/retrieval.yaml with your local paths for annotations, images, and checkpoints.

    2. Run Fine-tuning: Execute the script from the examples/albef directory.

    # In examples/albef/configs/retrieval.yaml
    datamodule_args:
      train_files: ["<my_annotations_root>/coco_train.json"]
      test_files: ["<my_annotations_root>/coco_test.json"]
      image_root: "<my_coco_images_root>"
      ...
    
    training_args:
      ...
      checkpoint_root: <my_checkpoint_root>