模型简介
模型特点
模型能力
使用案例
🚀 MAIRA - 2
MAIRA - 2是一款多模态变换器模型,专为从胸部X光片生成有根据或无根据的放射学报告而设计。它能助力医学研究人员进行相关研究,推动放射学报告生成领域的发展。
🚀 快速开始
我们将展示如何使用MAIRA - 2进行三种功能的推理:有根据或无根据的检查结果生成,以及短语定位。
环境设置
要运行以下示例代码,你需要安装以下包:
pillow
protobuf
sentencepiece
torch
transformers
注意:由于MAIRA - 2需要transformers>=4.46.0.dev0
,你可能需要暂时从源代码安装transformers。由于transformers主分支中的不兼容提交,当前的解决方法是安装提交88d960937c81a32bfb63356a2e8ecf7999619681之后但在提交0f49deacbff3e57cde45222842c0db6375e4fa43之前的transformers版本。
pip install git+https://github.com/huggingface/transformers.git@88d960937c81a32bfb63356a2e8ecf7999619681
首先,初始化模型并将其设置为评估模式。
from transformers import AutoModelForCausalLM, AutoProcessor
from pathlib import Path
import torch
model = AutoModelForCausalLM.from_pretrained("microsoft/maira-2", trust_remote_code=True)
processor = AutoProcessor.from_pretrained("microsoft/maira-2", trust_remote_code=True)
device = torch.device("cuda")
model = model.eval()
model = model.to(device)
我们需要获取一些数据来演示前向传播。 在这个示例中,我们将从IU X射线数据集收集一个示例,该数据集具有宽松的许可证。
import requests
from PIL import Image
def get_sample_data() -> dict[str, Image.Image | str]:
"""
Download chest X-rays from IU-Xray, which we didn't train MAIRA-2 on. License is CC.
We modified this function from the Rad-DINO repository on Huggingface.
"""
frontal_image_url = "https://openi.nlm.nih.gov/imgs/512/145/145/CXR145_IM-0290-1001.png"
lateral_image_url = "https://openi.nlm.nih.gov/imgs/512/145/145/CXR145_IM-0290-2001.png"
def download_and_open(url: str) -> Image.Image:
response = requests.get(url, headers={"User-Agent": "MAIRA-2"}, stream=True)
return Image.open(response.raw)
frontal_image = download_and_open(frontal_image_url)
lateral_image = download_and_open(lateral_image_url)
sample_data = {
"frontal": frontal_image,
"lateral": lateral_image,
"indication": "Dyspnea.",
"comparison": "None.",
"technique": "PA and lateral views of the chest.",
"phrase": "Pleural effusion." # For the phrase grounding example. This patient has pleural effusion.
}
return sample_data
sample_data = get_sample_data()
使用案例1和2:有根据或无根据的检查结果生成
我们可以根据输入的预处理方式来切换MAIRA - 2是否生成有根据的报告,因为它使用不同的提示。让我们从无根据的报告开始(get_grounding=False
)。在生成时,对于无根据的报告使用max_new_tokens = 300
,对于有根据的报告使用max_new_tokens = 450
以容纳额外的框和对象标记。
processed_inputs = processor.format_and_preprocess_reporting_input(
current_frontal=sample_data["frontal"],
current_lateral=sample_data["lateral"],
prior_frontal=None, # Our example has no prior
indication=sample_data["indication"],
technique=sample_data["technique"],
comparison=sample_data["comparison"],
prior_report=None, # Our example has no prior
return_tensors="pt",
get_grounding=False, # For this example we generate a non-grounded report
)
processed_inputs = processed_inputs.to(device)
with torch.no_grad():
output_decoding = model.generate(
**processed_inputs,
max_new_tokens=300, # Set to 450 for grounded reporting
use_cache=True,
)
prompt_length = processed_inputs["input_ids"].shape[-1]
decoded_text = processor.decode(output_decoding[0][prompt_length:], skip_special_tokens=True)
decoded_text = decoded_text.lstrip() # Findings generation completions have a single leading space
prediction = processor.convert_output_to_plaintext_or_grounded_sequence(decoded_text)
print("Parsed prediction:", prediction)
我们会得到类似这样的结果:
右侧有大量胸腔积液,并伴有右肺基底段肺不张。左肺清晰。未发现气胸。心脏纵隔轮廓和肺门轮廓正常。膈下无游离气体。腹部右上象限可见手术夹。
如果我们将get_grounding
设置为True
,MAIRA - 2将生成有根据的报告。在这个示例中,结果如下:
('There is a large right pleural effusion.', [(0.055, 0.275, 0.445, 0.665)]),
('The left lung is clear.', None),
('No pneumothorax is identified.', None),
('The cardiomediastinal silhouette is within normal limits.', None),
('The visualized osseous structures are unremarkable.', None)
生成的边界框坐标是框的左上角和右下角的(x, y)
坐标,例如(x_topleft, y_topleft, x_bottomright, y_bottomright)
。这些坐标是相对于_裁剪后的_图像(即MAIRA - 2最终作为输入的图像)的,因此在可视化时要小心。处理器提供了一个方法adjust_box_for_original_image_size
来获取相对于原始图像形状的框。
请注意,由于其有根据的报告训练数据来自不同的数据分布,MAIRA - 2在有根据和无根据的报告场景中生成的报告略有不同。
使用案例3:短语定位
这里的输入不同,因为我们为模型提供了一个要在图像中定位的短语。回想一下(get_sample_data
),我们这里的短语是“Pleural effusion”,我们已经知道这个图像中存在这个情况。
processed_inputs = processor.format_and_preprocess_phrase_grounding_input(
frontal_image=sample_data["frontal"],
phrase=sample_data["phrase"],
return_tensors="pt",
)
processed_inputs = processed_inputs.to(device)
with torch.no_grad():
output_decoding = model.generate(
**processed_inputs,
max_new_tokens=150,
use_cache=True,
)
prompt_length = processed_inputs["input_ids"].shape[-1]
decoded_text = processor.decode(output_decoding[0][prompt_length:], skip_special_tokens=True)
prediction = processor.convert_output_to_plaintext_or_grounded_sequence(decoded_text)
print("Parsed prediction:", prediction)
这会给我们类似这样的结果:
('Pleural effusion.', [(0.025, 0.345, 0.425, 0.575)])
同样,对于有根据的报告,我们必须记住边界框坐标是相对于MAIRA - 2看到的裁剪后的图像的,使用processor.adjust_box_for_original_image_size
来获取调整为原始图像形状的框。
✨ 主要特性
- 多模态设计:结合图像编码器、投影层和语言模型,实现从胸部X光片生成放射学报告。
- 多种输出形式:可以生成无根据的叙述性文本报告,也能生成有根据的报告,对描述的发现提供边界框定位。
- 短语定位功能:能够根据输入的短语在图像中定位相关发现。
📚 详细文档
模型详情
模型描述
MAIRA - 2由图像编码器RAD - DINO - MAIRA - 2(冻结使用)、投影层(从头开始训练)和语言模型vicuna - 7b - v1.5(完全微调)组成。
- 开发者:Microsoft Research Health Futures
- 模型类型:多模态变换器
- 自然语言处理语言:英语
- 许可证:MSRLA
- 微调基础模型(可选):vicuna - 7b - 1.5,RAD - DINO - MAIRA - 2
使用场景
MAIRA - 2仅用于研究目的,不应用于临床实践。MAIRA - 2的能力和特性,包括其在应用场景中的准确性和可靠性、在不同人群和用途中的公平性以及安全性和隐私性,尚未经过广泛测试。
直接使用
MAIRA - 2的输入包括一张胸部正位X光片,以及以下任意一项:
- 当前检查的侧位片
- 先前检查的正位片及相关的先前报告
- 当前检查的指征
- 当前检查的技术和对比部分
MAIRA - 2可以以以下两种形式生成当前检查的_检查结果_部分:
- 无任何图像注释的叙述性文本(这是典型的报告生成场景)。
- 有根据的报告,其中所有描述的发现都伴有零个或多个边界框,指示它们在当前正位图像上的位置。
MAIRA - 2还可以执行短语定位。在这种情况下,还必须为其提供一个输入短语。然后它将重复该短语并生成一个边界框,定位短语中描述的发现。
这些使用案例在下面的示例代码中进行了说明。
超出适用范围的使用
MAIRA - 2仅在来自成年人的英语胸部X光片报告数据集上进行了训练,预计在其他成像模式或解剖部位上无法正常工作。输入提示的变化(例如更改指令)可能会降低性能,因为该模型未针对任意用户输入进行优化。
如前所述,这是一个研究模型,不应在任何实际临床或生产场景中使用。
偏差、风险和局限性
数据偏差
MAIRA - 2在来自西班牙(从原始西班牙语翻译成英语)和美国的胸部X光片报告数据集上进行了训练,如下所列。不同卫生系统和地区的报告风格、患者人口统计学和疾病流行率以及图像采集协议可能会有所不同。这些因素将影响模型的泛化能力。
模型误差(虚构、遗漏)
如MAIRA - 2报告中更详细的概述,该模型在其任务上并非完美执行。因此,生成的(有根据的)报告中可能存在误差。
训练详情
我们最初并非使用此处提供的确切模型类来训练MAIRA - 2,但我们已检查其行为是相同的。我们提供这个类是为了便于研究复用和推理。
训练数据
MAIRA - 2在公共和私有胸部X光片数据集的混合数据上进行训练。每个示例包含一张或多张胸部X光图像以及相关的报告文本,有或没有定位(空间注释)。模型被训练为生成报告的_检查结果_部分,有或没有定位。
数据集 | 国家 | 无根据示例数量 | 有根据示例数量 |
---|---|---|---|
MIMIC - CXR | 美国 | 55218 | 595* |
PadChest | 西班牙 | 52828 | 3122 |
USMix(私有) | 美国 | 118031 | 53613 |
*我们使用MS - CXR短语定位数据集为MIMIC - CXR提供定位
示例。
环境影响
可以使用Lacoste等人(2019)中介绍的机器学习影响计算器来估算碳排放。
- 硬件类型:NVIDIA A100 GPUs
- 使用时长:1432小时
- 云服务提供商:Azure
- 计算区域:美国西部2
- 碳排放:107.4 CO₂ eq (表面上由该提供商抵消)
引用
如果你使用了MAIRA - 2模型,可以按照以下格式进行引用:
BibTeX:
@article{Bannur2024MAIRA2GR,
title={MAIRA-2: Grounded Radiology Report Generation},
author={Shruthi Bannur and Kenza Bouzid and Daniel C. Castro and Anton Schwaighofer and Anja Thieme and Sam Bond-Taylor and Maximilian Ilse and Fernando P\'{e}rez-Garc\'{i}a and Valentina Salvatelli and Harshita Sharma and Felix Meissen and Mercy Prasanna Ranjit and Shaury Srivastav and Julia Gong and Noel C. F. Codella and Fabian Falck and Ozan Oktay and Matthew P. Lungren and Maria T. A. Wetscherek and Javier Alvarez-Valle and Stephanie L. Hyland},
journal={arXiv},
year={2024},
volume={abs/2406.04449},
url={https://arxiv.org/abs/2406.04449}
}
APA:
Bannur*, S., Bouzid*, K., Castro, D. C., Schwaighofer, A., Thieme, A., Bond-Taylor, S., Ilse, M., Pérez-García, F., Salvatelli, V., Sharma, H., Meissen, F., Ranjit, M.P., Srivastav, S., Gong, J., Codella, N.C.F., Falck, F., Oktay, O., Lungren, M.P., Wetscherek, M.T., Alvarez-Valle, J., & Hyland, S. L. (2024). MAIRA-2: Grounded Radiology Report Generation. arXiv preprint abs/2406.04449.
模型卡片联系人
- Stephanie Hyland (
stephanie.hyland@microsoft.com
) - Shruthi Bannur (
shruthi.bannur@microsoft.com
)
📄 许可证
本模型遵循MSRLA许可证。
⚠️ 重要提示
请确认你已阅读并同意以下免责声明。本仓库中描述的模型和/或软件仅用于研究和开发。这些模型和/或软件不用于临床决策或任何其他临床用途,其临床使用性能尚未得到证实。你对这些模型和/或软件的任何使用承担全部责任,包括将其集成到任何用于临床用途的产品中。
💡 使用建议
MAIRA - 2仅用于研究,切勿用于临床实践。在使用过程中,注意输入提示的变化可能影响模型性能,且生成的报告可能存在误差。








