# jokieleung/cl-vqa architecture

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

- Interactive diagram: https://gitdiagram.com/jokieleung/cl-vqa
- Repository: https://github.com/jokieleung/cl-vqa

## Overview

##### Main components of the system
- Data Management & Preprocessing:
- Data download and preparation are handled by scripts in the tools/ directory (download.sh, process.sh, etc.).
- The project links to an external dataset (via bottom-up-attention-vqa) that provides visual features.
- Data Loading & Processing:
- The dataset.py script is responsible for loading and preprocessing the inputs (images and questions).
- Utilities (utils.py) assist with general operations.
- Model Components:
- base_model.py contains the basic model architecture.
- language_model.py implements text processing components.
- classifier.py and fc.py likely define the classifier and fully-connected layers needed for prediction.
- attention.py implements the attention mechanism which is a core part of VQA.
- vqa_debias_loss_functions.py houses the loss functions that handle debiasing during training.
- Training and Evaluation Pipeline:
- main.py is used for training, with presets for argument handling.
- train.py could be an alternate or legacy training script.
- eval.py is used to evaluate the trained model.
- Contrastive Learning Mechanism:
- Though not separated into its own module, the contrastive mechanism for handling counterfactual samples is likely integrated within the training and loss components, such as within vqa_debias_loss_functions.py and possibly the base_model or classifier.

##### Relationships and interactions between the components
- Data flows from the Data Management layer (tools/) into dataset.py where the visual and textual data are prepared.
- The loaded data is then passed into the Model Components (base_model, language_model, classifier, attention) during both training and inference.
- During training (main.py or train.py), the model uses specific loss functions (vqa_debias_loss_functions.py) that integrate contrastive learning and debiasing.
- Evaluation (eval.py) reuses parts of the model architecture to test performance, possibly integrating evaluation metrics defined elsewhere.
- Utilities in utils.py support various modules with common helper functions.

##### Important architectural patterns and design principles
- Modular Design: Each key functionality (preprocessing, model definition, and evaluation) is isolated in its own Python module, making the system easier to maintain and extend.
- Clear Separation of Concerns: The structure cleanly distinguishes between “data tools,” “model components,” and “training/evaluation scripts.”
- Integration of External Data: A dedicated data setup process is emphasized (via external scripts and links) ensuring the model leverages precomputed bottom-up visual attention features.
- Contrastive and Debiasing Approach: The architecture integrates contrastive learning elements (through specialized loss functions and potentially through a separation of counterfactual data pipelines).

##### Relevant technologies, frameworks, or libraries
- PyTorch (v1.1.0) is the deep learning framework underpinning model operations.
- Python 2.7 is used, so note any legacy compatibility aspects.
- h5py for handling data storage in HDF5 format.
- numpy for numerical computations.
- tqdm for progress tracking during training.
- Click for command-line interface operations.
- Shell scripts are used for data management and preprocessing.

- Data flows from the tools/download scripts to the dataset loader.
- Connections from the dataset loader to the various model components in the forward pass.
- A training loop that feeds back loss values from the debias loss function into model parameter updates.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Management & Preprocessing
    subgraph "Data Management & Preprocessing"
        A["Download Script"]:::preprocessing
        B["Processing Script"]:::preprocessing
        C["External Visual Features (bottom-up-attention)"]:::preprocessing
        A -->|"runs"| B
        B -->|"prepares data for"| C
    end

    %% Data Loading & Processing
    subgraph "Data Loading & Processing"
        D["Dataset Loader"]:::dataloader
        E["Utilities"]:::dataloader
    end

    %% Model Components
    subgraph "Model Components"
        F["Base Model Architecture"]:::model
        G["Language Processing Module"]:::model
        J["Attention Mechanism"]:::model
        H["Classifier"]:::model
        I["FC Layers"]:::model
        K["Debias & Contrastive Loss"]:::model
    end

    %% Training Pipeline
    subgraph "Training Pipeline"
        L["Main Training Script"]:::training
        M["Legacy Training Script"]:::training
    end

    %% Evaluation Pipeline
    subgraph "Evaluation Pipeline"
        N["Evaluation Script"]:::evaluation
    end

    %% Technical Dependencies
    subgraph "Technical Dependencies"
        O["PyTorch v1.1.0 & GPU Required"]:::dependencies
        P["Python 2.7, h5py, numpy, tqdm, click"]:::dependencies
    end

    %% Connections from Data Management to Data Loading
    C -->|"feeds raw data to"| D

    %% Connections from Data Loading to Model Components
    D -->|"loads data to"| F
    D -->|"loads data to"| G
    D -->|"loads data to"| J
    E -->|"supports"| D

    %% Internal Model Component relationships
    F -->|"features to"| H
    G -->|"textual features to"| H
    J -->|"attention outputs to"| H
    H -->|"fully-connected mapping"| I
    H -->|"loss computed by"| K

    %% Training Pipeline interactions
    L -->|"triggers training on"| F
    L -->|"computes loss via"| K
    M -->|"alternative training on"| F

    %% Feedback loop in training
    K -->|"backpropagates loss to"| F

    %% Evaluation Pipeline interactions
    N -->|"evaluates using"| F
    N -->|"evaluates using"| H

    %% Technical Dependencies supporting pipelines and model
    O --- L
    O --- N
    P --- L
    P --- N

    %% Click Events for Component Mapping
    click A "https://github.com/jokieleung/cl-vqa/blob/main/tools/download.sh"
    click B "https://github.com/jokieleung/cl-vqa/blob/main/tools/process.sh"
    click D "https://github.com/jokieleung/cl-vqa/blob/main/dataset.py"
    click E "https://github.com/jokieleung/cl-vqa/blob/main/utils.py"
    click F "https://github.com/jokieleung/cl-vqa/blob/main/base_model.py"
    click G "https://github.com/jokieleung/cl-vqa/blob/main/language_model.py"
    click H "https://github.com/jokieleung/cl-vqa/blob/main/classifier.py"
    click I "https://github.com/jokieleung/cl-vqa/blob/main/fc.py"
    click J "https://github.com/jokieleung/cl-vqa/blob/main/attention.py"
    click K "https://github.com/jokieleung/cl-vqa/blob/main/vqa_debias_loss_functions.py"
    click L "https://github.com/jokieleung/cl-vqa/blob/main/main.py"
    click M "https://github.com/jokieleung/cl-vqa/blob/main/train.py"
    click N "https://github.com/jokieleung/cl-vqa/blob/main/eval.py"

    %% Styles
    classDef preprocessing fill:#bbdefb,stroke:#333,stroke-width:2px;
    classDef dataloader fill:#c8e6c9,stroke:#333,stroke-width:2px;
    classDef model fill:#ffe0b2,stroke:#333,stroke-width:2px;
    classDef training fill:#f8bbd0,stroke:#333,stroke-width:2px;
    classDef evaluation fill:#d1c4e9,stroke:#333,stroke-width:2px;
    classDef dependencies fill:#cfd8dc,stroke:#333,stroke-width:2px,stroke-dasharray: 5,5;
```
