Files
ModelHub XC cb18c46f78 初始化项目,由ModelHub XC社区提供模型
Model: zpeng1989/Multimodel_Medical_Qwen25vl_3B_Model
Source: Original Platform
2026-10-05 12:57:15 +08:00

248 lines
7.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 医疗多模态问答大模型
## 项目概述
本项目旨在构建一个医疗多模态问答大模型,基于`qwen-vl2.5-3B`模型作为基座模型,使用LoRA(Low-Rank Adaptation)技术进行微调。模型训练数据结合了医疗图像和多轮问答对话,涵盖医学影像、诊断报告等多模态信息,并通过问答题目进行微调,以提升模型在医疗领域的多模态理解和问答能力。
## 模型架构
- **基座模型**: `qwen-vl2.5-3B`
- **微调方法**: LoRA(Low-Rank Adaptation)
- **训练数据**:
- 医疗图像(如CT、MRI、X光、病理图等)
- 多轮问答对话(基于诊断报告、医学文献等生成)
## 数据集
### 数据来源
1. **医疗图像**: 包含CT、MRI、X光、病理图等医学影像数据。
2. **多轮问答对话**: 基于诊断报告、医学文献等生成的问答对话数据。
### 数据预处理
- **图像处理**: 对医疗图像进行标准化处理(如尺寸调整、归一化等)。
- **文本处理**: 对问答对话进行清洗和标注,确保问题和答案的准确性。
- **多模态对齐**: 将图像与对应的文本信息进行对齐,构建多模态训练样本。
- **数据分割**: 将数据集分为训练集、验证集和测试集。
## 模型训练
### 微调方法
使用LoRA技术对`qwen-vl2.5`模型进行微调。LoRA通过在预训练模型的权重矩阵中引入低秩矩阵来减少参数量,从而在保持模型性能的同时降低计算成本。
### 训练步骤
1. **加载预训练模型**: 加载`qwen-vl2.5`模型。
2. **应用LoRA**: 在模型的关键层应用LoRA技术。
3. **训练模型**: 使用准备好的多模态数据集进行微调。
4. **验证与测试**: 在验证集和测试集上评估模型性能,调整超参数以优化结果。
## 模型评估
### 评估指标
- **准确率**: 模型在问答任务中的准确率。
- **召回率**: 模型能够正确回答的问题比例。
- **F1分数**: 准确率和召回率的调和平均数。
- **多模态理解能力**: 模型对图像和文本信息的综合理解能力。
### 评估结果
在测试集上的评估结果如下:
待更新
## 使用指南
### 环境配置
1. **Python版本**: 3.10+
2. **依赖库**:
- `transformers`
- `torch`
- `modelscope`
### 下载模型
SDK下载
```bash
#安装ModelScope
pip install modelscope
```
```python
#SDK模型下载
#模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('zpeng1989/Multimodel_Medical_Qwen25vl_3B_Model')
```
Git下载
```
#Git模型下载
git clone https://oauth2:eDTzbKYiKrNCswNiDx1s@www.modelscope.cn/zpeng1989/Multimodel_Medical_Qwen25vl_3B_Model.git
```
### 模型推理
以下是一个使用案例,展示如何加载模型并进行推理:
```python
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
# default: Load the model on the available device(s)
model = Qwen2_5_VLForConditionalGeneration.from_pretrained("qwen25_3b_vl_lora_sft_v20250511", torch_dtype="auto", device_map="auto"
)
# We recommend enabling flash_attention_2 for better acceleration and memory saving, especially in multi-image and video scenarios.
# model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
# "Qwen/Qwen2.5-VL-7B-Instruct",
# torch_dtype=torch.bfloat16,
# attn_implementation="flash_attention_2",
# device_map="auto",
# )
# default processor
#export_dir: /data/cgu/Model_Train/Model/qwen25_3b_vl_lora_sft_v20250511
processor = AutoProcessor.from_pretrained("qwen25_3b_vl_lora_sft_v20250511")
# The default range for the number of visual tokens per image in the model is 4-16384.
# You can set min_pixels and max_pixels according to your needs, such as a token range of 256-1280, to balance performance and cost.
# min_pixels = 256*28*28
# max_pixels = 1280*28*28
# processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct", min_pixels=min_pixels, max_pixels=max_pixels)
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "*.jpg",
},
{"type": "text", "text": "患者胸片上有什么问题?"},
],
}
]
# Preparation for inference
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to(model.device)
# Inference: Generation of the output
generated_ids = model.generate(**inputs, max_new_tokens=128)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
```
## 案例
### Case 01
![案例图](imgs/A.jpg)
输入问题:
```
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "A.jpg",
},
{"type": "text", "text": "帮我分析一下患者情况?"},
],
}
]
```
输出结果:
```
子宫动脉增强轴向TSE MRI图像显示肿瘤有增强和扩散的迹象,这表明血液中肿瘤区域对比剂的浓度增加。周围的血管,包括阴道、宫颈、宫体侧壁和输卵管,都受到了肿瘤的影响。与此相邻的骨质层显示出侵蚀现象,意味着骨骼组织已侵犯或延伸到了肿瘤。
```
### Case 02
![案例图](imgs/B.jpg)
输入问题:
```
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "B.jpg",
},
{"type": "text", "text": "图上黄色箭头具体指什么?帮我仔细分析一下?"},
],
}
]
```
输出结果:
```
图像中的黄色箭头指向了右下叶气道闭塞。气道闭塞是指支气管(将空气吸入肺部的管道)被阻塞或狭窄,可能由肿胀、炎症或其他原因导致血液凝块的形成。这会阻断空气流动,引起呼吸困难和其他呼吸道症状。根据CT扫描,肿瘤似乎将气道逐渐推向外侧,导致腔内阻塞。
```
### Case 03
![案例图](imgs/E.jpg)
输入问题:
```
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "E.jpg",
},
{"type": "text", "text": "患者胸片上有什么问题?"},
],
}
]
```
输出结果:
```
在患者的胸部X光片上,可见右侧膈下角增大,这可能提示有脓肿或积液存在。
```
## 致谢
- 感谢 `qwen-vl2.5` 模型的开发者。
- 感谢所有为医疗多模态数据集做出贡献的研究人员。
## 联系方式
如有任何问题,请联系 [592392714@qq.com]。
---
**注意**: 本项目仅供学术研究使用,不构成医疗建议。