跳到正文
Hugging Face Blog·· 2025-02-20精選AI 評分68

SmolVLM2 將影片理解帶到各類裝置

SmolVLM2: Bringing Video Understanding to Every Device

AI 導讀

Hugging Face 發佈 SmolVLM2,提供 2.2B、500M 和 256M 三種影片理解模型,並從推出首日起支援 Transformers 和 MLX,後者提供 Python 及 Swift API。

推薦理由

文章同時列出 2.2B、500M、256M 規模及 Video-MME 表現,方便比較不同參數量下的影片理解能力取捨。

正文 · 繁體中文

簡而言之: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 App。使用 500M 模型,用戶可直接在裝置上分析和理解影片內容,無須連接雲端。如果你有興趣開發以本機 AI 模型處理 iPhone 影片的應用程式,我們即將發布這項功能;填寫這份表格,與我們一起測試和開發!

VLC media player 整合

我們正與 VLC media player 合作整合 SmolVLM2,提供智能影片片段描述和導覽功能。用戶可用語意搜尋影片內容,按自然語言描述直接跳至相關片段。這項整合仍在開發中;你可在此 Space試用現有的播放清單建立器原型。

影片精華產生器

這款應用程式以 Hugging Face Space 形式提供,可處理長篇影片(1 小時以上),並自動擷取最重要時刻。我們已用足球比賽及其他長時間活動影片作廣泛測試,是一款強大的內容摘要工具。到我們的示範 Space 親自試用。

使用 Transformers 和 MLX 執行 SmolVLM2

SmolVLM2 自推出首日起即可搭配 transformers 和 MLX 使用。本節提供多種推論方式,以及影片和多圖教學。

Transformers

透過對話 API 使用 SmolVLM2 模型進行推論最為簡單;套用聊天模板便會自動準備所有輸入。

你可以如下載入模型。

# 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 和 MLX 執行 SmolVLM2,可使用優秀的 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

亦可使用 mlx-swift-examples 儲存庫搭配 Swift 語言;我們正是用它來建立 iPhone App。

在我們正在處理的 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 的 App 中整合 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:模型及示範合集

來源:Hugging Face Blog · huggingface.co