SmolVLM2:將影片理解帶到每一臺裝置

釋出於 2025年2月20日
在 GitHub 上更新

TL;DR: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 小時)的高質量專家註釋。在此瞭解更多資訊:https://video-mme.github.io/home_page.html

SmolVLM2 2.2B:我們視覺和影片領域的新明星選手

與之前的 SmolVLM 系列相比,我們的新 2.2B 模型在解決影像數學問題、識別照片中的文字、理解複雜圖表以及處理科學視覺問題方面表現更佳。這體現在模型在不同基準測試中的效能上。

SmolVLM2 Vision Score Gains

在影片任務方面,2.2B 物超所值。在我們評估的各種科學基準中,我們想強調其在 Video-MME 上的表現,它超越了所有現有 2B 模型。

我們之所以能夠在影片/影像效能上取得良好的平衡,得益於在 Apollo: An Exploration of Video Understanding in Large Multimodal Models 中發表的資料混合學習成果。

它的記憶體效率如此之高,甚至可以在免費的 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 影片理解

我們開發了一款 iPhone 應用程式,可完全在本地執行 SmolVLM2。透過使用我們的 500M 模型,使用者可以直接在裝置上分析和理解影片內容——無需雲端。對使用本地執行的 AI 模型構建 iPhone 影片處理應用程式感興趣嗎?我們即將釋出它——填寫此表單即可與我們一起測試和構建!

VLC 媒體播放器整合

我們正與 VLC 媒體播放器合作,整合 SmolVLM2,以提供智慧影片片段描述和導航。這種整合允許使用者語義化地搜尋影片內容,根據自然語言描述直接跳轉到相關部分。雖然這項工作仍在進行中,但您可以在此空間中嘗試當前的播放列表生成器原型。

影片精彩片段生成器

此應用程式作為一個 Hugging Face Space 提供,它能夠處理長影片(1 小時以上),並自動提取最精彩的時刻。我們已經對足球比賽和其他長時間事件進行了廣泛測試,使其成為內容摘要的強大工具。在我們的演示空間中親自嘗試一下。

使用 Transformers 和 MLX 執行 SmolVLM2

我們從第一天起就支援使用 Transformers 和 MLX 執行 SmolVLM2。在本節中,您可以找到不同的推理方案以及影片和多影像教程。

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://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])

使用 MLX 進行推理

要在 Apple Silicon 裝置上使用 Python 執行 SmolVLM2 和 MLX,您可以使用出色的 mlx-vlm 庫。首先,您需要使用以下命令從 此分支 安裝 `mlx-vlm`

pip install git+https://github.com/pcuenca/mlx-vlm.git@smolvlm

然後,您可以使用以下一行程式碼對單個影像進行推理,該程式碼使用了 SmolVLM2 的未量化 500M 版本

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。您可以在此處找到微調筆記本。

引用資訊

您可以按以下方式引用我們

@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 - 模型和演示合集

社群

📻 🎙️ 嘿,我為這篇博文生成了一個 AI 播客,快來聽聽看吧!

此播客透過 ngxson/kokoro-podcast-generator 生成,使用 DeepSeek-R1Kokoro-TTS

執行此指令碼時
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 "你能描述這張圖片嗎?"

======================================
測試錯誤
檔案:['https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg']

提示:<|im_start|>使用者:你能描述這張圖片嗎?
助手
回溯(最近一次呼叫在最後)
檔案“”,第 198 行,在 _run_module_as_main 中
檔案“”,第 88 行,在 _run_code 中
檔案“/opt/homebrew/Caskroom/miniconda/base/envs/playwright/lib/python3.12/site-packages/mlx_vlm/generate.py”,第 156 行,在
main()
檔案“/opt/homebrew/Caskroom/miniconda/base/envs/playwright/lib/python3.12/site-packages/mlx_vlm/generate.py”,第 141 行,在 main 中
輸出 = 生成(
^^^^^^^^^
檔案“/opt/homebrew/Caskroom/miniconda/base/envs/playwright/lib/python3.12/site-packages/mlx_vlm/utils.py”,第 1115 行,在生成中
對於 stream_generate(model, processor, prompt, image, **kwargs) 中的響應
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
檔案“/opt/homebrew/Caskroom/miniconda/base/envs/playwright/lib/python3.12/site-packages/mlx_vlm/utils.py”,第 1016 行,在 stream_generate 中
輸入 = 準備輸入(
^^^^^^^^^^^^^^^
檔案“/opt/homebrew/Caskroom/miniconda/base/envs/playwright/lib/python3.12/site-packages/mlx_vlm/utils.py”,第 806 行,在 prepare_inputs 中
processor.tokenizer.pad_token = processor.tokenizer.eos_token
^^^^^^^^^^^^^^^^^^^
檔案“/opt/homebrew/Caskroom/miniconda/base/envs/playwright/lib/python3.12/site-packages/transformers/tokenization_utils_base.py”,第 1108 行,在 __getattr__ 中
引發 AttributeError(f"{self.__class__.__name__} 沒有屬性 {key}")
AttributeError: GPT2TokenizerFast 沒有屬性 tokenizer。您是指:'_tokenizer'?

文章作者

你好 @leexiaobo2006 !你需要從 `main` 或這個穩定分支安裝 transformers

pip install git+https://github.com/huggingface/transformers@v4.49.0-SmolVLM-2

https://koshurai.medium.com/a-comprehensive-tutorial-on-using-smolvlm2-for-image-and-video-analysis-faa10e0710a6

這是一個關於如何在 Colab 上使用 SMOLVLM2 進行影像和影片分析的免費教程(使用 T4 GPU 免費層)

·
文章作者

不錯!您想將相同的內容作為社群文章釋出,以最大化可見性嗎?

在哪能找到應用演示程式碼 @pcuenq

·

做得好!

你知道影片精彩片段生成器的程式碼倉庫在哪裡嗎?謝謝! @pcuenq

·

嗨!
您可以在這裡找到:https://huggingface.co/spaces/HuggingFaceTB/SmolVLM2
然後點選三個點 -> 克隆倉庫

我見過很棒的 VLM,但它們逐幀推理,這可能導致誤報。

這個模型能理解影片(分塊地處理幀並理解影片的性質)嗎?

嗨 - 這個可以處理最長時長的影片是多少?

註冊登入 發表評論

© . This site is unofficial and not affiliated with Hugging Face, Inc.