The dataset viewer is not available because its heuristics could not detect any supported data files. You can try uploading some data files, or configuring the data files location manually.
YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
VLM2Vec Student Project: Hardness-Aware Visual RAG
Working backbone: Qwen/Qwen2-VL-2B-Instruct
Phi-3.5-Vision (VLM2Vec-Full checkpoint) has broken vision embedding loading. Qwen2-VL-2B works reliably.
⚠️ Critical Finding
Qwen2-VL requires <|image_pad|> token prepended to every text prompt that includes an image. Without it, the model ignores the image and all embeddings become identical.
# CORRECT (for Qwen2-VL)
prompt = f"<|image_pad|>{your_text}"
inputs = processor(text=[prompt], images=[image], ...)
# WRONG (image silently ignored)
inputs = processor(text=[your_text], images=[image], ...) # DON'T DO THIS
Also: use AutoModel (not AutoModelForCausalLM or AutoModelForVision2Seq) for Qwen2-VL embedding extraction.
Files (run in this order)
| Order | File | Purpose |
|---|---|---|
| 1 | smoke_test.py |
Verify model loads and distinguishes semantics |
| 2 | train_domain.py |
Fine-tune with hardness-weighted loss |
| 3 | ingest_corpus.py |
Render PDFs → embed pages → ChromaDB |
| 4 | evaluate_models.py |
Compare baseline vs fine-tuned metrics |
| 5 | demo_backend.py |
FastAPI retrieval server |
| 6 | demo_frontend.py |
Gradio side-by-side comparison UI |
The ONLY Rule
Run smoke_test.py first. If it fails, nothing else matters.
Download
git clone https://huggingface.co/datasets/Viraj-Sawad/vlm2vec-student-project
Quick Start (Colab)
!pip install -q transformers accelerate bitsandbytes pillow requests qwen-vl-utils
import torch
from transformers import AutoProcessor, AutoModel
from PIL import Image
import requests
model_name = "Qwen/Qwen2-VL-2B-Instruct"
processor = AutoProcessor.from_pretrained(model_name, trust_remote_code=True)
model = AutoModel.from_pretrained(model_name, torch_dtype=torch.bfloat16,
device_map="auto", trust_remote_code=True)
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
def embed(text, img):
prompt = f"<|image_pad|>{text}" # ← CRITICAL
inputs = processor(text=[prompt], images=[img], return_tensors="pt", padding=True)
inputs = {k: v.to(model.device) if isinstance(v, torch.Tensor) else v
for k, v in inputs.items()}
with torch.no_grad():
out = model(**inputs, output_hidden_states=True, return_dict=True)
h = out.hidden_states[-1]
emb = h[torch.arange(h.size(0)), inputs["attention_mask"].sum(1)-1, :]
emb = torch.nn.functional.normalize(emb, p=2, dim=-1)
return emb
emb_q = embed("Find images of cats sleeping", image)
emb_g = embed("Two cats sleeping on a pink couch", image)
emb_b = embed("A diagram of a neural network architecture", image)
print(f"Good: {torch.matmul(emb_q, emb_g.t()).item():.4f}")
print(f"Bad: {torch.matmul(emb_q, emb_b.t()).item():.4f}")
- Downloads last month
- 100