iT邦幫忙

2025 iThome 鐵人賽

DAY 16
0
生成式 AI

VLM系列 第 16

Day 16 :Granite Vision Model

  • 分享至 

  • xImage
  •  

Granite Vision 是 IBM 在其 Granite 系列模型中,針對「視覺+語言」任務(multimodal / vision-language)在2024年所推出的模型。 它特別設計來處理「文件理解」(document understanding)這類任務,例如從表格、圖表、示意圖、資訊圖表 (infographics) 等視覺資料中擷取結構化資訊。 它既能針對文檔類型的視覺資料 (如 PDF 裡的圖表或表格),也可以處理一般的影像 + 語言任務(如圖片問答)。

Granite Vision 是以開源授權(Apache 2.0)發布,適用於研究與商業用途。目前最新的模型是2025年發表的Granite-vision-3.3-2b,是一個緊湊高效的視覺語言模型,專為視覺文檔理解而設計,能夠從表格、圖表、信息圖表、繪圖、圖表等中自動提取內容。Granite-vision-3.3-2b 引進了數個新穎的實驗性功能,例如影像分割 、 文件標籤產生和多頁支援,這個最新的版本提供了增強的安全性。模型是在精心策劃的指令進行訓練的,訓練資料包括各種公共和合成數據集,旨在支持廣泛的文檔理解和一般圖像任務,Granite-vision-3.3-2b 是透過微調具有圖像和文字模態的 Granite 大型語言模型來訓練。

從模型參數量2B,可知它也是個輕量級的VLM模型。
測試結果granite-vision-3.3-2b及granite-vision-3.2-2b都無法在T4跑,T4顯示12.7G記憶體, 連模型都無法載入,在使用了torch_dtype = torch.float16後可載入模型,但推論時顯示記憶體已耗盡,改用granite-vision-3.1-2b-preview則可以在T4正常運行。

from transformers import AutoProcessor, AutoModelForVision2Seq
from huggingface_hub import hf_hub_download
import torch

device = "cuda" if torch.cuda.is_available() else "cpu"

model_path = "ibm-granite/granite-vision-3.1-2b-preview"
processor = AutoProcessor.from_pretrained(model_path)
model = AutoModelForVision2Seq.from_pretrained(model_path).to(device)

使用的圖片

推論

url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg"

conversation = [
    {
        "role": "user",
        "content": [
            {"type": "image", "url": url},
            {"type": "text", "text": "What is the image?"},
        ],
    },
]
inputs = processor.apply_chat_template(
    conversation,
    add_generation_prompt=True,
    tokenize=True,
    return_dict=True,
    return_tensors="pt"
).to(device)

output = model.generate(**inputs, max_new_tokens=100)
print(processor.decode(output[0], skip_special_tokens=True))

(輸出結果)

<|system|>
A chat between a curious user and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the user's questions.
<|user|>

What is the image?
<|assistant|>
A turquoise Volkswagen Beetle parked on a street.


上一篇
Day15:小而強VLM-SmolVLM2
下一篇
Day 17 :專業任務型VLM - LLaVA-Med
系列文
VLM20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言