模型简介
模型特点
模型能力
使用案例
license: mit license_link: https://huggingface.co/microsoft/Florence-2-large-ft/resolve/main/LICENSE
pipeline_tag: image-to-text tags:
- vision
Florence-2:推进统一表征应对多样化视觉任务
模型概述
此版本为微软原模型的复现版本,包含若干修复。相关修复的PR已提交至原模型仓库,在合并前暂使用此版本确保功能完整。
本仓库提供了微软Florence-2模型的HuggingFace transformers
实现。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 | 下游任务微调版 |
快速开始
使用以下代码快速体验模型:
import requests
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large-ft", trust_remote_code=True)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large-ft", 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")
generated_ids = model.generate(
input_ids=inputs["input_ids"],
pixel_values=inputs["pixel_values"],
max_new_tokens=1024,
do_sample=False,
num_beams=3
)
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)
任务能力
通过切换提示词,本模型可执行多种任务。首先定义通用执行函数:
点击展开
import requests
from PIL import Image
from transformers import AutoProcessor, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large-ft", trust_remote_code=True)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large-ft", 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")
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="黄色建筑前的绿色汽车")
目标检测
输出格式: {'<OD>': {'bboxes': [[x1,y1,x2,y2], ...], 'labels': [...]} }
prompt = "<OD>"
run_example(prompt)
密集区域描述
输出格式: {'<DENSE_REGION_CAPTION>' : {'bboxes': [[x1,y1,x2,y2], ...], 'labels': [...]} }
prompt = "<DENSE_REGION_CAPTION>"
run_example(prompt)
区域提议
输出格式: {'<REGION_PROPOSAL>': {'bboxes': [[x1,y1,x2,y2], ...], 'labels': [...]}}
prompt = "<REGION_PROPOSAL>"
run_example(prompt)
文字识别
prompt = "<OCR>"
run_example(prompt)
带区域文字识别
输出格式: {'<OCR_WITH_REGION>': {'quad_boxes': [[x1,y1,x2,y2,x3,y3,x4,y4], ...], 'labels': [...]}}
prompt = "<OCR_WITH_REGION>"
run_example(prompt)
更多示例详见演示笔记本
性能基准
零样本性能
下表展示通用视觉基础模型在图像描述和目标检测任务上的零样本表现(未使用任务训练数据):
方法 | 参数量 | COCO描述测试CIDEr | NoCaps验证CIDEr | TextCaps验证CIDEr | COCO检测验证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-base-ft和Florence-2-large-ft。下表对比专用模型与通用模型在描述和视觉问答任务的表现("▲"表示使用外部OCR输入):
方法 | 参数量 | COCO描述测试CIDEr | NoCaps验证CIDEr | TextCaps验证CIDEr | VQAv2测试准确率 | TextVQA测试准确率 | VizWiz VQA测试准确率 |
---|---|---|---|---|---|---|---|
专用模型 | |||||||
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检测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 |








