许可证: mit
许可证链接: https://huggingface.co/microsoft/Florence-2-base-ft/resolve/main/LICENSE
任务标签: 图像文本到文本
标签:
Florence-2:推进多样化视觉任务的统一表征
模型概述
此Hub仓库包含微软Florence-2模型的HuggingFace transformers
实现。
Florence-2是一种先进的视觉基础模型,采用基于提示的方法处理广泛的视觉和视觉语言任务。它能通过简单文本提示执行图像描述、目标检测和分割等任务。该模型利用包含1.26亿张图像、54亿标注的FLD-5B数据集实现多任务学习。其序列到序列架构使其在零样本和微调场景中均表现优异,成为极具竞争力的视觉基础模型。
技术资源:
模型 |
参数量 |
描述 |
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 |
下游任务微调版 |
快速开始
以下代码展示模型使用方法。所有模型均采用float16训练。
import requests
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-base-ft", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-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").to(device, torch_dtype)
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)
任务能力
通过修改提示词,该模型可执行多种任务。
展开查看任务示例
图像描述
run_example("<CAPTION>")
细粒度描述
run_example("<DETAILED_CAPTION>")
目标检测
输出格式:
{"<OD>": {"bboxes": [[x1,y1,x2,y2],...], "labels": [...]}}
密集区域描述
run_example("<DENSE_REGION_CAPTION>")
文字识别(OCR)
run_example("<OCR>")
更多示例详见演示笔记本
性能基准
零样本性能
模型 |
参数量 |
COCO描述CIDEr |
NoCaps CIDEr |
目标检测mAP |
Florence-2-base |
0.23B |
133.0 |
118.7 |
34.7 |
Florence-2-large |
0.77B |
135.6 |
120.8 |
37.5 |
微调后性能
模型 |
参数量 |
COCO描述CIDEr |
VQA准确率 |
目标检测mAP |
Florence-2-base-ft |
0.23B |
140.0 |
79.7 |
41.4 |
Florence-2-large-ft |
0.77B |
143.3 |
81.7 |
43.4 |
引用
@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}
}