🚀 WhyXrayCLIP 🩻 模型卡片
WhyXrayCLIP 能够将 X 光图像与文本描述进行对齐。它在医学图像零样本分类任务中表现出色,为医学研究提供了有力工具。
🚀 快速开始
安装依赖
使用以下命令安装所需的库:
pip install open_clip_torch
代码示例
import torch
from PIL import Image
import open_clip
model, _, preprocess = open_clip.create_model_and_transforms("hf-hub:yyupenn/whyxrayclip")
model.eval()
tokenizer = open_clip.get_tokenizer("ViT-L-14")
image = preprocess(Image.open("test_xray.jpg")).unsqueeze(0)
text = tokenizer(["enlarged heart", "pleural effusion"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
✨ 主要特性
- 图像文本对齐:WhyXrayCLIP 可以将 X 光图像与文本描述进行有效对齐。
- 性能优异:在多种胸部 X 光数据集的零样本和线性探测任务中,显著优于 PubMedCLIP、BioMedCLIP 等模型。
- 快速收敛:训练收敛速度快,但当前的对比目标可能未充分利用数据信息。
📦 安装指南
使用以下命令安装所需的库:
pip install open_clip_torch
💻 使用示例
基础用法
import torch
from PIL import Image
import open_clip
model, _, preprocess = open_clip.create_model_and_transforms("hf-hub:yyupenn/whyxrayclip")
model.eval()
tokenizer = open_clip.get_tokenizer("ViT-L-14")
image = preprocess(Image.open("test_xray.jpg")).unsqueeze(0)
text = tokenizer(["enlarged heart", "pleural effusion"])
with torch.no_grad(), torch.cuda.amp.autocast():
image_features = model.encode_image(image)
text_features = model.encode_text(text)
image_features /= image_features.norm(dim=-1, keepdim=True)
text_features /= text_features.norm(dim=-1, keepdim=True)
text_probs = (100.0 * image_features @ text_features.T).softmax(dim=-1)
print("Label probs:", text_probs)
📚 详细文档
用途
直接使用
WhyXrayCLIP 可用于零样本 X 光分类,计算 X 光图像与文本描述之间的相似度。
下游使用
可作为特征提取器,为下游任务从 X 光图像和文本描述中提取特征。
适用范围外使用
WhyXrayCLIP 不应用于临床诊断或治疗,也不用于任何临床决策。用户应了解模型的风险、偏差和局限性。
训练详情
训练数据
使用 MIMIC-CXR 数据集,仅选择 PA 和 AP X 光片,共 243,334 张图像,每张图像都配有医生撰写的临床报告。对报告进行预处理,提取医学相关发现,共组装 953K 图像 - 文本对用于训练。
训练细节
使用 OpenCLIP 的训练脚本,选择 ViT-L/14 作为骨干网络。在 4 个 RTX A6000 GPU 上进行 10 个 epoch 的训练,批量大小为 128,学习率为 1e−5。根据验证集上的最低对比损失选择检查点。
评估
测试数据
在 5 个 X 光分类数据集上进行评估:Pneumonia、COVID-QU、NIH-CXR、Open-i 和 VinDr-CXR。报告上述 5 个数据集的零样本和线性探测准确率。
基线模型
比较各种 CLIP 模型,包括 OpenAI-CLIP、OpenCLIP、PubMedCLIP、BioMedCLIP、PMC-CLIP 和 MedCLIP。在零样本和线性探测场景下评估这些模型。在零样本中,使用 GPT - 4 为每个类别生成提示,并使用图像与提示之间的余弦相似度的集合作为每个类别的分数。在线性探测中,使用 CLIP 模型作为图像编码器提取特征进行逻辑回归。此外,将 DenseNet - 121(在预训练数据集上使用交叉熵损失进行微调)作为线性探测的基线。
结果
下图显示了不同模型在五个胸部 X 光数据集上的平均零样本和线性探测性能。

引用
如果在你的工作中使用了该模型,请引用我们的论文:
@article{yang2024textbook,
title={A Textbook Remedy for Domain Shifts: Knowledge Priors for Medical Image Analysis},
author={Yue Yang and Mona Gandhi and Yufei Wang and Yifan Wu and Michael S. Yao and Chris Callison-Burch and James C. Gee and Mark Yatskar},
journal={arXiv preprint arXiv:2405.14839},
year={2024}
}
📄 许可证
本项目采用 MIT 许可证。