library_name: transformers
license: apache-2.0
language:
- en
base_model:
- mistralai/Pixtral-12B-2409
pipeline_tag: image-to-text
Pixtral-12B-Captioner-Relaxed 图像描述生成模型
模型介绍
Pixtral-12B-Captioner-Relaxed是基于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"图片路径")
def resize_image(image, target_size=768):
"""将图像短边调整至目标尺寸"""
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。