def test_imagegoal_sensor(): config = get_config() if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") config.defrost() config.TASK.SENSORS = ["IMAGEGOAL_SENSOR"] config.SIMULATOR.AGENT_0.SENSORS = ["RGB_SENSOR"] config.freeze() with habitat.Env(config=config, dataset=None) as env: # start position is checked for validity for the specific test scene valid_start_position = [-1.3731, 0.08431, 8.60692] pointgoal = [0.1, 0.2, 0.3] goal_position = np.add(valid_start_position, pointgoal) pointgoal_2 = [0.3, 0.2, 0.1] goal_position_2 = np.add(valid_start_position, pointgoal_2) # starting quaternion is rotated 180 degree along z-axis, which # corresponds to simulator using z-negative as forward action start_rotation = [0, 0, 0, 1] env.episode_iterator = iter( [ NavigationEpisode( episode_id="0", scene_id=config.SIMULATOR.SCENE, start_position=valid_start_position, start_rotation=start_rotation, goals=[NavigationGoal(position=goal_position)], ), NavigationEpisode( episode_id="1", scene_id=config.SIMULATOR.SCENE, start_position=valid_start_position, start_rotation=start_rotation, goals=[NavigationGoal(position=goal_position_2)], ), ] ) obs = env.reset() for _ in range(100): new_obs = env.step(sample_non_stop_action(env.action_space)) # check to see if taking non-stop actions will affect static image_goal assert np.allclose(obs["imagegoal"], new_obs["imagegoal"]) assert np.allclose(obs["rgb"].shape, new_obs["imagegoal"].shape) previous_episode_obs = obs _ = env.reset() for _ in range(10): new_obs = env.step(sample_non_stop_action(env.action_space)) # check to see if taking non-stop actions will affect static image_goal assert not np.allclose( previous_episode_obs["imagegoal"], new_obs["imagegoal"] ) assert np.allclose( previous_episode_obs["rgb"].shape, new_obs["imagegoal"].shape )
def test_pointgoal_with_gps_compass_sensor(): config = get_config() if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") config.defrost() config.TASK.SENSORS = [ "POINTGOAL_WITH_GPS_COMPASS_SENSOR", "COMPASS_SENSOR", "GPS_SENSOR", "POINTGOAL_SENSOR", ] config.TASK.POINTGOAL_WITH_GPS_COMPASS_SENSOR.DIMENSIONALITY = 3 config.TASK.POINTGOAL_WITH_GPS_COMPASS_SENSOR.GOAL_FORMAT = "CARTESIAN" config.TASK.POINTGOAL_SENSOR.DIMENSIONALITY = 3 config.TASK.POINTGOAL_SENSOR.GOAL_FORMAT = "CARTESIAN" config.TASK.GPS_SENSOR.DIMENSIONALITY = 3 config.freeze() with habitat.Env(config=config, dataset=None) as env: # start position is checked for validity for the specific test scene valid_start_position = [-1.3731, 0.08431, 8.60692] expected_pointgoal = [0.1, 0.2, 0.3] goal_position = np.add(valid_start_position, expected_pointgoal) # starting quaternion is rotated 180 degree along z-axis, which # corresponds to simulator using z-negative as forward action start_rotation = [0, 0, 0, 1] env.episode_iterator = iter( [ NavigationEpisode( episode_id="0", scene_id=config.SIMULATOR.SCENE, start_position=valid_start_position, start_rotation=start_rotation, goals=[NavigationGoal(position=goal_position)], ) ] ) env.reset() for _ in range(100): obs = env.step(sample_non_stop_action(env.action_space)) pointgoal = obs["pointgoal"] pointgoal_with_gps_compass = obs["pointgoal_with_gps_compass"] compass = float(obs["compass"][0]) gps = obs["gps"] # check to see if taking non-stop actions will affect static point_goal assert np.allclose( pointgoal_with_gps_compass, quaternion_rotate_vector( quaternion.from_rotation_vector( compass * np.array([0, 1, 0]) ).inverse(), pointgoal - gps, ), atol=1e-5, )
def smoke_test_sensor(config, N_STEPS=100): if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") valid_start_position = [-1.3731, 0.08431, 8.60692] expected_pointgoal = [0.1, 0.2, 0.3] goal_position = np.add(valid_start_position, expected_pointgoal) # starting quaternion is rotated 180 degree along z-axis, which # corresponds to simulator using z-negative as forward action start_rotation = [0, 0, 0, 1] test_episode = NavigationEpisode( episode_id="0", scene_id=config.SIMULATOR.SCENE, start_position=valid_start_position, start_rotation=start_rotation, goals=[NavigationGoal(position=goal_position)], ) with habitat.Env(config=config, dataset=None) as env: env.episode_iterator = iter([test_episode]) no_noise_obs = env.reset() assert no_noise_obs is not None actions = [ sample_non_stop_action(env.action_space) for _ in range(N_STEPS) ] for action in actions: assert env.step(action) is not None
def test_get_observations_at(): config = get_config() if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") config.defrost() config.TASK.SENSORS = [] config.SIMULATOR.AGENT_0.SENSORS = ["RGB_SENSOR", "DEPTH_SENSOR"] config.freeze() with habitat.Env(config=config, dataset=None) as env: # start position is checked for validity for the specific test scene valid_start_position = [-1.3731, 0.08431, 8.60692] expected_pointgoal = [0.1, 0.2, 0.3] goal_position = np.add(valid_start_position, expected_pointgoal) # starting quaternion is rotated 180 degree along z-axis, which # corresponds to simulator using z-negative as forward action start_rotation = [0, 0, 0, 1] env.episode_iterator = iter([ NavigationEpisode( episode_id="0", scene_id=config.SIMULATOR.SCENE, start_position=valid_start_position, start_rotation=start_rotation, goals=[NavigationGoal(position=goal_position)], ) ]) obs = env.reset() start_state = env.sim.get_agent_state() for _ in range(100): # Note, this test will not currently work for camera change actions # (look up/down), only for movement actions. new_obs = env.step(sample_non_stop_action(env.action_space)) for key, val in new_obs.items(): agent_state = env.sim.get_agent_state() if not (np.allclose(agent_state.position, start_state.position) and np.allclose(agent_state.rotation, start_state.rotation)): assert not np.allclose(val, obs[key]) obs_at_point = env.sim.get_observations_at( start_state.position, start_state.rotation, keep_agent_at_new_pose=False, ) for key, val in obs_at_point.items(): assert np.allclose(val, obs[key]) obs_at_point = env.sim.get_observations_at( start_state.position, start_state.rotation, keep_agent_at_new_pose=True, ) for key, val in obs_at_point.items(): assert np.allclose(val, obs[key]) agent_state = env.sim.get_agent_state() assert np.allclose(agent_state.position, start_state.position) assert np.allclose(agent_state.rotation, start_state.rotation)
def test_threaded_vectorized_env(): configs, datasets = _load_test_data() num_envs = len(configs) env_fn_args = tuple(zip(configs, datasets, range(num_envs))) with habitat.ThreadedVectorEnv(env_fn_args=env_fn_args) as envs: envs.reset() for _ in range(2 * configs[0].ENVIRONMENT.MAX_EPISODE_STEPS): observations = envs.step( sample_non_stop_action(envs.action_spaces[0], num_envs) ) assert len(observations) == num_envs
def test_rl_env(gpu2gpu): import habitat_sim if gpu2gpu and not habitat_sim.cuda_enabled: pytest.skip("GPU-GPU requires CUDA") config = get_config(CFG_TEST) if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") config.defrost() config.SIMULATOR.HABITAT_SIM_V0.GPU_GPU = gpu2gpu config.freeze() env = DummyRLEnv(config=config, dataset=None) env.episodes = [ NavigationEpisode( episode_id="0", scene_id=config.SIMULATOR.SCENE, start_position=[-3.0133917, 0.04623024, 7.3064547], start_rotation=[0, 0.163276, 0, 0.98658], goals=[ NavigationGoal(position=[-3.0133917, 0.04623024, 7.3064547]) ], info={"geodesic_distance": 0.001}, ) ] done = False env.reset() for _ in range(config.ENVIRONMENT.MAX_EPISODE_STEPS): observation, reward, done, info = env.step( action=sample_non_stop_action(env.action_space) ) # check for steps limit on environment assert done is True, "episodes should be over after max_episode_steps" env.reset() observation, reward, done, info = env.step( action={"action": StopAction.name} ) assert done is True, "done should be true after STOP action" env.close()
def test_task_actions_sampling_for_teleport(): config = habitat.get_config(config_paths=CFG_TEST) config.defrost() config.TASK.POSSIBLE_ACTIONS = config.TASK.POSSIBLE_ACTIONS + ["TELEPORT"] config.freeze() env = habitat.Env(config=config) env.reset() while not env.episode_over: action = sample_non_stop_action(env.action_space) habitat.logger.info(f"Action : " f"{action['action']}, " f"args: {action['action_args']}.") env.step(action) agent_state = env.sim.get_agent_state() habitat.logger.info(agent_state) env.close()
def test_rl_vectorized_envs(gpu2gpu): import habitat_sim if gpu2gpu and not habitat_sim.cuda_enabled: pytest.skip("GPU-GPU requires CUDA") configs, datasets = _load_test_data() for config in configs: config.defrost() config.SIMULATOR.HABITAT_SIM_V0.GPU_GPU = gpu2gpu config.freeze() num_envs = len(configs) env_fn_args = tuple(zip(configs, datasets, range(num_envs))) with habitat.VectorEnv( make_env_fn=make_rl_env, env_fn_args=env_fn_args ) as envs: envs.reset() for i in range(2 * configs[0].ENVIRONMENT.MAX_EPISODE_STEPS): outputs = envs.step( sample_non_stop_action(envs.action_spaces[0], num_envs) ) observations, rewards, dones, infos = [ list(x) for x in zip(*outputs) ] assert len(observations) == num_envs assert len(rewards) == num_envs assert len(dones) == num_envs assert len(infos) == num_envs tiled_img = envs.render(mode="rgb_array") new_height = int(np.ceil(np.sqrt(NUM_ENVS))) new_width = int(np.ceil(float(NUM_ENVS) / new_height)) print(f"observations: {observations}") h, w, c = observations[0]["rgb"].shape assert tiled_img.shape == ( h * new_height, w * new_width, c, ), "vector env render is broken" if (i + 1) % configs[0].ENVIRONMENT.MAX_EPISODE_STEPS == 0: assert all( dones ), "dones should be true after max_episode steps"
def test_eqa_task(): eqa_config = get_config(CFG_TEST) if not mp3d_dataset.Matterport3dDatasetV1.check_config_paths_exist( eqa_config.DATASET ): pytest.skip("Please download Matterport3D EQA dataset to data folder.") dataset = make_dataset( id_dataset=eqa_config.DATASET.TYPE, config=eqa_config.DATASET ) with habitat.Env(config=eqa_config, dataset=dataset) as env: env.episodes = list( filter( lambda e: int(e.episode_id) in TEST_EPISODE_SET[:EPISODES_LIMIT], dataset.episodes, ) ) env.reset() for i in range(10): action = sample_non_stop_action(env.action_space) if action["action"] != AnswerAction.name: env.step(action) metrics = env.get_metrics() del metrics["episode_info"] logger.info(metrics) correct_answer_id = env.current_episode.question.answer_token env.step( { "action": AnswerAction.name, "action_args": {"answer_id": correct_answer_id}, } ) metrics = env.get_metrics() del metrics["episode_info"] logger.info(metrics) assert metrics["answer_accuracy"] == 1 with pytest.raises(AssertionError): env.step({"action": MoveForwardAction.name})
def test_task_actions_sampling(config_file): config = habitat.get_config(config_paths=config_file) if not os.path.exists( config.DATASET.DATA_PATH.format(split=config.DATASET.SPLIT)): pytest.skip(f"Please download dataset to data folder " f"{config.DATASET.DATA_PATH}.") env = habitat.Env(config=config) env.reset() while not env.episode_over: action = sample_non_stop_action(env.action_space) habitat.logger.info(f"Action : " f"{action['action']}, " f"args: {action['action_args']}.") env.step(action) agent_state = env.sim.get_agent_state() habitat.logger.info(agent_state) env.close()
def _vec_env_test_fn(configs, datasets, multiprocessing_start_method, gpu2gpu): num_envs = len(configs) for cfg in configs: cfg.defrost() cfg.SIMULATOR.HABITAT_SIM_V0.GPU_GPU = gpu2gpu cfg.freeze() env_fn_args = tuple(zip(configs, datasets, range(num_envs))) envs = habitat.VectorEnv( env_fn_args=env_fn_args, multiprocessing_start_method=multiprocessing_start_method, ) envs.reset() for _ in range(2 * configs[0].ENVIRONMENT.MAX_EPISODE_STEPS): observations = envs.step( sample_non_stop_action(envs.action_spaces[0], num_envs)) assert len(observations) == num_envs
def test_collisions(): config = get_config() if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") config.defrost() config.TASK.MEASUREMENTS = ["COLLISIONS"] config.freeze() env = habitat.Env(config=config, dataset=None) env.reset() for _ in range(20): _random_episode(env, config) env.reset() assert env.get_metrics()["collisions"] is None prev_collisions = 0 prev_loc = env.sim.get_agent_state().position for _ in range(50): action = sample_non_stop_action(env.action_space) env.step(action) collisions = env.get_metrics()["collisions"]["count"] loc = env.sim.get_agent_state().position if (np.linalg.norm(loc - prev_loc) < 0.9 * config.SIMULATOR.FORWARD_STEP_SIZE and action["action"] == MoveForwardAction.name): # Check to see if the new method of doing collisions catches # all the same collisions as the old method assert collisions == prev_collisions + 1 # We can _never_ collide with standard turn actions if action["action"] != MoveForwardAction.name: assert collisions == prev_collisions prev_loc = loc prev_collisions = collisions env.close()
def test_noise_models_rgbd(): DEMO_MODE = False N_STEPS = 100 config = get_config() config.defrost() config.SIMULATOR.SCENE = ( "data/scene_datasets/habitat-test-scenes/skokloster-castle.glb") config.SIMULATOR.AGENT_0.SENSORS = ["RGB_SENSOR", "DEPTH_SENSOR"] config.freeze() if not os.path.exists(config.SIMULATOR.SCENE): pytest.skip("Please download Habitat test data to data folder.") valid_start_position = [-1.3731, 0.08431, 8.60692] expected_pointgoal = [0.1, 0.2, 0.3] goal_position = np.add(valid_start_position, expected_pointgoal) # starting quaternion is rotated 180 degree along z-axis, which # corresponds to simulator using z-negative as forward action start_rotation = [0, 0, 0, 1] test_episode = NavigationEpisode( episode_id="0", scene_id=config.SIMULATOR.SCENE, start_position=valid_start_position, start_rotation=start_rotation, goals=[NavigationGoal(position=goal_position)], ) print(f"{test_episode}") with habitat.Env(config=config, dataset=None) as env: env.episode_iterator = iter([test_episode]) no_noise_obs = [env.reset()] no_noise_states = [env.sim.get_agent_state()] actions = [ sample_non_stop_action(env.action_space) for _ in range(N_STEPS) ] for action in actions: no_noise_obs.append(env.step(action)) no_noise_states.append(env.sim.get_agent_state()) env.close() config.defrost() config.SIMULATOR.RGB_SENSOR.NOISE_MODEL = "GaussianNoiseModel" config.SIMULATOR.RGB_SENSOR.NOISE_MODEL_KWARGS = habitat.Config() config.SIMULATOR.RGB_SENSOR.NOISE_MODEL_KWARGS.INTENSITY_CONSTANT = 0.5 config.SIMULATOR.DEPTH_SENSOR.NOISE_MODEL = "RedwoodDepthNoiseModel" config.SIMULATOR.ACTION_SPACE_CONFIG = "pyrobotnoisy" config.SIMULATOR.NOISE_MODEL = habitat.Config() config.SIMULATOR.NOISE_MODEL.ROBOT = "LoCoBot" config.SIMULATOR.NOISE_MODEL.CONTROLLER = "Proportional" config.SIMULATOR.NOISE_MODEL.NOISE_MULTIPLIER = 0.5 config.freeze() env = habitat.Env(config=config, dataset=None) env.episode_iterator = iter([test_episode]) obs = env.reset() assert np.linalg.norm( obs["rgb"].astype(np.float) - no_noise_obs[0]["rgb"].astype(np.float)) > 1.5e-2 * np.linalg.norm( no_noise_obs[0]["rgb"].astype( np.float)), "No RGB noise detected." assert np.linalg.norm(obs["depth"].astype(np.float) - no_noise_obs[0]["depth"].astype(np.float) ) > 1.5e-2 * np.linalg.norm( no_noise_obs[0]["depth"].astype( np.float)), "No Depth noise detected." images = [] state = env.sim.get_agent_state() angle_diffs = [] pos_diffs = [] for action in actions: prev_state = state obs = env.step(action) state = env.sim.get_agent_state() position_change = np.linalg.norm(np.array(state.position) - np.array(prev_state.position), ord=2) if action["action"][:5] == "TURN_": angle_diff = abs( angle_between_quaternions(state.rotation, prev_state.rotation) - np.deg2rad(config.SIMULATOR.TURN_ANGLE)) angle_diffs.append(angle_diff) else: pos_diffs.append( abs(position_change - config.SIMULATOR.FORWARD_STEP_SIZE)) if DEMO_MODE: images.append(observations_to_image(obs, {})) if DEMO_MODE: images_to_video(images, "data/video/test_noise", "test_noise") assert (np.mean(angle_diffs) > 0.025), "No turn action actuation noise detected." assert (np.mean(pos_diffs) > 0.025), "No forward action actuation noise detected."