How 0x-nan/ruralmedvision works
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: train.py QLoRA train · data_prep.py Data prep · inference.py Inference · config.example.yaml Config template
Components 16
Data
External datasets and local metadata used to build training examples.
- HAM10000 · Dermoscopy images and lesion labels used for training and evaluation.
data/HAM10000_metadata.csv - HealthCareMagic · Clinical symptom-style text paired with image examples during preprocessing.
Core pipeline
Preprocessing, training, inference, and evaluation around the multimodal triage model.
- Data prep · Aligns image and text modalities into training-ready examples.
src/data_prep.py - QLoRA train · Runs 4-bit QLoRA fine-tuning for the Qwen2.5-VL base model.
src/train.py - Qwen VL · Multimodal foundation model used for triage inference and adapter training.
- Inference · Loads the multimodal model and emits structured predictions from image plus text.
src/inference.py - Evaluation · Computes baseline vs fine-tuned metrics, calibration, and per-class breakdowns.
src/evaluate.py - Eval notebook · Authoritative evaluation workflow and reported results.
RuralMed_Evaluation.ipynb - Occlusion saliency · Faithful visual explanation tied to the triage model.
Deployment
Artifact conversion and offline packaging for local execution.
- Merge weights · Combines the adapter with the base model before export.
merge_weights.py - Quantize · Converts the merged model into compact offline-friendly artifacts.
src/quantize.py - GGUF model · Compact model format used for llama.cpp-based local execution.
Artifacts
Stored model assets and experiment outputs that support reproducibility.
- QLoRA adapter · Lightweight learned weights merged later for deployment.
models/ruralmed_adapter - Metrics JSON · Aggregate evaluation statistics and per-run outputs stored as JSON.
metrics.json - Run outputs · Baseline, fine-tuned, and ablation prediction artifacts.
baseline_results.json
Config
Machine-specific paths and runtime settings kept outside source control.
- Config template · Template for local paths, model locations, and runtime parameters.
configs/config.example.yaml
Connections 19
- 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.