Skip to content

Instantly share code, notes, and snippets.

@jcjohnson
Created April 13, 2019 22:53
Show Gist options
  • Save jcjohnson/9f3173703f8578db787345d0ce61002a to your computer and use it in GitHub Desktop.
Save jcjohnson/9f3173703f8578db787345d0ce61002a to your computer and use it in GitHub Desktop.
import argparse, os, json, random
from collections import defaultdict
parser = argparse.ArgumentParser()
parser.add_argument('--questions_file',
default='../output/CLEVR_v1.0/questions/CLEVR_val_questions.json')
parser.add_argument('--template_dir', default='CLEVR_1.0_templates/')
parser.add_argument('--num_samples', type=int, default=5)
TEMPLATE_ORDER = [
'compare_integer.json',
'comparison.json',
'three_hop.json',
'single_and.json',
'same_relate.json',
'single_or.json',
'one_hop.json',
'two_hop.json',
'zero_hop.json',
]
def main(args):
# Load templates
num_loaded_templates = 0
templates = {}
family_index_to_template = {}
for fn in TEMPLATE_ORDER:
with open(os.path.join(args.template_dir, fn), 'r') as f:
base = os.path.splitext(fn)[0]
for i, template in enumerate(json.load(f)):
family_index_to_template[num_loaded_templates] = template
num_loaded_templates += 1
key = (fn, i)
templates[key] = template
print('Read %d templates from disk' % num_loaded_templates)
# Load questions
with open(args.questions_file, 'r') as f:
questions = json.load(f)['questions']
print('Read %d questions from disk' % len(questions))
# Group questions by family index
family_index_to_questions = defaultdict(list)
for q in questions:
family_index = q['question_family_index']
family_index_to_questions[family_index].append(q)
# Print out randomly selected questions and their recovered templates
for _ in range(args.num_samples):
q = random.choice(questions)
qfi = q['question_family_index']
template = family_index_to_template[qfi]
print(q['question'])
for t in template['text']:
print(t)
print()
if __name__ == '__main__':
main(parser.parse_args())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment