forked from allenai/aokvqa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
200 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import argparse | ||
import pathlib | ||
import json | ||
import glob | ||
|
||
from load_aokvqa import load_aokvqa | ||
|
||
|
||
def eval_aokvqa(dataset, preds, multiple_choice=False, strict=True): | ||
|
||
if isinstance(dataset, list): | ||
dataset = { dataset[i]['question_id'] : dataset[i] for i in range(len(dataset)) } | ||
|
||
if multiple_choice: | ||
dataset = {k:v for k,v in dataset.items() if v['difficult_direct_answer'] is False} | ||
|
||
if strict: | ||
dataset_qids = set(dataset.keys()) | ||
preds_qids = set(preds.keys()) | ||
assert dataset_qids.issubset(preds_qids) | ||
|
||
# dataset = q_id (str) : dataset element (dict) | ||
# preds = q_id (str) : prediction (str) | ||
|
||
acc = [] | ||
|
||
for q in dataset.keys(): | ||
if q not in preds.keys(): | ||
acc.append(0.0) | ||
continue | ||
|
||
pred = preds[q] | ||
choices = dataset[q]['choices'] | ||
direct_answers = dataset[q]['direct_answers'] | ||
|
||
## Multiple Choice setting | ||
if multiple_choice: | ||
if strict: | ||
assert pred in choices, 'Prediction must be a valid choice' | ||
correct_choice_idx = dataset[q]['correct_choice_idx'] | ||
acc.append( float(pred == choices[correct_choice_idx]) ) | ||
## Direct Answer setting | ||
else: | ||
num_match = sum([pred == da for da in direct_answers]) | ||
vqa_acc = min(1.0, num_match / 3.0) | ||
acc.append(vqa_acc) | ||
|
||
acc = sum(acc) / len(acc) * 100 | ||
|
||
return acc | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--aokvqa-dir', type=pathlib.Path, required=True, dest='aokvqa_dir') | ||
parser.add_argument('--split', type=str, choices=['train', 'val', 'test_w_ans'], required=True) | ||
parser.add_argument('--preds', type=str, required=True, dest='prediction_files') | ||
parser.add_argument('--multiple-choice', action='store_true', dest='multiple_choice') | ||
args = parser.parse_args() | ||
|
||
dataset = load_aokvqa(args.aokvqa_dir, args.split) | ||
|
||
for prediction_file in glob.glob(args.prediction_files): | ||
predictions = json.load(open(prediction_file, 'r')) | ||
|
||
acc = eval_aokvqa( | ||
dataset, | ||
predictions, | ||
multiple_choice=args.multiple_choice, | ||
ensure_valid_choice=False | ||
) | ||
|
||
print(prediction_file, acc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import argparse | ||
import pathlib | ||
import json | ||
|
||
from load_aokvqa import load_aokvqa | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--aokvqa-dir', type=pathlib.Path, required=True, dest='aokvqa_dir') | ||
parser.add_argument('--split', type=str, choices=['train', 'val', 'test'], required=True) | ||
parser.add_argument('--mc', type=argparse.FileType('r'), dest='mc_pred_file') | ||
parser.add_argument('--da', type=argparse.FileType('r'), dest='da_pred_file') | ||
parser.add_argument('--out', type=argparse.FileType('w'), dest='output_file') | ||
args = parser.parse_args() | ||
assert args.mc_pred_file or args.da_pred_file | ||
|
||
dataset = load_aokvqa(args.aokvqa_dir, args.split) | ||
mc_preds = json.load(args.mc_pred_file) if args.mc_pred_file else None | ||
da_preds = json.load(args.da_pred_file) if args.da_pred_file else None | ||
predictions = {} | ||
|
||
for d in dataset: | ||
q = d['question_id'] | ||
predictions[q] = {} | ||
if mc_preds and q in mc_preds.keys(): | ||
predictions[q]['multiple_choice'] = mc_preds[q] | ||
if da_preds and q in da_preds.keys(): | ||
predictions[q]['direct_answer'] = da_preds[q] | ||
|
||
json.dump(predictions, args.output_file) |
Oops, something went wrong.