This commit is contained in:
+20
-3
@@ -5,6 +5,13 @@ from typing import Any
|
||||
|
||||
from data.schema import COLORS, OBJECT_IDS, RELATIONS, SHAPES, Fact, SceneObject
|
||||
|
||||
HELD_OUT_SHAPES = {
|
||||
"red": "triangle",
|
||||
"blue": "square",
|
||||
"green": "star",
|
||||
"yellow": "circle",
|
||||
}
|
||||
|
||||
|
||||
def _relation(first: SceneObject, second: SceneObject, rng: random.Random) -> Fact:
|
||||
relation = rng.choice(RELATIONS)
|
||||
@@ -23,12 +30,23 @@ def _relation(first: SceneObject, second: SceneObject, rng: random.Random) -> Fa
|
||||
return Fact(first.object_id, relation, second.object_id), first, second
|
||||
|
||||
|
||||
def generate_scene(scene_id: int, seed: int, num_objects: int = 2) -> dict[str, Any]:
|
||||
def generate_scene(
|
||||
scene_id: int, seed: int, num_objects: int = 2, held_out_composition: bool = False
|
||||
) -> dict[str, Any]:
|
||||
if num_objects != 2:
|
||||
raise ValueError("The stage-one MVP currently supports exactly two objects.")
|
||||
rng = random.Random((seed * 1_000_003) + scene_id)
|
||||
colors = rng.sample(COLORS, k=num_objects)
|
||||
shapes = rng.sample(SHAPES, k=num_objects)
|
||||
if held_out_composition:
|
||||
first_shape = HELD_OUT_SHAPES[colors[0]]
|
||||
second_choices = [shape for shape in SHAPES if shape != first_shape]
|
||||
shapes = [first_shape, rng.choice(second_choices)]
|
||||
else:
|
||||
shapes = []
|
||||
for color in colors:
|
||||
choices = [shape for shape in SHAPES if shape != HELD_OUT_SHAPES[color]]
|
||||
choices = [shape for shape in choices if shape not in shapes]
|
||||
shapes.append(rng.choice(choices))
|
||||
objects = [
|
||||
SceneObject(OBJECT_IDS[index], colors[index], shapes[index], 0, 0)
|
||||
for index in range(num_objects)
|
||||
@@ -49,4 +67,3 @@ def generate_scene(scene_id: int, seed: int, num_objects: int = 2) -> dict[str,
|
||||
"objects": [item.to_dict() for item in objects],
|
||||
"visual_facts": [fact.to_dict() for fact in facts],
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user