示例#1
0
def construct_envs(config: Config, env_class: Type[Union[Env,
                                                         RLEnv]]) -> VectorEnv:
    r"""Create VectorEnv object with specified config and env class type.
    To allow better performance, dataset are split into small ones for
    each individual env, grouped by scenes.

    Args:
        config: configs that contain num_processes as well as information
        necessary to create individual environments.
        env_class: class type of the envs to be created.

    Returns:
        VectorEnv object created according to specification.
    """

    num_processes = config.NUM_PROCESSES
    configs = []
    env_classes = [env_class for _ in range(num_processes)]
    dataset = make_dataset(config.TASK_CONFIG.DATASET.TYPE)
    scenes = dataset.get_scenes_to_load(config.TASK_CONFIG.DATASET)

    if len(scenes) > 0:
        random.shuffle(scenes)

        assert len(scenes) >= num_processes, (
            "reduce the number of processes as there "
            "aren't enough number of scenes")

    scene_splits = [[] for _ in range(num_processes)]
    for idx, scene in enumerate(scenes):
        scene_splits[idx % len(scene_splits)].append(scene)

    assert sum(map(len, scene_splits)) == len(scenes)

    for i in range(num_processes):

        task_config = config.TASK_CONFIG.clone()
        task_config.defrost()
        if len(scenes) > 0:
            task_config.DATASET.CONTENT_SCENES = scene_splits[i]

        task_config.SIMULATOR.HABITAT_SIM_V0.GPU_DEVICE_ID = (
            config.SIMULATOR_GPU_ID)

        task_config.SIMULATOR.AGENT_0.SENSORS = config.SENSORS
        task_config.freeze()

        config.defrost()
        config.TASK_CONFIG = task_config
        config.freeze()
        configs.append(config.clone())

    envs = habitat.VectorEnv(
        make_env_fn=make_env_fn,
        env_fn_args=tuple(
            tuple(zip(configs, env_classes, range(num_processes)))),
    )
    return envs
示例#2
0
    def __init__(self, task_config: Optional[Config] = None) -> None:
        r"""..

        :param task_config: config to be used for creating the environment
        """
        dummy_config = Config()
        dummy_config.RL = Config()
        dummy_config.RL.SLACK_REWARD = -0.01
        dummy_config.RL.SUCCESS_REWARD = 10
        dummy_config.RL.WITH_TIME_PENALTY = True
        dummy_config.RL.DISTANCE_REWARD_SCALE = 1
        dummy_config.RL.WITH_DISTANCE_REWARD = True
        dummy_config.RL.defrost()
        dummy_config.TASK_CONFIG = task_config
        dummy_config.freeze()

        dataset = make_dataset(id_dataset=task_config.DATASET.TYPE,
                               config=task_config.DATASET)
        self._env = NavRLEnv(config=dummy_config, dataset=dataset)
示例#3
0
def construct_envs(config: Config, env_class: Type[Union[Env,
                                                         RLEnv]]) -> VectorEnv:
    r"""Create VectorEnv object with specified config and env class type.
    To allow better performance, dataset are split into small ones for
    each individual env, grouped by scenes.

    Args:
        config: configs that contain num_processes as well as information
        necessary to create individual environments.
        env_class: class type of the envs to be created
    Returns:
        VectorEnv object created according to specification.
    """

    num_processes = config.NUM_PROCESSES
    configs = []
    env_classes = [env_class for _ in range(num_processes)]
    dataset = make_dataset(config.TASK_CONFIG.DATASET.TYPE)
    scenes = dataset.get_scenes_to_load(config.TASK_CONFIG.DATASET)

    # rearrange scenes in the order of scene size since there is a severe imbalance of data size
    if "replica" in config.TASK_CONFIG.DATASET.SCENES_DIR:
        scenes_new = list()
        for scene in SCENES:
            if scene in scenes:
                scenes_new.append(scene)
        scenes = scenes_new

    if len(scenes) > 0:
        # random.shuffle(scenes)
        assert len(scenes) >= num_processes, (
            "reduce the number of processes as there "
            "aren't enough number of scenes")

    scene_splits = [[] for _ in range(num_processes)]
    for idx, scene in enumerate(scenes):
        scene_splits[idx % len(scene_splits)].append(scene)

    assert sum(map(len, scene_splits)) == len(scenes)

    for i in range(num_processes):
        task_config = config.TASK_CONFIG.clone()
        task_config.defrost()
        if len(scenes) > 0:
            task_config.DATASET.CONTENT_SCENES = scene_splits[i]
            logging.debug('All scenes: {}'.format(','.join(scene_splits[i])))

        # overwrite the task config with top-level config file
        task_config.SIMULATOR.HABITAT_SIM_V0.GPU_DEVICE_ID = (
            config.SIMULATOR_GPU_ID)
        task_config.SIMULATOR.AGENT_0.SENSORS = config.SENSORS
        task_config.freeze()

        config.defrost()
        config.TASK_CONFIG = task_config
        config.freeze()
        configs.append(config.clone())

    # use VectorEnv for the best performance and ThreadedVectorEnv for debugging
    if config.USE_SYNC_VECENV:
        env_launcher = SyncVectorEnv
        logging.info('Using SyncVectorEnv')
    elif config.USE_VECENV:
        env_launcher = habitat.VectorEnv
        logging.info('Using VectorEnv')
    else:
        env_launcher = habitat.ThreadedVectorEnv
        logging.info('Using ThreadedVectorEnv')
    envs = env_launcher(
        make_env_fn=make_env_fn,
        env_fn_args=tuple(
            tuple(zip(configs, env_classes, range(num_processes)))),
    )
    return envs