>從賬單收據中提取和分類費用。
>
paligemma 2專為各種視覺語言任務而設計,包括圖像和簡短的視頻字幕,視覺問題答案,光學特徵識別(OCR),對象檢測和分割。
>圖中使用的圖像的來源:google paligemma 2混合模型設計用於:
>步驟2:模型初始化 > bitsandbytes量化有助於減少記憶使用量的同時保持性能,從而可以在有限的GPU資源上運行大型模型。在此實現中,我們使用4位量化來進一步優化內存效率。
>步驟4:推理使用paligemma
> extract_total_amount()函數處理圖像以使用OCR從收據中提取總量。它構建了一個查詢(問題),指示模型僅提取數值值,然後調用ask_model()函數以生成模型的響應。
>對象檢測和分割:它在圖像中標識,標籤和片段對象進行結構化分析。
>步驟1:先決條件
>在開始之前,讓我們確保我們安裝了以下工具和庫:
>
pip install gradio -U bitsandbytes -U transformers -q
import gradio as gr
import torch
import pandas as pd
import matplotlib.pyplot as plt
from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor, BitsAndBytesConfig
from transformers import BitsAndBytesConfig
from PIL import Image
import re
>我們使用量化配置並加載Paligemma 2混合模型以優化性能。對於此演示,我們將使用448 x 448輸入圖像分辨率的10B參數模型。您需要至少具有40GB內存(COLAB配置)的T4 GPU來運行此模型。
device = "cuda" if torch.cuda.is_available() else "cpu"
# Model setup
model_id = "google/paligemma2-10b-mix-448"
bnb_config = BitsAndBytesConfig(
load_in_8bit=True, # Change to load_in_4bit=True for even lower memory usage
llm_int8_threshold=6.0,
)
# Load model with quantization
model = PaliGemmaForConditionalGeneration.from_pretrained(
model_id, quantization_config=bnb_config
).eval()
# Load processor
processor = PaliGemmaProcessor.from_pretrained(model_id)
# Print success message
print("Model and processor loaded successfully!")
步驟3:圖像處理現在,我們的圖像已準備好推理。
>
def ensure_rgb(image: Image.Image) -> Image.Image:
if image.mode != "RGB":
image = image.convert("RGB")
return image
>現在,我們設置了用於使用模型運行推斷的主要功能。此功能將帶有輸入圖像和問題,將它們整合到提示中,然後通過處理器將它們傳遞給模型。
>現在我們已經準備好主函數,我們將下一個從圖像中提取關鍵參數 - 在我們的情況下,這些是總金額和商品類別。pip install gradio -U bitsandbytes -U transformers -q
import gradio as gr
import torch
import pandas as pd
import matplotlib.pyplot as plt
from transformers import PaliGemmaForConditionalGeneration, PaliGemmaProcessor, BitsAndBytesConfig
from transformers import BitsAndBytesConfig
from PIL import Image
import re
>上面的功能創建了一個餅圖,以可視化不同類別的支出分佈。如果不存在有效的支出數據,它將生成一個空白的圖形,並帶有表示“無支出數據”的消息。否則,它將創建帶有類別標籤和百分比值的餅圖,確保比例且良好的可視化。
步驟6:同時分析多個賬單device = "cuda" if torch.cuda.is_available() else "cpu" # Model setup model_id = "google/paligemma2-10b-mix-448" bnb_config = BitsAndBytesConfig( load_in_8bit=True, # Change to load_in_4bit=True for even lower memory usage llm_int8_threshold=6.0, ) # Load model with quantization model = PaliGemmaForConditionalGeneration.from_pretrained( model_id, quantization_config=bnb_config ).eval() # Load processor processor = PaliGemmaProcessor.from_pretrained(model_id) # Print success message print("Model and processor loaded successfully!")
我們通常有多個賬單要分析,因此讓我們創建一個函數來同時處理所有賬單。
初始化存儲:我們創建用於存儲結果和圖像的列表,將total_spending設置為0,並為類別總計定義字典。
def ensure_rgb(image: Image.Image) -> Image.Image: if image.mode != "RGB": image = image.convert("RGB") return image處理每個賬單:
該功能將用戶輸入連接到process_multiple_bills()函數,以確保無縫數據提取和可視化。最後,demo.launch()函數啟動了用於實時互動的Gradio應用程序。
>
>我還嘗試了兩個基於圖像的賬單(亞馬遜購物發票)的演示,並得到以下結果。
注意:VLMS發現很難提取數字,這有時可能導致結果不正確。例如,它提取了以下第二賬單的錯誤總金額。這是可以使用較大型號或簡單地對現有模型進行微調來糾正。
>以上是Paligemma 2 Mix:Demo OCR項目的指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!