(Habitat)基本功能代码实现

Episode

在这里插入图片描述

  • 以一个导航任务为例 一个episode包含场景信息 agent起始位置和角度 目标信息等信息

完成一个episode的循环步骤


1 env = habitat.Env(config=habitat.get_config("configs/tasks/pointnav.yaml"))  #初始化环境
2 observations = env.reset()      #获取环境的观察对象并重置环境
3 #create an agent
4 while not env.episode_over: #有无达到最大步骤数
    action = agent.act()              # plan规划路径
    observations = env.step(action)   #通过action更新环境的观察对象 通过measure更新action 
    
5 if (action == HabitatSimActions.STOP						#如果收到停止动作或者到达目标点
    and observations["pointgoal_with_gps_compass"][0] < 0.2
    ):

Config.yaml

  • 配置文件的default都写在habitat/config/default.py

  • 以pointnav.yaml为例

ENVIRONMENT:
  MAX_EPISODE_STEPS: 500             #设置最大的可执行的episode数 一般一个action执行后算一个step
SIMULATOR:							 #配置simulator
  AGENT_0:
    SENSORS: ['RGB_SENSOR', 'DEPTH_SENSOR']	#设置需要用到sensor 这里是rgb图像和深度图像
  HABITAT_SIM_V0:
    GPU_DEVICE_ID: 0						#设置GPU
  RGB_SENSOR:								#设置rgb图像的大小
    WIDTH: 256
    HEIGHT: 256
  DEPTH_SENSOR:
    WIDTH: 256
    HEIGHT: 256

TASK:							# 配置Task
  TYPE: Nav-v0					# 配置为 Naigation-Task
  SUCCESS_DISTANCE: 0.2			# 判断到达goal的距离为小于等于0.2m
  SENSORS: ['POINTGOAL_WITH_GPS_COMPASS_SENSOR','POINTGOAL_SENSOR'] #设置需要用到的传感器 同上
  POINTGOAL_WITH_GPS_COMPASS_SENSOR:	#单独配置‘POINTGOAL_WITH_GPS_COMPASS_SENSOR’传感器
    GOAL_FORMAT: "POLAR"			
    DIMENSIONALITY: 2
  GOAL_SENSOR_UUID: pointgoal_with_gps_compass		#全局id 用来返回这个传感器的值
  POINTGOAL_SENSOR:
    GOAL_FORMAT: "POLAR"
    DIMENSIONALITY: 2

  MEASUREMENTS: ['DISTANCE_TO_GOAL', 'SUCCESS', 'SPL', 'TOP_DOWN_MAP'] #设置需要的measurement
  SUCCESS:
    SUCCESS_DISTANCE: 0.2

DATASET:                  #设置用到的数据集的路径
  DATA_PATH: habitat-lab/data/datasets/pointnav/habitat-test-scenes/v1/train/train.json.gz
  SCENES_DIR: habitat-lab/data/scene_datasets

自定义Measure和Sensor

  • 可以参考官方提供的habitat-lab/example/register_new_sensors_and_measures.py
  • 简介:采用官方接口自定义sensor(例子为返回当前agent位置)和meatures

sensor和measure的区别:

  • observations:
    在这里插入图片描述

    在执行下面两个语句的时候会返回变量observations,这个变量会包含config文件里面需要的sensors的输入值
    在这里插入图片描述

    _env = Env(config)
    observations = _env.reset()
    observations = _env.step(action)
    
  • metrics:

在这里插入图片描述

在执行下面语句的时候会返回一个metrics变量,里面包含config文件需要的measurements

```
metrics = _env.get_metrics()
```

具体方法:

  • 继承 habitat.Measure/Sensor 类

  • 重写类成员函数

    • Measure:

      # Define the measure and register it with habitatALL
      # By default, the things are registered with the class name
      @habitat.registry.register_measure   #@表示装饰器 是必要的 下同
      class EpisodeInfoExample(habitat.Measure):
          def __init__(self, sim, config, **kwargs: Any):
              # This measure only needs the config
              self._config = config
              super().__init__()
      	def _get_uuid(self, *args: Any, **kwargs: Any) -> str:
              return "episode_info"
         	# This is called whenver the environment is reset
      	def reset_metric(self, *args: Any, episode, **kwargs: Any):
          # This is called whenver an action is taken in the environment
          def update_metric(self, *args: Any, episode, action, **kwargs: Any):
      
    •     @classmethod
          def register_measure(cls, to_register=None, *, name: Optional[str] = None):
              r"""Register a measure to registry with key 'name'
      
              Args:
                  name: Key with which the measure will be registered.
                      If None will use the name of the class
      
              """
              #import父类Measure
              from habitat.core.embodied_task import Measure
      		# EpisodeInfoExample(```)== cls._register_impl(```)
              return cls._register_impl(
                  "measure", to_register, name, assert_type=Measure
              )
      
    • Sensor:

      # Define the sensor and register it with habitatALL
      # For the sensor, we will register it with a custom name
      @habitat.registry.register_sensor(name="my_supercool_sensor")
      class AgentPositionSensor(habitat.Sensor):
          def __init__(self, sim, config, **kwargs: Any):
              super().__init__(config=config)
              self._sim = sim
         	def _get_uuid(self, *args: Any, **kwargs: Any) -> str:
              return "agent_position"
         	def _get_sensor_type(self, *args: Any, **kwargs: Any):
              return habitat.SensorTypes.POSITION
          # Defines the size and range of the observations of the sensor
          def _get_observation_space(self, *args: Any, **kwargs: Any):
              return spaces.Box(
                  low=np.finfo(np.float32).min,
                  high=np.finfo(np.float32).max,
                  shape=(3,),
                  dtype=np.float32,
              )
          # This is called whenver reset is called or an action is taken
          def get_observation(
              self, observations, *args: Any, episode, **kwargs: Any
          ):
              # 返回agent当前pos
              return self._sim.get_agent_state().position
      

配置config变量:(可通过程序添加config内容 也可以通过直接改写config.yaml)

    # Get the default config node 读取config.yaml
    config = habitat.get_config(config_paths="habitat-lab/configs/tasks/pointnav.yaml")
    # Make this CfgNode and all of its children mutable.(使config可变)
    config.defrost()
	
    # 添加的内容都要为CfgNode
    # Add things to the config to for the measure
    config.TASK.EPISODE_INFO_EXAMPLE = habitat.Config()
    # The type field is used to look-up the measure in the registry.
    # By default, the things are registered with the class name
    config.TASK.EPISODE_INFO_EXAMPLE.TYPE = "EpisodeInfoExample"
    config.TASK.EPISODE_INFO_EXAMPLE.VALUE = 5
    # Add the measure to the list of measures in use
    config.TASK.MEASUREMENTS.append("EPISODE_INFO_EXAMPLE")

    # Now define the config for the sensor
    config.TASK.AGENT_POSITION_SENSOR = habitat.Config()
    # Use the custom name
    config.TASK.AGENT_POSITION_SENSOR.TYPE = "my_supercool_sensor"
    config.TASK.AGENT_POSITION_SENSOR.ANSWER_TO_LIFE = 42
    # Add the sensor to the list of sensors in use
    config.TASK.SENSORS.append("AGENT_POSITION_SENSOR")
    #冻结config 防止篡改
    config.freeze()

读取data

  • Measure:

    •     metric = env.get_metrics()
          print(metric["episode_info"]) #vvid
      
  • Sensor:

    • 	ovservation["agent_position"] #vvid
      
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值