|
| 1 | +import re |
| 2 | + |
| 3 | +import datasets |
| 4 | + |
| 5 | +egoplan2_features = datasets.Features( |
| 6 | + { |
| 7 | + "sample_id": datasets.Value("string"), |
| 8 | + "domain": datasets.Value("string"), |
| 9 | + "task_goal": datasets.Value("string"), |
| 10 | + "task_start_frame": datasets.Value("int64"), |
| 11 | + "current_observation_frame": datasets.Value("int64"), |
| 12 | + "formatted_question": datasets.Value("string"), |
| 13 | + "choice_a": datasets.Value("string"), |
| 14 | + "choice_b": datasets.Value("string"), |
| 15 | + "choice_c": datasets.Value("string"), |
| 16 | + "choice_d": datasets.Value("string"), |
| 17 | + "ground_truth": datasets.Value("string"), |
| 18 | + "video_file": datasets.Value("string"), |
| 19 | + "keyframes": datasets.Sequence(datasets.Image(decode=True)), |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def egoplan2_doc_to_visual(doc): |
| 25 | + return doc["keyframes"] |
| 26 | + |
| 27 | + |
| 28 | +def egoplan2_doc_to_text(doc, lmms_eval_specific_kwargs=None): |
| 29 | + return doc["formatted_question"] |
| 30 | + |
| 31 | + |
| 32 | +def extract_characters_regex(s): |
| 33 | + s = s.strip() |
| 34 | + answer_prefixes = [ |
| 35 | + "The best answer is", |
| 36 | + "The correct answer is", |
| 37 | + "The answer is", |
| 38 | + "The answer", |
| 39 | + "The best option is" "The correct option is", |
| 40 | + "Best answer:" "Best option:", |
| 41 | + ] |
| 42 | + for answer_prefix in answer_prefixes: |
| 43 | + s = s.replace(answer_prefix, "") |
| 44 | + |
| 45 | + if len(s.split()) > 10 and not re.search("[ABCD]", s): |
| 46 | + return "" |
| 47 | + |
| 48 | + matches = re.search(r"[ABCD]", s) |
| 49 | + if matches is None: |
| 50 | + return "" |
| 51 | + return matches[0] |
| 52 | + |
| 53 | + |
| 54 | +def egoplan2_process_results(doc, results): |
| 55 | + pred = results[0] |
| 56 | + pred_ans = extract_characters_regex(pred) |
| 57 | + # Only keep fields needed for aggregation (exclude keyframes to avoid OOM |
| 58 | + # during multi-GPU gather_object which pickles the entire dict). |
| 59 | + data_dict = { |
| 60 | + "sample_id": doc.get("sample_id"), |
| 61 | + "pred_answer": pred_ans, |
| 62 | + "ground_truth": doc["ground_truth"], |
| 63 | + } |
| 64 | + return {"egoplan2_mcq_accuracy": data_dict} |
| 65 | + |
| 66 | + |
| 67 | +def egoplan2_aggregate_results(results): |
| 68 | + correct_num = 0 |
| 69 | + for result in results: |
| 70 | + if result["pred_answer"] == result["ground_truth"]: |
| 71 | + correct_num += 1 |
| 72 | + question_num = len(results) |
| 73 | + accuracy = correct_num / question_num |
| 74 | + return accuracy |
0 commit comments