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

Upload inference_llava_install.py with huggingface_hub

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