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