File size: 5,320 Bytes
c94c8c9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import os
import json
import collections
from pathlib import Path
import torch
from evaluator.build import EVALUATOR_REGISTRY, BaseEvaluator
from data.data_utils import ScanQAAnswer, clean_answer
from common.box_utils import get_3d_box
from evaluator.build import EVALUATOR_REGISTRY
@EVALUATOR_REGISTRY.register()
class ScanQAEval(BaseEvaluator):
def __init__(self, cfg, accelerator, **kwargs):
self.target_metric = 'ans1_acc'
self.save_dir = Path(cfg.exp_dir) / "eval_results" / self.__class__.__name__
super().__init__(cfg, accelerator, **kwargs)
self.color_initials = ["which color", "what color", "what colour", "which colour"]
# if self.save:
train_data = json.load(open(os.path.join(cfg.data.scan_family_base,
'annotations/qa/ScanQA_v1.0_train.json'), encoding='utf-8'))
answer_counter = sum([data['answers'] for data in train_data], [])
answer_counter = collections.Counter(sorted(answer_counter))
answer_cands = answer_counter.keys()
self.answer_vocab = ScanQAAnswer(answer_cands)
def _contains_color(self, text: str) -> bool:
t = text.lower()
return any(color in t for color in self.color_terms) # self.color_terms is a list of color words/phrases
def batch_metrics(self, data_dict, include_count=False):
metrics = {}
total_count = len(data_dict['answer_scores'])
# ans
choice_1 = data_dict['answer_scores'].argmax(dim=-1)
choice_10 = torch.topk(data_dict['answer_scores'].detach(), 10, -1)[1]
correct1 = 0
correct10 = 0
non_color_correct1 = 0
non_color_correct10 = 0
non_color_total_count = 0
for i in range(data_dict['answer_label'].shape[0]):
question = data_dict['sentence'][i].lower()
if not any(init in question for init in self.color_initials):
non_color_total_count += 1
if data_dict['answer_label'][i, choice_1[i]] == 1:
if not any(init in question for init in self.color_initials):
non_color_correct1 += 1
else:
print(question, data_dict['answer_label'][i].argmax().item(), choice_1[i])
correct1 += 1
for j in range(10):
if data_dict['answer_label'][i, choice_10[i, j]] == 1:
correct10 += 1
if not any(init in question for init in self.color_initials):
non_color_correct10 += 1
break
metrics['ans1_acc'] = correct1
metrics['ans10_acc'] = correct10
metrics['non_color_ans1_acc'] = non_color_correct1
metrics['non_color_ans10_acc'] = non_color_correct10
# get obj cls acc
for key in data_dict.keys():
if key.endswith('logits') and data_dict[key].ndim == 3 and data_dict[key].shape[:2] == data_dict['obj_labels'].shape:
new_key = key.replace('logits', 'acc')
pred = torch.argmax(data_dict[key], dim=2)
gt = data_dict['obj_labels']
mask = data_dict['obj_masks']
metrics[new_key] = ((pred[mask] == gt[mask]).sum().item(), data_dict['obj_masks'].sum().item())
for key in metrics:
if isinstance(metrics[key], tuple):
# already has count
continue
if 'non_color' in key:
metrics[key] = (metrics[key], non_color_total_count)
else:
metrics[key] = (metrics[key], total_count)
if self.save:
for i in range(total_count):
answer_top10 = [self.answer_vocab.itos(choice_10[i, j].item()) for j in range(10)]
og3d_pred = torch.argmax(data_dict['og3d_logits'], dim=1)
box = data_dict['obj_boxes'][i, og3d_pred[i]].cpu().numpy()
box_center = box[0:3]
box_size = box[3:6]
pred_data = {
"scene_id": data_dict["scan_id"][i],
"question_id": data_dict["data_idx"][i],
"answer_top10": answer_top10,
"bbox": get_3d_box(box_center, box_size).tolist()
}
self.eval_results.append(pred_data)
if not include_count:
for key, v in metrics.items():
metrics[key] = v[0] / max(v[1], 1)
return metrics
@EVALUATOR_REGISTRY.register()
class ScanQAGenEval(ScanQAEval):
def __init__(self, cfg, accelerator, **kwargs):
super().__init__(cfg, accelerator, **kwargs)
def batch_metrics(self, data_dict, include_count=False):
metrics = {}
answer_preds = [clean_answer(a) for a in data_dict['answer_pred']]
answer_gts = [list(map(clean_answer, a)) for a in data_dict['answers']]
correct = len([1 for pred, gts in zip(answer_preds, answer_gts) if pred in gts])
metrics['ans1_acc'] = (correct, len(answer_preds))
if not include_count:
for key, v in metrics.items():
metrics[key] = v[0] / max(v[1], 1)
return metrics |