Comparison-aware anomaly detection with vision-language models. Extends Qwen2.5-VL-7B with a novel comparison-aware visual encoder achieving 78.74% on OmniDiff benchmark.
1import torch
2from transformers import AutoModelForVision2Seq, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5model = AutoModelForVision2Seq.from_pretrained(
6 "jiang-cc/AD-Copilot",
7 torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True,
8)
9processor = AutoProcessor.from_pretrained(
10 "jiang-cc/AD-Copilot",
11 min_pixels=64*28*28, max_pixels=1280*28*28, trust_remote_code=True,
12)
13
14messages = [{"role": "user", "content": [
15 {"type": "image", "image": "good.png"},
16 {"type": "image", "image": "test.png"},
17 {"type": "text", "text": "The first image is good. Is there any anomaly in the second image? A.yes, B.no."},
18]}]
19
20text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
21image_inputs, _ = process_vision_info(messages)
22inputs = processor(text=[text], images=[image_inputs], return_tensors="pt").to(model.device)
23
24with torch.inference_mode():
25 ids = model.generate(**inputs, max_new_tokens=128, do_sample=False)
26trimmed = [o[len(i):] for i, o in zip(inputs.input_ids, ids)]
27print(processor.batch_decode(trimmed, skip_special_tokens=True)[0])
1@article{adcopilot2025,
2 title={AD-Copilot: Comparison-Aware Anomaly Detection with Vision-Language Models},
3 author={Jiang, Xi and others},
4 journal={arXiv preprint arXiv:2603.13779},
5 year={2025}
6}