Ejemplo n.º 1
0
 def test_complicated_object_vec_to_scene(self):
     steps = 50
     _, _, images, objects = simulator.magic_ponies(
         self._task_complicated_object_test,
         self._ball_user_input_big,
         steps=steps,
         stride=1,
         need_images=True,
         need_featurized_objects=True)
     objects = phyre.simulation.Simulation(
         featurized_objects=objects).featurized_objects.features
     for i in range(len(images)):
         image = images[i]
         original_object_vec = objects[i]
         object_vec = original_object_vec
         for j in range(5):
             # Test no loss of information in conversion
             self.assertTrue(
                 np.allclose(original_object_vec,
                             simulator.scene_to_featurized_objects(
                                 phyre.objects_util.
                                 featurized_objects_vector_to_scene(
                                     object_vec)).features,
                             atol=1e-4))
             object_vec = simulator.scene_to_featurized_objects(
                 phyre.objects_util.featurized_objects_vector_to_scene(
                     object_vec)).features[0]
         self.assertTrue((image == simulator.scene_to_raster(
             phyre.objects_util.featurized_objects_vector_to_scene(
                 object_vec))).all())
Ejemplo n.º 2
0
 def test_render_with_input(self):
     scene = simulator.simulate_task_with_input(self._task,
                                                self._box_user_input,
                                                steps=1).sceneList[0]
     array = simulator.scene_to_raster(scene)
     self.assertEqual(len(array.shape), 2)
     self.assertEqual(array.shape[0], self._task.scene.height)
     self.assertEqual(array.shape[1], self._task.scene.width)
Ejemplo n.º 3
0
    def test_add_input_and_ponies(self):
        steps = 10
        task_simulation = simulator.simulate_task_with_input(
            self._task, self._ball_user_input, steps=steps, stride=1)

        is_solved, had_occlusions, images, scenes = simulator.magic_ponies(
            self._task,
            self._ball_user_input,
            steps=steps,
            stride=1,
            need_images=True,
            need_featurized_objects=True)

        self.assertEqual(is_solved, task_simulation.isSolution)
        self.assertEqual(len(images), steps)
        self.assertEqual(len(task_simulation.sceneList), steps)
        self.assertEqual(
            had_occlusions, task_simulation.sceneList[0].user_input_status ==
            scene_if.UserInputStatus.HAD_OCCLUSIONS)

        # Check images match target scenes
        self.assertFalse(
            np.array_equal(
                images[0],
                simulator.scene_to_raster(task_simulation.sceneList[-1])))
        self.assertTrue((images[-1] == simulator.scene_to_raster(
            task_simulation.sceneList[-1])).all())

        # Test just images works
        _, _, only_images, _ = simulator.magic_ponies(
            self._task,
            self._ball_user_input,
            steps=steps,
            stride=1,
            need_images=True,
            need_featurized_objects=False)
        self.assertTrue(np.array_equal(images, only_images))
        # Test just scenes works
        _, _, _, only_scenes = simulator.magic_ponies(
            self._task,
            self._ball_user_input,
            steps=steps,
            stride=1,
            need_images=False,
            need_featurized_objects=True)
        self.assertTrue(np.array_equal(scenes, only_scenes))
Ejemplo n.º 4
0
    def test_add_input_and_ponies(self):
        raise unittest.SkipTest
        steps = 10

        task_simulation = simulator.simulate_task_with_input(
            self._task, self._box_user_input, steps=steps, stride=1)

        is_solved, had_occlusions, images = simulator.magic_ponies(
            self._task, self._box_compressed_user_input, steps=steps, stride=1)

        self.assertEqual(is_solved, task_simulation.isSolution)
        self.assertEqual(len(images), steps)
        self.assertEqual(len(task_simulation.sceneList), steps)
        self.assertEqual(
            had_occlusions, task_simulation.sceneList[0].user_input_status ==
            scene_if.UserInputStatus.HAD_OCCLUSIONS)
        self.assertFalse((images[0] == simulator.scene_to_raster(
            task_simulation.sceneList[-1])).all())
        self.assertTrue((images[-1] == simulator.scene_to_raster(
            task_simulation.sceneList[-1])).all())
Ejemplo n.º 5
0
 def test_object_vec_to_scene(self):
     steps = 50
     _, _, images, objects = simulator.magic_ponies(
         self._task_object_test,
         self._ball_user_input_small,
         steps=steps,
         stride=1,
         need_images=True,
         need_featurized_objects=True)
     objects = phyre.simulation.Simulation(
         featurized_objects=objects).featurized_objects.features
     for i in range(len(images)):
         image = images[i]
         object_vec = objects[i]
         self.assertTrue(
             np.allclose(
                 object_vec,
                 simulator.scene_to_featurized_objects(
                     phyre.objects_util.featurized_objects_vector_to_scene(
                         object_vec)).features,
                 atol=1e-4))
         self.assertTrue((image == simulator.scene_to_raster(
             phyre.objects_util.featurized_objects_vector_to_scene(
                 object_vec))).all())
Ejemplo n.º 6
0
def get_scene_as_base64_image(scene, resize=None):
    arr = vis.observations_to_uint8_rgb(simulator.scene_to_raster(scene))
    return get_image_as_base64(arr, resize=resize)
Ejemplo n.º 7
0
 def render(self, scene):
     assert self._config['mode'] != DEMO_MODE
     pixels = simulator.scene_to_raster(scene).flatten().tolist()
     return scene_if.Image(width=scene.width,
                           height=scene.height,
                           values=pixels)
Ejemplo n.º 8
0
 def test_render(self):
     array = simulator.scene_to_raster(self._task.scene)
     self.assertEqual(len(array.shape), 2)
     self.assertEqual(array.shape[0], self._task.scene.height)
     self.assertEqual(array.shape[1], self._task.scene.width)
Ejemplo n.º 9
0
# plt.imshow(phyre.vis.observations_to_float_rgb(initial_scene))
# plt.show()

from phyre import simulator as sim
actions = simulator.build_discrete_action_space(max_actions=1000)
action = actions[20]  # Successful action for 3:28
action, is_valid = simulator.get_user_input(
    action)  # Phyre representation of the action

# If you want to brute force every solution until you get a success
# from phyre import SimulationStatus
# for action in actions:
# 	status = simulator.simulate_single(task_index, action, need_images=False)
# 	if status == SimulationStatus.SOLVED:

# Simulates task with the action, returning Thrift representations of intermediate images every frame
# Increase stride to decrease simulation time
result = sim.simulate_task_with_input(simulator.get_task(0), action, stride=1)

# Convert Thrift scene to array of pixels
images = [sim.scene_to_raster(scene) for scene in result.sceneList]
ims = [phyre.vis.observations_to_float_rgb(image) for image in images]

# Displays the simulation
delay = 0.0005  # Delay between frames
img = plt.imshow(ims[0])
for image in ims[1:]:
    img.set_data(image)
    plt.pause(delay)
    plt.draw()