How facebookresearch/tribev2 works
1. What this repo is
TRIBE v2 is a multimodal fMRI encoding system: it takes naturalistic stimuli from video, audio, and text and predicts brain responses on a common cortical surface representation. The codebase is split between an inference-facing model package and a training/experiment pipeline.
2. Primary runtime boundary
The core implementation is a Python 3.11+ / PyTorch project. Training uses PyTorch Lightning (via pl_module.py), while inference is exposed through a higher-level TribeModel wrapper in demo_utils.py. The README also indicates optional integrations for plotting/visualization and training tooling.
3. Model subsystem
tribev2/model.py contains the main FmriEncoder, described as a Transformer-based multimodal → fMRI model. Architecturally, this is the central compute boundary: upstream stimulus events are converted into features/tokens, then the encoder maps them into predicted response vectors over vertices/voxels.
4. Stimulus/event preprocessing
tribev2/eventstransforms.py and tribev2/demo_utils.py handle the conversion from raw inputs into model-ready event streams. The README makes clear that text can be converted into speech/transcription-derived word timings, and video/audio inputs are converted into timed events that align to the model’s temporal framing.
5. Inference flow
The public inference path is:
raw stimulus file (video_path, audio_path, or text_path) → event dataframe (get_events_dataframe) → prediction (predict) → (preds, segments).
This path is designed around a pretrained HuggingFace checkpoint (facebook/tribev2) and returns predictions on the fsaverage5 cortical mesh with an explicit 5-second lag offset to compensate for hemodynamic delay.
6. Training pipeline
tribev2/main.py and tribev2/pl_module.py form the experiment/training boundary. main.py likely orchestrates data setup and experiment configuration, while the Lightning module encapsulates optimization, batching, metrics, and training-step behavior. This separation keeps model definition distinct from training mechanics.
7. Study-specific dataset adapters
The tribev2/studies/ package contains per-dataset definitions for Algonauts2025, Lahner2024BOLD, Lebel2023BOLD, and Wen2017. These modules act as dataset adapters, normalizing heterogeneous study formats into the shared training pipeline so the model can be trained/evaluated across multiple neuroscience datasets.
8. Cross-study loading and splits
tribev2/utils.py and tribev2/utils_fmri.py provide the glue for multi-study work: loading, subject weighting, splitting, and fMRI-specific utilities. utils_fmri.py also handles surface projection between spaces such as MNI and fsaverage, plus ROI-oriented analysis support.
9. Grid search and experiment orchestration
The tribev2/grids/ package is the experiment-launch layer. defaults.py centralizes configuration, configs.py likely defines experiment variants, and run_cortical.py, run_subcortical.py, and test_run.py provide entry points for local smoke tests and Slurm-based grid execution. Environment variables like DATAPATH and SAVEPATH define the external data/output boundary.
10. Visualization subsystem
tribev2/plotting/ is an optional presentation layer for brain outputs. The README mentions PyVista and Nilearn backends, implying two rendering paths for cortical/subcortical views. This layer is separate from the modeling pipeline and consumes predicted brain maps plus mesh/ROI metadata.
11. End-to-end data flow
The overall architecture is:
raw naturalistic stimulus → event construction/transforms → multimodal feature encoding → transformer-based fMRI prediction → cortical surface projection/analysis → optional visualization.
The same core prediction output can feed either research evaluation or plotting, depending on which boundary is used.
12. Developer-facing entry points
The repository is organized around a small number of clear entry surfaces: TribeModel for inference, main.py/pl_module.py for training, grids/* for experiment launch, and plotting/* for visualization. The notebook tribe_demo.ipynb ties these together for interactive use.
Main entry points: utils.py Core utils · base.py Plot base · model.py FmriEncoder · demo_utils.py TribeModel
Components 21
Inference
Public stimulus-to-prediction path and pretrained model wrapper.
- TribeModel · High-level inference API for loading stimuli, building events, and producing predictions.
tribev2/demo_utils.py - Event transforms · Converts raw video, audio, and text inputs into timed model-ready event streams.
tribev2/eventstransforms.py - FmriEncoder · Transformer-based core model that maps multimodal stimulus features to fMRI responses.
tribev2/model.py
Training
Experiment entry points and Lightning training boundary.
- Main entry · Top-level training or experiment orchestration entry for running model workflows.
tribev2/main.py - Lightning module · PyTorch Lightning module that encapsulates optimization, stepping, and training behavior.
tribev2/pl_module.py
Data
Cross-study adapters and fMRI loading/projection utilities.
- Core utils · General repository utilities used across training, loading, and study workflows.
tribev2/utils.py - fMRI utils · fMRI-specific loading, surface projection, ROI handling, and cross-space utilities.
tribev2/utils_fmri.py - Algonauts2025 · Dataset adapter for the Algonauts 2025 study format.
tribev2/studies/algonauts2025.py - Lahner2024 · Dataset adapter for the Lahner 2024 BOLD study format.
tribev2/studies/lahner2024bold.py - Lebel2023 · Dataset adapter for the Lebel 2023 BOLD study format.
tribev2/studies/lebel2023bold.py - Wen2017 · Dataset adapter for the Wen 2017 study format.
tribev2/studies/wen2017.py
Visualization
Optional brain-map rendering backends.
- Plot base · Shared plotting foundation for brain visualization backends.
tribev2/plotting/base.py - Cortical plot · Cortical brain visualization utilities.
tribev2/plotting/cortical.py - PV cortical · PyVista-based cortical visualization path.
tribev2/plotting/cortical_pv.py - Subcortical plot · Subcortical brain visualization utilities.
tribev2/plotting/subcortical.py - Plot utils · Shared helpers for plotting and rendering data preparation.
tribev2/plotting/utils.py
Launch
Grid search and runtime orchestration.
- Defaults · Central configuration defaults for experiment launch and grid runs.
tribev2/grids/defaults.py - Configs · Named experiment variants and launch configurations.
tribev2/grids/configs.py - Cortical run · Entry point for cortical grid execution and batch runs.
tribev2/grids/run_cortical.py - Subcortical run · Entry point for subcortical grid execution and batch runs.
tribev2/grids/run_subcortical.py - Smoke test · Small local execution path for validating grid or run setup.
tribev2/grids/test_run.py
Connections 23
- TribeModel → Event transforms: build events · Converts raw stimulus inputs into timed event data.
- TribeModel → FmriEncoder: predict · Feeds events into the pretrained encoder to generate responses.
- Event transforms → FmriEncoder: tokens · Produces structured temporal inputs consumed by the model.
- Main entry → Lightning module: train · Main orchestration delegates optimization and step logic to Lightning.
- Main entry → Core utils: setup · Uses shared utilities for experiment and data preparation.
- Lightning module → FmriEncoder: wraps · Training module encapsulates the core encoder.
- Core utils → fMRI utils: fMRI ops · General utilities feed fMRI-specific loading and projection logic.
- Core utils → Algonauts2025: load · Shared pipeline consumes study-specific dataset adapters.
- Core utils → Lahner2024: load · Shared pipeline consumes study-specific dataset adapters.
- Core utils → Lebel2023: load · Shared pipeline consumes study-specific dataset adapters.
- Core utils → Wen2017: load · Shared pipeline consumes study-specific dataset adapters.
- Defaults → Configs: parameterize · Defaults underpin named experiment variants.
- Configs → Cortical run: launch · Configurations drive cortical experiment runs.
- Configs → Subcortical run: launch · Configurations drive subcortical experiment runs.
- Defaults → Smoke test: smoke · Defaults support the local validation path.
- Plot base → Cortical plot: extend · Shared plotting primitives are specialized for cortical views.
- Plot base → Subcortical plot: extend · Shared plotting primitives are specialized for subcortical views.
- Plot base → PV cortical: extend · Shared plotting primitives are specialized for PyVista cortical views.
- Plot utils → Cortical plot: support · Helpers supply common rendering data and formatting.
- Plot utils → Subcortical plot: support · Helpers supply common rendering data and formatting.
- fMRI utils → Plot base: analyze · Projected fMRI outputs and ROI metadata feed visualization.
- FmriEncoder → fMRI utils: project · Predictions are interpreted on cortical surface representations.
- TribeModel → Plot base: visualize · Inference outputs can be handed off to plotting backends.