Views
No views yet
| Stage | Task | Adapter | Description |
|---|---|---|---|
| 1 | Point Localization | stage1 | <point x='45.2' y='68.3'>suction device</point> |
| 2 | Bounding Box | stage2 | <box x1='20' y1='30' x2='60' y2='70'>tumor</box> |
| 3 | Motion Detection | stage3 | Temporal motion analysis between frames |
| 4 | Unified | stage4 | All tasks combined (recommended) |
1from transformers import Qwen2VLForConditionalGeneration, AutoProcessor, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5# Load base model (4-bit quantized for efficiency)
6bnb_config = BitsAndBytesConfig(
7 load_in_4bit=True,
8 bnb_4bit_quant_type="nf4",
9 bnb_4bit_compute_dtype=torch.bfloat16,
10)
11
12base = Qwen2VLForConditionalGeneration.from_pretrained(
13 "Qwen/Qwen2-VL-2B-Instruct",
14 quantization_config=bnb_config,
15 device_map="auto",
16 trust_remote_code=True
17)
18processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct")
19
20# Load unified adapter (stage4 - recommended for most tasks)
21model = PeftModel.from_pretrained(base, "mmrech/pitvqa-qwen2vl-unified-v2",
22 adapter_name="stage4", subfolder="stage4")
23
24# Or load multiple adapters and switch between them
25model.load_adapter("mmrech/pitvqa-qwen2vl-unified-v2", adapter_name="stage1", subfolder="stage1")
26model.load_adapter("mmrech/pitvqa-qwen2vl-unified-v2", adapter_name="stage2", subfolder="stage2")
27model.set_adapter("stage4") # Switch to unified adapter1from PIL import Image
2
3# Load surgical image
4image = Image.open("surgical_frame.jpg")
5
6# Point localization
7messages = [{"role": "user", "content": [
8 {"type": "image", "image": image},
9 {"type": "text", "text": "Point to the suction device in this surgical image."}
10]}]
11
12text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
13inputs = processor(text=[text], images=[image], return_tensors="pt").to(model.device)
14
15with torch.no_grad():
16 output = model.generate(**inputs, max_new_tokens=128, do_sample=False)
17
18response = processor.decode(output[0], skip_special_tokens=True)
19# Output: <point x='75.8' y='75.1'>suction device</point>Prompt: "Point to the {target} in this surgical image."
Output: <point x='45.2' y='68.3'>suction device</point>Prompt: "Draw a bounding box around the {target}."
Output: <box x1='20' y1='30' x2='60' y2='70'>tumor region</box>Prompt: "What surgical phase is shown?"
Output: sellar phasePrompt: "Describe the surgical instruments visible."
Output: The image shows a suction device in the lower right quadrant...mmrech/pitvqa-qwen2vl-unified-v2/
├── stage1/ # Point localization adapter
├── stage2/ # Bounding box adapter
├── stage3/ # Motion detection adapter
├── stage4/ # Unified adapter (all tasks)
└── showcase_examples_full.json # Verified examples with images1@misc{pitvqa2026,
2 title={PitVQA: Multi-Task Vision-Language Model for Pituitary Surgery},
3 author={Matheus Rech},
4 year={2026},
5 url={https://huggingface.co/mmrech/pitvqa-qwen2vl-unified-v2}
6}