def determinism_test(all_tests): # Redo the actions 20 times: # only do this if an object is picked up for k, test_point in all_tests.items(): initial_location = test_point['initial_location'] initial_rotation = test_point['initial_rotation'] all_commands = test_point['all_commands'] final_state = test_point['final_state'] initial_pose = test_point['initial_pose'] scene_name = test_point['scene_name'] controller.reset(scene_name) event1 = controller.step(action='TeleportFull', x=initial_location['x'], y=initial_location['y'], z=initial_location['z'], rotation=dict(x=0, y=initial_rotation, z=0), horizon=10) controller.step('PausePhysicsAutoSim') for cmd in all_commands: execute_command(controller, cmd, ADITIONAL_ARM_ARGS) last_event_success = controller.last_event.metadata[ 'lastActionSuccess'] current_state = get_current_full_state(controller) if not two_dict_equal(final_state, current_state): print('not deterministic') print('scene name', controller.last_event.metadata['sceneName']) print('initial pose', initial_pose) print('list of actions', all_commands) pdb.set_trace() else: print('test {} passed'.format(k))
def test_fast_emit_disabled(controller): event = controller.step(dict(action='RotateRight')) event_fast_emit = controller.step(dict(action='TestFastEmit', rvalue='foo')) # assert that when actionFastEmit is off that the objects are different assert id(event.metadata['objects']) != id( event_fast_emit.metadata['objects'])
def test_update_third_party_camera(controller): expectedPosition = dict(x=2.2, y=3.3, z=4.4) expectedRotation = dict(x=10, y=20, z=30) expectedInitialFieldOfView = 45.0 expectedFieldOfView2 = 55.0 expectedFieldOfViewDefault = 90.0 assert len(controller.last_event.metadata[ MultiAgentMetadata.thirdPartyCameras]) == 1, 'there should be 1 camera' e = controller.step( dict(action=Actions.UpdateThirdPartyCamera, thirdPartyCameraId=0, position=expectedPosition, rotation=expectedRotation)) camera = e.metadata[MultiAgentMetadata.thirdPartyCameras][0] assert_near(camera[ThirdPartyCameraMetadata.position], expectedPosition, 'position should have been updated') assert_near(camera[ThirdPartyCameraMetadata.rotation], expectedRotation, 'rotation should have been updated') assert camera[ ThirdPartyCameraMetadata. fieldOfView] == expectedInitialFieldOfView, 'fieldOfView should not have changed' # 0 is a special case, since nullable float does not get encoded properly, we need to pass 0 as null e = controller.step( dict(action=Actions.UpdateThirdPartyCamera, thirdPartyCameraId=0, fieldOfView=0)) camera = e.metadata[MultiAgentMetadata.thirdPartyCameras][0] assert camera[ ThirdPartyCameraMetadata. fieldOfView] == expectedInitialFieldOfView, 'fieldOfView should have been updated' e = controller.step( dict(action=Actions.UpdateThirdPartyCamera, thirdPartyCameraId=0, fieldOfView=expectedFieldOfView2)) camera = e.metadata[MultiAgentMetadata.thirdPartyCameras][0] assert camera[ ThirdPartyCameraMetadata. fieldOfView] == expectedFieldOfView2, 'fieldOfView should have been updated' e = controller.step( dict(action=Actions.UpdateThirdPartyCamera, thirdPartyCameraId=0, fieldOfView=-1)) camera = e.metadata[MultiAgentMetadata.thirdPartyCameras][0] assert camera[ ThirdPartyCameraMetadata. fieldOfView] == expectedFieldOfViewDefault, 'fieldOfView should have been updated to default' e = controller.step( dict(action=Actions.UpdateThirdPartyCamera, thirdPartyCameraId=0, fieldOfView=181)) camera = e.metadata[MultiAgentMetadata.thirdPartyCameras][0] assert camera[ ThirdPartyCameraMetadata. fieldOfView] == expectedFieldOfViewDefault, 'fieldOfView should have been updated to default'
def test_simple_example(): """ Taken from here: http://ai2thor.allenai.org/tutorials/examples """ controller = ai2thor.controller.Controller() controller.start() # Kitchens: FloorPlan1 - FloorPlan30 # Living rooms: FloorPlan201 - FloorPlan230 # Bedrooms: FloorPlan301 - FloorPlan330 # Bathrooms: FloorPLan401 - FloorPlan430 controller.reset('FloorPlan28') controller.step(dict(action='Initialize', gridSize=0.25)) event = controller.step(dict(action='MoveAhead')) # Numpy Array - shape (width, height, channels), channels are in RGB order event.frame # Numpy Array in BGR order suitable for use with OpenCV event.cv2img # current metadata dictionary that includes the state of the scene event.metadata
def dump_scene_controller(base_dir, controller): if controller.last_event is None: raise Exception("Controller must be reset and intialized to a scene") scene_name = controller.last_event.metadata['sceneName'] fc = FrameCounter() shutil.rmtree("%s/%s" % (base_dir, scene_name), ignore_errors=True) event = controller.step(action='GetReachablePositions') for p in event.metadata['reachablePositions']: action = copy.deepcopy(p) action['action'] = 'TeleportFull' action['horizon'] = 0.0 action['forceAction'] = True action['rotation'] = dict(y=0.0) event = controller.step(action) print(fc.counter) if event.metadata['lastActionSuccess']: look_up_down_write(controller, base_dir, fc, scene_name) for i in range(3): event = controller.step(action='RotateRight') look_up_down_write(controller, base_dir, fc, scene_name) index_metadata(base_dir, scene_name)
def determinism_test(all_tests): # Redo the actions 20 times: # only do this if an object is picked up for k, test_point in all_tests.items(): initial_location = test_point["initial_location"] initial_rotation = test_point["initial_rotation"] all_commands = test_point["all_commands"] final_state = test_point["final_state"] initial_pose = test_point["initial_pose"] scene_name = test_point["scene_name"] controller.reset(scene_name) controller.step( action="TeleportFull", x=initial_location["x"], y=initial_location["y"], z=initial_location["z"], rotation=dict(x=0, y=initial_rotation, z=0), horizon=10, ) controller.step("PausePhysicsAutoSim") for cmd in all_commands: execute_command(controller, cmd, ADITIONAL_ARM_ARGS) current_state = get_current_full_state(controller) if not two_dict_equal(final_state, current_state): print("not deterministic") print("scene name", controller.last_event.metadata["sceneName"]) print("initial pose", initial_pose) print("list of actions", all_commands) pdb.set_trace() else: print("test {} passed".format(k))
def standard_pose(): controller.step(action='TeleportFull', x=-1, y=0.9009995460510254, z=1, rotation=dict(x=0, y=180, z=0), horizon=10) controller.step('PausePhysicsAutoSim') pose = { "x": -1.0, "y": 0.9009995460510254, "z": 1.0, "rotation": 0, "horizon": 10 } controller.step(action='TeleportFull', x=pose['x'], y=pose['y'], z=pose['z'], rotation=dict(x=0.0, y=pose['rotation'], z=0.0), horizon=pose['horizon']) controller.step(action='MoveMidLevelArm', disableRendering=False, position=dict(x=0.00, y=0, z=0.35), speed=2, returnToStart=False, handCameraSpace=False) controller.step(action='MoveMidLevelArmHeight', disableRendering=False, y=0.8, speed=2, returnToStart=False)
def handle_open(): global event global current_location objects_map = {} for o in event.metadata['objects']: if o['visible'] and o['openable']: objects_map[o['objectId']] = o if not objects_map: return objectId = ask_questions("Which Object you want to open/close?", objects_map) open_status = objects_map[objectId]['isopen'] if open_status: event = controller.step(dict(action='CloseObject', objectId=objectId)) if event.metadata["lastActionSuccess"] == False: print("Close Failed: " + event.metadata["errorMessage"]) else: writefile("(Close {0} {1})".format(current_location, objects_map[objectId]['name'])) else: event = controller.step(dict(action='OpenObject', objectId=objectId)) if event.metadata["lastActionSuccess"] == False: print("Open Failed: " + event.metadata["errorMessage"]) else: writefile("(Open {0} {1})".format(current_location, objects_map[objectId]['name']))
def handle_toggle(): global event objects_map = get_toggle_objects_map(event) if not objects_map: return objectId = ask_questions("Which Object you want to On/Off?", objects_map) toggle_status = objects_map[objectId]['istoggled'] if toggle_status: event = controller.step( dict(action='ToggleObjectOff', objectId=objectId)) if event.metadata["lastActionSuccess"] == False: print("Turn Off Failed: " + event.metadata["errorMessage"]) else: writefile("(TurnOff {0})".format(objects_map[objectId]['name'])) else: event = controller.step( dict(action='ToggleObjectOn', objectId=objectId)) if event.metadata["lastActionSuccess"] == False: print("Toggle On Failed: " + event.metadata["errorMessage"]) else: writefile("(TurnOn {0})".format(objects_map[objectId]['name']))
def test_moveahead_mag(): controller.step(dict(action='Teleport', x=-1.5, z=-1.5, y=1.0), raise_for_failure=True) controller.step(dict(action='MoveAhead', moveMagnitude=0.5), raise_for_failure=True) position = controller.last_event.metadata['agent']['position'] assert position == dict(x=-1.0, z=-1.5, y=0.9799989461898804)
def test_moveahead_mag(controller): controller.step(dict(action='Teleport', x=-1.5, z=-1.5, y=1.1), raise_for_failure=True) controller.step(dict(action='MoveAhead', moveMagnitude=0.5), raise_for_failure=True) position = controller.last_event.metadata['agent']['position'] assert_near(position, dict(x=-1.0, z=-1.5, y=0.9009983))
def test_change_resolution(): event = controller.step(dict(action='Pass'), raise_for_failure=True) assert event.frame.shape == (300,300,3) event = controller.step(dict(action='ChangeResolution', x=400, y=400), raise_for_failure=True) assert event.frame.shape == (400,400,3) assert event.screen_width == 400 assert event.screen_height == 400 event = controller.step(dict(action='ChangeResolution', x=300, y=300), raise_for_failure=True)
def look_up_down_write(controller, base_dir, fc, scene_name): fc.inc() write_frame(controller.step(action='LookUp'), base_dir, scene_name, fc.counter) fc.inc() write_frame(controller.step(action='LookDown'), base_dir, scene_name, fc.counter) fc.inc() write_frame(controller.step(action='LookDown'), base_dir, scene_name, fc.counter) controller.step(action='LookUp')
def test_teleport(): controller.step(dict(action='Teleport', x=-1.5, z=-1.5, y=1.0), raise_for_failure=True) position = controller.last_event.metadata['agent']['position'] assert_near(position, dict(x=-1.5, z=-1.5, y=0.901)) controller.step(dict(action='Teleport', x=-2.0, z=-2.5, y=1.0), raise_for_failure=True) position = controller.last_event.metadata['agent']['position'] assert_near(position, dict(x=-2.0, z=-2.5, y=0.901))
def test_teleport(): controller.step(dict(action='Teleport', x=-1.5, z=-1.5, y=1.0), raise_for_failure=True) position = controller.last_event.metadata['agent']['position'] assert position == dict(x=-1.5, z=-1.5, y=0.9799992442131042) controller.step(dict(action='Teleport', x=-2.0, z=-2.5, y=1.0), raise_for_failure=True) position = controller.last_event.metadata['agent']['position'] assert position == dict(x=-2.0, z=-2.5, y=0.9799990057945251)
def include_object_data(controller: ai2thor.controller.Controller): needs_reset = len(controller.last_event.metadata["objects"]) == 0 try: if needs_reset: controller.step("ResetObjectFilter") assert controller.last_event.metadata["lastActionSuccess"] yield None finally: if needs_reset: controller.step("SetObjectFilter", objectIds=[]) assert controller.last_event.metadata["lastActionSuccess"]
def test_lookup(): e = controller.step(dict(action='RotateLook', rotation=0, horizon=0)) position = controller.last_event.metadata['agent']['position'] horizon = controller.last_event.metadata['agent']['cameraHorizon'] assert horizon == 0.0 e = controller.step(dict(action='LookUp')) assert e.metadata['agent']['position'] == position assert e.metadata['agent']['cameraHorizon'] == -30.0 assert e.metadata['agent']['rotation'] == dict(x=0, y=0, z=0) e = controller.step(dict(action='LookUp')) assert e.metadata['agent']['cameraHorizon'] == -30.0
def test_rotate_look(): e = controller.step(dict(action='RotateLook', rotation=0, horizon=0)) position = controller.last_event.metadata['agent']['position'] rotation = controller.last_event.metadata['agent']['rotation'] assert rotation == dict(x=0, y=0, z=0) e = controller.step(dict(action='RotateLook', rotation=90, horizon=31)) assert e.metadata['agent']['position'] == position assert int(e.metadata['agent']['cameraHorizon']) == 31 assert e.metadata['agent']['rotation']['y'] == 90.0 assert e.metadata['agent']['rotation']['x'] == 0.0 assert e.metadata['agent']['rotation']['z'] == 0.0
def test_add_third_party_camera(): assert len(controller.last_event.metadata['thirdPartyCameras']) == 0 e = controller.step(dict(action='AddThirdPartyCamera', position=dict(x=1.2, y=2.3, z=3.4), rotation=dict(x=30, y=40,z=50))) assert len(e.metadata['thirdPartyCameras']) == 1 assert_near(e.metadata['thirdPartyCameras'][0]['position'], dict(x=1.2, y=2.3, z=3.4)) assert_near(e.metadata['thirdPartyCameras'][0]['rotation'], dict(x=30, y=40, z=50)) assert len(e.third_party_camera_frames) == 1 assert e.third_party_camera_frames[0].shape == (300,300,3) e = controller.step(dict(action='UpdateThirdPartyCamera', thirdPartyCameraId=0, position=dict(x=2.2, y=3.3, z=4.4), rotation=dict(x=10, y=20,z=30))) assert_near(e.metadata['thirdPartyCameras'][0]['position'], dict(x=2.2, y=3.3, z=4.4)) assert_near(e.metadata['thirdPartyCameras'][0]['rotation'], dict(x=10, y=20, z=30))
def test_rotate_right(): e = controller.step(dict(action='RotateLook', rotation=0, horizon=0)) position = controller.last_event.metadata['agent']['position'] rotation = controller.last_event.metadata['agent']['rotation'] assert rotation == dict(x=0, y=0, z=0) horizon = controller.last_event.metadata['agent']['cameraHorizon'] e = controller.step(dict(action='RotateRight')) assert e.metadata['agent']['position'] == position assert e.metadata['agent']['cameraHorizon'] == horizon assert e.metadata['agent']['rotation']['y'] == 90.0 assert e.metadata['agent']['rotation']['x'] == 0.0 assert e.metadata['agent']['rotation']['z'] == 0.0
def test_lookdown(controller): e = controller.step(dict(action='RotateLook', rotation=0, horizon=0)) position = controller.last_event.metadata['agent']['position'] horizon = controller.last_event.metadata['agent']['cameraHorizon'] assert horizon == 0.0 e = controller.step(dict(action='LookDown')) assert e.metadata['agent']['position'] == position assert round(e.metadata['agent']['cameraHorizon']) == 30 assert e.metadata['agent']['rotation'] == dict(x=0, y=0, z=0) e = controller.step(dict(action='LookDown')) assert round(e.metadata['agent']['cameraHorizon']) == 60 e = controller.step(dict(action='LookDown')) assert round(e.metadata['agent']['cameraHorizon']) == 60
def arrange(controller, layout): try: params = layout_dict[layout] except KeyError: print("Layout not found") sys.exit(1) for move in params[0]: event = controller.step( dict(action='TeleportObject', objectId=objectId_dict[move[0]], x=move[1], y=move[2], z=move[3])) if params[1]: controller.step(dict(action='LookUp')) event = controller.step(dict(action='LookDown')) return event
def dump_scene(scene_name, base_dir, renderObjectImage=False, renderDepthImage=False, renderClassImage=False): controller = ai2thor.controller.Controller() controller.start(player_screen_height=448, player_screen_width=448) controller.reset(scene_name) event = controller.step(dict(action='Initialize', fieldOfView=90, gridSize=0.25, renderDepthImage=renderDepthImage, renderObjectImage=renderObjectImage, renderClassImage=renderClassImage)) dump_scene_controller(base_dir, controller) controller.stop()
def main(): wait = True offscreen_z = -3 # set z to here to teleport object off-screen # should work for objects we're using... if object still visible, decrease number controller = ai2thor.controller.Controller(quality='High') controller.start(player_screen_height=player_screen_height, player_screen_width=player_screen_width) controller.reset('FloorPlan1') controller.step(dict(action='Initialize', gridsize=0.25)) # move unnecessary objects offscreen # controller.step(dict(action='TeleportObject', objectId=objectId_dict['apple'], z=offscreen_z)) # controller.step(dict(action='TeleportObject', objectId=objectId_dict['bread'], z=offscreen_z)) # controller.step(dict(action='TeleportObject', objectId=objectId_dict['butter_knife'], z=offscreen_z)) # controller.step(dict(action='TeleportObject', objectId=objectId_dict['butter_knife'], z=offscreen_z)) # controller.step(dict(action='TeleportFull', x=0, y=1, z=-1.75, rotation=180, horizon=0)) if wait: input("press enter to close...")
def pickUp(event): for o in event.metadata['objects']: if o['visible'] and o['pickupable']: event = controller.step(dict(action='PickupObject', objectId=o['objectId']), raise_for_failure=True) object_id = o['objectId'] break
def test_action_dispatch_invalid_action(controller): caught_exception = False try: event = controller.step(dict(action='TestActionDispatchNoopFoo')) except ValueError as e: caught_exception = True assert caught_exception assert controller.last_event.metadata['errorCode'] == 'InvalidAction'
def run(file_name=None): # file_name = file_path.split('/')[-1].split('.')[0] controller = ai2thor.controller.Controller() controller.start() controller.reset("FloorPlan203") y_coord = 1.25 event = controller.step( dict(action='Initialize', gridSize=0.5, cameraY=y_coord, visibilityDistance=1.0)) all_visible_objects = list( np.unique([obj['objectType'] for obj in event.metadata['objects']])) rotation = 0.0 while True: # making a loop try: # used try so that if user pressed other than the given key error will not be shown key = click.getchar() if key == 'a': # Rotate Left rotation -= 22.5 if rotation < 0: rotation = rotation + 360 event = controller.step( dict(action='Rotate', rotation=rotation)) elif key == 'd': rotation += 22.5 if rotation > 360: rotation = rotation - 360 event = controller.step( dict(action='Rotate', rotation=rotation)) elif key == 'w': event = controller.step(dict(action='MoveAhead')) elif key == 's': event = controller.step(dict(action='MoveBack')) elif key == 'z': event = controller.step(dict(action='LookDown')) elif key == 'x': event = controller.step(dict(action='LookUp')) elif key == 'q': controller.stop() break elif key == 'r': scene = input("Scene id: ") controller.reset('FloorPlan{}'.format(scene)) event = controller.step( dict(action='Initialize', gridSize=0.5, cameraY=y_coord)) else: print("Key not supported! Try a, d, w, s, q, r.") print((event.metadata['agent']['position']['x'], event.metadata['agent']['position']['z'], event.metadata['agent']['rotation'])) # print([(obj['objectType'], obj['distance']) for obj in event.metadata['objects'] if obj['visible']]) except: print("Key not supported! Try a, d, w, s, q, r.")
def test_rectangle_aspect(): controller = ai2thor.controller.Controller() controller.releases_dir = releases_dir.__get__( controller, ai2thor.controller.Controller) print("trying to start unity") controller.start(player_screen_width=600, player_screen_height=300) print("started") controller.reset('FloorPlan28') event = controller.step(dict(action='Initialize', gridSize=0.25)) assert event.frame.shape == (300, 600, 3)
def test_simobj_filter(controller): objects = controller.last_event.metadata['objects'] unfiltered_object_ids = sorted([o['objectId'] for o in objects]) filter_object_ids = sorted([o['objectId'] for o in objects[0:3]]) e = controller.step( dict(action='SetObjectFilter', objectIds=filter_object_ids)) assert len(e.metadata['objects']) == len(filter_object_ids) filtered_object_ids = sorted( [o['objectId'] for o in e.metadata['objects']]) assert filtered_object_ids == filter_object_ids e = controller.step(dict(action='SetObjectFilter', objectIds=[])) assert len(e.metadata['objects']) == 0 e = controller.step(dict(action='ResetObjectFilter')) reset_filtered_object_ids = sorted( [o['objectId'] for o in e.metadata['objects']]) assert unfiltered_object_ids == reset_filtered_object_ids
def test_action_dispatch_missing_args(controller): caught_exception = False try: event = controller.step( dict(action='TestActionDispatchNoop', param6='foo')) print(event.metadata['actionReturn']) except ValueError as e: caught_exception = True assert caught_exception assert controller.last_event.metadata['errorCode'] == 'MissingArguments'