devMuniz02/layer-wise-anatomical-attention

Research code for layer-wise anatomical attention in chest X-ray report generation.

0

stars

80

commits

Python

primary language

Aug 23, 2026

updated

Browse cluster: Medical AI and Chest X-Ray Analysis

README

LAnA

Layer-Wise Anatomical Attention model

Best current model in this collection: manu02/LAnA-Arxiv

ArXiv LinkedIn GitHub Profile Portfolio GitHub Repo Hugging Face

Layer-Wise Anatomical Attention

Overview

LAnA is a medical report-generation project for chest X-ray images. The completed project is intended to generate radiology reports with a vision-language model guided by layer-wise anatomical attention built from predicted anatomical masks.

The architecture combines a DINOv3 vision encoder, lung and heart segmentation heads, and a GPT-2 decoder modified so each transformer layer receives a different anatomical attention bias derived from the segmentation mask.

Intended Use

  • Input: a chest X-ray image resized to 512x512 and normalized with ImageNet mean/std.
  • Output: a generated radiology report.
  • Best fit: research use, report-generation experiments, and anatomical-attention ablations.

How to Run

New users should prefer the standard Hugging Face flow below. The legacy snapshot/manual implementation lives on the snapshot-legacy branch for backward compatibility.

Implementation 1: Standard Hugging Face loading

import torch
from PIL import Image
from transformers import AutoModel, AutoProcessor

repo_id = "manu02/LAnA"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

processor = AutoProcessor.from_pretrained(repo_id, trust_remote_code=True)
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True)
model.move_non_quantized_modules(device)
model.eval()

image = Image.open("example.png").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
inputs = {name: tensor.to(device) for name, tensor in inputs.items()}

with torch.inference_mode():
    generated = model.generate(**inputs, max_new_tokens=150)

report = processor.batch_decode(generated, skip_special_tokens=True)[0]
print(report)

Batched inference uses the same path:

batch = processor(images=[image_a, image_b], return_tensors="pt")
batch = {name: tensor.to(device) for name, tensor in batch.items()}
generated = model.generate(**batch, max_new_tokens=150)
reports = processor.batch_decode(generated, skip_special_tokens=True)

HF_TOKEN is optional for this public standard-loading path. If you do not set one, the model still loads, but Hugging Face may show lower-rate-limit warnings.

Legacy snapshot branch

Use the snapshot/manual branch only if you specifically need the older import-based workflow:

  • Branch: snapshot-legacy
  • Download example: snapshot_download("manu02/LAnA", revision="snapshot-legacy")

Licensing and Redistribution Notice

This checkpoint bundles or derives from Meta DINOv3 model materials. Redistribution of those components must follow the DINOv3 license terms included in this repository. The project code remains available under the repository's own license, but the full packaged checkpoint should not be treated as MIT-only.

Research and Safety Disclaimer

This model is intended for research and educational use only. It is not a medical device, has not been validated for clinical deployment, and should not be used as a substitute for professional radiology review.

MIMIC Test Results

These comparison tables are refreshed across the full LAnA collection whenever any collection model is evaluated.

Cross-Model Comparison: All Frontal Test Studies (3041 studies)

MetricLAnA-MIMIC-CHEXPERTLAnA-MIMICLAnALAnA-v2LAnA-v3LAnA-v4LAnA-v5LAnA-Arxiv
ROUGE-L0.15130.16530.16860.16700.17450.16750.1702``
BLEU-10.17070.19160.20910.21740.23460.22440.2726``
BLEU-40.03570.03860.04170.04170.04840.04410.0503``
METEOR0.20790.22020.22980.20630.21290.20020.2607``
RadGraph F10.09180.09210.10240.10570.09390.07940.0853``
RadGraph entity F10.13990.14590.15870.15690.14410.14370.1481``
RadGraph relation F10.12460.13220.14430.14740.12800.12930.1308``
CheXpert F1 14-micro0.18290.15650.21160.14010.31160.21960.3552``
CheXpert F1 5-micro0.21830.15300.25120.25060.24860.05380.3777``
CheXpert F1 14-macro0.10950.07130.10950.04010.13630.07240.1790``
CheXpert F1 5-macro0.16340.10070.16440.10040.16860.03330.2647``

Cross-Model Comparison: Findings-Only Frontal Test Studies (2210 studies)

MetricLAnA-MIMIC-CHEXPERTLAnA-MIMICLAnALAnA-v2LAnA-v3LAnA-v4LAnA-v5LAnA-Arxiv
ROUGE-L0.15760.17200.17710.17710.18480.17530.1781``
BLEU-10.17540.20030.21770.22630.24800.23370.2774``
BLEU-40.04050.04490.04840.04870.05730.05090.0575``
METEOR0.22070.23470.24660.22400.23100.21370.2760``
RadGraph F10.10100.10000.11190.11810.10460.09060.09380.1831
RadGraph entity F10.15170.15770.17130.17390.15840.15660.15800.1831
RadGraph relation F10.13470.14130.15490.16280.14050.14100.13950.1596
CheXpert F1 14-micro0.16510.14420.19070.13650.29210.22050.31730.3228
CheXpert F1 5-micro0.21520.17160.24150.24550.23940.05550.33720.3745
CheXpert F1 14-macro0.10470.07000.10390.03810.13260.07140.16320.2190
CheXpert F1 5-macro0.16110.11120.15780.09520.16360.03420.23430.3354

Data

  • Full project datasets: CheXpert and MIMIC-CXR.
  • Intended project scope: train on curated chest X-ray/report data from both datasets and evaluate on MIMIC-CXR test studies.
  • Current released checkpoint datasets: MIMIC-CXR (findings-only) for training and MIMIC-CXR (findings-only) for validation.
  • Current published evaluation: MIMIC-CXR test split, frontal-only (PA/AP) studies.

Evaluation

  • Medical report metrics implemented in the repository include RadGraph F1 and CheXpert F1 (14-micro, 5-micro, 14-macro, 5-macro).

Experiment Model Descriptions

  • LAnA-MIMIC-CHEXPERT: This variant was trained on a combined dataset of CheXpert and MIMIC-CXR using LoRA fine-tuning with the AdamW optimizer.
  • LAnA-MIMIC: This model was trained on the MIMIC-CXR (findings-only) dataset using LoRA fine-tuning with the AdamW optimizer.
  • LAnA: This model was trained on the MIMIC-CXR (findings-only) dataset using full-model optimization with AdamW instead of LoRA.
  • LAnA-v2: This version keeps the same training setup as LAnA, but increases the effective global batch size from 16 to 128.
  • LAnA-v3: This version keeps the same training setup as LAnA, including the effective global batch size of 16, but changes how EOS is handled so training and generation follow the same behavior. The model no longer uses the EOS token during training, and generation remained greedy without stopping when an EOS token was produced. In the previous setup, decoding was also greedy, stopped at EOS, and used a maximum of 128 new tokens.
  • LAnA-v4: This version keeps the same decoding behavior as LAnA-v3, but increases the effective global batch size from 16 to 128.
  • LAnA-v5: This version uses the training recipe from the original LAnA paper, while switching to the legacy CXR-Findings-AI generation behavior.
  • LAnA-Arxiv: This model is the report-generation model created in the arXiv paper, packaged locally with its original legacy generation code.

Notes

  • Set HF_TOKEN with permission to access the DINOv3 repositories required by this model before downloading or running inference.
  • segmenters/ contains the lung and heart segmentation checkpoints used to build anatomical attention masks.
  • evaluations/mimic_test_metrics.json contains the latest saved MIMIC test metrics.

Contributors

devMuniz02

80 commits

devMuniz02/layer-wise-anatomical-attention

Research code for layer-wise anatomical attention in chest X-ray report generation.

0

stars

80

commits

Python

primary language

Aug 23, 2026

updated

Browse cluster: Medical AI and Chest X-Ray Analysis

README

LAnA

Layer-Wise Anatomical Attention model

Best current model in this collection: manu02/LAnA-Arxiv

ArXiv LinkedIn GitHub Profile Portfolio GitHub Repo Hugging Face

Layer-Wise Anatomical Attention

Overview

LAnA is a medical report-generation project for chest X-ray images. The completed project is intended to generate radiology reports with a vision-language model guided by layer-wise anatomical attention built from predicted anatomical masks.

The architecture combines a DINOv3 vision encoder, lung and heart segmentation heads, and a GPT-2 decoder modified so each transformer layer receives a different anatomical attention bias derived from the segmentation mask.

Intended Use

  • Input: a chest X-ray image resized to 512x512 and normalized with ImageNet mean/std.
  • Output: a generated radiology report.
  • Best fit: research use, report-generation experiments, and anatomical-attention ablations.

How to Run

New users should prefer the standard Hugging Face flow below. The legacy snapshot/manual implementation lives on the snapshot-legacy branch for backward compatibility.

Implementation 1: Standard Hugging Face loading

import torch
from PIL import Image
from transformers import AutoModel, AutoProcessor

repo_id = "manu02/LAnA"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

processor = AutoProcessor.from_pretrained(repo_id, trust_remote_code=True)
model = AutoModel.from_pretrained(repo_id, trust_remote_code=True)
model.move_non_quantized_modules(device)
model.eval()

image = Image.open("example.png").convert("RGB")
inputs = processor(images=image, return_tensors="pt")
inputs = {name: tensor.to(device) for name, tensor in inputs.items()}

with torch.inference_mode():
    generated = model.generate(**inputs, max_new_tokens=150)

report = processor.batch_decode(generated, skip_special_tokens=True)[0]
print(report)

Batched inference uses the same path:

batch = processor(images=[image_a, image_b], return_tensors="pt")
batch = {name: tensor.to(device) for name, tensor in batch.items()}
generated = model.generate(**batch, max_new_tokens=150)
reports = processor.batch_decode(generated, skip_special_tokens=True)

HF_TOKEN is optional for this public standard-loading path. If you do not set one, the model still loads, but Hugging Face may show lower-rate-limit warnings.

Legacy snapshot branch

Use the snapshot/manual branch only if you specifically need the older import-based workflow:

  • Branch: snapshot-legacy
  • Download example: snapshot_download("manu02/LAnA", revision="snapshot-legacy")

Licensing and Redistribution Notice

This checkpoint bundles or derives from Meta DINOv3 model materials. Redistribution of those components must follow the DINOv3 license terms included in this repository. The project code remains available under the repository's own license, but the full packaged checkpoint should not be treated as MIT-only.

Research and Safety Disclaimer

This model is intended for research and educational use only. It is not a medical device, has not been validated for clinical deployment, and should not be used as a substitute for professional radiology review.

MIMIC Test Results

These comparison tables are refreshed across the full LAnA collection whenever any collection model is evaluated.

Cross-Model Comparison: All Frontal Test Studies (3041 studies)

MetricLAnA-MIMIC-CHEXPERTLAnA-MIMICLAnALAnA-v2LAnA-v3LAnA-v4LAnA-v5LAnA-Arxiv
ROUGE-L0.15130.16530.16860.16700.17450.16750.1702``
BLEU-10.17070.19160.20910.21740.23460.22440.2726``
BLEU-40.03570.03860.04170.04170.04840.04410.0503``
METEOR0.20790.22020.22980.20630.21290.20020.2607``
RadGraph F10.09180.09210.10240.10570.09390.07940.0853``
RadGraph entity F10.13990.14590.15870.15690.14410.14370.1481``
RadGraph relation F10.12460.13220.14430.14740.12800.12930.1308``
CheXpert F1 14-micro0.18290.15650.21160.14010.31160.21960.3552``
CheXpert F1 5-micro0.21830.15300.25120.25060.24860.05380.3777``
CheXpert F1 14-macro0.10950.07130.10950.04010.13630.07240.1790``
CheXpert F1 5-macro0.16340.10070.16440.10040.16860.03330.2647``

Cross-Model Comparison: Findings-Only Frontal Test Studies (2210 studies)

MetricLAnA-MIMIC-CHEXPERTLAnA-MIMICLAnALAnA-v2LAnA-v3LAnA-v4LAnA-v5LAnA-Arxiv
ROUGE-L0.15760.17200.17710.17710.18480.17530.1781``
BLEU-10.17540.20030.21770.22630.24800.23370.2774``
BLEU-40.04050.04490.04840.04870.05730.05090.0575``
METEOR0.22070.23470.24660.22400.23100.21370.2760``
RadGraph F10.10100.10000.11190.11810.10460.09060.09380.1831
RadGraph entity F10.15170.15770.17130.17390.15840.15660.15800.1831
RadGraph relation F10.13470.14130.15490.16280.14050.14100.13950.1596
CheXpert F1 14-micro0.16510.14420.19070.13650.29210.22050.31730.3228
CheXpert F1 5-micro0.21520.17160.24150.24550.23940.05550.33720.3745
CheXpert F1 14-macro0.10470.07000.10390.03810.13260.07140.16320.2190
CheXpert F1 5-macro0.16110.11120.15780.09520.16360.03420.23430.3354

Data

  • Full project datasets: CheXpert and MIMIC-CXR.
  • Intended project scope: train on curated chest X-ray/report data from both datasets and evaluate on MIMIC-CXR test studies.
  • Current released checkpoint datasets: MIMIC-CXR (findings-only) for training and MIMIC-CXR (findings-only) for validation.
  • Current published evaluation: MIMIC-CXR test split, frontal-only (PA/AP) studies.

Evaluation

  • Medical report metrics implemented in the repository include RadGraph F1 and CheXpert F1 (14-micro, 5-micro, 14-macro, 5-macro).

Experiment Model Descriptions

  • LAnA-MIMIC-CHEXPERT: This variant was trained on a combined dataset of CheXpert and MIMIC-CXR using LoRA fine-tuning with the AdamW optimizer.
  • LAnA-MIMIC: This model was trained on the MIMIC-CXR (findings-only) dataset using LoRA fine-tuning with the AdamW optimizer.
  • LAnA: This model was trained on the MIMIC-CXR (findings-only) dataset using full-model optimization with AdamW instead of LoRA.
  • LAnA-v2: This version keeps the same training setup as LAnA, but increases the effective global batch size from 16 to 128.
  • LAnA-v3: This version keeps the same training setup as LAnA, including the effective global batch size of 16, but changes how EOS is handled so training and generation follow the same behavior. The model no longer uses the EOS token during training, and generation remained greedy without stopping when an EOS token was produced. In the previous setup, decoding was also greedy, stopped at EOS, and used a maximum of 128 new tokens.
  • LAnA-v4: This version keeps the same decoding behavior as LAnA-v3, but increases the effective global batch size from 16 to 128.
  • LAnA-v5: This version uses the training recipe from the original LAnA paper, while switching to the legacy CXR-Findings-AI generation behavior.
  • LAnA-Arxiv: This model is the report-generation model created in the arXiv paper, packaged locally with its original legacy generation code.

Notes

  • Set HF_TOKEN with permission to access the DINOv3 repositories required by this model before downloading or running inference.
  • segmenters/ contains the lung and heart segmentation checkpoints used to build anatomical attention masks.
  • evaluations/mimic_test_metrics.json contains the latest saved MIMIC test metrics.

Contributors

devMuniz02

80 commits

Languages

Python

100.0%