模型概述
模型特點
模型能力
使用案例
🚀 Idefics3
Idefics3是一個開放的多模態模型,它能夠接受任意的圖像和文本輸入序列,並輸出文本。該模型可以回答關於圖像的問題、描述視覺內容、基於多張圖像創作故事,或者在沒有視覺輸入的情況下單純作為語言模型使用。相較於Idefics1和Idefics2,Idefics3在OCR、文檔理解和視覺推理等方面的能力有了顯著提升。
🚀 快速開始
本部分展示了Idefics3-8B
的生成代碼片段。
import requests
import torch
from PIL import Image
from io import BytesIO
from transformers import AutoProcessor, AutoModelForVision2Seq
from transformers.image_utils import load_image
DEVICE = "cuda:0"
# Note that passing the image urls (instead of the actual pil images) to the processor is also possible
image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")
processor = AutoProcessor.from_pretrained("HuggingFaceM4/Idefics3-8B-Llama3")
model = AutoModelForVision2Seq.from_pretrained(
"HuggingFaceM4/Idefics3-8B-Llama3", torch_dtype=torch.bfloat16
).to(DEVICE)
# Create inputs
messages = [
{
"role": "user",
"content": [
{"type": "image"},
{"type": "text", "text": "What do we see in this image?"},
]
},
{
"role": "assistant",
"content": [
{"type": "text", "text": "In this image, we can see the city of New York, and more specifically the Statue of Liberty."},
]
},
{
"role": "user",
"content": [
{"type": "image"},
{"type": "text", "text": "And how about this image?"},
]
},
]
prompt = processor.apply_chat_template(messages, add_generation_prompt=True)
inputs = processor(text=prompt, images=[image1, image2], return_tensors="pt")
inputs = {k: v.to(DEVICE) for k, v in inputs.items()}
# Generate
generated_ids = model.generate(**inputs, max_new_tokens=500)
generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)
print(generated_texts)
文本生成推理
TODO.
✨ 主要特性
- 支持任意的圖像和文本輸入序列,輸出文本。
- 可用於圖像描述、視覺問答等多模態任務。
- 相較於Idefics1和Idefics2,在OCR、文檔理解和視覺推理等方面能力顯著提升。
📚 詳細文檔
模型概述
屬性 | 詳情 |
---|---|
開發者 | Hugging Face |
模型類型 | 多模態模型(圖像 + 文本) |
語言(NLP) | 英文 |
許可證 | Apache 2.0 |
父模型 | google/siglip-so400m-patch14-384 和 meta-llama/Meta-Llama-3.1-8B-Instruct |
更多信息資源 | - Idefics1論文:OBELICS: An Open Web-Scale Filtered Dataset of Interleaved Image-Text Documents - Idefics2論文:What matters when building vision-language models? - Idefics3論文:Building and better understanding vision-language models: insights and future directions |
用途
Idefics3-8B
可用於多模態(圖像 + 文本)任務的推理,輸入由文本查詢和一張(或多張)圖像組成,文本和圖像可以任意交錯。這些任務包括圖像描述、視覺問答等,但該模型不支持圖像生成。
Idefics3-8B的後訓練僅涉及有監督的微調階段,沒有進行基於人類反饋的強化學習(RLHF)對齊。因此,模型可能會給出簡短的回答,或者需要多次提示才能完全滿足用戶的請求。在助手的回覆前添加前綴,如“讓我們逐步解決這個問題”,已被證明可以有效影響生成的輸出。
若要在特定任務上微調Idefics3-8B
,我們提供了一個微調教程。其他用於Idefics2微調的資源(可輕鬆適配Idefics3):
模型優化
精度選擇
如果你的GPU支持,我們首先建議以半精度(torch.float16
或 torch.bfloat16
)加載(並運行推理)模型。
model = AutoModelForVision2Seq.from_pretrained(
"HuggingFaceM4/Idefics3-8B-Llama3",
+ torch_dtype=torch.bfloat16,
).to(DEVICE)
視覺編碼器效率
在初始化處理器(AutoProcessor.from_pretrained
)時,你可以通過添加 size= {"longest_edge": N*364}
來選擇圖像將被重新縮放的默認分辨率,其中 N
是你期望的值。在實踐中,N = 4
效果最佳(這是默認值),但對於非常大的圖像,傳遞 N = 5
可能會更合適。這將影響傳遞給語言模型的視覺標記數量。如果你受到GPU內存的限制,可以減小 N
的值,例如選擇 N = 3
或 N = 2
,特別是對於低分辨率的圖像。
使用Flash-attention 2加速生成
點擊展開。
首先,確保安裝了 flash-attn
。有關包的安裝,請參考Flash Attention的原始倉庫。只需將上述代碼片段修改為:
model = AutoModelForVision2Seq.from_pretrained(
"HuggingFaceM4/Idefics3-8B-Llama3",
+ torch_dtype=torch.bfloat16,
+ _attn_implementation="flash_attention_2",
).to(DEVICE)
濫用和超出範圍的使用
在高風險場景中使用該模型超出了其適用範圍。該模型並非為關鍵決策而設計,也不適用於對個人生計或福祉有重大影響的用途。模型輸出的內容看似事實,但可能並不正確。超出範圍的使用包括:
- 用於評估或評分個人,如用於就業、教育或信用評估。
- 將模型用於關鍵的自動決策、生成事實性內容、創建可靠的摘要或生成必須正確的預測。
故意使用該模型造成傷害、侵犯人權或進行其他惡意活動,屬於對該模型的濫用。這包括:
- 生成垃圾郵件。
- 傳播虛假信息和進行影響操作。
- 詆譭和誹謗。
- 騷擾和虐待。
- 欺騙。
- 未經同意的冒充和模仿。
- 未經同意的監視。
🔧 技術細節
Idefics3相較於Idefics2有了很大的改進,特別是在文檔理解任務上。它為各種特定用例的微調提供了堅實的基礎。
模型 | MMMU (驗證集) |
MathVista (測試集) |
MMStar (驗證集) |
DocVQA (測試集) |
TextVQA (驗證集) |
---|---|---|---|---|---|
Idefics2-8B | 45.2 | 52.2 | 49.5 | 74.0 | 73.0 |
Idefics3-8B | 46.6 | 58.4 | 55.9 | 87.7 | 74.9 |
與Idefics2相比,Idefics3有以下幾點變化:
- 我們使用169個視覺標記來編碼大小為364x364的圖像。每個圖像被分割成多個大小最大為364x364的子圖像,然後分別進行編碼。
- 對於微調數據集,我們擴展了The Cauldron,並添加了幾個數據集,包括Docmatix。我們將盡快把這些數據集推送到The Cauldron的同一倉庫(待辦事項)。
關於模型訓練的更多細節,請參考我們的技術報告。
📄 許可證
該模型基於兩個預訓練模型構建:google/siglip-so400m-patch14-384 和 meta-llama/Meta-Llama-3.1-8B-Instruct。我們以Apache 2.0許可證發佈Idefics3的檢查點。
引用
BibTeX:
@misc{laurençon2024building,
title={Building and better understanding vision-language models: insights and future directions.},
author={Hugo Laurençon and Andrés Marafioti and Victor Sanh and Léo Tronchon},
year={2024},
eprint={2408.12637},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
致謝
感謝@andito和@amyeroberts在集成到Transformers方面提供的幫助。








