Exemplo n.º 1
0
 def testSamplingContainmentSetMinusMultiple(self):
     base = distribs.Continuous('x', 2, 10)
     hold_out = distribs.Mixture([
         distribs.Discrete('x', [1, 4, 6]),
         distribs.Discrete('x', [3, 8, 9, 12]),
         distribs.Continuous('x', 3, 5),
     ])
     contained = [{'x': value} for value in [2.5, 5.5, 7, 9.5]]
     not_contained = [{'x': value} for value in [4, 6, 9, 11]]
     d = distribs.SetMinus(base, hold_out)
     test_sampling_and_containment(self, d, contained, not_contained)
Exemplo n.º 2
0
    def testCubeWithTunnel(self):
        cube = distribs.Product([
            distribs.Continuous('x', 0, 1),
            distribs.Continuous('y', 0, 1),
            distribs.Continuous('z', 0, 1),
        ])
        tunnel = distribs.Product([
            distribs.Continuous('x', 0.25, 0.75),
            distribs.Continuous('y', 0.25, 0.75),
        ])
        cube_with_tunnel = distribs.SetMinus(cube, tunnel)

        contained = [{
            'x': 0.2,
            'y': 0.2,
            'z': 0.2
        }, {
            'x': 0.2,
            'y': 0.2,
            'z': 0.5
        }, {
            'x': 0.2,
            'y': 0.5,
            'z': 0.5
        }]
        not_contained = [
            {
                'x': 0.5,
                'y': 0.5,
                'z': 0.5
            },
            {
                'x': 0.5,
                'y': 0.5,
                'z': 0.2
            },
        ]
        test_sampling_and_containment(self, cube_with_tunnel, contained,
                                      not_contained)
Exemplo n.º 3
0
import os
from spriteworld import factor_distributions as distribs
from spriteworld import sprite_generators
from spriteworld import tasks
from spriteworld.configs.cobra import common

TERMINATE_DISTANCE = 0.075
NUM_TARGETS = 1
NUM_DISTRACTORS = 1
MODES_TARGET_POSITIONS = {
    'train':
    distribs.SetMinus(
        distribs.Product((
            distribs.Continuous('x', 0.1, 0.9),
            distribs.Continuous('y', 0.1, 0.9),
        )),
        distribs.Product((
            distribs.Continuous('x', 0.5, 0.9),
            distribs.Continuous('y', 0.5, 0.9),
        )),
    ),
    'test':
    distribs.Product((
        distribs.Continuous('x', 0.5, 0.9),
        distribs.Continuous('y', 0.5, 0.9),
    )),
}


def get_config(mode='train'):
    """Generate environment config.
Exemplo n.º 4
0
def get_config(mode='train'):
    """Generate environment config.

  Args:
    mode: Unused task mode.

  Returns:
    config: Dictionary defining task/environment configuration. Can be fed as
      kwargs to environment.Environment.
  """

    # Factor distributions common to all objects.
    common_factors = distribs.Product([
        distribs.Continuous('x', 0.1, 0.9),
        distribs.Continuous('y', 0.1, 0.9),
        distribs.Continuous('angle', 0, 360, dtype='int32'),
    ])

    # train/test split for goal-finding object scales and clustering object colors
    goal_finding_scale_test = distribs.Continuous('scale', 0.08, 0.12)
    green_blue_colors = distribs.Product([
        distribs.Continuous('c1', 64, 256, dtype='int32'),
        distribs.Continuous('c2', 64, 256, dtype='int32'),
    ])
    if mode == 'train':
        goal_finding_scale = distribs.SetMinus(
            distribs.Continuous('scale', 0.05, 0.15),
            goal_finding_scale_test,
        )
        cluster_colors = distribs.Product([
            distribs.Continuous('c0', 128, 256, dtype='int32'),
            green_blue_colors
        ])
    elif mode == 'test':
        goal_finding_scale = goal_finding_scale_test
        cluster_colors = distribs.Product([
            distribs.Continuous('c0', 0, 128, dtype='int32'), green_blue_colors
        ])
    else:
        raise ValueError(
            'Invalid mode {}. Mode must be "train" or "test".'.format(mode))

    # Create clustering sprite generators
    sprite_gen_list = []
    cluster_shapes = [
        distribs.Discrete('shape', [s])
        for s in ['triangle', 'square', 'pentagon']
    ]
    for shape in cluster_shapes:
        factors = distribs.Product([
            common_factors,
            cluster_colors,
            shape,
            distribs.Continuous('scale', 0.08, 0.12),
        ])
        sprite_gen_list.append(
            sprite_generators.generate_sprites(factors, num_sprites=2))

    # Create goal-finding sprite generators
    goal_finding_colors = [
        distribs.Product([
            distribs.Continuous('c0', 192, 256, dtype='int32'),
            distribs.Continuous('c1', 0, 128, dtype='int32'),
            distribs.Continuous('c2', 64, 128, dtype='int32'),
        ]),
        distribs.Product([
            distribs.Continuous('c0', 0, 128, dtype='int32'),
            distribs.Continuous('c1', 192, 256, dtype='int32'),
            distribs.Continuous('c2', 64, 128, dtype='int32'),
        ])
    ]
    # Goal positions corresponding to the colors in goal_finding_colors
    goal_finding_positions = [(0., 0.5), (1., 0.5)]
    goal_finding_shapes = distribs.Discrete('shape', ['spoke_4', 'star_4'])
    for colors in goal_finding_colors:
        factors = distribs.Product([
            common_factors,
            goal_finding_scale,
            goal_finding_shapes,
            colors,
        ])
        sprite_gen_list.append(
            sprite_generators.generate_sprites(
                factors, num_sprites=lambda: np.random.randint(1, 3)))

    # Create distractor sprite generator
    distractor_factors = distribs.Product([
        common_factors,
        distribs.Discrete('shape', ['circle']),
        distribs.Continuous('c0', 64, 256, dtype='uint8'),
        distribs.Continuous('c1', 64, 256, dtype='uint8'),
        distribs.Continuous('c2', 64, 256, dtype='uint8'),
        distribs.Continuous('scale', 0.08, 0.12),
    ])
    sprite_gen_list.append(
        sprite_generators.generate_sprites(
            distractor_factors, num_sprites=lambda: np.random.randint(0, 3)))

    # Concat clusters into single scene to generate
    sprite_gen = sprite_generators.chain_generators(*sprite_gen_list)
    # Randomize sprite ordering to eliminate any task information from occlusions
    sprite_gen = sprite_generators.shuffle(sprite_gen)

    # Create the combined task of goal-finding and clustering
    task_list = []
    task_list.append(
        tasks.Clustering(cluster_shapes, terminate_bonus=0., reward_range=10.))
    for colors, goal_pos in zip(goal_finding_colors, goal_finding_positions):
        goal_finding_task = tasks.FindGoalPosition(distribs.Product(
            [colors, goal_finding_shapes]),
                                                   goal_position=goal_pos,
                                                   weights_dimensions=(1, 0),
                                                   terminate_distance=0.15,
                                                   raw_reward_multiplier=30)
        task_list.append(goal_finding_task)
    task = tasks.MetaAggregated(task_list,
                                reward_aggregator='sum',
                                termination_criterion='all')

    renderers = {
        'image':
        spriteworld_renderers.PILRenderer(image_size=(64, 64), anti_aliasing=5)
    }

    config = {
        'task': task,
        'action_space': action_spaces.SelectMove(scale=0.5),
        'renderers': renderers,
        'init_sprites': sprite_gen,
        'max_episode_length': 50,
        'metadata': {
            'name': os.path.basename(__file__),
            'mode': mode
        }
    }
    return config
Exemplo n.º 5
0
    def testCornerUnion(self):
        square_0 = distribs.Product([
            distribs.Continuous('x', 0, 3),
            distribs.Continuous('y', 0, 3),
        ])
        hold_out_0 = distribs.Product([
            distribs.Continuous('x', 1, 3),
            distribs.Continuous('y', 0, 2),
        ])
        square_1 = distribs.Product([
            distribs.Continuous('x', 2, 5),
            distribs.Continuous('y', 0, 3),
        ])
        hold_out_1 = distribs.Product([
            distribs.Continuous('x', 2, 4),
            distribs.Continuous('y', 1, 3),
        ])
        corner_0 = distribs.SetMinus(square_0, hold_out_0)
        corner_1 = distribs.SetMinus(square_1, hold_out_1)
        corner_union = distribs.Mixture([corner_0, corner_1])

        contained = [
            {
                'x': 0.5,
                'y': 0.5
            },
            {
                'x': 0.5,
                'y': 2.5
            },
            {
                'x': 2.5,
                'y': 2.5
            },
            {
                'x': 2.5,
                'y': 0.5
            },
            {
                'x': 4.5,
                'y': 0.5
            },
            {
                'x': 4.5,
                'y': 2.5
            },
        ]
        not_contained = [
            {
                'x': 1.5,
                'y': 0.5
            },
            {
                'x': 1.5,
                'y': 1.5
            },
            {
                'x': 2.5,
                'y': 1.5
            },
            {
                'x': 3.5,
                'y': 1.5
            },
            {
                'x': 3.5,
                'y': 2.5
            },
        ]
        test_sampling_and_containment(self, corner_union, contained,
                                      not_contained)
Exemplo n.º 6
0
 def testRaisesError(self):
     d_0 = distribs.Continuous('x', 0, 2)
     d_1 = distribs.Continuous('y', 1, 3)
     with self.assertRaises(ValueError):
         distribs.SetMinus(d_0, d_1)
Exemplo n.º 7
0
 def testSamplingContainmentSetMinusTwo(self, d_0, d_1, contained,
                                        not_contained):
     d = distribs.SetMinus(d_0, d_1)
     contained = [{'x': value} for value in contained]
     not_contained = [{'x': value} for value in not_contained]
     test_sampling_and_containment(self, d, contained, not_contained)