# fawazsammani/nlxgpt architecture

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

- Interactive diagram: https://gitdiagram.com/fawazsammani/nlxgpt
- Repository: https://github.com/fawazsammani/nlxgpt

## Overview

##### Project Type and Purpose
- This project is a research codebase for NLX-GPT—a model that generates natural language explanations for various vision and vision-language tasks (e.g., VQA-X, ACT-X, e-SNLI-VE, VCR). It supports training, predicting explanations, and even adversarial/retrieval evaluations.
- The README confirms that the system is built around fine-tuning and evaluation of language models (with a focus on natural language explanations) in combination with visual tasks.

##### Analyzing the File Structure
- Top-Level Scripts: Files like actX.py, esnlive.py, esnlive_concepts.py, vqaX.py, and vcr.py are the main entry points to run experiments for different tasks.
- Model Components: The “models” directory contains the core network modules (e.g., clip_vit.py, gpt.py, gpt_vcr.py) used to build the model architecture—indicating a reliance on both vision (CLIP/ViT) and language (GPT) models.
- Additional Functionalities:
– “explain_predict” folder: Contains scripts (ep_actX.py, ep_esnlive.py, ep_vqaX.py) and a README on generating predictions and explanations.
– “retrieval_attack” folder: Contains functionality for evaluation metrics, likely to test robustness or to perform retrieval-based attacks on the model.
– “utils” folder: Includes helper modules such as data_utils.py and eval_utils.py, as well as notebooks for preprocessing data and evaluation (e.g., for Coco Caption evaluation).
- Other Scripts: pretrain.py shows that there is functionality for pretraining, and supporting external evaluation or filtering (e.g., vcr_filter.py).

##### Main Components
a. Data Ingestion & Preprocessing:
– Inputs: Images and annotations from various datasets (as described in the README) including COCO, MPI, Flickr30k, and VCR.
– Utilities/Notebooks: Located in “utils” (e.g., nle_preprocess.ipynb, preprocess_for_cococaption_eval.ipynb) showing data cleaning and preparation steps.
b. Core Model Architecture:
– Model Modules: Components from “models” (CLIP-based vision module, GPT language model modules, and a specialized variant for VCR).
– Pretraining Layer: pretrain.py showcases the pretraining process (with potential use of pretrained captioning models).
c. Training and Finetuning Pipelines:
– Task-specific entry points: actX.py, esnlive.py (+ concepts variant), vqaX.py, and vcr.py each drive finetuning for the respective dataset task.
– Distributed Training: Integration with ‘accelerate’ for multi-GPU or distributed setups.
d. Prediction and Evaluation:
– Prediction Generation: The “explain_predict” folder handles generating and perhaps visualizing natural language explanations.
– Evaluation: Retrieval_attack and additional scripts (vcr_filter.py) handle evaluation and filtering, including using external tools (e.g., BERTScore, coco-caption).
e. External Interfaces and Dependencies:
– Gradio Demos: Mentioned as web-demo configurations for VQA-X and ACT-X.
– External Dependencies: PyTorch, CLIP, transformers, accelerate, and coco-caption for evaluation.

##### Relationships and Data Flow
- Data Flow: Start with raw images and annotations (downloadable as described in the README) → processed through preprocessing utilities → fed into the model during training (using pretrain.py and task-specific scripts) → generate predictions & explanations via explain_predict scripts → evaluate with retrieval_attack and filtering steps (vcr_filter.py, BERTScore, coco-caption).
- Components Interaction: The central NLX-GPT model (combining vision and language modules) is used by multiple pipelines. Training scripts call the model components; evaluation utilities leverage model predictions to gauge performance.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Input
    RD["Raw Data (Images & Annotations)"]:::external

    %% Data Ingestion & Preprocessing
    subgraph "Data Ingestion & Preprocessing"
        PN1["nle_preprocess.ipynb"]:::data
        PN2["preprocess_for_cococaption_eval.ipynb"]:::data
        DU["data_utils.py"]:::data
        EU["eval_utils.py"]:::data
    end

    %% Core Model Architecture
    subgraph "Core Model Architecture"
        CVM["clip_vit.py (Vision Module)"]:::model
        GLM["gpt.py (Language Module)"]:::model
        VCRM["gpt_vcr.py (Specialized VCR Model)"]:::model
    end

    %% Training and Finetuning Pipelines
    subgraph "Training and Finetuning Pipelines"
        ACT["actX.py (ACT-X)"]:::train
        ESL1["esnlive.py (e-SNLI-VE)"]:::train
        ESL2["esnlive_concepts.py (e-SNLI-VE Concepts)"]:::train
        VQA["vqaX.py (VQA-X)"]:::train
        VCR["vcr.py (VCR)"]:::train
        PRE["pretrain.py"]:::train
        ACC["Accelerate"]:::external
    end

    %% Prediction Generation
    subgraph "Prediction Generation"
        EPA["ep_actX.py"]:::predict
        EPE["ep_esnlive.py"]:::predict
        EPV["ep_vqaX.py"]:::predict
        EPRD["README.md (Predictions)"]:::predict
        EPDP["prepare data/README.md"]:::predict
    end

    %% Evaluation and Filtering
    subgraph "Evaluation and Filtering"
        RAA["ra_actX.py"]:::eval
        RAE["ra_esnlive.py"]:::eval
        RAV["ra_vqaX.py"]:::eval
        RARD["retrieval/README.md"]:::eval
        VCF["vcr_filter.py"]:::eval
        CC["coco-caption"]:::external
    end

    %% External Interfaces
    GRAD["Gradio Demos"]:::external

    %% Data Flow Connections
    RD --> PN1
    RD --> PN2
    RD --> DU
    RD --> EU

    PN1 --> CVM
    PN2 --> GLM
    DU --> CVM
    EU --> GLM

    CVM --> ACT
    CVM --> ESL1
    CVM --> ESL2
    CVM --> VQA
    CVM --> VCR
    GLM --> ACT
    GLM --> ESL1
    GLM --> ESL2
    GLM --> VQA
    GLM --> VCR
    VCRM --> VCR

    PRE --> CVM
    PRE --> GLM
    PRE --> ACC

    ACC --- ACT
    ACC --- ESL1
    ACC --- ESL2
    ACC --- VQA
    ACC --- VCR

    ACT --> EPA
    ESL1 --> EPE
    VQA --> EPV
    VCR --> EPA
    VCR --> EPV

    EPA --> RAA
    EPE --> RAE
    EPV --> RAV
    EPRD --> RARD
    EPDP --> RARD
    EPRD --> RARD

    CC --- RARD
    CC --- EU
    VCF --> RAV

    GRAD --- EPA
    GRAD --- EPE
    GRAD --- EPV

    %% Class Definitions for colors
    classDef data fill:#DAF7A6,stroke:#333,stroke-width:2px;
    classDef model fill:#FFC300,stroke:#333,stroke-width:2px;
    classDef train fill:#FF5733,stroke:#333,stroke-width:2px;
    classDef predict fill:#C70039,stroke:#333,stroke-width:2px;
    classDef eval fill:#900C3F,stroke:#333,stroke-width:2px;
    classDef external fill:#581845,stroke:#fff,stroke-width:2px;

    %% Click Events
    click PN1 "https://github.com/fawazsammani/nlxgpt/blob/main/utils/nle_preprocess.ipynb"
    click PN2 "https://github.com/fawazsammani/nlxgpt/blob/main/utils/preprocess_for_cococaption_eval.ipynb"
    click DU "https://github.com/fawazsammani/nlxgpt/blob/main/utils/data_utils.py"
    click EU "https://github.com/fawazsammani/nlxgpt/blob/main/utils/eval_utils.py"
    click CVM "https://github.com/fawazsammani/nlxgpt/blob/main/models/clip_vit.py"
    click GLM "https://github.com/fawazsammani/nlxgpt/blob/main/models/gpt.py"
    click VCRM "https://github.com/fawazsammani/nlxgpt/blob/main/models/gpt_vcr.py"
    click ACT "https://github.com/fawazsammani/nlxgpt/blob/main/actX.py"
    click ESL1 "https://github.com/fawazsammani/nlxgpt/blob/main/esnlive.py"
    click ESL2 "https://github.com/fawazsammani/nlxgpt/blob/main/esnlive_concepts.py"
    click VQA "https://github.com/fawazsammani/nlxgpt/blob/main/vqaX.py"
    click VCR "https://github.com/fawazsammani/nlxgpt/blob/main/vcr.py"
    click PRE "https://github.com/fawazsammani/nlxgpt/blob/main/pretrain.py"
    click EPA "https://github.com/fawazsammani/nlxgpt/blob/main/explain_predict/ep_actX.py"
    click EPE "https://github.com/fawazsammani/nlxgpt/blob/main/explain_predict/ep_esnlive.py"
    click EPV "https://github.com/fawazsammani/nlxgpt/blob/main/explain_predict/ep_vqaX.py"
    click EPRD "https://github.com/fawazsammani/nlxgpt/blob/main/explain_predict/README.md"
    click EPDP "https://github.com/fawazsammani/nlxgpt/blob/main/explain_predict/prepare data/README.md"
    click RAA "https://github.com/fawazsammani/nlxgpt/blob/main/retrieval_attack/ra_actX.py"
    click RAE "https://github.com/fawazsammani/nlxgpt/blob/main/retrieval_attack/ra_esnlive.py"
    click RAV "https://github.com/fawazsammani/nlxgpt/blob/main/retrieval_attack/ra_vqaX.py"
    click RARD "https://github.com/fawazsammani/nlxgpt/blob/main/retrieval_attack/retrieval/README.md"
    click VCF "https://github.com/fawazsammani/nlxgpt/blob/main/utils/vcr_filter.py"
```
