说明文档
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/SmolVLM.png" width="800" height="auto" alt="图像描述">
SmolVLM
SmolVLM 是一个小巧的开源多模态模型,可以接受任意序列的图像和文本输入并生成文本输出。SmolVLM 设计高效,可以回答关于图像的问题、描述视觉内容、基于多张图像创作故事,或者在没有视觉输入的情况下作为纯语言模型使用。其轻量级架构使其适用于设备端应用,同时在多模态任务上保持出色的性能。
模型概要
- 开发者: Hugging Face 🤗
- 模型类型: 多模态模型(图像+文本)
- 语言: 英语
- 许可证: Apache 2.0
- 架构: 基于 Idefics3(见技术摘要)
资源
- 演示: SmolVLM 演示
- 博客: 博客文章
用途
SmolVLM 可用于多模态(图像+文本)任务的推理,其中输入包含文本查询以及一张或多张图像。文本和图像可以任意交错,支持图像描述、视觉问答和基于视觉内容的故事讲述等任务。该模型不支持图像生成。
要在特定任务上微调 SmolVLM,您可以参考微调教程。 <!-- todo: add link to fine-tuning tutorial -->
技术摘要
SmolVLM 利用轻量级的 SmolLM2 语言模型提供紧凑而强大的多模态体验。与之前的 Idefics 模型相比,它引入了多项改进:
- 图像压缩: 我们引入了比 Idefics3 更激进的图像压缩,使模型推理更快、内存占用更少。
- 视觉标记编码: SmolVLM 使用 81 个视觉标记来编码 384×384 大小的图像块。较大的图像会被分割成多个块,分别进行编码,在不影响性能的情况下提高效率。
有关训练和架构的更多详细信息,请参阅我们的技术报告。
快速开始
您可以使用 transformers 来加载、推理和微调 SmolVLM。
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForVision2Seq
from transformers.image_utils import load_image
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# 加载图像
image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
image2 = load_image("https://huggingface.co/spaces/merve/chameleon-7b/resolve/main/bee.jpg")
# 初始化处理器和模型
processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM-Instruct")
model = AutoModelForVision2Seq.from_pretrained(
"HuggingFaceTB/SmolVLM-Instruct",
torch_dtype=torch.bfloat16,
_attn_implementation="flash_attention_2" if DEVICE == "cuda" else "eager",
).to(DEVICE)
# 创建输入消息
messages = [
{
"role": "user",
"content": [
{"type": "image"},
{"type": "image"},
{"type": "text", "text": "Can you describe the two images?"}
]
},
]
# 准备输入
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, images=[image1, image2], return_tensors="pt")
inputs = inputs.to(DEVICE)
# 生成输出
generated_ids = model.generate(**inputs, max_new_tokens=500)
generated_texts = processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)
print(generated_texts[0])
"""
Assistant: The first image shows a green statue of the Statue of Liberty standing on a stone pedestal in front of a body of water.
The statue is holding a torch in its right hand and a tablet in its left hand. The water is calm and there are no boats or other objects visible.
The sky is clear and there are no clouds. The second image shows a bee on a pink flower.
The bee is black and yellow and is collecting pollen from the flower. The flower is surrounded by green leaves.
"""
模型优化
精度: 为了获得更好的性能,如果您的硬件支持,请以半精度(torch.float16 或 torch.bfloat16)加载和运行模型。
from transformers import AutoModelForVision2Seq
import torch
model = AutoModelForVision2Seq.from_pretrained(
"HuggingFaceTB/SmolVLM-Instruct",
torch_dtype=torch.bfloat16
).to("cuda")
您还可以使用 bitsandbytes、torchao 或 Quanto 以 4/8 位量化方式加载 SmolVLM。有关其他选项,请参阅此页面。
from transformers import AutoModelForVision2Seq, BitsAndBytesConfig
import torch
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
model = AutoModelForVision2Seq.from_pretrained(
"HuggingFaceTB/SmolVLM-Instruct",
quantization_config=quantization_config,
)
视觉编码器效率: 在初始化处理器时,可以通过设置 size={"longest_edge": N*384} 来调整图像分辨率,其中 N 是您想要的值。默认的 N=4 效果良好,生成的输入图像大小为 1536×1536。对于文档,N=5 可能更有益。减小 N 可以节省 GPU 内存,适用于较低分辨率的图像。如果您想在视频上进行微调,这也很有用。
滥用和超出范围的使用
SmolVLM 不适用于高风险场景或影响个人福祉或生计的关键决策过程。该模型可能会生成看起来真实但不准确的内容。滥用包括但不限于:
- 禁止用途:
- 评估或对个人进行评分(例如在就业、教育、信贷方面)
- 关键的自动化决策
- 生成不可靠的事实性内容
- 恶意活动:
- 垃圾信息生成
- 虚假信息传播活动
- 骚扰或虐待
- 未经授权的监视
许可证
SmolVLM 基于形状优化的 SigLIP 作为图像编码器,以及 SmolLM2 作为文本解码器部分。
我们在 Apache 2.0 许可证下发布 SmolVLM 检查点。
训练详情
训练数据
训练数据来自 The Cauldron 和 Docmatix 数据集,重点关注文档理解(25%)和图像描述(18%),同时在视觉推理、图表理解和通用指令遵循等其他关键能力上保持平衡覆盖。 <img src="https://huggingface.co/HuggingFaceTB/SmolVLM-Instruct/resolve/main/mixture_the_cauldron.png" alt="示例图像" style="width:90%;" />
评估
| 模型 | MMMU (val) | MathVista (testmini) | MMStar (val) | DocVQA (test) | TextVQA (val) | 最小 GPU 内存需求 (GB) |
|---|---|---|---|---|---|---|
| SmolVLM | 38.8 | 44.6 | 42.1 | 81.6 | 72.7 | 5.02 |
| Qwen-VL 2B | 41.1 | 47.8 | 47.5 | 90.1 | 79.7 | 13.70 |
| InternVL2 2B | 34.3 | 46.3 | 49.8 | 86.9 | 73.4 | 10.52 |
| PaliGemma 3B 448px | 34.9 | 28.7 | 48.3 | 32.2 | 56.0 | 6.72 |
| moondream2 | 32.4 | 24.3 | 40.3 | 70.5 | 65.2 | 3.87 |
| MiniCPM-V-2 | 38.2 | 39.8 | 39.1 | 71.9 | 74.1 | 7.88 |
| MM1.5 1B | 35.8 | 37.2 | 0.0 | 81.0 | 72.5 | NaN |
alvarobartt/SmolVLM-Instruct-Handler
作者 alvarobartt
创建时间: 2024-12-04 09:15:41+00:00
更新时间: 2024-12-04 11:55:30+00:00
在 Hugging Face 上查看