florence-2-cookbook

anyantudreMIT活跃维护

微软 Florence-2 视觉语言模型的非官方实战笔记本合集,覆盖推理、自动标注和微调,方便快速上手做目标检测、OCR 等视觉任务。

Unofficial notebooks and resources for Microsoft's Florence-2 vision-language model: inference, auto-labelling and fine-tuning.

暂无版本225star +1 / 7 天21fork同步于 2026-11-11 05:59 UTC

怎么上手

由 README 提炼,执行前请确认
pip install transformers torch pillow requests
model_id = "florence-community/Florence-2-base-ft"
硬件要求按 README 说明

README 未给出具体显存要求;示例代码用 dtype=torch.float16 并 .to("cuda"),需 NVIDIA GPU;笔记本 7 同时测了 CPU 上的延迟与内存。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

Florence-2 视觉语言模型的实战笔记本合集,覆盖推理、自动标注与微调

微软 Florence-2 视觉语言模型的非官方实战笔记本合集,覆盖推理、自动标注和微调,方便快速上手做目标检测、OCR 等视觉任务。

核心能力

  • 8 个 Colab 笔记本覆盖 14 个内置任务的推理
  • 用 <OD> 批量标注图片并导出 YOLO 格式
  • 提供 DocVQA 全量微调与 Roboflow 检测集 LoRA 微调示例
  • 从 token 概率给检测框打置信度并过滤误检
  • 批量多区域提示与 GPU/CPU 速度基准测试

容易踩的坑

原始 microsoft/Florence-2-* 检查点需 trust_remote_code=True,在新版 Transformers 上会报 _supports_sdpa 或 forced_bos_token_id 错误,应改用 florence-community 检查点;需联网从 Hugging Face 下载模型权重;README 称用 transformers 5.22 测试,版本过旧可能不兼容。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

0 个版本

暂无 release 数据(可能没有发版,或尚未同步)。

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Florence-2 Cookbook

    arXiv Models on Hugging Face Transformers docs License: MIT

    Hands-on notebooks and a short guide to Florence-2, Microsoft's lightweight vision-language model: run inference on 14 built-in tasks, auto-label images, fine-tune the model on your own data, score its boxes, answer questions and measure its speed.

    For models, tools, papers and tutorials from the wider ecosystem, see the Awesome Florence-2 list.

    [!NOTE] This is an independent community project. Florence-2 was created by Microsoft; this repository is not affiliated with or endorsed by Microsoft. For the original work, see the paper and the official model card.

    Why this repository

    In early 2024, I was working on a project that needed a vision-language model for several different tasks, on limited hardware. None of the models available at the time gave satisfying results in that setting. When Microsoft released Florence-2, I was impressed by how well it performed across so many tasks for its size. This repository gathers the notebooks and notes I put together while exploring it.

    Contents

    Notebooks

    #NotebookWhat it coversOpen
    1Sample inferenceAll 14 built-in tasks: captioning, detection, grounding, segmentation, OCR, cascaded tasksOpen In Colab
    2Data auto-labellingLabel a folder of images with <OD> and export YOLO annotationsOpen In Colab
    3Fine-tuning on DocVQAFull fine-tuning for document question answeringOpen In Colab
    4Fine-tuning on a detection datasetLoRA fine-tuning on a Roboflow dataset, with mAP and confusion matrixOpen In Colab
    5Multi-region promptsAsk about many boxes in one batched call: describe, categorize or read every regionOpen In Colab
    6Confidence scoresScore boxes from the model's token probabilities and filter boxes drawn for absent objects, tested on COCOOpen In Colab
    7Speed benchmarkLatency, throughput and memory per task, model size, precision and decoding, on GPU and CPUOpen In Colab
    8Question answeringAnswer open and multiple-choice questions with the -ft checkpoints, measured on A-OKVQAOpen In Colab

    Notebook 1 is adapted from Microsoft's sample inference notebook, notebook 3 from the Hugging Face fine-tuning tutorial, and notebook 4 from Roboflow's notebook. Credit goes to their authors.

    Quickstart

    Recent versions of Hugging Face Transformers support Florence-2 natively through the florence-community checkpoints (tested with transformers 5.22):

    import requests
    import torch
    from PIL import Image
    from transformers import AutoProcessor, Florence2ForConditionalGeneration
    
    model_id = "florence-community/Florence-2-base-ft"
    model = Florence2ForConditionalGeneration.from_pretrained(model_id, dtype=torch.float16).to("cuda")
    processor = AutoProcessor.from_pretrained(model_id)
    
    url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg"
    image = Image.open(requests.get(url, stream=True).raw)
    
    task = "<OD>"
    inputs = processor(text=task, images=image, return_tensors="pt").to("cuda", torch.float16)
    generated_ids = model.generate(**inputs, max_new_tokens=1024, num_beams=3)
    text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
    print(processor.post_process_generation(text, task=task, image_size=image.size))
    # {'<OD>': {'bboxes': [[34, 160, 597, 371], ...], 'labels': ['car', 'wheel', 'wheel']}}
    

    [!TIP] The original microsoft/Florence-2-* checkpoints load with trust_remote_code=True, and that custom code breaks on recent Transformers versions (errors such as '_supports_sdpa' or forced_bos_token_id). Use the florence-community checkpoints instead.

    About Florence-2

    Florence-2 (Xiao et al., 2023) is an open-source (MIT) vision foundation model from Microsoft. It handles many vision and vision-language tasks with a single model: you choose the task with a text prompt, and the model answers in text. Despite its small size, it matches or beats much larger models such as Kosmos-2 (1.6B) in zero-shot evaluations.

    Models

    ModelSizeDescription
    Florence-2-base (original · transformers)0.23BPretrained on FLD-5B
    Florence-2-large (original · transformers)0.77BPretrained on FLD-5B
    Florence-2-base-ft (original · transformers)0.23BFine-tuned on a collection of downstream tasks
    Florence-2-large-ft (original · transformers)0.77BFine-tuned on a collection of downstream tasks

    Tasks

    TaskPromptExtra input
    Caption<CAPTION>, <DETAILED_CAPTION>, <MORE_DETAILED_CAPTION>none
    Object detection<OD>none
    Dense region caption<DENSE_REGION_CAPTION>none
    Region proposal<REGION_PROPOSAL>none
    Phrase grounding<CAPTION_TO_PHRASE_GROUNDING>caption
    Referring expression segmentation<REFERRING_EXPRESSION_SEGMENTATION>phrase
    Region to segmentation<REGION_TO_SEGMENTATION>region (<loc_x1><loc_y1><loc_x2><loc_y2>)
    Open-vocabulary detection<OPEN_VOCABULARY_DETECTION>phrase
    Region to category / description<REGION_TO_CATEGORY>, <REGION_TO_DESCRIPTION>region
    OCR<OCR>, <OCR_WITH_REGION>none

    Region coordinates are quantized to [0, 999]. Notebook 1 shows every task and its output format.

    How it works

    Vision tasks differ in spatial hierarchy (image-level or pixel-level) and in semantic granularity (a label or a detailed description). Florence-2 covers all of them with one sequence-to-sequence model.

    A DaViT vision encoder turns the image into visual embeddings. These are concatenated with the embeddings of the text prompt and passed to a transformer encoder-decoder, which generates text. For region tasks, the vocabulary includes location tokens: boxes are written as (x0, y0, x1, y1) and polygons as a clockwise list of vertices.

    Training data: FLD-5B

    The model's strength comes mostly from its data. The authors built FLD-5B, with 5.4 billion annotations (boxes, masks, captions, grounding) on 126 million images, generated largely automatically with specialist models and filtered with quality checks.

    Citation

    If you use Florence-2, please cite the original paper:

    @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}
    }
    

    License

    The code and notebooks in this repository are released under the MIT License. Florence-2's weights are MIT-licensed by Microsoft, and linked projects keep their own licenses.