Model setup recipes

Finding objects with Florence-2-base: one 0.23B vision model

The model file is small; what it returns is coordinates and tags, so you need code to read it.

Florence-2 handles several vision tasks through prompts1 rather than fine-tuning. The base model is 0.23B with 463 MB of weights. Changing the tag on the same image yields captions or object boxes, and post-processing turns the output into coordinates.

Requirements and key details
  • Switching the prompt tag switches the task.
  • Results become coordinates only after post_process_generation.
  • It runs repository Python code, so trust_remote_code is required.

One model, several tasks

Florence-2 is a sequence-to-sequence model that takes an image and text together. Tags such as <CAPTION>, <OD> and <OCR> make one set of weights caption, detect or read text.

Each task needs a different output length. Detection emits a box for every object, so max_new_tokens needs headroom; a caption is short.

A person at a laptop is surrounded by connected document, spreadsheet, folder, image, settings, and robot panels
One tag drives captioning, detection and OCR from the same weights.

The code and reading the boxes

Bundle the image and tag with AutoProcessor, then pass the generated tokens2 to post_process_generation to get boxes scaled to the image. Skipping that step leaves a raw string with tags.

Each box is an (x, y) pair for top-left and bottom-right. Drawing them once shows immediately whether the values line up.

Running detection
from PIL import Image
import torch
from transformers import AutoProcessor, AutoModelForCausalLM

name = "microsoft/Florence-2-base"
model = AutoModelForCausalLM.from_pretrained(
    name, torch_dtype=torch.float32, trust_remote_code=True)
processor = AutoProcessor.from_pretrained(name, trust_remote_code=True)

image = Image.open("photo.jpg").convert("RGB")
prompt = "<OD>"
inputs = processor(text=prompt, images=image, return_tensors="pt")
with torch.no_grad():
    ids = model.generate(input_ids=inputs["input_ids"],
        pixel_values=inputs["pixel_values"],
        max_new_tokens=1024, num_beams=3)
text = processor.batch_decode(ids, skip_special_tokens=False)[0]
print(processor.post_process_generation(
    text, task=prompt, image_size=(image.width, image.height)))
This runs float32 on the CPU; switch to float16 when a GPU is present.

Two things to check first

The card requires trust_remote_code=True because the model ships its own Python. Enable it only for repositories you trust, and remember the code is re-fetched when the revision changes.

The base here is pretrained. For immediate detection the fine-tuned -ft variant fits better; base is the starting point. The card's table separates base from base-ft for that reason.

A small computer running a search system beside document stacks
Coordinates are usable only after post-processing.

Judging with a single photo

Try three of your own photos per task: do the boxes wrap the objects, does the caption match, does OCR3 keep the digits? That settles fit quickly.

If it does not fit, compare large or -ft on the same photos. Keep the code and tags identical so only the model changes.

An RTX 3090 in a case, colored blocks moving along a conveyor to shelves, and cards printed with network diagrams on the work surface
Test on three of your own photos; if it misses, compare -ft.

Terminology notes

  1. Prompt — The input text sent to a model. For generative models it describes the desired result and strongly shapes the output.

    Back to the text
  2. Token — A unit into which a model divides input or output for processing.

    Back to the text
  3. OCR — The process of recognizing characters in images or scanned documents and turning them into text. Table layout and reading order still need the original image to verify.

    Back to the text

Frequently asked questions

What should you check first in this article?
Florence-2 handles several vision tasks through prompts rather than fine-tuning. The base model is 0.23B with 463 MB of weights. Changing the tag on the same image yields captions or object boxes, and post-processing turns the output into coordinates.