# 0x-nan/ruralmedvision architecture

> Architecture diagram of the GitHub repository 0x-nan/ruralmedvision, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/0x-nan/ruralmedvision
- Repository: https://github.com/0x-nan/ruralmedvision

## Overview

1. **Core purpose**
   RuralMed Vision is a research pipeline for offline skin-lesion triage. The central architecture is a multimodal model that takes a dermoscopy image plus symptom text and emits a structured JSON-like prediction with `condition`, `severity`, and `dx_code`.

2. **Main model boundary**
   The primary inference unit is **Qwen2.5-VL-7B-Instruct** with a **QLoRA adapter** trained on the project data. The base model is not modified directly during training; instead, a lightweight LoRA adapter is learned and later merged for deployment.

3. **Data sources and input preparation**
   Training/evaluation data comes from two distinct sources:
   - **HAM10000** for lesion images and labels
   - **HealthCareMagic-100k** for symptom-style clinical text
   `src/data_prep.py` is the preprocessing boundary that aligns these modalities into training-ready examples. The repo stores HAM10000 metadata locally in `data/HAM10000_metadata.csv`; images are expected to be downloaded separately.

4. **Training subsystem**
   `src/train.py` implements the fine-tuning loop. It uses **QLoRA** with 4-bit NF4 quantization, targeting attention and MLP projection modules. This is the subsystem that learns the adapter weights under constrained GPU memory (the README targets an 8GB-class consumer GPU).

5. **Evaluation subsystem**
   `src/evaluate.py` and `RuralMed_Evaluation.ipynb` form the evaluation boundary. The notebook is the authoritative source of reported metrics and handles:
   - baseline vs fine-tuned comparison
   - confusion matrix / per-class breakdown
   - calibration analysis
   - metrics stored in JSON artifacts such as `metrics.json`, `baseline_results.json`, `ft_results.json`, and `ablation_results.json`

6. **Inference subsystem**
   `src/inference.py` is the runtime path for loading the multimodal model and producing predictions from image + text inputs. It also contains the repo’s uncertainty-related logic, but the README explicitly notes that the MC-dropout implementation is currently broken and not used for reported results.

7. **Deployment/edge packaging path**
   The trained adapter is not the final deployable artifact. The deployment flow is:
   - merge LoRA adapter into the base model via `merge_weights.py`
   - export/quantize via `src/quantize.py`
   - produce a **GGUF** artifact for offline execution with **llama.cpp**
   This is the edge-deployment boundary: HF-format training artifacts become a compact offline model suitable for local inference.

8. **Model artifact layout**
   `models/ruralmed_adapter/` contains the adapter-side configuration and tokenizer/preprocessor files needed to reproduce training or load the adapter. The actual adapter weights are distributed separately via GitHub Releases, not committed to the repo. The merged/quantized artifacts are also generated outside this folder.

9. **Configuration and reproducibility**
   `configs/config.example.yaml` is the template for machine-specific paths and runtime settings. The repo expects a local `configs/config.yaml` (gitignored). This keeps data locations, model paths, and training parameters outside source control.

10. **Metrics and experiment outputs**
    The JSON files in the repo root are experiment outputs rather than code:
    - `baseline_results.json` and `ft_results.json` capture per-sample predictions
    - `ablation_results.json` captures modality comparisons
    - `metrics.json` captures aggregate evaluation statistics
    These files support reproducibility and make the evaluation notebook self-contained.

11. **Explainability boundary**
    The project’s visual explanation story is intentionally split:
    - the **occlusion saliency** map is tied to the triage model and is the faithful component
    - the textual rationale shown in the README figure is generated by a separate model and is post-hoc
    This means the explainability pipeline is not a single integrated model feature; it is a decoupled analysis layer around the triage system.

12. **External services and tooling that matter architecturally**
    The repository depends on:
    - **Hugging Face** model assets for the Qwen base model
    - **Kaggle** for HAM10000 acquisition
    - **llama.cpp** for GGUF conversion/offline inference
    - **GitHub Releases** for distributing large adapter/quantized artifacts
    These are not just conveniences; they define where large artifacts live and how the offline deployment path is assembled.

13. **What is not implemented as architecture**
    The README calls out federated LoRA aggregation as a future design idea, not working code. So for an architecture diagram, it should not be drawn as an active subsystem in this repo.

14. **Suggested high-level flow**
    The repo’s real end-to-end flow is: external datasets → preprocessing → QLoRA fine-tuning → evaluation notebook/metrics → adapter merge → quantization to GGUF → offline inference. The stable boundaries are data prep, training, evaluation, and deployment packaging.

Main entry points: [`src/train.py`](https://github.com/0x-nan/ruralmedvision/blob/master/src/train.py) (QLoRA train), [`src/data_prep.py`](https://github.com/0x-nan/ruralmedvision/blob/master/src/data_prep.py) (Data prep), [`src/inference.py`](https://github.com/0x-nan/ruralmedvision/blob/master/src/inference.py) (Inference), [`configs/config.example.yaml`](https://github.com/0x-nan/ruralmedvision/blob/master/configs/config.example.yaml) (Config template)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_data["Data"]
  node_node_ham10000[("HAM10000<br/>dataset")]
  node_node_healthcaremagic[("HealthCareMagic<br/>text dataset")]
end

subgraph group_group_core["Core pipeline"]
  node_node_data_prep["Data prep<br/>preprocess<br/>[data_prep.py]"]
  node_node_train["QLoRA train<br/>fine-tune<br/>[train.py]"]
  node_node_base_model{{"Qwen VL<br/>base model"}}
  node_node_inference["Inference<br/>runtime<br/>[inference.py]"]
  node_node_evaluate["Evaluation<br/>metrics<br/>[evaluate.py]"]
  node_node_notebook["Eval notebook<br/>analysis"]
  node_node_occ["Occlusion saliency<br/>explainability"]
end

subgraph group_group_deploy["Deployment"]
  node_node_merge["Merge weights<br/>packaging<br/>[merge_weights.py]"]
  node_node_quantize["Quantize<br/>export<br/>[quantize.py]"]
  node_node_gguf[("GGUF model<br/>offline artifact")]
end

subgraph group_group_artifacts["Artifacts"]
  node_node_adapter["QLoRA adapter"]
  node_node_metrics["Metrics JSON<br/>results<br/>[metrics.json]"]
  node_node_results["Run outputs<br/>results"]
end

subgraph group_group_config["Config"]
  node_node_config["Config template<br/>settings"]
end

node_node_ham10000 -->|"images + labels"| node_node_data_prep
node_node_healthcaremagic -->|"symptom text"| node_node_data_prep
node_node_data_prep -->|"training set"| node_node_train
node_node_base_model -->|"frozen base"| node_node_train
node_node_train -->|"learned adapter"| node_node_adapter
node_node_adapter -->|"loaded at runtime"| node_node_inference
node_node_base_model -->|"foundation"| node_node_inference
node_node_inference -.->|"image attribution"| node_node_occ
node_node_train -->|"candidate model"| node_node_evaluate
node_node_base_model -->|"baseline"| node_node_evaluate
node_node_evaluate -->|"reported via"| node_node_notebook
node_node_notebook -->|"stores"| node_node_results
node_node_notebook -->|"stores"| node_node_metrics
node_node_adapter -->|"adapter weights"| node_node_merge
node_node_merge -->|"merged model"| node_node_quantize
node_node_quantize -->|"offline artifact"| node_node_gguf
node_node_config -.->|"paths"| node_node_data_prep
node_node_config -.->|"runtime settings"| node_node_train
node_node_config -.->|"runtime settings"| node_node_inference

click node_node_ham10000 "https://github.com/0x-nan/ruralmedvision/blob/master/data/HAM10000_metadata.csv"
click node_node_data_prep "https://github.com/0x-nan/ruralmedvision/blob/master/src/data_prep.py"
click node_node_train "https://github.com/0x-nan/ruralmedvision/blob/master/src/train.py"
click node_node_adapter "https://github.com/0x-nan/ruralmedvision/tree/master/models/ruralmed_adapter"
click node_node_inference "https://github.com/0x-nan/ruralmedvision/blob/master/src/inference.py"
click node_node_evaluate "https://github.com/0x-nan/ruralmedvision/blob/master/src/evaluate.py"
click node_node_notebook "https://github.com/0x-nan/ruralmedvision/blob/master/RuralMed_Evaluation.ipynb"
click node_node_merge "https://github.com/0x-nan/ruralmedvision/blob/master/merge_weights.py"
click node_node_quantize "https://github.com/0x-nan/ruralmedvision/blob/master/src/quantize.py"
click node_node_config "https://github.com/0x-nan/ruralmedvision/blob/master/configs/config.example.yaml"
click node_node_metrics "https://github.com/0x-nan/ruralmedvision/blob/master/metrics.json"
click node_node_results "https://github.com/0x-nan/ruralmedvision/blob/master/baseline_results.json"

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_node_ham10000,node_node_healthcaremagic toneBlue
class node_node_data_prep,node_node_train,node_node_base_model,node_node_inference,node_node_evaluate,node_node_notebook,node_node_occ toneAmber
class node_node_merge,node_node_quantize,node_node_gguf toneMint
class node_node_adapter,node_node_metrics,node_node_results toneRose
class node_node_config toneIndigo
```

## Components

### Data

External datasets and local metadata used to build training examples.

- [HAM10000](https://github.com/0x-nan/ruralmedvision/blob/master/data/HAM10000_metadata.csv): dataset · Dermoscopy images and lesion labels used for training and evaluation. · `data/HAM10000_metadata.csv`
- **HealthCareMagic**: text dataset · Clinical symptom-style text paired with image examples during preprocessing.

### Core pipeline

Preprocessing, training, inference, and evaluation around the multimodal triage model.

- [Data prep](https://github.com/0x-nan/ruralmedvision/blob/master/src/data_prep.py): preprocess · Aligns image and text modalities into training-ready examples. · `src/data_prep.py`
- [QLoRA train](https://github.com/0x-nan/ruralmedvision/blob/master/src/train.py): fine-tune · Runs 4-bit QLoRA fine-tuning for the Qwen2.5-VL base model. · `src/train.py`
- **Qwen VL**: base model · Multimodal foundation model used for triage inference and adapter training.
- [Inference](https://github.com/0x-nan/ruralmedvision/blob/master/src/inference.py): runtime · Loads the multimodal model and emits structured predictions from image plus text. · `src/inference.py`
- [Evaluation](https://github.com/0x-nan/ruralmedvision/blob/master/src/evaluate.py): metrics · Computes baseline vs fine-tuned metrics, calibration, and per-class breakdowns. · `src/evaluate.py`
- [Eval notebook](https://github.com/0x-nan/ruralmedvision/blob/master/RuralMed_Evaluation.ipynb): analysis · Authoritative evaluation workflow and reported results. · `RuralMed_Evaluation.ipynb`
- **Occlusion saliency**: explainability · Faithful visual explanation tied to the triage model.

### Deployment

Artifact conversion and offline packaging for local execution.

- [Merge weights](https://github.com/0x-nan/ruralmedvision/blob/master/merge_weights.py): packaging · Combines the adapter with the base model before export. · `merge_weights.py`
- [Quantize](https://github.com/0x-nan/ruralmedvision/blob/master/src/quantize.py): export · Converts the merged model into compact offline-friendly artifacts. · `src/quantize.py`
- **GGUF model**: offline artifact · Compact model format used for llama.cpp-based local execution.

### Artifacts

Stored model assets and experiment outputs that support reproducibility.

- [QLoRA adapter](https://github.com/0x-nan/ruralmedvision/tree/master/models/ruralmed_adapter): adapter · Lightweight learned weights merged later for deployment. · `models/ruralmed_adapter`
- [Metrics JSON](https://github.com/0x-nan/ruralmedvision/blob/master/metrics.json): results · Aggregate evaluation statistics and per-run outputs stored as JSON. · `metrics.json`
- [Run outputs](https://github.com/0x-nan/ruralmedvision/blob/master/baseline_results.json): results · Baseline, fine-tuned, and ablation prediction artifacts. · `baseline_results.json`

### Config

Machine-specific paths and runtime settings kept outside source control.

- [Config template](https://github.com/0x-nan/ruralmedvision/blob/master/configs/config.example.yaml): settings · Template for local paths, model locations, and runtime parameters. · `configs/config.example.yaml`

## Connections

- HAM10000 → Data prep: images + labels · Provides lesion metadata and image-side supervision for preprocessing.
- HealthCareMagic → Data prep: symptom text · Provides clinical language used to enrich triage examples.
- Data prep → QLoRA train: training set · Produces aligned multimodal examples for adapter fine-tuning.
- Qwen VL → QLoRA train: frozen base · Base model is kept intact while QLoRA learns adapter weights.
- QLoRA train → QLoRA adapter: learned adapter · Outputs the trained QLoRA adapter and associated processor assets.
- QLoRA adapter → Inference: loaded at runtime · Adapter is used with the base model for multimodal prediction.
- Qwen VL → Inference: foundation · Inference starts from the pretrained Qwen multimodal model.
- Inference → Occlusion saliency: image attribution · Occlusion saliency is the faithful explanation path around triage predictions.
- QLoRA train → Evaluation: candidate model · Evaluation compares fine-tuned checkpoints against baseline behavior.
- Qwen VL → Evaluation: baseline · Baseline results are computed from the unadapted model.
- Evaluation → Eval notebook: reported via · The notebook is the authoritative analysis and reporting boundary.
- Eval notebook → Run outputs: stores · Captures baseline, fine-tuned, and ablation per-sample outputs.
- Eval notebook → Metrics JSON: stores · Captures aggregate statistics and calibration-oriented metrics.
- QLoRA adapter → Merge weights: adapter weights · Merged into the base model for deployment packaging.
- Merge weights → Quantize: merged model · Merged checkpoint is prepared for compact export.
- Quantize → GGUF model: offline artifact · Produces GGUF for llama.cpp-compatible local inference.
- Config template → Data prep: paths · Supplies dataset and filesystem locations.
- Config template → QLoRA train: runtime settings · Supplies model, memory, and training parameters.
- Config template → Inference: runtime settings · Supplies inference-time paths and loading options.
