本文翻译整理自:SmolVLM2: Bringing Video Understanding to Every Device
https://huggingface.co/blog/smolvlm2
文章目录
TL;DR: SmolVLM 现在可以观看 📺 并拥有更好的视觉理解
一、关于 SmolVLM2
SmolVLM2 代表了我们在视频理解方面的根本转变——从需要大量计算资源的巨型模型转向高效模型,这些模型可以在任何地方运行。
我们的目标很简单:让视频理解在各种设备和使用场景下变得触手可及,从手机到服务器。
我们发布了三种尺寸的模型(2.2B、500M和256M),MLX ready(从零开始就支持Python 和 Swift API)。我们已经将所有模型和演示都放在了这个集合中。
想要立即尝试SmolVLM2?查看我们的交互式聊天界面,您可以通过一个简单直观的界面测试SmolVLM2 2.2B的视觉和视频理解能力。
二、 技术细节
我们推出了三个新的模型,参数量分别为256M、500M和22B。22B模型是视觉和视频任务的优选,而500M和256M模型代表了有史以来发布的最小的视频语言模型。
虽然它们体积小巧,但每单位内存消耗下的性能超过了任何现有模型。在查看视频-MME(视频领域的科学基准)时,SmolVLM2加入了2B范围内的前沿模型家族,并在更小的空间中处于领先地位。
Video-MME因其广泛的覆盖范围而脱颖而出,包括各种视频类型、不同的时长(11秒到1小时)、多种数据模式(包括字幕和音频),以及跨越900个视频、总计254小时的高质量专家注释。了解更多信息这里。
1、SmolVLM2 2.2B: 我们新的视觉和视频明星选手
与之前的SmolVLM系列相比,我们的新2.2B模型在用图像解决数学问题、阅读照片中的文本、理解复杂图表以及应对科学视觉问题方面表现得更好。这在不同基准测试中的模型性能中体现出来:
当涉及到视频任务时,2.2B 是物有所值的。在我们评估的各种科学基准上,我们想突出它在 Video-MME 上的性能,它优于所有现有的 2B 模型。
我们能够在大型多模态模型中实现良好的视频/图像性能平衡,这得益于在Apollo: An Exploration of Video Understanding in Large Multimodal Models中发布的数据混合学习成果。
它如此节省内存,以至于你甚至可以在免费的 Google Colab 上运行它。
# Install transformers from `main` or from this stable branch:
!pip install git+https://github.com/huggingface/transformers@v4.49.0-SmolVLM-2
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
model_path = "HuggingFaceTB/SmolVLM2-2.2B-Instruct"
processor = AutoProcessor.from_pretrained(model_path)
model = AutoModelForImageTextToText.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
_attn_implementation="flash_attention_2"
).to("cuda")
messages = [
{
"role": "user",
"content": [
{"type": "video", "path": "path_to_video.mp4"},
{"type": "text", "text": "Describe this video in detail"}
]
},
]
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device, dtype=torch.bfloat16)
generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64)
generated_texts = processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)
print(generated_texts[0])
2、越来越小:遇见500M和256M视频模型
没有人敢在今天之前发布如此小的视频模型。
我们的新模型 SmolVLM2-500M-Video-Instruct 拥有与 SmolVLM 2.2B 非常接近的视频功能,但大小却小得多:我们用不到四分之一的参数就获得了相同的视频理解能力 🤯。
然后是我们的这个小实验,SmolVLM2-256M-Video-Instruct。把它想象成我们的“如果”项目——如果我们能将小型模型的边界再进一步推进会怎样?从几周前我们从基础模型 SmolVLM-256M-Instruct 中获得的灵感,我们想看看我们在视频理解方面能走多远。虽然它更多的是一个实验性发布,但我们希望它能激发一些创造性的应用和专门的微调项目。
3、SmolVLM2演示应用套件
为了展示我们在小型视频模型中的愿景,我们构建了三个实际应用,展示了这些模型的多样性。
3.1 iPhone 视频理解
https://www.youtube.com/shorts/G1yQlHTk_Ig
我们创建了一个完全本地运行的iPhone应用程序,其中运行了SmolVLM2。使用我们的500M模型,用户可以直接在他们的设备上分析和理解视频内容——无需云服务。
感兴趣于构建使用本地运行的AI模型进行视频处理的iPhone应用程序吗?我们很快就会发布——填写此表格以测试和与我们共建!
3.2 VLC媒体播放器集成
与VLC媒体播放器合作,我们正在集成SmolVLM2以提供智能视频片段描述和导航。此集成使用户能够通过语义搜索视频内容,根据自然语言描述直接跳转到相关部分。
虽然这项工作仍在进行中,但您可以在这个空间中尝试当前的播放列表构建器原型。
3.3 视频亮点生成器
可作为 Hugging Face Space 使用,该应用程序可以处理长视频(1小时以上)并自动提取最显著的时刻。我们已经在足球比赛和其他漫长事件上进行了广泛的测试,使其成为内容摘要的强大工具。在我们的演示空间中亲自尝试。
三、使用 SmolVLM2 与 Transformer 和 MLX
我们自第一天起就使SmolVLM2可以与transformers和MLX一起使用。在本节中,您可以找到不同的视频和多个图像推理替代方案和教程。
1、Transformers
最简单的方式来运行 SmolVLM2 模型的推理是通过会话 API - 应用聊天模板会自动处理所有输入。
您可以通过以下方式加载模型。
# Install transformers from `main` or from this stable branch:
!pip install git+https://github.com/huggingface/transformers@v4.49.0-SmolVLM-2
from transformers import AutoProcessor, AutoModelForImageTextToText
import torch
processor = AutoProcessor.from_pretrained(model_path)
model = AutoModelForImageTextToText.from_pretrained(
model_path,
torch_dtype=torch.bfloat16,
_attn_implementation="flash_attention_2"
).to("cuda")
1.1 视频推理
您可以通过传递 {"type": "video", "path": {video_path}}
来通过聊天模板传递视频。下面是一个完整的示例。
messages = [
{
"role": "user",
"content": [
{"type": "video", "path": "path_to_video.mp4"},
{"type": "text", "text": "Describe this video in detail"}
]
},
]
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device, dtype=torch.bfloat16)
generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64)
generated_texts = processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)
print(generated_texts[0])
1.2 多图像推理
除了视频,SmolVLM2 还支持多图像对话。您可以通过聊天模板使用相同的 API,使用文件系统路径、URL 或 PIL.Image
对象提供每张图像:
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": "What are the differences between these two images?"},
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
{"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/0052a70beed5bf71b92610a43a52df6d286cd5f3/diffusers/rabbit.jpg"},
]
},
]
inputs = processor.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device, dtype=torch.bfloat16)
generated_ids = model.generate(**inputs, do_sample=False, max_new_tokens=64)
generated_texts = processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)
print(generated_texts[0])
2、使用 MLX 进行推理
要使用 Python 在 Apple Silicon 设备上运行 SmolVLM2 并使用 MLX,你可以使用优秀的 mlx-vlm 库。首先,你需要使用以下命令从 这个分支 安装 mlx-vlm
:
pip install git+https://github.com/pcuenca/mlx-vlm.git@smolvlm
然后,您可以使用以下单行命令在单个图像上运行推理,该命令使用未量化的500M版本SmolVLM2:
python -m mlx_vlm.generate \
--model mlx-community/SmolVLM2-500M-Video-Instruct-mlx \
--image https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg \
--prompt "Can you describe this image?"
我们还创建了一个简单的视频理解脚本。您可以使用如下方式:
python -m mlx_vlm.smolvlm_video_generate \
--model mlx-community/SmolVLM2-500M-Video-Instruct-mlx \
--system "Focus only on describing the key dramatic action or notable event occurring in this video segment. Skip general context or scene-setting details unless they are crucial to understanding the main action." \
--prompt "What is happening in this video?" \
--video /Users/pedro/Downloads/IMG_2855.mov \
--prompt "Can you describe this image?"
请注意,系统提示对于使模型符合期望行为非常重要。您可以使用它来,例如,描述所有场景和过渡,或者提供一个关于正在发生什么的简短总结。
四、Swift MLX
Swift 语言也通过 mlx-swift-examples 仓库 得到支持,这是我们构建我们 iPhone 应用所使用的。
直到我们的正在进行中的PR完成并合并,您必须从这个分支编译项目,然后您可以在Mac上按照以下方式使用llm-tool
CLI。
对于图像推理:
./mlx-run --debug llm-tool \
--model mlx-community/SmolVLM2-500M-Video-Instruct-mlx \
--prompt "Can you describe this image?" \
--image https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg \
--temperature 0.7 --top-p 0.9 --max-tokens 100
视频分析也得到支持,并提供系统提示。我们发现系统提示对于视频理解特别有帮助,可以驱动模型达到我们感兴趣的详细程度。这是一个视频推理示例:
./mlx-run --debug llm-tool \
--model mlx-community/SmolVLM2-500M-Video-Instruct-mlx \
--system "Focus only on describing the key dramatic action or notable event occurring in this video segment. Skip general context or scene-setting details unless they are crucial to understanding the main action." \
--prompt "What is happening in this video?" \
--video /Users/pedro/Downloads/IMG_2855.mov \
--temperature 0.7 --top-p 0.9 --max-tokens 100
如果您使用 MLX 和 Swift 在您的应用程序中集成了 SmolVLM2,我们很想了解!请随时在下面的评论部分给我们留言!
微调 SmolVLM2
您可以使用transformers对SmolVLM2进行微调 🤗 我们为了演示目的,在Colab上使用VideoFeedback数据集中的视频-字幕对,对500M变体进行了微调。
由于500M变体较小,最好使用完全微调而不是QLoRA或LoRA,同时您可以尝试在cB变体上应用QLoRA。您可以在这里找到微调笔记本。
其它
引用信息
您可以使用以下方式引用我们:
@unpublished{marafioti2025smolvlm,
title = {SmolVLM: Redefining small and efficient multimodal models},
author = {Marafioti, Andr\'{e}s and Zohar, Orr and Farr\'{e}, Miquel and Noyan, Merve and Bakouch, Elie and Cuenca, Pedro and Zakka, Cyril and Ben Allal, Loubna and Lozhkov, Anton and Tazi, Nouamane and Srivastav, Vaibhav and Lochner, Joshua and Larcher, Hugo and Morlon, Mathieu and Tunstall, Lewis and von Werra, Leandro and Wolf, Thomas},
year = {2025},
}
阅读更多
我们想感谢Raushan Turganbay、Arthur Zucker和Pablo Montalvo Leroux为transformers模型所做的贡献。
我们期待看到您使用SmolVLM2构建的所有东西!如果您想了解更多关于SmolVLM系列模型的信息,请随时阅读以下内容:
SmolVLM2 - 模型和演示集合
2025-03-09(日)