(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.loads(q) for q in 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 line in tqdm(questions): |
| 42 | idx = line["question_id"] |
| 43 | image_file = line["image"] |
| 44 | qs = line["text"] |
| 45 | cur_prompt = qs |
| 46 | if model.config.mm_use_im_start_end: |
| 47 | qs = DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_TOKEN + DEFAULT_IM_END_TOKEN + '\n' + qs |
| 48 | else: |
| 49 | qs = DEFAULT_IMAGE_TOKEN + '\n' + qs |
| 50 | |
| 51 | conv = conv_templates[args.conv_mode].copy() |
| 52 | conv.append_message(conv.roles[0], qs) |
| 53 | conv.append_message(conv.roles[1], None) |
| 54 | prompt = conv.get_prompt() |
| 55 | |
| 56 | input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0).cuda() |
| 57 | |
| 58 | image = Image.open(os.path.join(args.image_folder, image_file)) |
| 59 | image_tensor = image_processor.preprocess(image, return_tensors='pt')['pixel_values'][0] |
| 60 | |
| 61 | stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2 |
| 62 | keywords = [stop_str] |
| 63 | stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids) |
| 64 | |
| 65 | with torch.inference_mode(): |
| 66 | output_ids = model.generate( |
| 67 | input_ids, |
| 68 | images=image_tensor.unsqueeze(0).half().cuda(), |
| 69 | do_sample=True if args.temperature > 0 else False, |
| 70 | temperature=args.temperature, |
| 71 | top_p=args.top_p, |
| 72 | num_beams=args.num_beams, |
| 73 | # no_repeat_ngram_size=3, |
| 74 | max_new_tokens=1024, |
| 75 | use_cache=True) |
| 76 | |
| 77 | input_token_len = input_ids.shape[1] |
| 78 | n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item() |
| 79 | if n_diff_input_output > 0: |
| 80 | print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids') |
| 81 | outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0] |
| 82 | outputs = outputs.strip() |
| 83 | if outputs.endswith(stop_str): |
| 84 | outputs = outputs[:-len(stop_str)] |
| 85 | outputs = outputs.strip() |
| 86 |
no test coverage detected