(args)
| 27 | |
| 28 | |
| 29 | def eval_model(args): |
| 30 | # Model |
| 31 | disable_torch_init() |
| 32 | model_path = os.path.expanduser(args.model_path) |
| 33 | model_name = get_model_name_from_path(model_path) |
| 34 | tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, args.model_base, model_name) |
| 35 | |
| 36 | questions = json.load(open(os.path.expanduser(args.question_file), "r")) |
| 37 | questions = get_chunk(questions, args.num_chunks, args.chunk_idx) |
| 38 | answers_file = os.path.expanduser(args.answers_file) |
| 39 | os.makedirs(os.path.dirname(answers_file), exist_ok=True) |
| 40 | ans_file = open(answers_file, "w") |
| 41 | for i, line in enumerate(tqdm(questions)): |
| 42 | idx = line["id"] |
| 43 | question = line['conversations'][0] |
| 44 | qs = question['value'].replace('<image>', '').strip() |
| 45 | cur_prompt = qs |
| 46 | |
| 47 | if 'image' in line: |
| 48 | image_file = line["image"] |
| 49 | image = Image.open(os.path.join(args.image_folder, image_file)) |
| 50 | image_tensor = image_processor.preprocess(image, return_tensors='pt')['pixel_values'][0] |
| 51 | images = image_tensor.unsqueeze(0).half().cuda() |
| 52 | if getattr(model.config, 'mm_use_im_start_end', False): |
| 53 | qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs |
| 54 | else: |
| 55 | qs = DEFAULT_IMAGE_TOKEN + '\n' + qs |
| 56 | cur_prompt = '<image>' + '\n' + cur_prompt |
| 57 | else: |
| 58 | images = None |
| 59 | |
| 60 | if args.single_pred_prompt: |
| 61 | qs = qs + '\n' + "Answer with the option's letter from the given choices directly." |
| 62 | cur_prompt = cur_prompt + '\n' + "Answer with the option's letter from the given choices directly." |
| 63 | |
| 64 | conv = conv_templates[args.conv_mode].copy() |
| 65 | conv.append_message(conv.roles[0], qs) |
| 66 | conv.append_message(conv.roles[1], None) |
| 67 | prompt = conv.get_prompt() |
| 68 | |
| 69 | input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda() |
| 70 | |
| 71 | stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2 |
| 72 | keywords = [stop_str] |
| 73 | stopping_criteria = [KeywordsStoppingCriteria(keywords, tokenizer, input_ids)] if conv.version == "v0" else None |
| 74 | |
| 75 | with torch.inference_mode(): |
| 76 | output_ids = model.generate( |
| 77 | input_ids, |
| 78 | images=images, |
| 79 | do_sample=True if args.temperature > 0 else False, |
| 80 | temperature=args.temperature, |
| 81 | max_new_tokens=1024, |
| 82 | use_cache=True, |
| 83 | stopping_criteria=stopping_criteria, |
| 84 | ) |
| 85 | |
| 86 | input_token_len = input_ids.shape[1] |
no test coverage detected