# kaist-ailab/syncvsr architecture

> Architecture diagram of the GitHub repository kaist-ailab/syncvsr, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kaist-ailab/syncvsr
- Repository: https://github.com/kaist-ailab/syncvsr

## Overview

##### And Represent the Main Components

- Data Preparation & Preprocessing:
- “Video Dataset Preparation” pipeline. This includes:
▪ Downloading data (using wget commands for LRW, LRS datasets).
▪ Preprocessing pipelines (files like preprocess_roi.py, preprocess_pkl.py in both LRS/video and LRW/video).
▪ Tokenized audio files (external audio-tokens downloaded from releases, with example code that loads pickle files).

- Configuration & Setup:
- “config” directories found in both LRS/video and LRW/video (as well as LRW/landmark). These YAML and shell script files are used to specify training, inference, and model hyperparameters.
- Additional setup scripts such as setup.sh in various places that configure dependencies.

- Data Module & Dataloader:
- LRW’s similar source data components (e.g., dataset.py in LRW/landmark/src and LRW/video/src/data.py).

- Model Architecture:
- The model is built using PyTorch along with custom layers for the encoder and a special linear projection (audio_projection) that synchronizes visual data with quantized audio tokens.
▪ The Encoder: That processes video frames (with special tokens for non-autoregressive predictions) to generate latent representations.
▪ The Audio Projection Module: Shown as an explicit block that projects encoder outputs into the space of distributed audio tokens. Indicate its role in producing logits for audio reconstruction.
▪ Loss Components: Visual Speech Recognition (VSR) objectives (Word classification, CTC, LM loss) combined with an audio reconstruction loss scaled by a factor λ_audio.
- Use of architectures borrowed from the “espnet” folder, which contains multiple ASR components (e.g., beam_search, transformer encoder/decoder layers, and scorer interfaces).

- Training & Inference Pipeline:
- Two training and inference flows:
▪ Sentence-Level VSR (LRS): “main.py”, “lightning.py” in LRS/video that run training and evaluation using lrs2.yaml and lrs3.yaml configurations.
▪ Word-Level VSR (LRW): “train.py”, “inference.py” within LRW/video along with corresponding configs (e.g., bert-12l-512d_LRW_96_bf16_rrc_WB.yaml).
▪ For the LRW/landmark branch, add its own training pipeline where different network configurations are used (see the transformer config scripts).

- External Dependencies & Additional Tools:
- Use of external libraries and frameworks such as PyTorch (and potentially lightning for training orchestration), fairseq (for optional audio quantization), ffmpeg, and additional system libraries.
- How quantized audio tokens are incorporated either by pre-downloaded tokens or by running the wav2vec quantizer.

##### Relationships and Data Flow

- From preprocessing, flow data into the datamodule (for both video and audio).

- Interaction Points:
- Where configurations from YAML files feed into model training/inference, ensuring that dynamic parameters are adjustable.
- How external tokenized audio files are loaded and integrated into the data pipeline, and highlight fast paths for inference versus full training.

##### Architectural Patterns & Design Principles

- Modular Design:
- Use of separation of concerns (for example, model definition vs. dataloader vs. configuration), which makes the system extensible.

- End-to-End Pipeline:
- Geometry should highlight the end-to-end nature of the system, starting with raw video and audio data, applying preprocessing, passing through a specialized VSR model, and ending with loss computation/training updates.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Data Ingestion
    RawData["Raw Video & Audio Data"]:::external

    %% Data Preparation & Preprocessing
    subgraph "Data Preparation & Preprocessing"
        Download["Data Download (wget)"]:::external
        LRSPreprocess["LRS Preprocess (video)"]:::data
        LRWPreprocessROI["LRW Preprocess ROI"]:::data
        LRWPreprocessPKL["LRW Preprocess PKL"]:::data
    end

    %% Configuration & Setup
    subgraph "Configuration & Setup"
        LRSConfig["LRS Config (YAML)"]:::config
        LRSSetup["LRS Setup.sh"]:::config
        LRWVideoConfig["LRW Video Config (YAML)"]:::config
        LRWVideoSetup["LRW Video Setup.sh"]:::config
        LRWLandmarkConfig["LRW Landmark Config"]:::config
        LRWLandmarkSetup["LRW Landmark Setup.sh"]:::config
    end

    %% Data Module & Dataloader
    subgraph "Data Module & Dataloader"
        LRSDataModule["LRS Datamodule"]:::datamodule
        LRWVideoData["LRW Video Data Loader"]:::datamodule
        LRWLandmarkDataset["LRW Landmark Dataset"]:::datamodule
    end

    %% Model Architecture
    subgraph "Model Architecture"
        Encoder["Encoder Module"]:::model
        AudioProjection["Audio Projection Module"]:::model
        LossComp["Final Loss Computation (VSR & Audio Loss)"]:::model
        LRSESPnet["LRS ESPnet Components"]:::model
        LRWTCN["LRW TCN Components"]:::model
        LRWLandmarkModel["LRW Landmark Model"]:::model
    end

    %% Training & Inference Pipeline
    subgraph "Training & Inference Pipeline"
        %% Sentence-Level VSR
        subgraph "Sentence-Level VSR"
            LRSMain["LRS Main.py"]:::training
            LRSLightning["LRS Lightning.py"]:::training
        end
        %% Word-Level VSR
        subgraph "Word-Level VSR"
            LRWTrain["LRW Train.py"]:::training
            LRWInference["LRW Inference.py"]:::training
            LRWLightning["LRW Lightning.py"]:::training
        end
        %% Landmark VSR
        subgraph "Landmark VSR"
            LRWLandmarkTraining["LRW Landmark Training.py"]:::training
            LRWLandmarkMain["LRW Landmark Main.py"]:::training
        end
    end

    %% External Dependencies & Tools
    subgraph "External Dependencies & Tools"
        ExternalTools["PyTorch, Lightning, fairseq, ffmpeg"]:::external
        LRSspm["LRS SPM Tokenization"]:::external
    end

    %% Data Flow Connections
    RawData --> Download
    Download --> LRSPreprocess
    Download --> LRWPreprocessROI
    Download --> LRWPreprocessPKL

    LRSConfig --> LRSDataModule
    LRWVideoConfig --> LRWVideoData
    LRWLandmarkConfig --> LRWLandmarkDataset

    LRSSetup --> LRSDataModule
    LRWVideoSetup --> LRWVideoData
    LRWLandmarkSetup --> LRWLandmarkDataset

    LRSPreprocess --> LRSDataModule
    LRWPreprocessROI --> LRWVideoData
    LRWPreprocessPKL --> LRWVideoData

    LRSDataModule --> Encoder
    LRWVideoData --> Encoder
    LRWLandmarkDataset --> Encoder

    Encoder --> AudioProjection
    AudioProjection --> LossComp

    LRSESPnet --> Encoder
    LRWTCN --> Encoder
    LRWLandmarkModel --> Encoder

    LossComp --> LRSMain
    LossComp --> LRWTrain
    LossComp --> LRWLandmarkTraining

    LRSMain --> LRSLightning
    LRWTrain --> LRWInference
    LRWTrain --> LRWLightning
    LRWLandmarkTraining --> LRWLandmarkMain

    ExternalTools --> LRSDataModule
    ExternalTools --> Encoder
    ExternalTools --> LRSMain
    ExternalTools --> LRWTrain
    ExternalTools --> LRWLandmarkTraining

    LRSspm --> LRSPreprocess

    LRSConfig --> LRSMain
    LRWVideoConfig --> LRWTrain
    LRWLandmarkConfig --> LRWLandmarkTraining

    %% Click Events for Component Mapping
    click LRSPreprocess "https://github.com/kaist-ailab/syncvsr/tree/main/LRS/video/preprocess"
    click LRWPreprocessROI "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/src/preprocess_roi.py"
    click LRWPreprocessPKL "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/src/preprocess_pkl.py"
    click LRSConfig "https://github.com/kaist-ailab/syncvsr/tree/main/LRS/video/config"
    click LRSSetup "https://github.com/kaist-ailab/syncvsr/blob/main/LRS/video/setup.sh"
    click LRWVideoConfig "https://github.com/kaist-ailab/syncvsr/tree/main/LRW/video/config"
    click LRWVideoSetup "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/setup.sh"
    click LRWLandmarkConfig "https://github.com/kaist-ailab/syncvsr/tree/main/LRW/landmark/config"
    click LRWLandmarkSetup "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/landmark/setup.sh"
    click LRSDataModule "https://github.com/kaist-ailab/syncvsr/tree/main/LRS/video/datamodule"
    click LRWVideoData "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/src/data.py"
    click LRWLandmarkDataset "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/landmark/src/dataset.py"
    click LRSESPnet "https://github.com/kaist-ailab/syncvsr/tree/main/LRS/video/espnet"
    click LRWTCN "https://github.com/kaist-ailab/syncvsr/tree/main/LRW/video/src/tcn"
    click LRWLandmarkModel "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/landmark/src/modeling.py"
    click LRSMain "https://github.com/kaist-ailab/syncvsr/blob/main/LRS/video/main.py"
    click LRSLightning "https://github.com/kaist-ailab/syncvsr/blob/main/LRS/video/lightning.py"
    click LRWTrain "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/src/train.py"
    click LRWInference "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/src/inference.py"
    click LRWLightning "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/video/src/lightning.py"
    click LRWLandmarkTraining "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/landmark/src/training.py"
    click LRWLandmarkMain "https://github.com/kaist-ailab/syncvsr/blob/main/LRW/landmark/src/main.py"
    click LRSspm "https://github.com/kaist-ailab/syncvsr/tree/main/LRS/video/spm"

    %% Class Definitions for Colors and Styles
    classDef data fill:#FAD7A0,stroke:#333,stroke-width:2px;
    classDef config fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef datamodule fill:#A9DFBF,stroke:#333,stroke-width:2px;
    classDef model fill:#F5B7B1,stroke:#333,stroke-width:2px;
    classDef training fill:#D2B4DE,stroke:#333,stroke-width:2px;
    classDef external fill:#F9E79F,stroke:#333,stroke-width:2px;
```
