Views
No views yet
pip install git+https://github.com/LLaVA-VL/LLaVA-NeXT.git`llava:1import copy
2from llava.model.builder import load_pretrained_model
3from llava.mm_utils import get_model_name_from_path, process_images, tokenizer_image_token
4from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN, IGNORE_INDEX
5from llava.conversation import conv_templates, SeparatorStyle
6model_name = "llava_qwen"
7tokenizer, llava_model, image_processor, _ = load_pretrained_model(
8 "Salesforce/cogalign-llava-ov-0_5b",
9 None,
10 model_name,
11 device_map="cpu",
12)
13llava_model.cuda()1chocolate = load_dataset("khhuang/CHOCOLATE")["test"]
2chocolate_df = pd.DataFrame(chocolate)
3chocolate_df_lvlm = chocolate_df.loc[chocolate_df.split=="LVLM",:]
4
5instance = chocolate_df_lvlm.iloc[2]
6caption = ' '.join(instance.sentences)
7prompt = f"""
8You are given a chart and a caption, you are tasked to detect whether the caption is factually
9consistent with the chart. A caption is factually consistent with the chart if it describes the datapoints within the charts without factual errors (e.g. wrong label, value, trends).
10[Start of Caption]
11{caption}
12[End of Caption]
13For the above caption, you should respond 'Answer: Yes' if it is factually consistent with the chart. Otherwise, respond 'Answer: No'. Do not provide explanation or other thing.
14"""
15
16url = instance.image_path
17image = Image.open(requests.get(url, stream=True).raw)
18image_tensor = process_images([image], image_processor, llava_model.config)
19image_tensor = [_image.to(dtype=torch.float16, device='cuda') for _image in image_tensor]
20
21conv_template = "qwen_1_5" # Make sure you use correct chat template for different models
22question = DEFAULT_IMAGE_TOKEN + f"\n{prompt}"
23conv = copy.deepcopy(conv_templates[conv_template])
24conv.append_message(conv.roles[0], question)
25conv.append_message(conv.roles[1], None)
26prompt_question = conv.get_prompt()
27
28input_ids = tokenizer_image_token(prompt_question, tokenizer, IMAGE_TOKEN_INDEX, return_tensors="pt").unsqueeze(0).to('cuda')
29image_sizes = [image.size]
30
31
32cont = llava_model.generate(
33 input_ids,
34 images=image_tensor,
35 image_sizes=image_sizes,
36 do_sample=False,
37 temperature=0,
38 max_new_tokens=4096,
39)
40response = tokenizer.batch_decode(cont, skip_special_tokens=True)[0]
41print(f'User: {question}\nAssistant: {response}')@misc{huang-etal-2025-cogalign,
title = "Why Vision Language Models Struggle with Visual Arithmetic? Towards Enhanced Chart and Geometry Understanding",
author = "Huang, Kung-Hsiang and
Qin, Can and
Qiu, Haoyi and
Laban, Philippe and
Joty, Shafiq and
Xiong, Caiming and
Wu, Chien-Sheng",
year = "2025",
eprint={2502.11492},
archivePrefix = "arXiv",
primaryClass={cs.AI}
}