【项目实战】通过LLaMaFactory+Qwen2-VL-2B微调一个多模态医疗大模型

前言

随着多模态大模型的发展,其不仅限于文字处理,更能够在图像、视频、音频方面进行识别与理解。医疗领域中,医生们往往需要对各种医学图像进行处理,以辅助诊断和治疗。如果将多模态大模型与图像诊断相结合,那么这会极大地提升诊断效率。

项目目标

训练一个医疗多模态大模型,用于图像诊断。

刚好家里老爷子近期略感头疼,去医院做了脑部CT,诊断患有垂体瘤,我将尝试使用多模态大模型进行进一步诊断。

实现过程

1. 数据集准备

为了训练模型,需要准备大量的医学图像数据。通过搜索我们找到以下训练数据:

数据名称:MedTrinity-25M
数据地址:https://github.com/UCSC-VLAA/MedTrinity-25M
数据简介:MedTrinity-25M数据集是一个用于医学图像分析和计算机视觉研究的大型数据集。
数据来源:该数据集由加州大学圣克鲁兹分校(UCSC)提供,旨在促进医学图像处理和分析的研究。
数据量:MedTrinity-25M包含约2500万条医学图像数据,涵盖多种医学成像技术,如CT、MRI和超声等。
数据内容
该数据集有两份,分别是 25Mdemo25Mfull

25Mdemo (约162,000条)数据集内容如下:

25Mfull (约24,800,000条)数据集内容如下:

2. 数据下载

2.1 安装Hugging Face的Datasets库
pip install datasets
2.2 下载数据集
from datasets import load_dataset

# 加载数据集
ds = load_dataset("UCSC-VLAA/MedTrinity-25M", "25M_demo", cache_dir="cache")

执行结果:

说明:

  • 以上方法是使用HuggingFace的Datasets库下载数据集,下载的路径为当前脚本所在路径下的cache文件夹。
  • 使用HuggingFace下载需要能够访问https://huggingface.co/ 并且在网站上申请数据集读取权限才可以。
  • 如果没有权限访问HuggingFace,可以关注一起AI技术公众号后,回复 “MedTrinity”获取百度网盘下载地址。
2.3 预览数据集
# 查看训练集的前1个样本
print(ds['train'][:1]) 

运行结果:

{
   
    'image': [<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=512x512 at 0x15DD6D06530>], 
    'id': ['8031efe0-1b5c-11ef-8929-000066532cad'], 
    'caption': ['The image is a non-contrasted computed tomography (CT) scan of the brain, showing the cerebral structures without any medical devices present. The region of interest, located centrally and in the middle of the image, exhibits an area of altered density, which is indicative of a brain hemorrhage. This area is distinct from the surrounding brain tissue, suggesting a possible hematoma or bleeding within the brain parenchyma. The location and characteristics of this abnormality may suggest a relationship with the surrounding brain tissue, potentially causing a mass effect or contributing to increased intracranial pressure.'
    ]
}

使用如下命令对数据集的图片进行可视化查看:

# 可视化image内容
from PIL import Image
import matplotlib.pyplot as plt

image = ds['train'][0]['image']  # 获取第一张图像

plt.imshow(image)
plt.axis('off')  # 不显示坐标轴
plt.show()

运行结果:

### 使用Llama Factory进行多模态模型微调 为了使用Llama Factory进行多模态模型的微调,可以遵循一系列特定的方法和技术来准备环境、加载预训练模型以及调整超参数。以下是详细的指南: #### 准备工作 确保安装了必要的依赖库和工具包。这通常涉及设置Python虚拟环境并安装所需的软件包。 ```bash pip install transformers datasets torch accelerate ``` #### 加载预训练模型 选择合适的预训练模型作为起点非常重要。对于13B规模的模型,在给定的情况下推荐使用`llama13B`[^2]。可以通过Hugging Face Transformers库轻松加载此模型及其配置文件。 ```python from transformers import AutoModelForCausalLM, AutoTokenizer model_name_or_path = "meta-llama/Llama-2-13b-chat-hf" tokenizer = AutoTokenizer.from_pretrained(model_name_or_path) model = AutoModelForCausalLM.from_pretrained(model_name_or_path) ``` #### 数据集处理 针对多模态任务的数据集需要特别注意图像和其他非文本输入类型的处理方式。应采用适当的技术将这些不同形式的信息转换成适合神经网络消费的形式。 ```python import torchvision.transforms as transforms from PIL import Image from datasets import load_dataset dataset = load_dataset('path_to_your_multimodal_dataset') transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), ]) def preprocess_data(example): image = example['image'] text = example['text'] img_tensor = transform(Image.open(image)) input_ids = tokenizer(text, return_tensors="pt").input_ids return {"pixel_values": img_tensor.unsqueeze(0), "labels": input_ids} processed_datasets = dataset.map(preprocess_data, batched=True) ``` #### 微调过程 定义训练参数,并启动实际的微调流程。这里假设已经准备好了一个适配器层或其他轻量级结构用于迁移学习目的。 ```python from transformers import Trainer, TrainingArguments training_args = TrainingArguments( output_dir="./results", num_train_epochs=3, per_device_train_batch_size=8, save_steps=500, ) trainer = Trainer( model=model, args=training_args, train_dataset=processed_datasets["train"], ) trainer.train() ``` 通过上述步骤,能够有效地利用Llama Factory框架完成对大型语言模型特别是像`llama13B`这样的多模态版本实施针对性优化的工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

洞明智能

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值