模型概述
模型特點
模型能力
使用案例
🚀 上傳的微調模型
本項目上傳了經過微調的模型,該模型基於通義千問 Qwen2-VL 進行優化,能高效準確地從圖像中提取文本,可廣泛應用於文檔、表格等的 OCR 任務。
🚀 快速開始
以下是使用該模型的示例代碼:
加載模型和處理器
from transformers import AutoProcessor, AutoModelForImageTextToText
processor = AutoProcessor.from_pretrained("JackChew/Qwen2-VL-2B-OCR")
model = AutoModelForImageTextToText.from_pretrained("JackChew/Qwen2-VL-2B-OCR")
加載圖像
# Load your image
from PIL import Image
image_path = "xxxxx" # Replace with your image path
image = Image.open(image_path)
準備模型、預處理輸入並進行推理
import requests
import torch
from torchvision import io
from typing import Dict
model = model.to("cuda")
conversation = [
{
"role":"user",
"content":[
{
"type":"image",
},
{
"type":"text",
"text":"extract all data from this payslip without miss anything"
}
]
}
]
# Preprocess the inputs
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
# Excepted output: '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<|vision_start|><|image_pad|><|vision_end|>Describe this image.<|im_end|>\n<|im_start|>assistant\n'
inputs = processor(text=[text_prompt], images=[image], padding=True, return_tensors="pt")
inputs = inputs.to('cuda')
# Inference: Generation of the output
output_ids = model.generate(**inputs, max_new_tokens=2048)
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(output_text)
推理過程中處理 CUDA 內存問題
如果在模型推理過程中遇到 CUDA 內存問題,常見的解決方法是調整輸入圖像的大小以減小其佔用的內存。這有助於減少內存佔用,使模型更高效地處理圖像。
# Resize the image to reduce its size (e.g., scale to half its original size)
image = image.resize((image.width // 2, image.height // 2))
✨ 主要特性
- 高效微調:使用 Unsloth 和 Huggingface 的 TRL 庫,訓練速度提升了 2 倍。
- 精準 OCR:專注於文本提取,能夠從文檔、表格和工資單等圖像中提取完整文本,確保信息不丟失。
- 廣泛應用:可應用於工資系統、金融、法律文件分析等多個領域。
📦 安裝指南
文檔未提供具體安裝步驟,暫不展示。
💻 使用示例
基礎用法
from transformers import AutoProcessor, AutoModelForImageTextToText
processor = AutoProcessor.from_pretrained("JackChew/Qwen2-VL-2B-OCR")
model = AutoModelForImageTextToText.from_pretrained("JackChew/Qwen2-VL-2B-OCR")
from PIL import Image
image_path = "xxxxx" # Replace with your image path
image = Image.open(image_path)
import requests
import torch
from torchvision import io
from typing import Dict
model = model.to("cuda")
conversation = [
{
"role":"user",
"content":[
{
"type":"image",
},
{
"type":"text",
"text":"extract all data from this payslip without miss anything"
}
]
}
]
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
inputs = processor(text=[text_prompt], images=[image], padding=True, return_tensors="pt")
inputs = inputs.to('cuda')
output_ids = model.generate(**inputs, max_new_tokens=2048)
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(output_text)
高級用法
# 處理 CUDA 內存問題
from PIL import Image
image_path = "xxxxx" # Replace with your image path
image = Image.open(image_path)
image = image.resize((image.width // 2, image.height // 2))
from transformers import AutoProcessor, AutoModelForImageTextToText
processor = AutoProcessor.from_pretrained("JackChew/Qwen2-VL-2B-OCR")
model = AutoModelForImageTextToText.from_pretrained("JackChew/Qwen2-VL-2B-OCR")
import requests
import torch
from torchvision import io
from typing import Dict
model = model.to("cuda")
conversation = [
{
"role":"user",
"content":[
{
"type":"image",
},
{
"type":"text",
"text":"extract all data from this payslip without miss anything"
}
]
}
]
text_prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
inputs = processor(text=[text_prompt], images=[image], padding=True, return_tensors="pt")
inputs = inputs.to('cuda')
output_ids = model.generate(**inputs, max_new_tokens=2048)
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(inputs.input_ids, output_ids)]
output_text = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(output_text)
📚 詳細文檔
模型描述
通義千問 QWEN OCR 是一個專注於文本提取的專有模型,專門用於從文檔、表格和工資單等圖像中提取文本。該模型的主要目標是從圖像中提取完整的文本,確保不遺漏任何信息。
Qwen2-VL-2B-OCR 是 unsloth/Qwen2-VL-2B-Instruct 的微調版本,專門針對光學字符識別(OCR)進行了優化。該模型經過訓練,能夠從圖像中提取完整的文本,尤其適用於工資單、發票和表格等文檔。模型旨在提供準確的文本提取,同時儘量減少信息損失,確保捕獲每一個細節。
該模型採用了先進的圖像文本生成技術,可無縫應用於各種 OCR 任務,包括從具有結構化佈局的複雜文檔中提取文本。
預期用途
該模型的主要用途是從圖像或文檔中提取數據,特別是從工資單和表格中提取數據,確保不遺漏任何關鍵細節。它可應用於多個領域,如工資系統、金融、法律文件分析以及任何需要文檔提取的領域。
提示示例:
- 文本:模型最適合處理
"Extract all text from image/payslip without miss anything"
這樣的提示。
模型基準
基準測試 | InternVL2-2B | MiniCPM-V 2.0 | Qwen2-VL-2B |
---|---|---|---|
MMMUval | 36.3 | 38.2 | 41.1 |
DocVQAtest | 86.9 | - | 90.1 |
InfoVQAtest | 58.9 | - | 65.5 |
ChartQAtest | 76.2 | - | 73.5 |
TextVQAval | 73.4 | - | 79.7 |
OCRBench | 781 | 605 | 794 |
MTVQA | - | - | 20.0 |
VCRen easy | - | - | 81.45 |
VCRzh easy | - | - | 46.16 |
RealWorldQA | 57.3 | 55.8 | 62.9 |
MMEsum | 1876.8 | 1808.6 | 1872.0 |
MMBench-ENtest | 73.2 | 69.1 | 74.9 |
MMBench-CNtest | 70.9 | 66.5 | 73.5 |
MMBench-V1.1test | 69.6 | 65.8 | 72.2 |
MMT-Benchtest | - | - | 54.5 |
MMStar | 49.8 | 39.1 | 48.0 |
MMVetGPT-4-Turbo | 39.7 | 41.0 | 49.5 |
HallBenchavg | 38.0 | 36.1 | 41.7 |
MathVistatestmini | 46.0 | 39.8 | 43.0 |
MathVision | - | - | 12.4 |
微調後,該模型在提取工資單的所有相關部分方面有了顯著改進,包括之前缺失的 扣除項 部分。
示例輸出對比
微調後模型
以下是從工資單中提取的數據:
員工信息:
- 入職日期:2018 - 06 - 23
- 工資週期:2021 年 8 月
- 員工姓名:Sally Harley
- 職位:營銷主管
- 部門:營銷
收入:
收入 | 金額 | 扣除項 | 金額 |
---|---|---|---|
基本工資 | 10000 | 公積金 | 1200 |
獎金 | 1000 | 職業稅 | 500 |
房租補貼 | 400 | 貸款 | 400 |
餐補 | 200 | 9500 |
總收入:$11,600 總扣除項:$2,100 實發工資:$9,500
僱主簽名 員工簽名
原始模型
原始模型提取了以下數據,但遺漏了 扣除項 部分:
- 入職日期:2018 - 06 - 23
- 工資週期:2021 年 8 月
- 員工姓名:Sally Harley
- 職位:營銷主管
- 部門:營銷
- 收入:
- 基本工資:$10,000
- 獎金:$1,000
- 房租補貼:$400
- 餐補:$200
- 總收入:$11,600
- 總扣除項:$2,100
- 實發工資:$9,500
- 僱主簽名:[簽名]
- 員工簽名:[簽名]
- 這是系統生成的工資單
🔧 技術細節
模型微調細節
該模型使用 Unsloth 框架進行微調,藉助 Huggingface 的 TRL(訓練強化學習)庫,訓練速度提高了 2 倍。應用了 LoRA(低秩自適應)技術,僅微調一小部分參數,顯著減少了訓練時間和計算資源。微調同時關注視覺和語言層,確保模型能夠高效處理複雜的 OCR 任務。
可訓練的總參數:57,901,056
硬件要求
要運行此模型,建議使用至少具有 16 GB VRAM 的 GPU。由於訓練需要大量內存,對於內存較小的 GPU,可能需要使用較小的批量大小或梯度累積。
模型架構
如果您想了解更多關於模型的架構及其詳細規格,可以查看 Hugging Face 上的源頁面:Qwen2-VL-2B-Instruct Model Page
📄 許可證
- 許可證:apache - 2.0
模型信息表格
屬性 | 詳情 |
---|---|
基礎模型 | unsloth/Qwen2-VL-2B-Instruct-16Bit |
標籤 | text-generation-inference, text-extraction, transformers, unsloth/Qwen2-VL-2B-Instruct-16Bit |
開發者 | JackChew |
微調來源模型 | unsloth/Qwen2-VL-2B-Instruct-16Bit |
許可證 | apache - 2.0 |
語言 | en |








