WUBIAO commited on
Commit
f7fcf4e
·
verified ·
1 Parent(s): f5f65e5

Upload inference_llava_mistral.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. inference_llava_mistral.py +206 -0
inference_llava_mistral.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import argparse
2
+ import torch
3
+ import os
4
+ import json
5
+ from tqdm import tqdm
6
+ import shortuuid
7
+
8
+ from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
9
+ from llava.conversation import conv_templates, SeparatorStyle
10
+ from llava.model.builder import load_pretrained_model
11
+ from llava.utils import disable_torch_init
12
+ from llava.mm_utils import tokenizer_image_token, process_images, get_model_name_from_path
13
+
14
+ from PIL import Image
15
+ import math
16
+ ########################################################
17
+ os.environ["CUDA_VISIBLE_DEVICES"] = "7"
18
+ ################################################∂########
19
+
20
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_lora8_demo'
21
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_iter2000_0709_merge'
22
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_10000_multidig_v1_P020_0709_merge'
23
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_10000_multidig_v1_e1_0709_merge'
24
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_motivation_iter400_e3_merge'
25
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_motivation_iter400_merge'
26
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_e1000_merge'
27
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_e050_merge'
28
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_e020_merge'
29
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_e010_merge'
30
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_e005_merge'
31
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_single_e5_merge'
32
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b-aitw_merge'
33
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-vicuna-7b'
34
+ # args_model_path = '/code/ICLR_2024/Model/llava-v1.6-mistral-7b'
35
+
36
+
37
+ args_model_path = '/scratch/zbz5349/ICLR_2024/LLaVA_Mobile_V1/checkpoints/llava-v1.6-7b-task-lora_three_blip_e1_lre4_mistral_0823'
38
+
39
+ # args_model_path = '/data/zbz5349/ICLR_2024/checkpoints/llava-v1.6-7b-task-lora_all_e1_mistral_0807'
40
+ # args_model_path = '/data/zbz5349/ICLR_2024/checkpoints/llava-v1.6-7b-task-lora_all_e5_0802'
41
+
42
+ # args_model_path = '/data/zbz5349/ICLR_2024/checkpoints/llava-v1.6-7b-task-lora_all_blip_e10_H800_0806'
43
+
44
+ # args_model_path = '/code/ICLR_2024/LLaVA/checkpoints/llava-v1.6-7b-task-lora_general_dual_iter2000_0715_03'
45
+ # args_model_path = '/code/ICLR_2024/LLaVA/checkpoints/llava-v1.6-7b-task-lora_general_dual_iter2000_0715'
46
+ # args_model_path = '/code/ICLR_2024/LLaVA/checkpoints/llava-v1.6-7b-task-lora_general_dual_non_iter2000_0715'
47
+ # args_model_path = '/code/ICLR_2024/LLaVA/checkpoints/llava-v1.6-7b-task-lora_template_H800'
48
+ # args_model_path = '/code/ICLR_2024/LLaVA/checkpoints/llava-v1.6-7b-task-lora_template'
49
+ # args_model_path = '/code/ICLR_2024/LLaVA/checkpoints/llava-v1.6-7b-task-lora_single_blip_iter2000_0709'
50
+ # args_model_path = '/code/ICLR_2024/Model/checkpoints/llava-v1.6-7b-task-lora_single_blip_a100_e1000_0703'
51
+
52
+
53
+ args_model_base = '/data/zbz5349/ICLR_2024/Model/llava-v1.6-mistral-7b'
54
+ # args_model_base = '/scratch/zbz5349/ICLR_2024/LLaVA_Mobile_V1/init_model/llava-v1.6-vicuna-7b'
55
+ # args_model_base = 'xtuner/llava-phi-3-mini'
56
+
57
+
58
+
59
+ disable_torch_init()
60
+ model_path = os.path.expanduser(args_model_path)
61
+ model_name = get_model_name_from_path(model_path)
62
+ tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, args_model_base, model_name)
63
+
64
+ # ############################
65
+ # # model = model.bfloat16()
66
+ # tokenizer.pad_token = "[PAD]"
67
+ # tokenizer.padding_side = "left"
68
+ # ############################
69
+
70
+ def split_list(lst, n):
71
+ """Split a list into n (roughly) equal-sized chunks"""
72
+ chunk_size = math.ceil(len(lst) / n) # integer division
73
+ return [lst[i:i+chunk_size] for i in range(0, len(lst), chunk_size)]
74
+
75
+
76
+ def get_chunk(lst, n, k):
77
+ chunks = split_list(lst, n)
78
+ return chunks[k]
79
+
80
+
81
+ import json
82
+
83
+ def read_json(file_path):
84
+ with open(file_path, 'r', encoding='utf-8') as file:
85
+ data = json.load(file)
86
+ return data
87
+
88
+ def write_json(file_path, data):
89
+ with open(file_path, 'w', encoding='utf-8') as file:
90
+ json.dump(data, file, ensure_ascii=False, indent=4)
91
+
92
+
93
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/blip/single_blip_test_llava_800_caption_history_without_label_v3.json'
94
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/blip/single_blip_test_llava_800.json'
95
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/blip/single_blip_test_llava.json'
96
+
97
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/blip/single_blip_test_llava_800.json'
98
+ # args_answers_file = '/code/ICLR_2024/Auto-GUI/dataset/json/single_blip_test_llava_800_all_e1_H800.json'
99
+
100
+ args_question_file = '/data/zbz5349/ICLR_2024/data/general_blip_test_llava.json'
101
+ args_answers_file = '/data/zbz5349/ICLR_2024/json/general_blip_test_llava_three_lre4_e1_mistral_0822.json'
102
+
103
+
104
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/blip/install_blip_test_llava.json'
105
+ # args_answers_file = '/code/ICLR_2024/Auto-GUI/dataset/json/install_blip_test_llava_all_e1_H800.json'
106
+
107
+
108
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/json/general_blip_test_llava_dual_non_400.json'
109
+ # args_answers_file = '/code/ICLR_2024/Auto-GUI/dataset/json/general_blip_test_llava_dual_non_400_2000iter.json'
110
+
111
+ # args_question_file = '/code/ICLR_2024/Auto-GUI/dataset/json/general_blip_test_llava_dual_400.json'
112
+ # args_answers_file = '/code/ICLR_2024/Auto-GUI/dataset/json/general_blip_test_llava_dual_400_2000iter.json'
113
+
114
+
115
+
116
+ args_num_chunks = 1
117
+ args_chunk_idx = 0
118
+
119
+ questions = json.load(open(os.path.expanduser(args_question_file), "r"))
120
+ questions = get_chunk(questions, args_num_chunks, args_chunk_idx)
121
+ answers_file = os.path.expanduser(args_answers_file)
122
+ os.makedirs(os.path.dirname(answers_file), exist_ok=True)
123
+ ans_file = open(answers_file, "w")
124
+
125
+
126
+
127
+ args_image_folder = '/data/zbz5349/ICLR_2024/data/'
128
+ args_single_pred_prompt = True
129
+ args_conv_mode = "llava_v0"
130
+
131
+
132
+
133
+ right_answer = []
134
+ for i, line in enumerate(tqdm(questions)):
135
+
136
+
137
+ idx = line["id"]
138
+ question = line['conversations'][0]
139
+ qs = question['value'].replace('<image>', '').strip()
140
+ cur_prompt = qs
141
+
142
+ if 'image' in line:
143
+ image_file = line["image"]
144
+ image = Image.open(os.path.join(args_image_folder, image_file))
145
+ image_tensor = process_images([image], image_processor, model.config)[0]
146
+ images = image_tensor.unsqueeze(0).half().cuda()
147
+ image_sizes = [image.size]
148
+ if getattr(model.config, 'mm_use_im_start_end', False):
149
+ qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs
150
+ else:
151
+ qs = DEFAULT_IMAGE_TOKEN + '\n' + qs
152
+ cur_prompt = '<image>' + '\n' + cur_prompt
153
+ else:
154
+ images = None
155
+ image_sizes = None
156
+
157
+ if args_single_pred_prompt:
158
+ # qs = qs + '\n' + "Answer with the option's letter from the given choices directly."
159
+ # cur_prompt = cur_prompt + '\n' + "Answer with the option's letter from the given choices directly."
160
+
161
+ qs = qs + '\n'
162
+ cur_prompt = cur_prompt
163
+
164
+
165
+ conv = conv_templates[args_conv_mode].copy()
166
+ conv.append_message(conv.roles[0], qs)
167
+ conv.append_message(conv.roles[1], None)
168
+ prompt = conv.get_prompt()
169
+
170
+ input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda()
171
+
172
+
173
+
174
+ # import pdb; pdb.set_trace()
175
+ # try:
176
+ with torch.inference_mode():
177
+ output_ids = model.generate(
178
+ input_ids,
179
+ images=images,
180
+ image_sizes=image_sizes,
181
+ do_sample=True if 0.2 > 0 else False,
182
+ temperature=0.2,
183
+ max_new_tokens=1024,
184
+ use_cache=True,
185
+ )
186
+
187
+ # except:
188
+ # continue
189
+ # data = [[1, 9123, 8402, 16747, 29918, 3149]]
190
+ # output_ids = torch.tensor(data, device=input_ids.device)
191
+
192
+
193
+ outputs = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0].strip()
194
+
195
+ # import pprint
196
+ # pprint.pprint(outputs)
197
+ # print('-------------------------------')
198
+ # pprint.pprint(line['conversations'][1]['value'])
199
+ # print('===========================================================')
200
+
201
+ temp = {}
202
+ temp['gt'] = line['conversations'][1]['value']
203
+ temp['pred'] = outputs
204
+ right_answer.append(temp)
205
+
206
+ write_json(args_answers_file, right_answer)