Views
No views yet
1pip install opencv-python
2pip install albumentations
3pip install accelerate
4torch==2.2.1
5transformers==4.39.0 # may work with more recent version1import io
2import requests
3import torch
4from PIL import Image
5from transformers import AutoModelForCausalLM, AutoTokenizer
6import tempfile
7
8# step 1: Setup constants
9model_name = "StanfordAIMI/CheXagent-2-3b-srrg-findings"
10dtype = torch.bfloat16
11device = "cuda"
12
13# step 2: Load Processor and Model
14tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
15model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", trust_remote_code=True)
16model = model.to(dtype)
17model.eval()
18
19# step 3: Download image from URL, save to a local file, and prepare path list
20url = "https://huggingface.co/IAMJB/interpret-cxr-impression-baseline/resolve/main/effusions-bibasal.jpg"
21resp = requests.get(url)
22resp.raise_for_status()
23
24# Use a NamedTemporaryFile so it lives on disk
25with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmpfile:
26 tmpfile.write(resp.content)
27 local_path = tmpfile.name # this is a real file path on disk
28
29paths = [local_path]
30
31prompt = "Structured Radiology Report Generation for Findings Section"
32# build the multimodal input
33query = tokenizer.from_list_format(
34 [*([{"image": img} for img in paths]), {"text": prompt}]
35)
36
37# format as a chat conversation
38conv = [
39 {"from": "system", "value": "You are a helpful assistant."},
40 {"from": "human", "value": query},
41]
42
43# tokenize and generate
44input_ids = tokenizer.apply_chat_template(
45 conv, add_generation_prompt=True, return_tensors="pt"
46)
47output = model.generate(
48 input_ids.to(device),
49 do_sample=False,
50 num_beams=1,
51 temperature=1.0,
52 top_p=1.0,
53 use_cache=True,
54 max_new_tokens=512,
55)[0]
56
57# decode the “findings” text
58response = tokenizer.decode(output[input_ids.size(1) : -1])
59print(response)Lungs and Airways:
- No evidence of pneumothorax.
Pleura:
- Bilateral pleural effusions.
Cardiovascular:
- Cardiomegaly.
Other:
- Bibasilar opacities.
- Mild pulmonary edema.@inproceedings{delbrouck-etal-2025-automated,
title = "Automated Structured Radiology Report Generation",
author = "Delbrouck, Jean-Benoit and
Xu, Justin and
Moll, Johannes and
Thomas, Alois and
Chen, Zhihong and
Ostmeier, Sophie and
Azhar, Asfandyar and
Li, Kelvin Zhenghao and
Johnston, Andrew and
Bluethgen, Christian and
Reis, Eduardo Pontes and
Muneer, Mohamed S and
Varma, Maya and
Langlotz, Curtis",
editor = "Che, Wanxiang and
Nabende, Joyce and
Shutova, Ekaterina and
Pilehvar, Mohammad Taher",
booktitle = "Proceedings of the 63rd Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)",
month = jul,
year = "2025",
address = "Vienna, Austria",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2025.acl-long.1301/",
doi = "10.18653/v1/2025.acl-long.1301",
pages = "26813--26829",
ISBN = "979-8-89176-251-0",
abstract = "Automated radiology report generation from chest X-ray (CXR) images has the potential to improve clinical efficiency and reduce radiologists' workload. However, most datasets, including the publicly available MIMIC-CXR and CheXpert Plus, consist entirely of free-form reports, which are inherently variable and unstructured. This variability poses challenges for both generation and evaluation: existing models struggle to produce consistent, clinically meaningful reports, and standard evaluation metrics fail to capture the nuances of radiological interpretation. To address this, we introduce Structured Radiology Report Generation (SRRG), a new task that reformulates free-text radiology reports into a standardized format, ensuring clarity, consistency, and structured clinical reporting. We create a novel dataset by restructuring reports using large language models (LLMs) following strict structured reporting desiderata. Additionally, we introduce SRR-BERT, a fine-grained disease classification model trained on 55 labels, enabling more precise and clinically informed evaluation of structured reports. To assess report quality, we propose F1-SRR-BERT, a metric that leverages SRR-BERT{'}s hierarchical disease taxonomy to bridge the gap between free-text variability and structured clinical reporting. We validate our dataset through a reader study conducted by five board-certified radiologists and extensive benchmarking experiments."
}