Files
Gymnasium/gym/wrappers/pixel_observation.py

184 lines
6.8 KiB
Python
Raw Normal View History

2022-05-13 13:58:19 +01:00
"""Wrapper for augmenting observations by pixel values."""
import collections
import copy
from collections.abc import MutableMapping
from typing import Any, Dict, Optional, Tuple
import numpy as np
2022-05-13 13:58:19 +01:00
import gym
from gym import spaces
2021-07-29 02:26:34 +02:00
STATE_KEY = "state"
2022-05-13 13:58:19 +01:00
class PixelObservationWrapper(gym.ObservationWrapper):
"""Augment observations by pixel values.
Observations of this wrapper will be dictionaries of images.
You can also choose to add the observation of the base environment to this dictionary.
In that case, if the base environment has an observation space of type :class:`Dict`, the dictionary
of rendered images will be updated with the base environment's observation. If, however, the observation
space is of type :class:`Box`, the base environment's observation (which will be an element of the :class:`Box`
space) will be added to the dictionary under the key "state".
Example:
>>> import gym
>>> env = PixelObservationWrapper(gym.make('CarRacing-v1'))
>>> obs = env.reset()
>>> obs.keys()
odict_keys(['pixels'])
>>> obs['pixels'].shape
(400, 600, 3)
>>> env = PixelObservationWrapper(gym.make('CarRacing-v1'), pixels_only=False)
>>> obs = env.reset()
>>> obs.keys()
odict_keys(['state', 'pixels'])
>>> obs['state'].shape
(96, 96, 3)
>>> obs['pixels'].shape
(400, 600, 3)
>>> env = PixelObservationWrapper(gym.make('CarRacing-v1'), pixel_keys=('obs',))
>>> obs = env.reset()
>>> obs.keys()
odict_keys(['obs'])
>>> obs['obs'].shape
(400, 600, 3)
"""
2021-07-29 15:39:42 -04:00
def __init__(
2022-05-13 13:58:19 +01:00
self,
env: gym.Env,
pixels_only: bool = True,
render_kwargs: Optional[Dict[str, Dict[str, Any]]] = None,
pixel_keys: Tuple[str, ...] = ("pixels",),
2021-07-29 15:39:42 -04:00
):
"""Initializes a new pixel Wrapper.
Args:
env: The environment to wrap.
2022-05-13 13:58:19 +01:00
pixels_only (bool): If ``True`` (default), the original observation returned
by the wrapped environment will be discarded, and a dictionary
2022-05-13 13:58:19 +01:00
observation will only include pixels. If ``False``, the
observation dictionary will contain both the original
observations and the pixel observations.
2022-05-13 13:58:19 +01:00
render_kwargs (dict): Optional dictionary containing that maps elements of ``pixel_keys``to
keyword arguments passed to the :meth:`self.render` method.
pixel_keys: Optional custom string specifying the pixel
2022-05-13 13:58:19 +01:00
observation's key in the ``OrderedDict`` of observations.
Defaults to ``(pixels,)``.
Raises:
2022-05-13 13:58:19 +01:00
AssertionError: If any of the keys in ``render_kwargs``do not show up in ``pixel_keys``.
ValueError: If ``env``'s observation space is not compatible with the
wrapper. Supported formats are a single array, or a dict of
arrays.
2022-05-13 13:58:19 +01:00
ValueError: If ``env``'s observation already contains any of the
specified ``pixel_keys``.
TypeError: When an unexpected pixel type is used
"""
super().__init__(env)
2022-05-13 13:58:19 +01:00
# Avoid side-effects that occur when render_kwargs is manipulated
render_kwargs = copy.deepcopy(render_kwargs)
if render_kwargs is None:
render_kwargs = {}
2022-05-13 13:58:19 +01:00
for key in render_kwargs:
assert key in pixel_keys, (
"The argument render_kwargs should map elements of "
"pixel_keys to dictionaries of keyword arguments. "
f"Found key '{key}' in render_kwargs but not in pixel_keys."
)
for key in pixel_keys:
render_kwargs.setdefault(key, {})
2021-07-29 02:26:34 +02:00
render_mode = render_kwargs[key].pop("mode", "rgb_array")
assert render_mode == "rgb_array", render_mode
render_kwargs[key]["mode"] = "rgb_array"
wrapped_observation_space = env.observation_space
if isinstance(wrapped_observation_space, spaces.Box):
self._observation_is_dict = False
invalid_keys = {STATE_KEY}
2021-07-29 02:26:34 +02:00
elif isinstance(wrapped_observation_space, (spaces.Dict, MutableMapping)):
self._observation_is_dict = True
invalid_keys = set(wrapped_observation_space.spaces.keys())
else:
raise ValueError("Unsupported observation space structure.")
if not pixels_only:
# Make sure that now keys in the `pixel_keys` overlap with
# `observation_keys`
overlapping_keys = set(pixel_keys) & set(invalid_keys)
if overlapping_keys:
2021-07-29 15:39:42 -04:00
raise ValueError(
f"Duplicate or reserved pixel keys {overlapping_keys!r}."
2021-07-29 15:39:42 -04:00
)
if pixels_only:
self.observation_space = spaces.Dict()
elif self._observation_is_dict:
self.observation_space = copy.deepcopy(wrapped_observation_space)
else:
self.observation_space = spaces.Dict()
self.observation_space.spaces[STATE_KEY] = wrapped_observation_space
# Extend observation space with pixels.
2022-05-13 13:58:19 +01:00
self.env.reset()
pixels_spaces = {}
for pixel_key in pixel_keys:
2019-10-18 23:57:17 +02:00
pixels = self.env.render(**render_kwargs[pixel_key])
if np.issubdtype(pixels.dtype, np.integer):
low, high = (0, 255)
elif np.issubdtype(pixels.dtype, np.float):
2021-07-29 02:26:34 +02:00
low, high = (-float("inf"), float("inf"))
else:
raise TypeError(pixels.dtype)
2021-07-29 15:39:42 -04:00
pixels_space = spaces.Box(
shape=pixels.shape, low=low, high=high, dtype=pixels.dtype
)
pixels_spaces[pixel_key] = pixels_space
self.observation_space.spaces.update(pixels_spaces)
self._pixels_only = pixels_only
self._render_kwargs = render_kwargs
self._pixel_keys = pixel_keys
def observation(self, observation):
2022-05-13 13:58:19 +01:00
"""Updates the observations with the pixel observations.
Args:
observation: The observation to add pixel observations for
Returns:
The updated pixel observations
"""
pixel_observation = self._add_pixel_observation(observation)
return pixel_observation
def _add_pixel_observation(self, wrapped_observation):
if self._pixels_only:
observation = collections.OrderedDict()
elif self._observation_is_dict:
observation = type(wrapped_observation)(wrapped_observation)
else:
observation = collections.OrderedDict()
observation[STATE_KEY] = wrapped_observation
2021-07-29 15:39:42 -04:00
pixel_observations = {
pixel_key: self.env.render(**self._render_kwargs[pixel_key])
for pixel_key in self._pixel_keys
}
observation.update(pixel_observations)
return observation