🧾 This work is published in
Nature Computational Science.
1# Create environment
2conda create -n clover python=3.10
3conda activate clover
4
5pip install torch==2.4.0 torchvision==0.19.0 --extra-index-url https://download.pytorch.org/whl/cu118
6
7pip install transformers==4.52.4 accelerate qwen-vl-utils
1from transformers import Qwen2_5_VLForConditionalGeneration, AutoTokenizer, AutoProcessor
2from qwen_vl_utils import process_vision_info
3import torch
4# default: Load the model and processer on the available device(s)
5model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
6 "jline/CLOVER-Qwen2.5-VL", torch_dtype=torch.bfloat16, device_map="auto"
7)
8processor = AutoProcessor.from_pretrained("jline/CLOVER-Qwen2.5-VL")
9
10messages = [
11 {
12 "role": "user",
13 "content": [
14 {
15 "type": "image",
16 "image": "./image_path.png",
17 },
18 {"type": "text", "text": "Describe this image."},
19 ],
20 }
21]
22
23# Preparation for inference
24text = processor.apply_chat_template(
25 messages, tokenize=False, add_generation_prompt=True
26)
27image_inputs, video_inputs = process_vision_info(messages)
28inputs = processor(
29 text=[text],
30 images=image_inputs,
31 videos=video_inputs,
32 padding=True,
33 return_tensors="pt",
34)
35inputs = inputs.to("cuda")
36
37# Inference: Generation of the output
38generated_ids = model.generate(**inputs, max_new_tokens=128)
39generated_ids_trimmed = [
40 out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
41]
42output_text = processor.batch_decode(
43 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
44)
45print(output_text)
1@article{chen2025cost,
2 title={Cost-effective instruction learning for pathology vision and language analysis},
3 author={Chen, K. and Liu, M. and Yan, F. and others},
4 journal={Nature Computational Science},
5 year={2025},
6 doi={10.1038/s43588-025-00818-5}
7}