blip-2-vision-language

Generates image captions, answers visual questions, and retrieves images using BLIP-2 vision-language models.

Updated Oct 7, 2022
One-click install
npx skills add https://github.com/tamagusko/linux-cfg --skill blip-2-vision-language-tamagusko
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: blip-2-vision-language
Source: https://github.com/tamagusko/linux-cfg/tree/main/dotfiles/claude/skills/blip-2
Command: npx skills add https://github.com/tamagusko/linux-cfg --skill blip-2-vision-language-tamagusko

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires transformers, torch, Pillow, and includes references (resource) components.

What problem does it solve? Applying vision-language models like BLIP-2 requires navigating model variants, memory constraints, and preprocessing details. This Skill provides working code patterns for image captioning, visual question answering, and image-text retrieval without trial-and-error setup. ## Core Features & Use Cases - Image Captioning & VQA: Generate natural-language captions and answer questions about images using HuggingFace Transformers or the LAVIS library with OPT and FlanT5 backends. - Memory Optimization: Apply INT8/INT4 quantization, batch processing, and generation controls to fit models from 3GB to 26GB VRAM budgets. - Fine-tuning & Deployment: LoRA fine-tuning, multi-GPU training, Gradio/FastAPI serving, and ONNX export covered in the references. - Use Case: Build a visual Q&A system where a user uploads a photo and asks multiple questions like "What objects are present?" using the blip2-flan-t5-xl model on a single GPU. ## Quick Start Use the BLIP-2 skill to generate a caption for my image file photo.jpg with the blip2-opt-2.7b model.

Frequently Asked Questions about blip-2-vision-language

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I generate image captions with BLIP-2 in Python?

Load Blip2Processor and Blip2ForConditionalGeneration from HuggingFace Transformers with the Salesforce/blip2-opt-2.7b checkpoint, pass a PIL RGB image through the processor, and call model.generate with max_new_tokens set. Decode the output token IDs with processor.batch_decode to get the caption string.

BLIP-2 vs LLaVA vs InstructBLIP for visual question answering?

BLIP-2 excels at zero-shot captioning and VQA with low training cost since only the Q-Former is trained. InstructBLIP is its successor with better instruction following, while LLaVA targets instruction-following multimodal chat. For production chat, proprietary models like GPT-4V are alternatives.

How much GPU memory does BLIP-2 need?

blip2-opt-2.7b needs about 8GB in FP16, blip2-opt-6.7b about 16GB, and blip2-flan-t5-xxl about 26GB. Using INT8 or INT4 quantization via BitsAndBytesConfig reduces these to roughly 3-8GB, letting larger variants fit on consumer GPUs.

Why does BLIP-2 give CUDA out of memory errors?

The model exceeds available VRAM for the chosen precision and batch size. Fix it by enabling 8-bit or 4-bit quantization, switching to a smaller variant like blip2-opt-2.7b, processing images sequentially with torch.cuda.empty_cache between calls, or offloading layers to CPU.

Can BLIP-2 be fine-tuned on a custom dataset?

Yes, LoRA fine-tuning with the peft library is the recommended approach, targeting attention projection layers while keeping the frozen vision encoder and LLM unchanged. You can also train only the Q-Former by freezing all parameters whose names do not contain qformer.

Why does BLIP-2 hallucinate objects not in the image?

Hallucinations come from high-temperature sampling and vague prompts. Lower the temperature, use beam search with do_sample disabled, ask specific questions like "Is there a person in this image?", and prefer the FlanT5 variants which follow instructions more reliably.