TRIBE v2

repository·main·Indexed 25 days ago

https://github.com/facebookresearch/tribev2

A deep multimodal foundation model for in-silico neuroscience that predicts fMRI brain responses to naturalistic stimuli, including video, audio, and text. It maps multimodal representations onto the fsaverage5 cortical mesh (~20k vertices). The library provides the TribeModel class for high-level inference via HuggingFace pretrained models, tools for generating events dataframes from various media, and utilities for training models from scratch.

Tokens
4.5K
Snippets
10
Records
29
Agent score
85%

What's inside tribev2

  1. Train a model from scratch

    main

    To train a model from scratch, follow these steps:

    1. Set environment variables

    Configure your data and output paths using environment variables (or edit tribev2/grids/defaults.py directly):

    • DATAPATH: Path to your studies.
    • SAVEPATH: Path to your output.

    2. Run training

    • Local test run: Use the test run module.
    • Grid search on Slurm: Use the cortical or subcortical run modules.
    # 1. Set environment variables
    export DATAPATH="/path/to/studies"
    export SAVEPATH="/path/to/output"
    
    # 2. Run training
    # Local test run
    python -m tribev2.grids.test_run
    
    # Grid search on Slurm
    python -m tribev2.grids.run_cortical
    python -m tribev2.grids.run_subcortical
  2. Install TRIBE v2

    main

    You can install TRIBE v2 in different modes depending on your requirements:

    • Basic (Inference only): Installs the core package.
    • With brain visualization: Adds dependencies for plotting.
    • With training dependencies: Adds dependencies for training (e.g., PyTorch Lightning, W&B).
  3. Use TribeModel for high-level inference

    main

    The TribeModel class provides a high-level wrapper for generating fMRI-like brain-activity predictions from text, audio, or video inputs. It follows a from_pretrained / predict pattern.

    Typical workflow:

    1. Load a model using from_pretrained (from a local directory or HuggingFace Hub).
    2. Generate an events DataFrame using get_events_dataframe by providing a path to a text, audio, or video file.
    3. Run inference using predict to get brain activity predictions and corresponding segments.
    model = TribeModel.from_pretrained("facebook/tribev2")
    events = model.get_events_dataframe(video_path="clip.mp4")
    preds, segments = model.predict(events)
  4. Install TRIBE v2

    main

    To install TRIBE v2 with plotting support, use uv pip to install directly from the GitHub repository. If using Google Colab, ensure you have activated a GPU runtime before installation and restart your environment after the installation completes.

    !uv pip install "tribev2[plotting] @ git+https://github.com/facebookresearch/tribev2.git"
  5. Predict brain responses using TribeModel

    main

    Use TribeModel.from_pretrained to load a model from HuggingFace. You can then generate an events dataframe from video, audio, or text and pass it to model.predict to get fMRI brain response predictions.

    Key Details:

    • Predictions are for the "average" subject on the fsaverage5 cortical mesh (~20k vertices).
    • Predictions are offset by 5 seconds in the past to compensate for hemodynamic lag.
    • get_events_dataframe accepts video_path, text_path, or audio_path. Text is automatically converted to speech and transcribed for word-level timings.
    from tribev2 import TribeModel
    
    model = TribeModel.from_pretrained("facebook/tribev2", cache_folder="./cache")
    
    df = model.get_events_dataframe(video_path="path/to/video.mp4")
    preds, segments = model.predict(events=df)
    print(preds.shape)  # (n_timesteps, n_vertices)
  6. Configure FmriEncoder settings

    main

    The FmriEncoder class is a configuration object used to define the architecture of an fMRI encoder. It allows you to specify projectors, combiners, encoders, and various hyperparameters for handling multi-modal data.

    Key configuration options include:

    • projector: A BaseModelConfig used to project input features.
    • combiner: An optional Mlp to combine projected modalities.
    • encoder: An optional TransformerEncoder for temporal modeling.
    • hidden: The hidden dimension size (default: 256).
    • max_seq_len: Maximum sequence length (default: 1024).
    • dropout: Dropout rate applied to the encoder and projector.
    • extractor_aggregation: How to aggregate features from different modalities. Options: "stack", "sum", or "cat" (default: "cat").
    • layer_aggregation: How to aggregate layers within a modality. Options: "mean" or "cat" (default: "cat").
    • modality_dropout: Probability of dropping an entire modality during training.
    • temporal_dropout: Probability of dropping a timestep during training.
    • temporal_smoothing: Optional TemporalSmoothing configuration.
  7. Configure TemporalSmoothing

    main

    The TemporalSmoothing class provides a 1D convolutional layer with a Gaussian kernel for temporal smoothing.

    Configuration options:

    • kernel_size: The size of the smoothing kernel (default: 9).
    • sigma: The standard deviation of the Gaussian kernel. If None, the kernel is not initialized with Gaussian weights and remains a standard convolution.
  8. Generate an events dataframe from video

    main

    To process a video, use model.get_events_dataframe(video_path=...). This method automatically extracts audio, transcribes speech using WhisperX, and prepares the multimodal features (visual, audio, and text) required for prediction. The resulting dataframe contains columns such as type, start, duration, filepath, text, and context.

    video_path = CACHE_FOLDER / "sample_video.mp4"
    df = model.get_events_dataframe(video_path=video_path)
  9. Use FmriEncoderModel for forward passes

    main

    The FmriEncoderModel is a torch.nn.Module that processes SegmentData batches.

    forward(batch: SegmentData, pool_outputs: bool = True) -> torch.Tensor

    • batch: A SegmentData object containing the input tensors (e.g., modality data and subject_id).
    • pool_outputs: If True, applies AdaptiveAvgPool1d to the output to reduce the temporal dimension to n_output_timesteps. If False, returns the full temporal sequence.

    Returns a tensor of shape [Batch, Outputs, Timesteps] (if pooled) or [Batch, Outputs, Original_Timesteps] (if not pooled).

  10. Split events into train and validation sets using SplitEvents

    main

    The SplitEvents class is an EventsTransform used to partition event data into training and validation sets. It uses a val_ratio to determine the split and relies on a predefined mapping of studies to specific attributes (like chunk, task, story, etc.) to ensure splits are consistent within a study.

    Note: The splitting logic is study-dependent. The assign_splits function looks up the study name in a hardcoded SPLIT_ATTRIBUTES dictionary to find the column used for splitting.

  11. Create video events from image events using CreateVideosFromImages

    main

    The CreateVideosFromImages class transforms Image events into Video events by generating .mp4 files from the image paths.

    Configuration:

    • fps: The frames per second for the generated video (default: 10).
    • remove_images: If True, the original Image events are removed from the DataFrame after video creation (default: True).
    • infra: Uses exca.MapInfra for parallel processing (default: exca.MapInfra(cluster="processpool")).

    Dependencies:

    • Requires moviepy for video generation.