Image-Text-to-Text
PEFT
Safetensors
English
medgemma
medical-ai
chest-xray
radiology
qlora
spatial-localization
paligemma
conversational
Instructions to use muhammedsayeedurrahman/ExplainMyXray-MedGemma-QLoRA with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use muhammedsayeedurrahman/ExplainMyXray-MedGemma-QLoRA with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("google/medgemma-4b-it") model = PeftModel.from_pretrained(base_model, "muhammedsayeedurrahman/ExplainMyXray-MedGemma-QLoRA") - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
library_name: peft
|
| 3 |
+
base_model: google/medgemma-4b-it
|
| 4 |
+
tags:
|
| 5 |
+
- medgemma
|
| 6 |
+
- medical-ai
|
| 7 |
+
- chest-xray
|
| 8 |
+
- radiology
|
| 9 |
+
- qlora
|
| 10 |
+
- peft
|
| 11 |
+
- spatial-localization
|
| 12 |
+
- paligemma
|
| 13 |
+
license: mit
|
| 14 |
+
datasets:
|
| 15 |
+
- PadChest
|
| 16 |
+
- Indiana-CXR
|
| 17 |
+
language:
|
| 18 |
+
- en
|
| 19 |
+
pipeline_tag: image-text-to-text
|
| 20 |
+
---
|
| 21 |
+
|
| 22 |
+
# ExplainMyXray — MedGemma-4B QLoRA Adapter
|
| 23 |
+
|
| 24 |
+
**AI-powered chest X-ray interpretation with disease localisation.**
|
| 25 |
+
|
| 26 |
+
> Kaggle MedGemma Impact Challenge Submission
|
| 27 |
+
|
| 28 |
+
## Model Description
|
| 29 |
+
|
| 30 |
+
This is a QLoRA adapter for [google/medgemma-4b-it](https://huggingface.co/google/medgemma-4b-it) that adds:
|
| 31 |
+
|
| 32 |
+
1. **Structured Radiology Reports** — FINDINGS, LOCATIONS, and IMPRESSION sections
|
| 33 |
+
2. **Spatial Disease Localisation** — Bounding boxes via PaliGemma `<loc>` tokens
|
| 34 |
+
|
| 35 |
+
### Training
|
| 36 |
+
|
| 37 |
+
| Phase | Dataset | Samples | Description |
|
| 38 |
+
|-------|---------|---------|-------------|
|
| 39 |
+
| Phase 1 | PadChest | 34,614 | Diagnostic text generation |
|
| 40 |
+
| Phase 2 | Indiana CXR | ~200 | Spatial `<loc>` token training with unfrozen `multi_modal_projector` |
|
| 41 |
+
|
| 42 |
+
### Architecture
|
| 43 |
+
|
| 44 |
+
- **Base Model:** google/medgemma-4b-it (PaliGemma: SigLIP + Gemma 3)
|
| 45 |
+
- **Method:** 4-bit QLoRA (NF4, double quantization)
|
| 46 |
+
- **LoRA:** r=32, alpha=64, targets: q_proj, v_proj
|
| 47 |
+
- **Special:** `multi_modal_projector` unfrozen in Phase 2 for geometric reasoning
|
| 48 |
+
|
| 49 |
+
### Results
|
| 50 |
+
|
| 51 |
+
| Metric | Score |
|
| 52 |
+
|--------|-------|
|
| 53 |
+
| Token Accuracy | 84.53% |
|
| 54 |
+
| Zero-Shot Spatial Generalisation | 100% (3 test cases) |
|
| 55 |
+
|
| 56 |
+
## Usage
|
| 57 |
+
|
| 58 |
+
```python
|
| 59 |
+
from transformers import AutoProcessor, BitsAndBytesConfig, PaliGemmaForConditionalGeneration
|
| 60 |
+
from peft import PeftModel
|
| 61 |
+
import torch
|
| 62 |
+
from PIL import Image
|
| 63 |
+
|
| 64 |
+
# Load base model with 4-bit quantization
|
| 65 |
+
bnb_config = BitsAndBytesConfig(
|
| 66 |
+
load_in_4bit=True,
|
| 67 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 68 |
+
bnb_4bit_use_double_quant=True,
|
| 69 |
+
bnb_4bit_quant_type="nf4",
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
base_model = PaliGemmaForConditionalGeneration.from_pretrained(
|
| 73 |
+
"google/medgemma-4b-it",
|
| 74 |
+
quantization_config=bnb_config,
|
| 75 |
+
device_map="auto",
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Load adapter
|
| 79 |
+
model = PeftModel.from_pretrained(base_model, "muhammedsayeedurrahman/ExplainMyXray-MedGemma-QLoRA")
|
| 80 |
+
model.eval()
|
| 81 |
+
|
| 82 |
+
# Load processor
|
| 83 |
+
processor = AutoProcessor.from_pretrained("muhammedsayeedurrahman/ExplainMyXray-MedGemma-QLoRA")
|
| 84 |
+
|
| 85 |
+
# Run inference
|
| 86 |
+
image = Image.open("chest_xray.png").convert("RGB")
|
| 87 |
+
conversation = [
|
| 88 |
+
{"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "Locate abnormalities."}]}
|
| 89 |
+
]
|
| 90 |
+
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
|
| 91 |
+
inputs = processor(text=text_prompt, images=image, return_tensors="pt").to(model.device)
|
| 92 |
+
|
| 93 |
+
with torch.no_grad():
|
| 94 |
+
output_ids = model.generate(**inputs, max_new_tokens=128)
|
| 95 |
+
|
| 96 |
+
generated_ids = output_ids[0][inputs["input_ids"].shape[1]:]
|
| 97 |
+
print(processor.decode(generated_ids, skip_special_tokens=True))
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
## Links
|
| 101 |
+
|
| 102 |
+
- **GitHub:** [ExplainMyXray](https://github.com/muhammedsayeedurrahman/ExplainMyXray)
|
| 103 |
+
- **Base Model:** [google/medgemma-4b-it](https://huggingface.co/google/medgemma-4b-it)
|
| 104 |
+
|
| 105 |
+
## License
|
| 106 |
+
|
| 107 |
+
MIT — For educational and research purposes only. Not intended for clinical diagnostic use.
|