|
| 1 | +import logging |
| 2 | +import re |
| 3 | +from PIL import Image |
| 4 | +import numpy as np |
| 5 | +import io |
| 6 | +import pandas as pd |
| 7 | +from collections import defaultdict |
| 8 | +from lmms_eval.filters.extraction import ExtendedRegexFilter |
| 9 | +from lmms_eval.filters.transformation import MapFilter |
| 10 | +import re |
| 11 | + |
| 12 | +eval_logger = logging.getLogger("lmms-eval") |
| 13 | + |
| 14 | + |
| 15 | +def msr_doc_to_text(doc, lmms_eval_specific_kwargs=None): |
| 16 | + question = doc["question"].strip() |
| 17 | + if "pre_prompt" in lmms_eval_specific_kwargs and lmms_eval_specific_kwargs["pre_prompt"] != "": |
| 18 | + question = f"{lmms_eval_specific_kwargs['pre_prompt']}{question}" |
| 19 | + if "post_prompt" in lmms_eval_specific_kwargs and lmms_eval_specific_kwargs["post_prompt"] != "": |
| 20 | + question = f"{question}{lmms_eval_specific_kwargs['post_prompt']}" |
| 21 | + return question |
| 22 | + |
| 23 | + |
| 24 | +def msr_doc_to_visual(doc): |
| 25 | + # image_list = [image.convert("RGB") for image in doc["images"]] |
| 26 | + image_list = [] |
| 27 | + for img_data in doc["images"]: |
| 28 | + image = Image.open(io.BytesIO(img_data)) |
| 29 | + image = image.convert("RGB") |
| 30 | + image_list.append(image) |
| 31 | + return image_list |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +def extract_single_choice_with_word_boundary(pred, gt): |
| 37 | + pattern_1 = r'``([^`]*)``' |
| 38 | + match = re.search(pattern_1, pred) |
| 39 | + if match: |
| 40 | + pred = match.group(1) |
| 41 | + |
| 42 | + pattern_2 = r'`([^`]*)`' |
| 43 | + match = re.search(pattern_2, pred) |
| 44 | + if match: |
| 45 | + pred = match.group(1) |
| 46 | + |
| 47 | + pattern_add = r'\{([^}]*)\}' |
| 48 | + match = re.search(pattern_add, pred) |
| 49 | + if match: |
| 50 | + pred = match.group(1) |
| 51 | + |
| 52 | + pattern_3 = r'\b[A-D]\b(?!\s[a-zA-Z])' |
| 53 | + match = re.search(pattern_3, pred) |
| 54 | + if match: |
| 55 | + pred = match.group() |
| 56 | + else: |
| 57 | + return None |
| 58 | + |
| 59 | + answer = gt.lower().replace("\n", " ").strip() |
| 60 | + predict = pred.lower().replace("\n", " ").strip() |
| 61 | + try: |
| 62 | + if answer == predict[0]: |
| 63 | + return 1.0 |
| 64 | + elif predict[0] == "(" and answer == predict[1]: |
| 65 | + return 1.0 |
| 66 | + elif predict[0:7] == "option " and answer == predict[7]: |
| 67 | + return 1.0 |
| 68 | + elif predict[0:14] == "the answer is " and answer == predict[14]: |
| 69 | + return 1.0 |
| 70 | + except Exception as e: |
| 71 | + return 0.0 |
| 72 | + return 0.0 |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +def msr_process_results(doc, results): |
| 77 | + """ |
| 78 | + Args: |
| 79 | + doc: a instance of the eval dataset |
| 80 | + results: [pred] |
| 81 | + Returns: |
| 82 | + a dictionary with key: metric name, value: metric value |
| 83 | + """ |
| 84 | + pred = results[0] |
| 85 | + gt = doc["answer"] |
| 86 | + |
| 87 | + score = extract_single_choice_with_word_boundary(pred, gt) |
| 88 | + category = doc["question_type"] |
| 89 | + l2_category = doc["question_type"] |
| 90 | + if score is None: |
| 91 | + return {category: {"question_id": doc["id"], "l2_category": l2_category, "score": 0, "note": "can not find anwser"}, "average": {"question_id": doc["id"], "l2_category": l2_category, "score": 0, "note": "can not find anwser"}} |
| 92 | + return {category: {"question_id": doc["id"], "l2_category": l2_category, "score": score}, "average": {"question_id": doc["id"], "l2_category": l2_category, "score": score}} |
| 93 | + |
| 94 | + |
| 95 | +def msr_aggregate_results(results): |
| 96 | + """ |
| 97 | + Args: |
| 98 | + results: a list of values returned by process_results |
| 99 | + Returns: |
| 100 | + A score |
| 101 | + """ |
| 102 | + l2_category_scores = defaultdict(list) |
| 103 | + for result in results: |
| 104 | + score = result["score"] |
| 105 | + l2_category = result["l2_category"] |
| 106 | + l2_category_scores[l2_category].append(score) |
| 107 | + |
| 108 | + l2_category_avg_score = {} |
| 109 | + for l2_category, scores in l2_category_scores.items(): |
| 110 | + avg_score = sum(scores) / len(scores) |
| 111 | + l2_category_avg_score[l2_category] = avg_score |
| 112 | + eval_logger.info(f"{l2_category}: {avg_score:.2f}") |
| 113 | + |
| 114 | + all_scores = [score for scores in l2_category_scores.values() for score in scores] |
| 115 | + avg_score = sum(all_scores) / len(all_scores) if all_scores else 0.0 |
| 116 | + return avg_score |
| 117 | + |
0 commit comments