🚀 Pixtral-12B-Captioner-Relaxed
Pixtral-12B-Captioner-Relaxed 是一个经过指令微调的版本,基于先进的多模态大语言模型 Pixtral-12B-2409。此微调版本基于为文本到图像模型精心策划的数据集,能为给定图像提供更详细的描述。
📦 基本信息
属性 |
详情 |
库名称 |
transformers |
许可证 |
apache-2.0 |
基础模型 |
mistralai/Pixtral-12B-2409 |
任务类型 |
图像转文本 |
🚀 快速开始
环境要求
12B 模型在半精度下需要 24GB 的显存。模型可以加载 8 位或 4 位量化,但性能可能会下降。
代码示例
from PIL import Image
from transformers import LlavaForConditionalGeneration, AutoProcessor
from transformers import BitsAndBytesConfig
import torch
import matplotlib.pyplot as plt
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_quant_type="nf4"
)
model_id = "Ertugrul/Pixtral-12B-Captioner-Relaxed"
model = LlavaForConditionalGeneration.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16)
processor = AutoProcessor.from_pretrained(model_id)
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": "Describe the image.\n"},
{
"type": "image",
}
],
}
]
PROMPT = processor.apply_chat_template(conversation, add_generation_prompt=True)
image = Image.open(r"PATH_TO_YOUR_IMAGE")
def resize_image(image, target_size=768):
"""Resize the image to have the target size on the shortest side."""
width, height = image.size
if width < height:
new_width = target_size
new_height = int(height * (new_width / width))
else:
new_height = target_size
new_width = int(width * (new_height / height))
return image.resize((new_width, new_height), Image.LANCZOS)
image = resize_image(image, 768)
inputs = processor(text=PROMPT, images=image, return_tensors="pt").to("cuda")
with torch.no_grad():
with torch.autocast(device_type="cuda", dtype=torch.bfloat16):
generate_ids = model.generate(**inputs, max_new_tokens=384, do_sample=True, temperature=0.3, use_cache=True, top_k=20)
output_text = processor.batch_decode(generate_ids[:, inputs.input_ids.shape[1]:], skip_special_tokens=True, clean_up_tokenization_spaces=True)[0]
print(output_text)
✨ 主要特性
- 增强细节:生成更全面、细致的图像描述。
- 约束放宽:与基础模型相比,提供限制更少的图像描述。
- 自然语言输出:使用自然语言描述图像中的不同对象及其位置。
- 图像生成优化:生成与最先进的文本到图像生成模型兼容的标题。
⚠️ 重要提示
此微调模型针对创建文本到图像数据集进行了优化。因此,在其他复杂任务上的性能可能低于原始模型。
📚 详细文档
更多详细选项,请参考 Pixtral-12B-2409 或 mistral-community/pixtral-12b 的文档。
你也可以尝试 Qwen2-VL-7B-Captioner-Relaxed,这是一个较小的替代模型,采用了类似的训练方式。
📄 许可证
本项目采用 apache-2.0 许可证。