模型概述
模型特點
模型能力
使用案例
🚀 Florence-2:推動多種視覺任務的統一表示
Florence-2是一個先進的視覺基礎模型,採用基於提示的方法處理廣泛的視覺和視覺語言任務。它可以解釋簡單的文本提示,執行圖像描述、目標檢測和分割等任務。該模型利用包含1.26億張圖像和54億個註釋的FLD - 5B數據集進行多任務學習。其序列到序列的架構使其在零樣本和微調設置中都表現出色,是一個極具競爭力的視覺基礎模型。
🚀 快速開始
本Hub倉庫包含了微軟Florence - 2模型在HuggingFace的transformers
庫中的實現。所有模型均使用float16進行訓練。使用以下代碼開始使用該模型:
import requests
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
prompt = "<OD>"
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=4096,
num_beams=3,
do_sample=False
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(generated_text, task="<OD>", image_size=(image.width, image.height))
print(parsed_answer)
✨ 主要特性
- 多任務處理:能夠處理多種視覺和視覺語言任務,如圖像描述、目標檢測、分割等。
- 提示驅動:通過簡單的文本提示即可執行各種任務。
- 多任務學習:利用FLD - 5B數據集進行多任務學習。
- 序列到序列架構:在零樣本和微調設置中都能表現出色。
💻 使用示例
基礎用法
import requests
import torch
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
image = Image.open(requests.get(url, stream=True).raw)
def run_example(task_prompt, text_input=None):
if text_input is None:
prompt = task_prompt
else:
prompt = task_prompt + text_input
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
num_beams=3
)
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
parsed_answer = processor.post_process_generation(generated_text, task=task_prompt, image_size=(image.width, image.height))
print(parsed_answer)
高級用法
不同任務示例
點擊展開
圖像描述
prompt = "<CAPTION>"
run_example(prompt)
詳細圖像描述
prompt = "<DETAILED_CAPTION>"
run_example(prompt)
更詳細圖像描述
prompt = "<MORE_DETAILED_CAPTION>"
run_example(prompt)
圖像描述到短語定位
圖像描述到短語定位任務需要額外的文本輸入,即圖像描述。 圖像描述到短語定位結果格式: {'<CAPTION_TO_PHRASE_GROUNDING>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['', '', ...]}}
task_prompt = "<CAPTION_TO_PHRASE_GROUNDING>"
results = run_example(task_prompt, text_input="A green car parked in front of a yellow building.")
目標檢測
目標檢測結果格式: {'<OD>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['label1', 'label2', ...]} }
prompt = "<OD>"
run_example(prompt)
密集區域描述
密集區域描述結果格式: {'<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['label1', 'label2', ...]} }
prompt = "<DENSE_REGION_CAPTION>"
run_example(prompt)
區域提議
區域提議結果格式: {'<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...], 'labels': ['', '', ...]}}
prompt = "<REGION_PROPOSAL>"
run_example(prompt)
光學字符識別(OCR)
prompt = "<OCR>"
run_example(prompt)
帶區域的OCR
帶區域的OCR輸出格式: {'<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
prompt = "<OCR_WITH_REGION>"
run_example(prompt)
輸出目標檢測置信度分數
def run_example_with_score(task_prompt, text_input=None):
if text_input is None:
prompt = task_prompt
else:
prompt = task_prompt + text_input
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
num_beams=3,
return_dict_in_generate=True,
output_scores=True,
)
generated_text = processor.batch_decode(generated_ids.sequences, skip_special_tokens=False)[0]
prediction, scores, beam_indices = generated_ids.sequences, generated_ids.scores, generated_ids.beam_indices
transition_beam_scores = model.compute_transition_scores(
sequences=prediction,
scores=scores,
beam_indices=beam_indices,
)
parsed_answer = processor.post_process_generation(sequence=generated_ids.sequences[0],
transition_beam_score=transition_beam_scores[0],
task=task_prompt, image_size=(image.width, image.height)
)
print(parsed_answer)
prompt = "<OD>"
run_example_with_score(prompt)
更多詳細示例,請參考notebook
📚 詳細文檔
模型概述
這是Florence - 2 - large模型的繼續預訓練版本,上下文長度為4k,僅使用了0.1B個樣本進行繼續預訓練,因此可能訓練得不夠充分。此外,OCR任務已更新為使用行分隔符('\n')。COCO目標檢測平均精度(AP)為39.8。
資源和技術文檔
模型信息
屬性 | 詳情 |
---|---|
模型類型 | Florence - 2是一個先進的視覺基礎模型,採用基於提示的方法處理廣泛的視覺和視覺語言任務 |
訓練數據 | FLD - 5B數據集,包含1.26億張圖像和54億個註釋 |
不同版本模型
模型 | 模型大小 | 模型描述 |
---|---|---|
Florence - 2 - base[HF] | 0.23B | 使用FLD - 5B進行預訓練的模型 |
Florence - 2 - large[HF] | 0.77B | 使用FLD - 5B進行預訓練的模型 |
Florence - 2 - base - ft[HF] | 0.23B | 在一系列下游任務上微調的模型 |
Florence - 2 - large - ft[HF] | 0.77B | 在一系列下游任務上微調的模型 |
基準測試
Florence - 2零樣本性能
以下表格展示了通用視覺基礎模型在圖像描述和目標檢測評估任務上的零樣本性能。這些模型在訓練階段未接觸評估任務的訓練數據。
方法 | 參數數量 | COCO圖像描述測試CIDEr | NoCaps驗證CIDEr | TextCaps驗證CIDEr | COCO目標檢測驗證集2017 mAP |
---|---|---|---|---|---|
Flamingo | 80B | 84.3 | - | - | - |
Florence - 2 - base | 0.23B | 133.0 | 118.7 | 70.1 | 34.7 |
Florence - 2 - large | 0.77B | 135.6 | 120.8 | 72.8 | 37.5 |
以下表格繼續展示了在其他視覺語言評估任務上的性能比較。
方法 | Flickr30k測試R@1 | Refcoco驗證準確率 | Refcoco測試 - A準確率 | Refcoco測試 - B準確率 | Refcoco+驗證準確率 | Refcoco+測試 - A準確率 | Refcoco+測試 - B準確率 | Refcocog驗證準確率 | Refcocog測試準確率 | Refcoco RES驗證mIoU |
---|---|---|---|---|---|---|---|---|---|---|
Kosmos - 2 | 78.7 | 52.3 | 57.4 | 47.3 | 45.5 | 50.7 | 42.2 | 60.6 | 61.7 | - |
Florence - 2 - base | 83.6 | 53.9 | 58.4 | 49.7 | 51.5 | 56.4 | 47.9 | 66.3 | 65.1 | 34.6 |
Florence - 2 - large | 84.4 | 56.3 | 61.6 | 51.4 | 53.6 | 57.9 | 49.9 | 68.0 | 67.0 | 35.8 |
Florence - 2微調性能
我們在一系列下游任務上對Florence - 2模型進行微調,得到了兩個通用模型Florence - 2 - base - ft和Florence - 2 - large - ft,它們可以執行廣泛的下游任務。
以下表格比較了專用模型和通用模型在各種圖像描述和視覺問答(VQA)任務上的性能。專用模型針對每個任務進行了特定的微調,而通用模型則以任務無關的方式在所有任務上進行微調。符號"▲"表示使用外部OCR作為輸入。
方法 | 參數數量 | COCO圖像描述Karpathy測試CIDEr | NoCaps驗證CIDEr | TextCaps驗證CIDEr | VQAv2測試 - dev準確率 | TextVQA測試 - dev準確率 | VizWiz VQA測試 - dev準確率 |
---|---|---|---|---|---|---|---|
專用模型 | |||||||
CoCa | 2.1B | 143.6 | 122.4 | - | 82.3 | - | - |
BLIP - 2 | 7.8B | 144.5 | 121.6 | - | 82.2 | - | - |
GIT2 | 5.1B | 145.0 | 126.9 | 148.6 | 81.7 | 67.3 | 71.0 |
Flamingo | 80B | 138.1 | - | - | 82.0 | 54.1 | 65.7 |
PaLI | 17B | 149.1 | 127.0 | 160.0▲ | 84.3 | 58.8 / 73.1▲ | 71.6 / 74.4▲ |
PaLI - X | 55B | 149.2 | 126.3 | 147.0 / 163.7▲ | 86.0 | 71.4 / 80.8▲ | 70.9 / 74.6▲ |
通用模型 | |||||||
Unified - IO | 2.9B | - | 100.0 | - | 77.9 | - | 57.4 |
Florence - 2 - base - ft | 0.23B | 140.0 | 116.7 | 143.9 | 79.7 | 63.6 | 63.6 |
Florence - 2 - large - ft | 0.77B | 143.3 | 124.9 | 151.1 | 81.7 | 73.5 | 72.6 |
方法 | 參數數量 | COCO目標檢測驗證集2017 mAP | Flickr30k測試R@1 | RefCOCO驗證準確率 | RefCOCO測試 - A準確率 | RefCOCO測試 - B準確率 | RefCOCO+驗證準確率 | RefCOCO+測試 - A準確率 | RefCOCO+測試 - B準確率 | RefCOCOg驗證準確率 | RefCOCOg測試準確率 | RefCOCO RES驗證mIoU |
---|---|---|---|---|---|---|---|---|---|---|---|---|
專用模型 | ||||||||||||
SeqTR | - | - | - | 83.7 | 86.5 | 81.2 | 71.5 | 76.3 | 64.9 | 74.9 | 74.2 | - |
PolyFormer | - | - | - | 90.4 | 92.9 | 87.2 | 85.0 | 89.8 | 78.0 | 85.8 | 85.9 | 76.9 |
UNINEXT | 0.74B | 60.6 | - | 92.6 | 94.3 | 91.5 | 85.2 | 89.6 | 79.8 | 88.7 | 89.4 | - |
Ferret | 13B | - | - | 89.5 | 92.4 | 84.4 | 82.8 | 88.1 | 75.2 | 85.8 | 86.3 | - |
通用模型 | ||||||||||||
UniTAB | - | - | - | 88.6 | 91.1 | 83.8 | 81.0 | 85.4 | 71.6 | 84.6 | 84.7 | - |
Florence - 2 - base - ft | 0.23B | 41.4 | 84.0 | 92.6 | 94.8 | 91.5 | 86.8 | 91.7 | 82.2 | 89.8 | 82.2 | 78.0 |
Florence - 2 - large - ft | 0.77B | 43.4 | 85.2 | 93.4 | 95.3 | 92.0 | 88.3 | 92.9 | 83.6 | 91.2 | 91.7 | 80.5 |
🔧 技術細節
BibTex引用信息
@article{xiao2023florence,
title={Florence-2: Advancing a unified representation for a variety of vision tasks},
author={Xiao, Bin and Wu, Haiping and Xu, Weijian and Dai, Xiyang and Hu, Houdong and Lu, Yumao and Zeng, Michael and Liu, Ce and Yuan, Lu},
journal={arXiv preprint arXiv:2311.06242},
year={2023}
}
📄 許可證
本項目採用MIT許可證,詳情請見許可證鏈接。









