|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# How to get details from RoboHive Environments \n", |
| 9 | + "RoboHive supports three ways to query the environment based on the nature of inforamtion required. Following information can be queried from an active environment\n", |
| 10 | + "1. Get observations (configured using `obs_keys`)\n", |
| 11 | + "2. Get proprioception (configured via `proprio_keys`)\n", |
| 12 | + "3. Get exteroception (configured via `visual_keys`)\n", |
| 13 | + "\n", |
| 14 | + "Lets go through them in details one at a time. First we laod an environment, preconfigured with respective keys, and step it a few times - \n", |
| 15 | + "(we will go through the key configuration details in another tutorial)." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "import gym \n", |
| 25 | + "import robohive\n", |
| 26 | + "\n", |
| 27 | + "# create an env and reset\n", |
| 28 | + "env = gym.make('door_v2d-v1')\n", |
| 29 | + "env.reset()\n", |
| 30 | + "\n", |
| 31 | + "# Lets step is few times\n", |
| 32 | + "for _ in range(5):\n", |
| 33 | + " allinfo_tdt = env.step(env.action_space.sample())" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "attachments": {}, |
| 38 | + "cell_type": "markdown", |
| 39 | + "metadata": {}, |
| 40 | + "source": [ |
| 41 | + "### 1. get observations\n", |
| 42 | + "This is the most commonly used method to get the current (state based) observation from the environment. Observation are customized using `env.obs_keys` and can contain any details that the environment has access to. RoboHive's doesn't put any restrictions on the information that can be provided by the environment.\n", |
| 43 | + "\n", |
| 44 | + "RoboHive env provides an `env.get_obs()` method to query observations. It triggers following sequence of events -\n", |
| 45 | + "1. Uses robot (sim/hardware) to get sensors\n", |
| 46 | + "2. Reconstructs (partially) observed-sim `env.sim_obsd` using (noisy) sensor data\n", |
| 47 | + "3. Build the full observation dictionary `env.obs_dict` using the observed-sim\n", |
| 48 | + "4. Build obs vector from the obs_dict (using the `env.obs_keys`)" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "cell_type": "code", |
| 53 | + "execution_count": null, |
| 54 | + "metadata": {}, |
| 55 | + "outputs": [], |
| 56 | + "source": [ |
| 57 | + "# get current observation vector (internally updates env.sim_obsd & env.obs_dict)\n", |
| 58 | + "obs = env.get_obs()" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "attachments": {}, |
| 63 | + "cell_type": "markdown", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "### 2. get proprioception\n", |
| 67 | + "This method is used to get *only proprioceptive signals* from the robohive environments. Proprioceptive signals are customized using `env.proprio_keys` and typically consists of proprioceptive sensors that are available to the agent/robots. Proprioception can be acquired in two ways \n", |
| 68 | + "1. Recover from *existing* observation dictionary\n", |
| 69 | + "2. Update observation and get proprioception alongside (default behavior)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "# Recover from existing observaton dictionary \n", |
| 79 | + "time, proprio_vec, proprio_dict = env.get_proprioception(env.obs_dict)\n", |
| 80 | + "\n", |
| 81 | + "# Update observation and get proprioception alongside\n", |
| 82 | + "obs = env.get_obs(update_proprioception=True)\n", |
| 83 | + "# you can get access proprioception dictionary now\n", |
| 84 | + "print(env.proprio_dict)" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "attachments": {}, |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "### 3. get exteroception\n", |
| 93 | + "This method is used to get *only exteroceptive signals* from the robohive environments. exteroceptive signals typically consists of exteroceptive sensors that are available to the agent/robots. Currently we are fosuing primarily on cameras as exteroceptive inputs. It can be customized using `env.visual_keys`. Expanding to other exteroceptive modalities while possible and within scope, is not a tested functionality yet.\n", |
| 94 | + "\n", |
| 95 | + "Note that exteroceptive sensors are expensive (compute + bandwidth), therefore its requires users to make explicit calls for an update. It can be acquired in two ways -\n", |
| 96 | + "\n", |
| 97 | + "1. Make an explicit call to `env.get_exteroception()`\n", |
| 98 | + "2. Update observation and get proprioception alongside (False by default)" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "# Make an explicit call to update\n", |
| 108 | + "extero_dict = env.get_exteroception()\n", |
| 109 | + "\n", |
| 110 | + "# Update obdervation and get exteroception alongside\n", |
| 111 | + "obs = env.get_obs(update_exteroception=True)\n", |
| 112 | + "print(env.visual_dict.keys())" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "attachments": {}, |
| 117 | + "cell_type": "markdown", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "### 4. get everything\n", |
| 121 | + "If it's of interest to get all the information at once, `env_get_obs()` can be asked to make all the updates.\n" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "code", |
| 126 | + "execution_count": null, |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "obs = env.get_obs(update_proprioception=True, update_exteroception=True)\n", |
| 131 | + "print(f\"time = {env.obs_dict['time']}\")\n", |
| 132 | + "print(f\"obs vector = {obs}\")\n", |
| 133 | + "print(f\"obs_dict = {env.obs_dict.keys()}\")\n", |
| 134 | + "print(f\"proprio_dict = {env.proprio_dict.keys()}\")\n", |
| 135 | + "print(f\"visual_dict = {env.visual_dict.keys()}\")" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "attachments": {}, |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "# Practical tips\n", |
| 144 | + "\n", |
| 145 | + "### 1. Why ['time', 'time'] as observation?\n", |
| 146 | + "Note the observation vector - it looks too simple for such an environment of this complexity. This seems a little weird!\n", |
| 147 | + "It is a common practice in *RoboHive* to use ['time', 'time'] as observation for envs designed for visual diversity. This is for two reasons \n", |
| 148 | + "1. To avoid leaking oracle information via obs to the agents studying visual generalization \n", |
| 149 | + "2. Single ['time'] key leads to silent singleton exansion when inputs of higher dimenstions are required. Replcicating the `time` keys twice helps to catch/expose this bug.\n", |
| 150 | + "\n", |
| 151 | + "### 2. Ways to ask for obs, proprioception, exteroception?\n", |
| 152 | + "The flexibility of RoboHive allows multiple ways to ask for information from the env. We outline a of options below - " |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "# Recover all info at current timestep: obs(t), rwd(t), done(t), info(t)\n", |
| 162 | + "obs_t, rwd_t, done_t, info_t = env.env.forward(update_proprioception=True, update_exteroception=True)\n", |
| 163 | + "print(f\"time = {env.obs_dict['time']}\")\n", |
| 164 | + "print(f\"obs vector = {obs_t}\")\n", |
| 165 | + "print(f\"obs_dict = {env.obs_dict.keys()}\")\n", |
| 166 | + "print(f\"proprio_dict = {env.proprio_dict.keys()}\")\n", |
| 167 | + "print(f\"visual_dict = {env.visual_dict.keys()}\")\n", |
| 168 | + "\n", |
| 169 | + "# Recover info at the next timestep: obs(t+dt), rwd(t+dt), done(t+dt), info(t+dt)\n", |
| 170 | + "obs_tdt, rwd_tdt, done_tdt, info_tdt = env.env.step(env.action_space.sample(), update_proprioception=True, update_exteroception=True)\n", |
| 171 | + "print(f\"time = {env.obs_dict['time']}\")\n", |
| 172 | + "print(f\"obs vector = {obs_tdt}\")\n", |
| 173 | + "print(f\"obs_dict = {env.obs_dict.keys()}\")\n", |
| 174 | + "print(f\"proprio_dict = {env.proprio_dict.keys()}\")\n", |
| 175 | + "print(f\"visual_dict = {env.visual_dict.keys()}\")" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "code", |
| 180 | + "execution_count": null, |
| 181 | + "metadata": {}, |
| 182 | + "outputs": [], |
| 183 | + "source": [ |
| 184 | + "# close the env\n", |
| 185 | + "env.close()" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "metadata": { |
| 190 | + "kernelspec": { |
| 191 | + "display_name": "robohive", |
| 192 | + "language": "python", |
| 193 | + "name": "python3" |
| 194 | + }, |
| 195 | + "language_info": { |
| 196 | + "codemirror_mode": { |
| 197 | + "name": "ipython", |
| 198 | + "version": 3 |
| 199 | + }, |
| 200 | + "file_extension": ".py", |
| 201 | + "mimetype": "text/x-python", |
| 202 | + "name": "python", |
| 203 | + "nbconvert_exporter": "python", |
| 204 | + "pygments_lexer": "ipython3", |
| 205 | + "version": "3.8.16" |
| 206 | + }, |
| 207 | + "orig_nbformat": 4 |
| 208 | + }, |
| 209 | + "nbformat": 4, |
| 210 | + "nbformat_minor": 2 |
| 211 | +} |
0 commit comments