# mirthai/med3dvlm architecture

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

- Interactive diagram: https://gitdiagram.com/mirthai/med3dvlm
- Repository: https://github.com/mirthai/med3dvlm

## Overview

##### Project type and purpose
– Med3DVLM is a research-oriented Vision-Language Model for 3D medical image analysis.
– It consists of data preparation, model training (contrastive and multi-modal), evaluation, and demo/inference (CLI & a small web app).

##### Break down the main components
A. Data Layer
- Raw datasets (M3D-Cap, M3D-VQA) in `data/` (NIfTI files + CSVs)
- Data-prep scripts in `src/utils` (e.g., `m3d_cap_data_prepare_128.py`, `rename_csv.py`)
B. Dataset APIs
- `src/dataset/clip_dataset.py` and `mllm_dataset.py` load NIfTI volumes + text
- `src/dataset/prompt_templates.py` holds textual prompts
C. Model Layer
- Encoder: `src/model/encoder/` (DCFormer, ViT builder)
- Projector: `src/model/projector/` (MLP, MHSA)
- LLM adapter: `src/model/llm/qwen.py`
- Overall VLM architecture: `src/model/vlm_arch.py`
- Contrastive CLIP head: `src/model/CLIP.py`
D. Training & Pre-training
- Scripts in `src/train/` (train_clip.py, train_vlm.py, trainer.py)
- Shell wrappers in `scripts/` (train_clip.sh, pretrain_mm_projector.sh, finetune_lora.sh, merge_lora_weights_and_save_hf_model.sh)
E. Evaluation
- Python entry points in `src/eval/` (eval_caption.py, eval_clip.py, eval_vqa.py)
- Shell wrappers in `scripts/eval/`
F. Demo & Serving
- CLI demo: `src/demo/demo.py`
- Minimal web app: `app.py` (likely Flask/FastAPI)
G. Configuration & Environment
- `env.yaml`, `requirements.txt`, `.gitignore`

##### Relationships and data flow
– Raw data → data-prep utils → preprocessed NIfTI + CSV → dataset loaders → model backbones/projectors → loss heads → output model weights
– Model weights → `trainer.py` → saved checkpoints in `output/` or `models/`
– Checkpoints → evaluation scripts → metrics (R@1, METEOR, VQA acc)
– Final model + weights → CLI demo or `app.py` web server → user queries

##### Architectural patterns
– Modular ML pipeline: clear separation of data, model, training, evaluation, and serving layers
– Two-stage training: 1) contrastive (SigLIP) 2) multi-modal pre-training and LoRA fine-tuning
– Dual-stream MLP-Mixer projector fusing low/high-level features + text embeddings
– Decomposed 3D convs in DCFormer for efficient volumetric encoding

##### Technologies & frameworks
– PyTorch 2.6.0 + TorchVision + MONAI for 3D medical images
– Deepspeed for large-scale training
– Conda/Pip for environment (`env.yaml`, `requirements.txt`)
– NIfTI file format, HuggingFace datasets, ModelScope

##### Detailed element checklist
– Component name labels (e.g., “DCFormer Encoder”)
– Notes on patterns (e.g., “Decomposed 3D convs → reduced memory”)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data & Preprocessing Layer
    subgraph "Data & Preprocessing" 
        direction TB
        RawData[(Raw Datasets)]:::storage
        PrepScripts["Data Prep Scripts"]:::data
        Preprocessed[(Preprocessed Data)]:::storage
    end

    %% Dataset API Layer
    subgraph "Dataset API" 
        direction TB
        ClipLoader["CLIP Dataset Loader"]:::data
        MLLMLoader["MLLM Dataset Loader"]:::data
        PromptTemplates["Prompt Templates"]:::data
    end

    %% Model Components Layer
    subgraph "Model Components"
        direction TB
        subgraph Encoder
            DCFormer["DCFormer Encoder"]:::model
            ViT["ViT Builder"]:::model
            EncBuilder["Encoder Builder"]:::model
        end
        subgraph Projector
            MLP["MLP Projector"]:::model
            MHSA["MHSA Projector"]:::model
            ProjBuilder["Projector Builder"]:::model
        end
        LLMAdapter["LLM Adapter (Qwen)"]:::model
        VLMArch["VLM Architecture"]:::model
        CLIPHead["Contrastive CLIP Head"]:::model
    end

    %% Training & Evaluation Layer
    subgraph "Training & Evaluation"
        direction TB
        TrainScripts["Training Scripts"]:::train
        ShellWrappers["Training Shell Wrappers"]:::train
        Trainer["Trainer Module"]:::train
        EvalScripts["Evaluation Scripts"]:::train
        EvalWrappers["Evaluation Shell Wrappers"]:::train
        Checkpoints[(Model Checkpoints)]:::storage
        Metrics[(Evaluation Metrics)]:::storage
    end

    %% Serving & Demo Layer
    subgraph "Serving & Demo"
        direction TB
        CLI["CLI Demo Interface"]:::serve
        WebApp["Web App Server"]:::serve
        User[(User)]:::external
    end

    %% External Services
    HF["HuggingFace / ModelScope"]:::external

    %% Config & Env
    EnvFiles["Config & Env Files"]:::external

    %% Connections
    RawData -->|"NIfTI + CSV"| PrepScripts
    PrepScripts -->|"Preprocessed NIfTI + CSV"| Preprocessed
    Preprocessed -->|"Loaded as tensors"| ClipLoader
    Preprocessed -->|"Loaded as tensors"| MLLMLoader
    ClipLoader -->|"/api/text prompts"| PromptTemplates
    MLLMLoader -->|"prompt templates"| PromptTemplates
    ClipLoader -->|"tensors"| DCFormer
    MLLMLoader -->|"tensors"| DCFormer
    DCFormer -->|"features"| EncBuilder
    ViT -->|"patch embeddings"| EncBuilder
    EncBuilder -->|"image embeddings"| Projector
    MLP -->|"projected features"| ProjBuilder
    MHSA -->|"attended features"| ProjBuilder
    ProjBuilder -->|"joint embeddings"| VLMArch
    PromptTemplates -->|"text embeddings"| VLMArch
    VLMArch -->|"multi-modal features"| CLIPHead
    CLIPHead -->|"contrastive loss"| Trainer
    Trainer -->|"checkpoints"| Checkpoints
    Trainer -->|"metrics"| Metrics
    Checkpoints -->|"load for eval"| EvalScripts
    EvalScripts -->|"compute metrics"| Metrics
    ShellWrappers -->|"> trainers"| Trainer
    EvalWrappers -->|"> eval scripts"| EvalScripts
    Checkpoints -->|"deploy"| CLI
    Checkpoints -->|"deploy"| WebApp
    CLI -->|"CLI calls"| User
    WebApp -->|"HTTP calls"| User
    HF -->|"download pretrained"| DCFormer
    EnvFiles -->|"setup env"| PrepScripts

    %% Click Events
    click RawData "https://github.com/mirthai/med3dvlm/tree/main/data/"
    click PrepScripts "https://github.com/mirthai/med3dvlm/blob/main/src/utils/m3d_cap_data_prepare_128.py"
    click PrepScripts "https://github.com/mirthai/med3dvlm/blob/main/src/utils/rename_csv.py"
    click ClipLoader "https://github.com/mirthai/med3dvlm/blob/main/src/dataset/clip_dataset.py"
    click MLLMLoader "https://github.com/mirthai/med3dvlm/blob/main/src/dataset/mllm_dataset.py"
    click PromptTemplates "https://github.com/mirthai/med3dvlm/blob/main/src/dataset/prompt_templates.py"
    click DCFormer "https://github.com/mirthai/med3dvlm/blob/main/src/model/encoder/dcformer.py"
    click ViT "https://github.com/mirthai/med3dvlm/blob/main/src/model/encoder/vit.py"
    click EncBuilder "https://github.com/mirthai/med3dvlm/blob/main/src/model/encoder/builder.py"
    click MLP "https://github.com/mirthai/med3dvlm/blob/main/src/model/projector/mlp.py"
    click MHSA "https://github.com/mirthai/med3dvlm/blob/main/src/model/projector/mhsa.py"
    click ProjBuilder "https://github.com/mirthai/med3dvlm/blob/main/src/model/projector/builder.py"
    click LLMAdapter "https://github.com/mirthai/med3dvlm/blob/main/src/model/llm/qwen.py"
    click VLMArch "https://github.com/mirthai/med3dvlm/blob/main/src/model/vlm_arch.py"
    click CLIPHead "https://github.com/mirthai/med3dvlm/blob/main/src/model/CLIP.py"
    click TrainScripts "https://github.com/mirthai/med3dvlm/blob/main/src/train/train_clip.py"
    click TrainScripts "https://github.com/mirthai/med3dvlm/blob/main/src/train/train_vlm.py"
    click Trainer "https://github.com/mirthai/med3dvlm/blob/main/src/train/trainer.py"
    click ShellWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/train_clip.sh"
    click ShellWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/pretrain_mm_projector.sh"
    click ShellWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/finetune_lora.sh"
    click ShellWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/merge_lora_weights_and_save_hf_model.sh"
    click EvalScripts "https://github.com/mirthai/med3dvlm/blob/main/src/eval/eval_caption.py"
    click EvalScripts "https://github.com/mirthai/med3dvlm/blob/main/src/eval/eval_clip.py"
    click EvalScripts "https://github.com/mirthai/med3dvlm/blob/main/src/eval/eval_vqa.py"
    click EvalWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/eval/eval_caption.sh"
    click EvalWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/eval/eval_clip.sh"
    click EvalWrappers "https://github.com/mirthai/med3dvlm/blob/main/scripts/eval/eval_vqa.sh"
    click CLI "https://github.com/mirthai/med3dvlm/blob/main/src/demo/demo.py"
    click WebApp "https://github.com/mirthai/med3dvlm/blob/main/app.py"
    click EnvFiles "https://github.com/mirthai/med3dvlm/blob/main/env.yaml"
    click EnvFiles "https://github.com/mirthai/med3dvlm/blob/main/requirements.txt"
    click EnvFiles "https://github.com/mirthai/med3dvlm/blob/main/.gitignore"

    %% Styling
    classDef data fill:#cce5ff,stroke:#004085,color:#004085
    classDef storage fill:#e2e3e5,stroke:#6c757d,color:#6c757d
    classDef model fill:#d4edda,stroke:#155724,color:#155724
    classDef train fill:#fff3cd,stroke:#856404,color:#856404
    classDef serve fill:#f5c6cb,stroke:#721c24,color:#721c24
    classDef external fill:#f8f9fa,stroke:#343a40,color:#343a40
```
