Views
No views yet

1conda create -n quickstart python=3.10
2conda activate quickstart
3pip install torch transformers accelerate pillowpython ColonR1/quickstart.py to run it, as shown in the following code.1import torch
2from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
3from PIL import Image
4import warnings
5import os
6
7warnings.filterwarnings('ignore')
8device = "cuda" if torch.cuda.is_available() else "cpu"
9
10MODEL_PATH = "ai4colonoscopy/ColonR1"
11IMAGE_PATH = "assets/example.jpg"
12Question = "Does the image contain a polyp? Answer me with Yes or No."
13
14print(f"[Info] Loading model from {MODEL_PATH}...")
15model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
16 MODEL_PATH,
17 torch_dtype=torch.bfloat16,
18 attn_implementation="flash_attention_2",
19 device_map="auto"
20)
21model.eval()
22
23processor = AutoProcessor.from_pretrained(MODEL_PATH)
24
25if not os.path.exists(IMAGE_PATH):
26 raise FileNotFoundError(f"Image not found at {IMAGE_PATH}. Please provide a valid image path.")
27
28image = Image.open(IMAGE_PATH).convert("RGB")
29
30TASK_SUFFIX = (
31 "Your task: 1. First, Think through the question step by step, enclose your reasoning process "
32 "in <think>...</think> tags. 2. Then provide the correct answer inside <answer>...</answer> tags. "
33 "3. No extra information or text outside of these tags."
34)
35
36final_question = f"{Question}messages = [
{
"role": "user",
"content": [
{"type": "image", "image": IMAGE_PATH},
{"type": "text", "text": final_question},
],
}
]
print("[Info] Processing inputs...")
text_prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = processor(
text=[text_prompt],
images=[image],
padding=True,
return_tensors="pt",
).to(device)
print("[Info] Generating response...")
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=1024,
do_sample=False
)
generated_ids_trimmed = generated_ids[:, inputs.input_ids.shape[1]:]
output_text = processor.batch_decode(generated_ids_trimmed, skip_special_tokens=True)[0]
print(output_text)
```@article{ji2025colonx,
title={Colon-X: Advancing Intelligent Colonoscopy from Multimodal Understanding to Clinical Reasoning},
author={Ji, Ge-Peng and Liu, Jingyi and Fan, Deng-Ping and Barnes, Nick},
journal={arXiv preprint arXiv:2512.03667},
year={2025}
}