Fine-tuning Florence-2 - Microsoft's Cutting-edge Vision Language Models
Florence-2, Microsoft's small (0.2B/0.7B) sequence-to-sequence vision-language model built from a DaViT encoder, BERT text embeddings and an encoder-decoder transformer pre-trained on the 126M-image FLD-5B dataset, can be fine-tuned to handle visual question answering, a task the released checkpoints don't support. Fine-tuning on DocVQA for seven epochs raised validation Levenshtein similarity from 0 to 57.0. The low-resource setup froze the vision encoder and used batch size 6 on a single A100 (or 1 on a T4), while a full fine-tune at batch size 64 took 70 minutes on 8 H100s. A small learning rate of 1e-6 avoided rapid overfitting, and a PyTorch walkthrough covers the dataset, collator and training loop.
Related posts
- Docmatix - a huge dataset for Document Visual Question Answering
Hugging Face mediumAdeptDocmatix is a Document Visual Question Answering dataset with 2.4 million images and 9.5 million Q/A pairs drawn from 1.3 million PDFs, roughly 240x larger than earlier DocVQA datasets. Q/A pairs were generated with Phi-3-small from PDFA OCR transcriptions. About 15% of generations were discarded as hallucinations using regex code detection and an "unanswerable" keyword filter, and prompts were tuned toward about four diverse, human-like pairs per page. Fine-tuning the 0.7B Florence-2 on a small slice of Docmatix and then DocVQA raised ANLS from 60.1 to 71.4. That is within about 5% of the 8B Idefics2.
- Preference Optimization for Vision Language Models
Hugging Face hardMasterTRL now supports direct preference optimization (DPO) for vision language models, trained on chosen/rejected answer pairs rather than fixed labels. A walkthrough fine-tunes Idefics2-8b on the RLAIF-V dataset, formatting image-plus-question prompts with the processor's chat template. Full float32 DPO training would need about 160 GB (policy, reference model, gradients, AdamW states), but bfloat16 plus LoRA cuts trainable parameters to 55M and memory to roughly 32 GB. Batch size 2 with 32 gradient accumulation steps and gradient checkpointing fits activations, and training is judged by rising accuracy and reward margin.
- Zero-shot image-to-text generation with BLIP-2
Hugging Face mediumAdeptBLIP-2 connects a frozen pre-trained image encoder (ViT) to a frozen LLM (OPT or Flan-T5) through a lightweight Querying Transformer (Q-Former), the only trainable component. This cuts trainable parameters and pre-training cost while reaching state-of-the-art results on visual-language tasks. Q-Former is pre-trained in two stages. The first uses image-text contrastive, image-grounded generation and image-text matching losses. The second feeds its query outputs to the LLM as a visual prefix under a causal LM loss. Code examples with Hugging Face Transformers show zero-shot image captioning, prompted captioning, visual question answering with a "Question: Answer:" template, and chat-style prompting, which must stay within the 512-token context.
- A Dive into Vision-Language Models
Hugging Face hardMasterVision-language models pair an image encoder and a text encoder with a strategy for fusing the two, and the pre-training objective determines which downstream tasks they handle well. The main families are contrastive learning (CLIP, ALIGN, LiT), PrefixLM and frozen PrefixLM (SimVLM, Frozen, ClipCap, Flamingo), cross-attention fusion into language model layers (VisualGPT, FIBER), combined masked-language modeling and image-text matching (VisualBERT, FLAVA), and training-free methods (MaGiC, ASIF). Frozen-backbone approaches let teams train with limited aligned image-text data, while PrefixLM-only models are mostly limited to captioning and VQA. The post also surveys common pre-training datasets and shows how to experiment with these models in 🤗 Transformers.
- PaliGemma – Google's Cutting-Edge Open Vision Language Model
Hugging Face mediumAdeptPaliGemma is Google's open vision-language model family that pairs a SigLIP-So400m image encoder with a Gemma-2B text decoder through a linear projector. It ships as pretrained, mix and task-specific fine-tuned checkpoints at 224, 448 and 896 resolutions. Behaviour is steered with task prefixes such as "detect" and "segment", and detections come back as <loc> tokens normalized to 1024. Images become 256 to 4096 prefix tokens with full attention over the image and prompt, so higher resolutions cost much more memory while helping mainly fine-grained tasks like OCR. The post includes transformers code for inference, 4-bit loading, and fine-tuning the decoder with full training or QLoRA, plus big_vision transfer configs.