|
| 1 | +from collections import defaultdict |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | + |
| 5 | +from ...smp.file import load |
| 6 | + |
| 7 | +# for MMOral-OPG-Bench |
| 8 | + |
| 9 | + |
| 10 | +def build_mmoral_opg_gpt4_prompt(line): |
| 11 | + question = line['question'] |
| 12 | + gt = str(line['answer']) |
| 13 | + prediction = str(line['prediction']) |
| 14 | + # Keep this prompt readable and flake8-friendly (avoid overly long lines). |
| 15 | + prompt = """ |
| 16 | +Given the question, compare the ground truth and prediction from AI |
| 17 | +models, to generate a correctness score for the prediction. |
| 18 | +The correctness score is 0.0 (totally wrong), 0.1, 0.2, 0.3, 0.4, 0.5, |
| 19 | +0.6, 0.7, 0.8, 0.9, or 1.0 (totally right). |
| 20 | +Just complete the last space of the correctness score. |
| 21 | +
|
| 22 | +Question | Ground truth | Prediction | Correctness |
| 23 | +--- | --- | --- | --- |
| 24 | +How many teeth are visualized in the radiograph? | 30 teeth are visualized with clear anatomical |
| 25 | +definition. | 30 | 1.0 |
| 26 | +How many teeth are visualized in the radiograph? | 30 teeth are visualized with clear anatomical |
| 27 | +definition. | 29 teeth are visualized with clear anatomical definition. | 0.0 |
| 28 | +What is the status of the wisdom teeth in the radiograph? | Three |
| 29 | +wisdom teeth are detected, all of which are impacted: #18, #28, and #48. |
| 30 | +| #18: impacted, #28: impacted, #48: erupted | 0.7 |
| 31 | +What is the condition of the teeth #26 and #14? | Teeth #26 and #14 |
| 32 | +show signs of periapical abscesses. | Teeth #26 and #23 show signs |
| 33 | +of periapical abscesses. | 0.5 |
| 34 | +What is the condition of the bone architecture and visible structures in |
| 35 | +the jaw? | No apparent bone loss is observed. Bilateral mandibular |
| 36 | +canals and maxillary sinuses are clearly visible. | Bilateral |
| 37 | +mandibular canals and maxillary sinuses are clearly visible. | 0.5 |
| 38 | +What is the clinical priority concern regarding the periapical lesions? |
| 39 | +| Periapical cysts at #11 and #12, and granuloma at #46 require |
| 40 | +endodontic evaluation. | Periapical lesions at #11, #12, and #46 |
| 41 | +require endodontic evaluation. | 0.8 |
| 42 | +What radiographic features are visible in tooth #31 on the panoramic X-ray? | [\n |
| 43 | +{\"Teeth position\": {\"point_2d\": [1242, 726]}},\n |
| 44 | +{\"Crown\": {\"box_2d\": [1220, 637, 1266, 741]}}\n |
| 45 | +] | Crown | 0.8 |
| 46 | +What radiographic features are visible in tooth #31 on the panoramic X-ray? | [\n |
| 47 | +{\"Teeth position\": {\"point_2d\": [1242, 726]}},\n |
| 48 | +{\"Crown\": {\"box_2d\": [1220, 637, 1266, 741]}}\n |
| 49 | +] | Crown at position: [1230, 627, 1276, 750] | 0.9 |
| 50 | +What radiographic features are visible in tooth #31 on the panoramic X-ray? | [\n |
| 51 | +{\"Teeth position\": {\"point_2d\": [1242, 726]}},\n |
| 52 | +{\"Crown\": {\"box_2d\": [1220, 637, 1266, 741]}}\n |
| 53 | +] | Teeth at position: {\"point_2d\": [1242, 726]}},\n |
| 54 | +{Crown at position: {\"box_2d\": [1230, 627, 1276, 750]}} | 1.0 |
| 55 | +""" |
| 56 | + gpt4_prompt = prompt + '\n' + ' | '.join( |
| 57 | + [question, gt.replace('<AND>', ' <AND> ').replace('<OR>', ' <OR> '), prediction, '']) |
| 58 | + return gpt4_prompt |
| 59 | + |
| 60 | + |
| 61 | +def MMOral_opg_auxeval(model, line): |
| 62 | + def float_cvt(s): |
| 63 | + try: |
| 64 | + return float(s) |
| 65 | + except ValueError: |
| 66 | + return None |
| 67 | + |
| 68 | + prompt = build_mmoral_opg_gpt4_prompt(line) |
| 69 | + log = '' |
| 70 | + retry = 5 |
| 71 | + for i in range(retry): |
| 72 | + output = model.generate(prompt, temperature=i * 0.5) |
| 73 | + score = float_cvt(output) |
| 74 | + if score is None: |
| 75 | + log += f'Try {i}: output is {output}, failed to parse.\n' |
| 76 | + elif score < 0 or score > 1: |
| 77 | + log += f'Try {i}: output is {output}, invalid score: {score}.\n' |
| 78 | + else: |
| 79 | + log += 'Succeed' |
| 80 | + return dict(log=log, score=score) |
| 81 | + log += 'All 5 retries failed.\n' |
| 82 | + return dict(log=log, score=0.0) |
| 83 | + |
| 84 | + |
| 85 | +def MMOral_opg_acc(result_file): |
| 86 | + data = load(result_file) |
| 87 | + tot = defaultdict(lambda: 0) |
| 88 | + score = defaultdict(lambda: 0) |
| 89 | + lt = len(data) |
| 90 | + cate2_list = [] |
| 91 | + for i in range(lt): |
| 92 | + item = data.iloc[i] |
| 93 | + cate = item['category'] |
| 94 | + cate2 = cate.replace(',', '_') |
| 95 | + if cate2 not in cate2_list: |
| 96 | + cate2_list.append(cate2) |
| 97 | + grade = float(item['score']) |
| 98 | + cate_list = ['Teeth', 'Patho', 'HisT', 'Jaw', 'SumRec', 'Report'] |
| 99 | + for capa in cate_list: |
| 100 | + if capa in cate: |
| 101 | + tot[capa] += 1 |
| 102 | + score[capa] += grade |
| 103 | + tot['Overall'] += 1 |
| 104 | + tot[cate2] += 1 |
| 105 | + score['Overall'] += grade |
| 106 | + score[cate2] += grade |
| 107 | + |
| 108 | + res = defaultdict(list) |
| 109 | + res2 = defaultdict(list) |
| 110 | + cate_list.append('Overall') |
| 111 | + cate2_list.append('Overall') |
| 112 | + for k in cate_list: |
| 113 | + res['Category'].append(k) |
| 114 | + res['tot'].append(tot[k]) |
| 115 | + res['acc'].append(score[k] / tot[k] * 100) |
| 116 | + for v in cate2_list: |
| 117 | + res2['Category'].append(v) |
| 118 | + res2['tot'].append(tot[v]) |
| 119 | + res2['acc'].append(score[v] / tot[v] * 100) |
| 120 | + res = pd.DataFrame(res) |
| 121 | + res2 = pd.DataFrame(res2) |
| 122 | + return res, res2 |
0 commit comments