Views
No views yet
1from transformers import Qwen2VLForConditionalGeneration, AutoProcessor
2from peft import PeftModel
3import torch
4
5# Load base model
6model = Qwen2VLForConditionalGeneration.from_pretrained(
7 "Qwen/Qwen2-VL-7B-Instruct",
8 torch_dtype=torch.bfloat16,
9 device_map="auto"
10)
11
12# Load LoRA adapter
13model = PeftModel.from_pretrained(model, "adalvi/qwen2vl-lora-coco")
14model.eval()
15
16# Load processor
17processor = AutoProcessor.from_pretrained("adalvi/qwen2vl-lora-coco")Qwen2VL Base) was evaluated using the prompt "Caption this image." with max_new_tokens=21.1from qwen_vl_utils import process_vision_info
2
3messages = [
4 {
5 "role": "user",
6 "content": [
7 {"type": "image", "image": "<path_or_url_to_image>"},
8 {"type": "text", "text": "Caption this image."},
9 ],
10 }
11]
12
13# Apply chat template
14text = processor.apply_chat_template(
15 messages, tokenize=False, add_generation_prompt=True
16)
17
18# Process image inputs
19image_inputs, video_inputs = process_vision_info(messages)
20
21# Tokenize
22inputs = processor(
23 text=[text],
24 images=image_inputs,
25 videos=video_inputs if video_inputs else None,
26 padding=True,
27 return_tensors="pt",
28).to("cuda")
29
30# Generate caption
31generated_ids = model.generate(**inputs, max_new_tokens=21)
32
33# Trim prompt tokens and decode
34generated_ids_trimmed = [
35 out_ids[len(in_ids):]
36 for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
37]
38caption = processor.batch_decode(
39 generated_ids_trimmed,
40 skip_special_tokens=True,
41 clean_up_tokenization_spaces=False
42)[0]
43
44print(caption)B@4 = BLEU-4, M = METEOR, C = CIDEr, S = SPICE, CLIP-Score and RefCLIP-Score
| Model | B@4 | M | C | S | CLIP-S | RefCLIP-S |
|---|---|---|---|---|---|---|
| Qwen2VL Base | 16.9 | 26.0 | 47.1 | 20.3 | 81.0 | 81.9 |
| Qwen2VL Fine-tuned (this adapter) | 40.0 | 30.7 | 137.5 | 24.2 | 78.6 | 84.0 |
| Model | In-C | In-S | Near-C | Near-S | Out-C | Out-S | Overall-C | Overall-S | CLIP-S |
|---|---|---|---|---|---|---|---|---|---|
| Qwen2VL Base | 48.5 | 14.8 | 51.0 | 14.5 | 57.4 | 14.7 | 53.3 | 14.6 | 81.4 |
| Qwen2VL Fine-tuned (this adapter) | 118.4 | 15.3 | 120.0 | 15.6 | 123.1 | 15.7 | 122.3 | 15.6 | 79.2 |
1@misc{dalvi2026_HDFLIM,
2 title={Hyperdimensional Cross-Modal Alignment of Frozen Language and Image Models for Efficient Image Captioning},
3 author={Abhishek Dalvi and Vasant Honavar},
4 year={2026},
5 eprint={2602.23588},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV},
8 url={https://arxiv.org/abs/2602.23588}
9}