Views
No views yet

1@inproceedings{li2025chemvlm,
2 title={Chemvlm: Exploring the power of multimodal large language models in chemistry area},
3 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},
4 booktitle={Proceedings of the AAAI Conference on Artificial Intelligence},
5 volume={39},
6 number={1},
7 pages={415--423},
8 year={2025}
9}| Datasets | MMChemOCR | CMMU | MMCR-bench | Reaction type |
|---|---|---|---|---|
| metrics | tanimoto similarity\tani@1.0 | score(%, GPT-4o helps judge) | score(%, GPT-4o helps judge) | Accuracy(%) |
| scores of ChemVLM-8b | 81.75/57.69 | 52.7(SOTA) | 33.6 | 16.79 |
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
89
90tokenizer = AutoTokenizer.from_pretrained('AI4Chem/ChemVLM-8B', trust_remote_code=True)
91model = AutoModelForCausalLM.from_pretrained(
92 "AI4Chem/ChemVLM-8B",
93 torch_dtype=torch.bfloat16,
94 low_cpu_mem_usage=True,
95 trust_remote_code=True
96).cuda().eval()
97
98query = "Please describe the molecule in the image."
99image_path = "your image path"
100pixel_values = load_image(image_path, max_num=6).to(torch.bfloat16).cuda()
101
102gen_kwargs = {"max_length": 1000, "do_sample": True, "temperature": 0.7, "top_p": 0.9}
103
104response = model.chat(tokenizer, pixel_values, query, gen_kwargs)
105print(response)pip install transformers>=4.37.0 sentencepiece einops timm accelerate>=0.26.0. Make sure to have torch and torchvision installed as well.