Views
No views yet
@inproceedings{li2025chemvlm,
title={Chemvlm: Exploring the power of multimodal large language models in chemistry area},
author={Li, Junxian and Zhang, Di and Wang, Xunzhi and Hao, Zeying and Lei, Jingdi and Tan, Qian and Zhou, Cai and Liu, Wei and Yang, Yaotian and Xiong, Xinrui and others},
booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
volume={39},
number={1},
pages={415--423},
year={2025}
}transformers>=4.37.0 is needed)pip install sentencepiece
pip install einops
pip install timm
pip install accelerate>=0.26.0 1from transformers import AutoTokenizer, AutoModelforCasualLM
2import torch
3import torchvision.transforms as T
4import transformers
5from torchvision.transforms.functional import InterpolationMode
6
7
8IMAGENET_MEAN = (0.485, 0.456, 0.406)
9IMAGENET_STD = (0.229, 0.224, 0.225)
10
11IMAGENET_MEAN = (0.485, 0.456, 0.406)
12IMAGENET_STD = (0.229, 0.224, 0.225)
13
14
15def build_transform(input_size):
16 MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
17 transform = T.Compose([
18 T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
19 T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
20 T.ToTensor(),
21 T.Normalize(mean=MEAN, std=STD)
22 ])
23 return transform
24
25
26def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
27 best_ratio_diff = float('inf')
28 best_ratio = (1, 1)
29 area = width * height
30 for ratio in target_ratios:
31 target_aspect_ratio = ratio[0] / ratio[1]
32 ratio_diff = abs(aspect_ratio - target_aspect_ratio)
33 if ratio_diff < best_ratio_diff:
34 best_ratio_diff = ratio_diff
35 best_ratio = ratio
36 elif ratio_diff == best_ratio_diff:
37 if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
38 best_ratio = ratio
39 return best_ratio
40
41
42def dynamic_preprocess(image, min_num=1, max_num=6, image_size=448, use_thumbnail=False):
43 orig_width, orig_height = image.size
44 aspect_ratio = orig_width / orig_height
45
46 # calculate the existing image aspect ratio
47 target_ratios = set(
48 (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
49 i * j <= max_num and i * j >= min_num)
50 target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
51
52 # find the closest aspect ratio to the target
53 target_aspect_ratio = find_closest_aspect_ratio(
54 aspect_ratio, target_ratios, orig_width, orig_height, image_size)
55
56 # calculate the target width and height
57 target_width = image_size * target_aspect_ratio[0]
58 target_height = image_size * target_aspect_ratio[1]
59 blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
60
61 # resize the image
62 resized_img = image.resize((target_width, target_height))
63 processed_images = []
64 for i in range(blocks):
65 box = (
66 (i % (target_width // image_size)) * image_size,
67 (i // (target_width // image_size)) * image_size,
68 ((i % (target_width // image_size)) + 1) * image_size,
69 ((i // (target_width // image_size)) + 1) * image_size
70 )
71 # split the image
72 split_img = resized_img.crop(box)
73 processed_images.append(split_img)
74 assert len(processed_images) == blocks
75 if use_thumbnail and len(processed_images) != 1:
76 thumbnail_img = image.resize((image_size, image_size))
77 processed_images.append(thumbnail_img)
78 return processed_images
79
80
81def load_image(image_file, input_size=448, max_num=6):
82 image = Image.open(image_file).convert('RGB')
83 transform = build_transform(input_size=input_size)
84 images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
85 pixel_values = [transform(image) for image in images]
86 pixel_values = torch.stack(pixel_values)
87 return pixel_values
88
89tokenizer = AutoTokenizer.from_pretrained('AI4Chem/ChemVLM-26B-1-2', trust_remote_code=True)
90
91query = "Please describe the molecule in the image."
92image_path = "your image path"
93pixel_values = load_image(image_path, max_num=6).to(torch.bfloat16).cuda()
94
95
96model = AutoModelForCausalLM.from_pretrained(
97 "AI4Chem/ChemVLM-26B-1-2",
98 torch_dtype=torch.bfloat16,
99 low_cpu_mem_usage=True,
100 trust_remote_code=True
101).to(device).eval().cuda()
102
103gen_kwargs = {"max_length": 1000, "do_sample": True, "temperature": 0.7, "top_p": 0.9}
104
105response = model.chat(tokenizer, pixel_values, query, gen_kwargs)