Views
No views yet
pip install transformers==4.57.1 accelerate==1.12.0 torchvision==0.24.1 qwen-vl-utils==0.0.141import torch
2from transformers import Qwen3VLForConditionalGeneration, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5# default: Load the model on the available device(s)
6model = Qwen3VLForConditionalGeneration.from_pretrained(
7 "microsoft/UniRG-CXR", dtype=torch.bfloat16, device_map="auto"
8)
9
10# We recommend enabling flash_attention_2 for better acceleration and memory saving.
11# model = Qwen3VLForConditionalGeneration.from_pretrained(
12# "microsoft/UniRG-CXR",
13# dtype=torch.bfloat16,
14# attn_implementation="flash_attention_2",
15# device_map="auto",
16# )
17
18# You can set min_pixels and max_pixels according to your needs.
19min_pixels = 262144
20max_pixels = 262144
21processor = AutoProcessor.from_pretrained("microsoft/UniRG-CXR", min_pixels=min_pixels, max_pixels=max_pixels)
22
23messages = [
24 {
25 "role": "user",
26 "content": [
27
28 {"type": "text", "text": "This is a radiology report generation task. Here is the context:"},
29 {
30 "type": "image",
31 "image": "<input your image path here>",
32 },
33 {"type": "text", "text": "Given the image and the context, directly provide the report in the following format:\nFindings: [write the findings] Impression: [write the impression]\nNow write the report in the format above."},
34
35 ],
36 }
37]
38
39# Preparation for inference
40text = processor.apply_chat_template(
41 messages, tokenize=False, add_generation_prompt=True
42)
43image_inputs, video_inputs = process_vision_info(messages)
44inputs = processor(
45 text=[text],
46 images=image_inputs,
47 videos=video_inputs,
48 padding=True,
49 return_tensors="pt",
50)
51
52
53inputs = inputs.to(device="cuda")
54
55# Inference: Generation of the output
56generated_ids = model.generate(**inputs, max_new_tokens=4000)
57generated_ids_trimmed = [
58 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
59]
60output_text = processor.batch_decode(
61 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
62)
63print(output_text)
641from vllm import LLM, SamplingParams
2from transformers import AutoProcessor
3
4min_pixels = 262144
5max_pixels = 262144
6processor = AutoProcessor.from_pretrained("microsoft/UniRG-CXR", min_pixels=min_pixels, max_pixels=max_pixels)
7
8llm = LLM(
9 model="microsoft/UniRG-CXR",
10 trust_remote_code=True,
11 dtype="bfloat16",
12 max_model_len=8192,
13 tensor_parallel_size=4,
14 gpu_memory_utilization=0.8,
15 limit_mm_per_prompt={"image": 1}
16)
17
18# Set up sampling parameters
19sampling_params = SamplingParams(
20 temperature=0.0,
21 max_tokens=4000,
22)
23
24image_data = []
25
26
27
28image_data = ['Your image path']
29messages = [
30 {
31 "role": "user",
32 "content": [
33
34 {"type": "text", "text": "This is a radiology report generation task. Here is the context:"},
35 {
36 "type": "image",
37 "image": image_data[0],
38 },
39 {"type": "text", "text": "Given the image and the context, directly provide the report in the following format:\nFindings: [write the findings] Impression: [write the impression]\nNow write the report in the format above."},
40
41 ],
42 }
43]
44
45prompt = processor.apply_chat_template(
46 messages, tokenize=False, add_generation_prompt=True)
47
48if image_data:
49 mm_prompt = {
50 "prompt": prompt,
51 "multi_modal_data": {"image": image_data}
52 }
53else:
54 mm_prompt = {"prompt": prompt}
55
56# Generate response
57outputs = llm.generate([mm_prompt], sampling_params)
58
59# Print the generated response
60for output in outputs:
61 prompt = output.prompt
62 generated_text = output.outputs[0].text
63 print(f"Prompt: {prompt}")
64 print(f"Generated text: {generated_text}")
65 print("-" * 50)@article{liu2026scaling,
title={Scaling medical imaging report generation with multimodal reinforcement learning},
author={Liu, Qianchu and Zhang, Sheng and Qin, Guanghui and Gu, Yu and Jin, Ying and Preston, Sam and Xu, Yanbo and Kiblawi, Sid and Yim, Wen-wai and Ossowski, Tim and others},
journal={arXiv preprint arXiv:2601.17151},
year={2026}
}