Skip to content

Commit

Permalink
DOCS: Adding a tutorial on how to access the observation, proprio and…
Browse files Browse the repository at this point in the history
… extero information
  • Loading branch information
vikashplus committed Apr 13, 2023
1 parent 8894dd5 commit a7821f8
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 6 deletions.
20 changes: 14 additions & 6 deletions robohive/tutorials/1_env_registration_and_customization.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,14 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "e8878d86",
"metadata": {},
"source": [
"### 2. Registering env variants\n",
"(note: there is a seperate tutorial specifically on how to customize obs/proprio/extero of an env)\n",
"\n",
"While every RoboHive env are packaged with care, research projects often require flexibility to customize the prepackaed environments. RoboHive provides functionality to easily create variants of preregistered environments. We have found this functionality to be really useful when multiple closely related envs are required. For example -- env variations during hyper parameter sweeps, testing a policy on diffeernt env conditions, system identification."
]
},
Expand All @@ -144,16 +147,12 @@
"outputs": [],
"source": [
"from robohive.envs.env_variants import register_env_variant\n",
"base_env_name = \"kitchen-v3\"\n",
"base_env_name = \"FK1_RelaxFixed-v4\"\n",
"\n",
"# Register a variant of the kitchen env\n",
"base_env_variants={\n",
" 'max_episode_steps':50, # special key\n",
" 'obj_goal': {\"lightswitch_joint\": -0.7}, # obj_goal keys will be updated\n",
" 'obs_keys_wt': { # obs_keys_wt will be updated\n",
" 'robot_jnt': 5.0,\n",
" 'obj_goal': 5.0,\n",
" 'objs_jnt': 5.0,}\n",
"}\n",
"variant_env_name = register_env_variant(env_id=base_env_name, variants=base_env_variants)"
]
Expand Down Expand Up @@ -225,17 +224,26 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "5fd3b3bf",
"metadata": {},
"source": [
"In the output you will note that the two envs have different settings but have the same id. This is confusing/misleading.\n",
"\n",
"**NOTE:** Passing kwargs during make is not advisable as the updated env have the same id as the original env. This leads to two potential issues \n",
"\n",
"1. *Confusion while reporting results* - Reporting results using the env's name while its configurations have been changed leads to reproducibility issues. If env's configuration changes are needed, it is recommended to instead to use env_variant to register the altered name with its own unique id. This is a very common mistake in the field. Lets fix this! \n",
"**Recommendation**: For reporting results RoboHive recommends projects/papers to use `register_env_variant` at top of their scripts to create a unique env name `<PAPERNAME>_FrankaReachRandom-v0` if default envs are customized in any way.\n",
"\n",
"2. *Confusion while usage* - two env with the same id but different properties can lead to confusion during development/usage. "
]
},
{
"cell_type": "markdown",
"id": "00507dd8",
"metadata": {},
"source": []
}
],
"metadata": {
Expand All @@ -254,7 +262,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.8.16"
}
},
"nbformat": 4,
Expand Down
211 changes: 211 additions & 0 deletions robohive/tutorials/3_get_obs_proprio_extero.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to get details from RoboHive Environments \n",
"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",
"1. Get observations (configured using `obs_keys`)\n",
"2. Get proprioception (configured via `proprio_keys`)\n",
"3. Get exteroception (configured via `visual_keys`)\n",
"\n",
"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",
"(we will go through the key configuration details in another tutorial)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gym \n",
"import robohive\n",
"\n",
"# create an env and reset\n",
"env = gym.make('door_v2d-v1')\n",
"env.reset()\n",
"\n",
"# Lets step is few times\n",
"for _ in range(5):\n",
" allinfo_tdt = env.step(env.action_space.sample())"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. get observations\n",
"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",
"\n",
"RoboHive env provides an `env.get_obs()` method to query observations. It triggers following sequence of events -\n",
"1. Uses robot (sim/hardware) to get sensors\n",
"2. Reconstructs (partially) observed-sim `env.sim_obsd` using (noisy) sensor data\n",
"3. Build the full observation dictionary `env.obs_dict` using the observed-sim\n",
"4. Build obs vector from the obs_dict (using the `env.obs_keys`)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# get current observation vector (internally updates env.sim_obsd & env.obs_dict)\n",
"obs = env.get_obs()"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. get proprioception\n",
"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",
"1. Recover from *existing* observation dictionary\n",
"2. Update observation and get proprioception alongside (default behavior)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Recover from existing observaton dictionary \n",
"time, proprio_vec, proprio_dict = env.get_proprioception(env.obs_dict)\n",
"\n",
"# Update observation and get proprioception alongside\n",
"obs = env.get_obs(update_proprioception=True)\n",
"# you can get access proprioception dictionary now\n",
"print(env.proprio_dict)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. get exteroception\n",
"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",
"\n",
"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",
"\n",
"1. Make an explicit call to `env.get_exteroception()`\n",
"2. Update observation and get proprioception alongside (False by default)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Make an explicit call to update\n",
"extero_dict = env.get_exteroception()\n",
"\n",
"# Update obdervation and get exteroception alongside\n",
"obs = env.get_obs(update_exteroception=True)\n",
"print(env.visual_dict.keys())"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. get everything\n",
"If it's of interest to get all the information at once, `env_get_obs()` can be asked to make all the updates.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"obs = env.get_obs(update_proprioception=True, update_exteroception=True)\n",
"print(f\"time = {env.obs_dict['time']}\")\n",
"print(f\"obs vector = {obs}\")\n",
"print(f\"obs_dict = {env.obs_dict.keys()}\")\n",
"print(f\"proprio_dict = {env.proprio_dict.keys()}\")\n",
"print(f\"visual_dict = {env.visual_dict.keys()}\")"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Practical tips\n",
"\n",
"### 1. Why ['time', 'time'] as observation?\n",
"Note the observation vector - it looks too simple for such an environment of this complexity. This seems a little weird!\n",
"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",
"1. To avoid leaking oracle information via obs to the agents studying visual generalization \n",
"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",
"\n",
"### 2. Ways to ask for obs, proprioception, exteroception?\n",
"The flexibility of RoboHive allows multiple ways to ask for information from the env. We outline a of options below - "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Recover all info at current timestep: obs(t), rwd(t), done(t), info(t)\n",
"obs_t, rwd_t, done_t, info_t = env.env.forward(update_proprioception=True, update_exteroception=True)\n",
"print(f\"time = {env.obs_dict['time']}\")\n",
"print(f\"obs vector = {obs_t}\")\n",
"print(f\"obs_dict = {env.obs_dict.keys()}\")\n",
"print(f\"proprio_dict = {env.proprio_dict.keys()}\")\n",
"print(f\"visual_dict = {env.visual_dict.keys()}\")\n",
"\n",
"# Recover info at the next timestep: obs(t+dt), rwd(t+dt), done(t+dt), info(t+dt)\n",
"obs_tdt, rwd_tdt, done_tdt, info_tdt = env.env.step(env.action_space.sample(), update_proprioception=True, update_exteroception=True)\n",
"print(f\"time = {env.obs_dict['time']}\")\n",
"print(f\"obs vector = {obs_tdt}\")\n",
"print(f\"obs_dict = {env.obs_dict.keys()}\")\n",
"print(f\"proprio_dict = {env.proprio_dict.keys()}\")\n",
"print(f\"visual_dict = {env.visual_dict.keys()}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# close the env\n",
"env.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "robohive",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit a7821f8

Please sign in to comment.