Florence-2:推进多种视觉任务的统一表征

Florence-2:推进多种视觉任务的统一表征

模型摘要

该 Hub 存储库包含来自微软的 Florence-2 模型的 HuggingFacetransformers实现。

Florence-2 是一种先进的视觉基础模型,它使用基于提示的方法来处理各种视觉和视觉语言任务。Florence-2 可以解释简单的文本提示来执行字幕、对象检测和分割等任务。它利用我们的 FLD-5B 数据集(包含 1.26 亿张图像中的 54 亿条注释)来掌握多任务学习。该模型的序列到序列架构使其能够在零样本和微调设置中表现出色,证明是一个具有竞争力的视觉基础模型。

资源和技术文档:

模型模型大小模型描述
佛罗伦萨-2-基[HF]0.23亿使用 FLD-5B 进行预训练的模型
佛罗伦萨-2-大[HF]0.77亿使用 FLD-5B 进行预训练的模型
佛罗伦萨-2-基-英尺[HF]0.23亿在一系列下游任务上对模型进行微调
佛罗伦萨-2-大-英尺[HF]0.77亿在一系列下游任务上对模型进行微调

如何开始使用该模型

使用以下代码开始使用该模型。所有模型均使用 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=1024,
    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)

任务

该模型能够通过改变提示来执行不同的任务。

首先,让我们定义一个函数来运行提示。

点击展开

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)

以下是可以执行的任务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 结果格式:{'<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)

更多详细示例,请参阅笔记本

基准

Florence-2 零样本性能

下表展示了通用视觉基础模型在图像字幕和物体检测评估任务上的零样本性能。这些模型在训练阶段没有接触过评估任务的训练数据。

方法#参数COCO Cap. 测试 CIDErNoCaps val CIDErTextCaps val CIDErCOCO Det. val2017 mAP
火烈鸟80B84.3---
佛罗伦萨-2-基地0.23亿133.0118.770.134.7
佛罗伦萨-2-大0.77亿135.6120.872.837.5

下表继续与其他视觉语言评估任务的表现进行比较。

方法Flickr30k 测试 R@1Refcoco val 准确度Refcoco 测试-A 准确度Refcoco 测试-B 准确度Refcoco+ val 准确度Refcoco+ 测试-A 准确度Refcoco+ test-B 准确度参考认知值准确度Refcocog 测试准确度Refcoco RES 值
科斯莫斯-278.752.357.447.345.550.742.260.661.7-
佛罗伦萨-2-基地83.653.958.449.751.556.447.966.365.134.6
佛罗伦萨-2-大84.456.361.651.453.657.949.968.067.035.8

佛罗伦萨-2号微调性能

我们通过一系列下游任务对 Florence-2 模型进行微调,得到了两个通用模型Florence-2-base-ftFlorence-2-large-ft,可以执行广泛的下游任务。

下表比较了专家模型和通才模型在各种字幕和视觉问答 (VQA) 任务上的表现。专家模型针对每项任务进行了专门的微调,而通才模型则以与任务无关的方式在所有任务上进行了微调。符号“▲”表示使用外部 OCR 作为输入。

方法# 参数COCO Caption Karpathy 测试 CIDErNoCaps val CIDErTextCaps val CIDErVQAv2 测试-dev 加速TextVQA 测试-dev 权限VizWiz VQA 测试开发 Acc
专业模特
科卡2.1B143.6122.4-82.3--
BLIP-27.8亿144.5121.6-82.2--
吉特25.1B145.0126.9148.681.767.371.0
火烈鸟80B138.1--82.054.165.7
巴利17B149.1127.0160.0▲84.358.8 / 73.1▲71.6 / 74.4▲
帕利一X55B149.2126.3147.0 / 163.7▲86.071.4 / 80.8▲70.9 / 74.6▲
通才模型
统一IO2.9亿-100.0-77.9-57.4
佛罗伦萨-2-基-英尺0.23亿140.0116.7143.979.763.663.6
佛罗伦萨-2-大-英尺0.77亿143.3124.9151.181.773.572.6
方法# 参数COCO Det. val2017 mAPFlickr30k 测试 R@1RefCOCO val 准确率RefCOCO 测试-A 准确率RefCOCO 测试-B 准确率RefCOCO+ val 准确率RefCOCO+ 测试-A 准确率RefCOCO+ 测试-B 准确率RefCOCOg 值准确度RefCOCOg 测试准确度RefCOCO RES 值 mIoU
专业模特
序列TR---83.786.581.271.576.364.974.974.2-
聚合成型机---90.492.987.285.089.878.085.885.976.9
联合国际电信联盟0.74亿60.6-92.694.391.585.289.679.888.789.4-
雪貂13B--89.592.484.482.888.175.285.886.3-
通才模型
联合技术咨询委员会---88.691.183.881.085.471.684.684.7-
佛罗伦萨-2-基-英尺0.23亿41.484.092.694.891.586.891.782.289.882.278.0
佛罗伦萨-2-大-英尺0.77亿43.485.293.495.392.088.392.983.691.291.780.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}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

强化学习曾小健

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值