File size: 2,593 Bytes
806c783
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import random
import ast
SEED=42
random.seed(SEED)


def generate_shape_color_question_and_answer(info):
    """
    Ask about a specific (shape, color) pair.
    
    Returns:
        question (str): e.g.
            "In the scene 'skywalk' under 'middle' lighting, how many black cones are there?"
        answer   (int)
    """
    # parse the stringified keys into real tuples
    parsed = {}
    for k, count in info.get("shape_color_counts", {}).items():
        shape, color = ast.literal_eval(k)
        parsed[(shape, color)] = count

    (shape, color), cnt = random.choice(list(parsed.items()))
    question = (
        f"In the scene, "
        f"how many {color} {shape}'s are there?"
    )
    return question, cnt


def generate_shape_question_and_answer(info):
    """
    Ask about the total number of a given shape, agnostic of color.
    
    Returns:
        question (str): e.g.
            "In the scene 'skywalk' under 'middle' lighting, how many cones are there in total?"
        answer   (int)
    """
    # first try the provided shape_counts
    shape_counts = info.get("shape_counts", {})
    
    # fallback: aggregate from shape_color_counts if needed
    if not shape_counts:
        agg = {}
        for k, count in info.get("shape_color_counts", {}).items():
            shape, _ = ast.literal_eval(k)
            agg[shape] = agg.get(shape, 0) + count
        shape_counts = agg

    shape, total = random.choice(list(shape_counts.items()))
    question = (
        f"In the scene, "
        f"how many {shape}'s are there in total?"
    )
    return question, total

import os
import json

base_dir = "3D_DoYouSeeMe/shape_discrimination"

os.listdir(base_dir)

data_list = []
for filename in os.listdir(base_dir):
    if filename.endswith(".json"):
        # print(filename)
        with open(os.path.join(base_dir, filename), "r") as f:
            data = f.read()
        data = json.loads(data)
        q, a = generate_shape_question_and_answer(data)

        num_shapes = data["num_shapes"]
        max_instances_per_shape = data["max_instances_per_shape"]
        min_visibility = data["min_visibility"]
        
        data_list.append({"filename": os.path.splitext(filename)[0] + ".png",
                          "question": q,
                          "answer": a,
                          "sweep": [num_shapes, max_instances_per_shape, min_visibility]})

import pandas as pd
df = pd.DataFrame(data_list)
df.to_csv(os.path.join(base_dir, "dataset_info.csv"), index=False)