跳到正文
原文
Hugging Face Blog·· 2025-02-20精选AI 评分67

Hugging Face 发布 SmolVLM2:256M 到 2.2B 的视频理解模型

SmolVLM2: Bringing Video Understanding to Every Device

AI 导读

Hugging Face 发布 SmolVLM2 系列视频理解模型,包含 2.2B、500M 和 256M 三种参数规模,并同步支持 Transformers 与 MLX。

推荐理由

Hugging Face 发布三档小参数视频理解模型,并给出 iPhone、VLC 等端侧落地方式,可对照其内存与性能取舍。

正文 · AI 翻译

简而言之:SmolVLM 现在可以观看 📺,并具备更强的视觉理解能力

SmolVLM2 代表了我们对视频理解思考方式的根本转变——从需要大量计算资源的庞大模型,转向可在任何地方运行的轻量模型。我们的目标很简单:让视频理解能够覆盖从手机到服务器的所有设备和使用场景。

我们发布三种规模的模型(2.2B、500M 和 256M),从第一天起就支持 MLX(Python 和 Swift API)。我们已在此合集中提供所有模型和演示。

想立即试用 SmolVLM2 吗?请查看我们的交互式聊天界面,你可以通过一个简单直观的界面测试 SmolVLM2 2.2B 的视觉和视频理解能力。

目录

技术细节

我们推出三个新模型,分别拥有 256M、500M 和 2.2B 参数。2.2B 模型是视觉和视频任务的首选,而 500M 和 256M 模型则代表了有史以来发布的最小视频语言模型。

虽然它们体积小巧,但按内存消耗计算,它们超越了任何现有模型。从 Video-MME(视频领域首屈一指的科学基准)来看,SmolVLM2 在 2B 量级上加入了前沿模型家族,而在更小的空间中我们领先群雄。

SmolVLM2 Performance

Video-MME 作为一项综合基准表现突出,因为它广泛覆盖了多种视频类型、不同时长(11 秒到 1 小时)、多种数据模态(包括字幕和音频),以及高质量专家标注,涵盖 900 个视频、总计 254 小时。了解更多请点击此处。

SmolVLM2 2.2B:我们视觉与视频领域的新明星

与之前的 SmolVLM 系列相比,我们新的 2.2B 模型在解决带图像的数学问题、读取照片中的文字、理解复杂图表以及应对科学视觉问题方面表现更佳。这体现在模型在不同基准上的表现:

SmolVLM2 Vision Score Gains

在视频任务方面,2.2B 性价比很高。在我们评估它的各项科学基准中,我们想特别强调它在 Video-MME 上的表现,它超越了所有现有的 2B 模型。

得益于Apollo:大型多模态模型中视频理解的探索中发布的数据混合研究成果,我们得以在视频/图像性能上取得良好平衡。

它的内存效率极高,甚至可以在免费的 Google Colab 中运行。

Python 代码

# 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])

更进一步缩小:认识 500M 和 256M 视频模型

直到今天,没有人敢于发布如此小的视频模型。

我们全新的 SmolVLM2-500M-Video-Instruct 模型在视频能力上非常接近 SmolVLM 2.2B,但体积仅为其一小部分:我们以不到四分之一参数量的规模,实现了相同的视频理解能力 🤯。

接下来是我们的小实验——SmolVLM2-256M-Video-Instruct。可以把它看作我们的“如果”项目——如果我们能进一步突破小模型的边界会怎样?受几周前 IBM 在我们基础 SmolVLM-256M-Instruct 上取得的成果启发,我们想看看在视频理解上我们能走多远。虽然这更像是一个实验性发布,但我们希望它能激发一些创意应用和专门的微调项目。

SmolVLM2 演示应用套件

为了展示我们在小型视频模型上的愿景,我们构建了三个实用应用,以展示这些模型的多功能性。

iPhone 视频理解

我们创建了一个完全在本地运行 SmolVLM2 的 iPhone 应用。使用我们的 500M 模型,用户可以直接在设备上分析和理解视频内容——无需云端。有兴趣构建在本地运行 AI 模型的 iPhone 视频处理应用吗?我们很快就会发布它——填写此表单来测试并与我们一起构建!

VLC 媒体播放器集成

我们正在与 VLC 媒体播放器合作,集成 SmolVLM2 以提供智能的视频片段描述和导航。这一集成允许用户以语义方式搜索视频内容,根据自然语言描述直接跳转到相关部分。虽然这项工作仍在进行中,但你可以在此 space 中试用当前的播放列表构建器原型。

视频精彩片段生成器

该应用作为一个 HF Mirror Space 提供,可接收长视频(1 小时以上)并自动提取最重要的时刻。我们已在足球比赛和其他冗长事件上进行了广泛测试,使其成为内容摘要的强大工具。在我们的演示 space 中亲自试用。

将 SmolVLM2 与 Transformers 和 MLX 结合使用

我们从第一天起就让 SmolVLM2 可与 transformers 和 MLX 一起使用。在本节中,你可以找到针对视频和多张图像的不同推理替代方案和教程。

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")

视频推理

你可以通过传入 {"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])

多图像推理

除了视频之外,SmolVLM2 还支持多图像对话。你可以通过聊天模板使用相同的 API,使用文件系统路径、URL 或 PIL.Image 对象提供每张图像:

messages = [
    {
        "role": "user",
        "content": [
            {"type": "text", "text": "What are the differences between these two images?"},
          {"type": "image", "url": "https://hf-mirror.com/datasets/huggingface/documentation-images/resolve/main/bee.jpg"},
          {"type": "image", "url": "https://hf-mirror.com/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])

使用 MLX 推理

要在 Apple Silicon 设备上使用 Python 通过 MLX 运行 SmolVLM2,你可以使用出色的 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://hf-mirror.com/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 最终确定并合并之前,你必须从这个 fork 编译该项目,然后你就可以在 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://hf-mirror.com/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。你可以在这里找到微调 notebook。

引用信息

你可以按以下方式引用我们:

@article{marafioti2025smolvlm,
  title={SmolVLM: Redefining small and efficient multimodal models}, 
  author={Andrés Marafioti and Orr Zohar and Miquel Farré and Merve Noyan and Elie Bakouch and Pedro Cuenca and Cyril Zakka and Loubna Ben Allal and Anton Lozhkov and Nouamane Tazi and Vaibhav Srivastav and Joshua Lochner and Hugo Larcher and Mathieu Morlon and Lewis Tunstall and Leandro von Werra and Thomas Wolf},
  journal={arXiv preprint arXiv:2504.05299},
  year={2025}
}

阅读更多

我们要感谢 Raushan Turganbay、Arthur Zucker 和 Pablo Montalvo Leroux 将该模型贡献给 transformers。

我们期待看到你用 SmolVLM2 构建的所有东西! 如果你想了解更多关于 SmolVLM 系列模型的信息,欢迎阅读以下内容:

SmolVLM2 - 模型和演示合集

来源:Hugging Face Blog · hf-mirror.com