🚀 Pixtral-12B-Captioner-Relaxed
Pixtral-12B-Captioner-Relaxed 是 Pixtral-12B-2409 經過指令微調後的版本,Pixtral-12B-2409 是一款先進的多模態大語言模型。這個微調版本基於為文本到圖像模型精心策劃的數據集,能夠為給定圖像提供更為詳細的描述。
🚀 快速開始
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)
✨ 主要特性
- 細節增強:能夠生成更全面、細緻的圖像描述。
- 約束放寬:與基礎模型相比,提供的圖像描述限制更少。
- 自然語言輸出:使用自然語言描述圖像中的不同對象,並指明它們的位置。
- 圖像生成優化:生成的圖像描述格式與最先進的文本到圖像生成模型兼容。
⚠️ 重要提示
此微調模型針對創建文本到圖像數據集進行了優化。因此,在其他複雜任務上的性能可能低於原始模型。
📦 安裝指南
12B 模型在半精度下需要 24GB 的顯存。模型可以以 8 位或 4 位量化方式加載,但預計性能會下降。
📚 詳細文檔
如需更多詳細選項,請參考 Pixtral-12B-2409 或 mistral-community/pixtral-12b 的文檔。
你也可以嘗試 Qwen2-VL-7B-Captioner-Relaxed,這是一個較小的替代模型,採用了類似的訓練方式。
📄 許可證
本項目採用 Apache-2.0 許可證。