🚀 Qwen2.5-VL-7B-Captioner-Relaxed
Qwen2.5-VL-7B-Captioner-Relaxed 是一个经过指令微调的版本,基于先进的多模态大语言模型 Qwen/Qwen2.5-VL-7B-Instruct。这是 Ertugrul/Qwen2-VL-7B-Captioner-Relaxed 的更新版本,使用 Qwen2.5 基础模型进行了重新训练。 这个微调版本基于为文本到图像模型精心策划的数据集,能为给定图像提供更详细的描述。它构建于改进后的 Qwen2.5 架构之上。
🚀 快速开始
环境要求
如果你遇到 KeyError: 'qwen2_vl'
或 ImportError: cannot import name 'Qwen2VLForConditionalGeneration' from 'transformers'
等错误,请尝试从源代码安装最新版本的 transformers 库:
pip install git+https://github.com/huggingface/transformers accelerate
代码示例
import torch
from PIL import Image
from transformers import (
AutoModelForImageTextToText,
AutoProcessor
)
model_id = "Ertugrul/Qwen2.5-VL-7B-Captioner-Relaxed"
image_path = "path/to/your/image.jpg"
model = AutoModelForImageTextToText.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
)
min_pixels = 256*28*28
max_pixels = 1280*28*28
processor = AutoProcessor.from_pretrained(model_id, max_pixels=max_pixels, min_pixels=min_pixels)
system_message = "You are an expert image describer."
def generate_description(path, model, processor):
image_inputs = Image.open(path).convert("RGB")
messages = [
{
"role": "system",
"content": [{"type": "text", "text": system_message}],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Describe this image."},
{"type": "image", "image": image_inputs},
],
},
]
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
inputs = processor(
text=[text],
images=image_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to(model.device)
generated_ids = model.generate(**inputs, max_new_tokens=512, min_p=0.1, do_sample=True, temperature=1.5)
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
)
return output_text[0]
description = generate_description(image_path, model, processor)
print(description)
✨ 主要特性
- 增强细节:生成更全面、细致的图像描述。
- 约束放宽:与基础模型相比,提供限制更少的图像描述。
- 自然语言输出:使用自然语言描述图像中的不同对象并指定其位置。
- 针对图像生成优化:生成与最先进的文本到图像生成模型兼容的标题。
- 改进的基础模型:利用 Qwen2.5 的优势,可能带来更好的整体性能和理解能力。
⚠️ 重要提示
这个微调模型是为创建文本到图像数据集而优化的。因此,在其他任务上的性能可能低于原始模型。
📚 详细文档
更多详细选项,请参考 Qwen/Qwen2.5-VL-7B-Instruct 文档。
📄 许可证
本项目采用 apache-2.0 许可证。
属性 |
详情 |
模型类型 |
多模态 |
基础模型 |
Qwen/Qwen2.5-VL-7B-Instruct |
管道标签 |
图像文本到文本 |
许可证 |
apache-2.0 |