# milab-xenos/buvfm architecture

> Architecture diagram of the GitHub repository milab-xenos/buvfm, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/milab-xenos/buvfm
- Repository: https://github.com/milab-xenos/buvfm

## Overview

BUVFM is a breast-ultrasound video foundation-model project. Its principal user workflow screens video for likely lesions, then grades risk with a VJEPA classifier and Grad-CAM; the repository also supports pretraining, fine-tuning, evaluation, and model distillation. The graph preserves those distinct product branches and the shared video-data and model layers. Some branches are documented in the README but their implementation was not sampled, so those links cite the README rather than implying source inspection.

Main entry points: [`buildmodel.py`](https://github.com/milab-xenos/buvfm/blob/main/buildmodel.py) (Model builder), [`src/models/vision_transformer.py`](https://github.com/milab-xenos/buvfm/blob/main/src/models/vision_transformer.py) (Video transformer), [`src/datasets/data_manager.py`](https://github.com/milab-xenos/buvfm/blob/main/src/datasets/data_manager.py) (Data manager), [`pipeline_utils.py`](https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py) (Risk grading)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_demo["Screening and demo"]
  node_demo_app["Gradio app<br/>[app.py]"]
  node_stage1["Lesion screening<br/>[pipeline_utils.py]"]
  node_stage3["Risk grading<br/>[pipeline_utils.py]"]
  node_pipeline["Demo orchestrator<br/>[pipeline_utils.py]"]
  node_risk_result["Risk and heatmaps"]
  node_qc["Quality screening<br/>[quality_con.py]"]
end

subgraph group_model["Video models"]
  node_builder["Model builder<br/>[buildmodel.py]"]
  node_vit["Video transformer"]
  node_attention["Classifier head"]
  node_transformer_blocks["Transformer blocks<br/>[modules.py]"]
  node_patch_embed["Patch embedding<br/>[patch_embed.py]"]
  node_pos_embed["Position embeddings<br/>[pos_embs.py]"]
end

subgraph group_data["Video data"]
  node_video_data["Video dataset<br/>[video_dataset.py]"]
  node_data_manager["Data manager<br/>[data_manager.py]"]
  node_image_data["ImageNet dataset<br/>[imagenet1k.py]"]
  node_video_transforms["Video transforms<br/>[transforms.py]"]
end

subgraph group_training["Training and evaluation"]
  node_app_train["Pretraining entry<br/>[main.py]"]
  node_pretrain_engine["VJEPA trainer<br/>[train.py]"]
  node_eval["Frozen evaluation<br/>[eval.py]"]
  node_finetune["Downstream fine-tuning<br/>[train.py]"]
  node_inference["Distributed inference"]
end

subgraph group_distillation["Model distillation"]
  node_distill_cls["Classification distillation<br/>[distill.py]"]
  node_distill_det["Detection distillation<br/>[train.py]"]
end

node_user(("Video user"))

node_user -->|"submits video"| node_demo_app
node_demo_app -->|"invokes pipeline"| node_pipeline
node_pipeline -->|"runs screening"| node_stage1
node_pipeline -.->|"runs grading"| node_stage3
node_stage3 -->|"builds classifier"| node_builder
node_builder -->|"constructs backbone"| node_vit
node_builder -->|"constructs head"| node_attention
node_vit -->|"uses blocks"| node_transformer_blocks
node_vit -->|"uses embedding"| node_patch_embed
node_vit -->|"uses positions"| node_pos_embed
node_stage3 -->|"produces grading"| node_risk_result
node_user -.->|"screens clips"| node_qc
node_data_manager -->|"loads videos"| node_video_data
node_data_manager -->|"loads images"| node_image_data
node_app_train -->|"launches training"| node_pretrain_engine
node_pretrain_engine -->|"requests batches"| node_data_manager
node_pretrain_engine -->|"uses transforms"| node_video_transforms
node_eval -->|"requests data"| node_data_manager
node_eval -->|"uses classifier"| node_attention
node_finetune -.->|"fine-tunes model"| node_builder
node_inference -->|"loads model"| node_builder
node_inference -.->|"reads clips"| node_video_data
node_distill_cls -->|"builds student"| node_builder
node_distill_det -->|"builds detector"| node_builder

click node_demo_app "https://github.com/milab-xenos/buvfm/blob/main/app.py"
click node_stage1 "https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py"
click node_stage3 "https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py"
click node_pipeline "https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py"
click node_qc "https://github.com/milab-xenos/buvfm/blob/main/distill/quality_con.py"
click node_builder "https://github.com/milab-xenos/buvfm/blob/main/buildmodel.py"
click node_vit "https://github.com/milab-xenos/buvfm/blob/main/src/models/vision_transformer.py"
click node_attention "https://github.com/milab-xenos/buvfm/blob/main/src/models/attentive_pooler.py"
click node_transformer_blocks "https://github.com/milab-xenos/buvfm/blob/main/src/models/utils/modules.py"
click node_patch_embed "https://github.com/milab-xenos/buvfm/blob/main/src/models/utils/patch_embed.py"
click node_pos_embed "https://github.com/milab-xenos/buvfm/blob/main/src/models/utils/pos_embs.py"
click node_video_data "https://github.com/milab-xenos/buvfm/blob/main/src/datasets/video_dataset.py"
click node_data_manager "https://github.com/milab-xenos/buvfm/blob/main/src/datasets/data_manager.py"
click node_image_data "https://github.com/milab-xenos/buvfm/blob/main/src/datasets/imagenet1k.py"
click node_video_transforms "https://github.com/milab-xenos/buvfm/blob/main/src/datasets/utils/video/transforms.py"
click node_app_train "https://github.com/milab-xenos/buvfm/blob/main/pretraining/app/main.py"
click node_pretrain_engine "https://github.com/milab-xenos/buvfm/blob/main/pretraining/app/vjepa/train.py"
click node_eval "https://github.com/milab-xenos/buvfm/blob/main/pretraining/evals/video_classification_frozen/eval.py"
click node_finetune "https://github.com/milab-xenos/buvfm/blob/main/train.py"
click node_inference "https://github.com/milab-xenos/buvfm/blob/main/inference_ddp_old.py"
click node_distill_cls "https://github.com/milab-xenos/buvfm/blob/main/distill/cls/distill.py"
click node_distill_det "https://github.com/milab-xenos/buvfm/blob/main/distill/det/train.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_demo_app,node_stage1,node_stage3,node_pipeline,node_risk_result,node_qc,node_user toneBlue
class node_builder,node_vit,node_attention,node_transformer_blocks,node_patch_embed,node_pos_embed toneAmber
class node_video_data,node_data_manager,node_image_data,node_video_transforms toneMint
class node_app_train,node_pretrain_engine,node_eval,node_finetune,node_inference toneRose
class node_distill_cls,node_distill_det toneIndigo
```

## Components

### Screening and demo

- [Gradio app](https://github.com/milab-xenos/buvfm/blob/main/app.py): `app.py`
- [Lesion screening](https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py): `pipeline_utils.py`
- [Risk grading](https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py): `pipeline_utils.py`
- [Demo orchestrator](https://github.com/milab-xenos/buvfm/blob/main/pipeline_utils.py): `pipeline_utils.py`
- **Risk and heatmaps**
- [Quality screening](https://github.com/milab-xenos/buvfm/blob/main/distill/quality_con.py): `distill/quality_con.py`

### Video models

- [Model builder](https://github.com/milab-xenos/buvfm/blob/main/buildmodel.py): `buildmodel.py`
- [Video transformer](https://github.com/milab-xenos/buvfm/blob/main/src/models/vision_transformer.py): `src/models/vision_transformer.py`
- [Classifier head](https://github.com/milab-xenos/buvfm/blob/main/src/models/attentive_pooler.py): `src/models/attentive_pooler.py`
- [Transformer blocks](https://github.com/milab-xenos/buvfm/blob/main/src/models/utils/modules.py): `src/models/utils/modules.py`
- [Patch embedding](https://github.com/milab-xenos/buvfm/blob/main/src/models/utils/patch_embed.py): `src/models/utils/patch_embed.py`
- [Position embeddings](https://github.com/milab-xenos/buvfm/blob/main/src/models/utils/pos_embs.py): `src/models/utils/pos_embs.py`

### Video data

- [Video dataset](https://github.com/milab-xenos/buvfm/blob/main/src/datasets/video_dataset.py): `src/datasets/video_dataset.py`
- [Data manager](https://github.com/milab-xenos/buvfm/blob/main/src/datasets/data_manager.py): `src/datasets/data_manager.py`
- [ImageNet dataset](https://github.com/milab-xenos/buvfm/blob/main/src/datasets/imagenet1k.py): `src/datasets/imagenet1k.py`
- [Video transforms](https://github.com/milab-xenos/buvfm/blob/main/src/datasets/utils/video/transforms.py): `src/datasets/utils/video/transforms.py`

### Training and evaluation

- [Pretraining entry](https://github.com/milab-xenos/buvfm/blob/main/pretraining/app/main.py): `pretraining/app/main.py`
- [VJEPA trainer](https://github.com/milab-xenos/buvfm/blob/main/pretraining/app/vjepa/train.py): `pretraining/app/vjepa/train.py`
- [Frozen evaluation](https://github.com/milab-xenos/buvfm/blob/main/pretraining/evals/video_classification_frozen/eval.py): `pretraining/evals/video_classification_frozen/eval.py`
- [Downstream fine-tuning](https://github.com/milab-xenos/buvfm/blob/main/train.py): `train.py`
- [Distributed inference](https://github.com/milab-xenos/buvfm/blob/main/inference_ddp_old.py): `inference_ddp_old.py`

### Model distillation

- [Classification distillation](https://github.com/milab-xenos/buvfm/blob/main/distill/cls/distill.py): `distill/cls/distill.py`
- [Detection distillation](https://github.com/milab-xenos/buvfm/blob/main/distill/det/train.py): `distill/det/train.py`

### Other components

- **Video user**

## Connections

- Video user → Gradio app: submits video
- Gradio app → Demo orchestrator: invokes pipeline (evidence: `app.py`)
- Demo orchestrator → Lesion screening: runs screening (evidence: `pipeline_utils.py`)
- Demo orchestrator → Risk grading: runs grading (evidence: `pipeline_utils.py`)
- Risk grading → Model builder: builds classifier (evidence: `pipeline_utils.py`)
- Model builder → Video transformer: constructs backbone (evidence: `buildmodel.py`)
- Model builder → Classifier head: constructs head (evidence: `buildmodel.py`)
- Video transformer → Transformer blocks: uses blocks (evidence: `src/models/vision_transformer.py`)
- Video transformer → Patch embedding: uses embedding (evidence: `src/models/vision_transformer.py`)
- Video transformer → Position embeddings: uses positions (evidence: `src/models/vision_transformer.py`)
- Risk grading → Risk and heatmaps: produces grading
- Video user → Quality screening: screens clips
- Data manager → Video dataset: loads videos (evidence: `src/datasets/data_manager.py`)
- Data manager → ImageNet dataset: loads images (evidence: `src/datasets/data_manager.py`)
- Pretraining entry → VJEPA trainer: launches training (evidence: `pretraining/app/main.py`)
- VJEPA trainer → Data manager: requests batches (evidence: `pretraining/app/vjepa/train.py`)
- VJEPA trainer → Video transforms: uses transforms
- Frozen evaluation → Data manager: requests data (evidence: `pretraining/evals/video_classification_frozen/eval.py`)
- Frozen evaluation → Classifier head: uses classifier (evidence: `pretraining/evals/video_classification_frozen/eval.py`)
- Downstream fine-tuning → Model builder: fine-tunes model
- Distributed inference → Model builder: loads model (evidence: `inference_ddp_old.py`)
- Distributed inference → Video dataset: reads clips
- Classification distillation → Model builder: builds student (evidence: `distill/cls/distill.py`)
- Detection distillation → Model builder: builds detector (evidence: `distill/det/train.py`)
