模型简介
模型特点
模型能力
使用案例
🚀 Florence-2:推动多种视觉任务的统一表示
Florence-2 是一个先进的视觉基础模型,采用基于提示的方法处理广泛的视觉和视觉语言任务。它能解读简单文本提示,执行图像描述、目标检测和分割等任务。该模型利用包含 54 亿个注释、1.26 亿张图像的 FLD - 5B 数据集进行多任务学习,其序列到序列架构使其在零样本和微调设置中都表现出色。
🚀 快速开始
使用以下代码开始使用该模型:
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)
✨ 主要特性
- 多任务处理:通过基于提示的方法,可处理多种视觉和视觉语言任务,如图像描述、目标检测和分割等。
- 多任务学习:利用 FLD - 5B 数据集进行多任务学习,提升模型性能。
- 序列到序列架构:在零样本和微调设置中都能表现出色。
📚 详细文档
模型概述
这是微软模型的一个副本,进行了一些修复。修复的 PR 在原始模型上处于开放状态,在合并之前,我使用这个副本来确保一切设置正确。
此 Hub 仓库包含了 HuggingFace 的 transformers
对微软 Florence - 2 模型的实现。
Florence - 2 是一个先进的视觉基础模型,采用基于提示的方法来处理广泛的视觉和视觉语言任务。它可以解释简单的文本提示,以执行诸如图像描述、目标检测和分割等任务。该模型利用我们的 FLD - 5B 数据集,该数据集包含 1.26 亿张图像上的 54 亿个注释,来掌握多任务学习。模型的序列到序列架构使其在零样本和微调设置中都能表现出色,证明它是一个有竞争力的视觉基础模型。
资源和技术文档
模型信息
属性 | 详情 |
---|---|
模型类型 | 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)
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)
以下是 Florence - 2
可以执行的任务:
点击展开
图像描述
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 结果格式:
{'
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)
更多详细示例,请参考 notebook
基准测试
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 测试 - 开发集准确率 | 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 目标检测验证集 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 许可证,详情请见 LICENSE。








