Observation Wrappers#
Base Class#
- class gymnasium.ObservationWrapper(env: Env[ObsType, ActType])#
Superclass of wrappers that can modify observations using
observation()
forreset()
andstep()
.If you would like to apply a function to only the observation before passing it to the learning code, you can simply inherit from
ObservationWrapper
and overwrite the methodobservation()
to implement that transformation. The transformation defined in that method must be reflected by theenv
observation space. Otherwise, you need to specify the new observation space of the wrapper by settingself.observation_space
in the__init__()
method of your wrapper.Among others, Gymnasium provides the observation wrapper
TimeAwareObservation
, which adds information about the index of the timestep to the observation.Constructor for the observation wrapper.
- observation(observation: ObsType) WrapperObsType #
Returns a modified observation.
- Parameters:
observation – The
env
observation- Returns:
The modified observation
Available Observation Wrappers#
- class gymnasium.wrappers.TransformObservation(env: Env, f: Callable[[Any], Any])#
Transform the observation via an arbitrary function
f
.The function
f
should be defined on the observation space of the base environment,env
, and should, ideally, return values in the same space.If the transformation you wish to apply to observations returns values in a different space, you should subclass
ObservationWrapper
, implement the transformation, and set the new observation space accordingly. If you were to use this wrapper instead, the observation space would be set incorrectly.Example
>>> import gymnasium as gym >>> import numpy as np >>> env = gym.make('CartPole-v1') >>> env = TransformObservation(env, lambda obs: obs + 0.1*np.random.randn(*obs.shape)) >>> env.reset() array([-0.08319338, 0.04635121, -0.07394746, 0.20877492])
Initialize the
TransformObservation
wrapper with an environment and a transform functionf
.- Parameters:
env – The environment to apply the wrapper
f – A function that transforms the observation
- class gymnasium.wrappers.FilterObservation(env: Env, filter_keys: Optional[Sequence[str]] = None)#
Filter Dict observation space by the keys.
Example
>>> import gymnasium as gym >>> env = gym.wrappers.TransformObservation( ... gym.make('CartPole-v1'), lambda obs: {'obs': obs, 'time': 0} ... ) >>> env.observation_space = gym.spaces.Dict(obs=env.observation_space, time=gym.spaces.Discrete(1)) >>> env.reset() {'obs': array([-0.00067088, -0.01860439, 0.04772898, -0.01911527], dtype=float32), 'time': 0} >>> env = FilterObservation(env, filter_keys=['obs']) >>> env.reset() {'obs': array([ 0.04560107, 0.04466959, -0.0328232 , -0.02367178], dtype=float32)} >>> env.step(0) ({'obs': array([ 0.04649447, -0.14996664, -0.03329664, 0.25847703], dtype=float32)}, 1.0, False, {})
A wrapper that filters dictionary observations by their keys.
- Parameters:
env – The environment to apply the wrapper
filter_keys – List of keys to be included in the observations. If
None
, observations will not be filtered and this wrapper has no effect
- Raises:
ValueError – If the environment’s observation space is not
spaces.Dict
ValueError – If any of the filter_keys are not included in the original env’s observation space
- class gymnasium.wrappers.FlattenObservation(env: Env)#
Observation wrapper that flattens the observation.
Example
>>> import gymnasium as gym >>> env = gym.make('CarRacing-v1') >>> env.observation_space.shape (96, 96, 3) >>> env = FlattenObservation(env) >>> env.observation_space.shape (27648,) >>> obs, info = env.reset() >>> obs.shape (27648,)
Flattens the observations of an environment.
- Parameters:
env – The environment to apply the wrapper
- class gymnasium.wrappers.FrameStack(env: Env, num_stack: int, lz4_compress: bool = False)#
Observation wrapper that stacks the observations in a rolling manner.
For example, if the number of stacks is 4, then the returned observation contains the most recent 4 observations. For environment ‘Pendulum-v1’, the original observation is an array with shape [3], so if we stack 4 observations, the processed observation has shape [4, 3].
Note
To be memory efficient, the stacked observations are wrapped by
LazyFrame
.The observation space must be
Box
type. If one usesDict
as observation space, it should applyFlattenObservation
wrapper first.After
reset()
is called, the frame buffer will be filled with the initial observation. I.e. the observation returned byreset()
will consist of num_stack many identical frames.
Example
>>> import gymnasium as gym >>> env = gym.make('CarRacing-v1') >>> env = FrameStack(env, 4) >>> env.observation_space Box(4, 96, 96, 3) >>> obs = env.reset() >>> obs.shape (4, 96, 96, 3)
Observation wrapper that stacks the observations in a rolling manner.
- Parameters:
env (Env) – The environment to apply the wrapper
num_stack (int) – The number of frames to stack
lz4_compress (bool) – Use lz4 to compress the frames internally
- class gymnasium.wrappers.GrayScaleObservation(env: Env, keep_dim: bool = False)#
Convert the image observation from RGB to gray scale.
Example
>>> env = gym.make('CarRacing-v1') >>> env.observation_space Box(0, 255, (96, 96, 3), uint8) >>> env = GrayScaleObservation(gym.make('CarRacing-v1')) >>> env.observation_space Box(0, 255, (96, 96), uint8) >>> env = GrayScaleObservation(gym.make('CarRacing-v1'), keep_dim=True) >>> env.observation_space Box(0, 255, (96, 96, 1), uint8)
Convert the image observation from RGB to gray scale.
- Parameters:
env (Env) – The environment to apply the wrapper
keep_dim (bool) – If True, a singleton dimension will be added, i.e. observations are of the shape AxBx1. Otherwise, they are of shape AxB.
- class gymnasium.wrappers.NormalizeObservation(env: Env, epsilon: float = 1e-8)#
This wrapper will normalize observations s.t. each coordinate is centered with unit variance.
Note
The normalization depends on past trajectories and observations will not be normalized correctly if the wrapper was newly instantiated or the policy was changed recently.
This wrapper will normalize observations s.t. each coordinate is centered with unit variance.
- Parameters:
env (Env) – The environment to apply the wrapper
epsilon – A stability parameter that is used when scaling the observations.
- class gymnasium.wrappers.PixelObservationWrapper(env: Env, pixels_only: bool = True, render_kwargs: Optional[Dict[str, Dict[str, Any]]] = None, pixel_keys: Tuple[str, ...] = ('pixels',))#
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
Dict
, the dictionary of rendered images will be updated with the base environment’s observation. If, however, the observation space is of typeBox
, the base environment’s observation (which will be an element of theBox
space) will be added to the dictionary under the key “state”.Example
>>> import gymnasium as gym >>> env = PixelObservationWrapper(gym.make('CarRacing-v1', render_mode="rgb_array")) >>> obs = env.reset() >>> obs.keys() odict_keys(['pixels']) >>> obs['pixels'].shape (400, 600, 3) >>> env = PixelObservationWrapper(gym.make('CarRacing-v1', render_mode="rgb_array"), 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', render_mode="rgb_array"), pixel_keys=('obs',)) >>> obs = env.reset() >>> obs.keys() odict_keys(['obs']) >>> obs['obs'].shape (400, 600, 3)
Initializes a new pixel Wrapper.
- Parameters:
env – The environment to wrap.
pixels_only (bool) – If True (default), the original observation returned by the wrapped environment will be discarded, and a dictionary observation will only include pixels. If False, the observation dictionary will contain both the original observations and the pixel observations.
render_kwargs (dict) – Optional dictionary containing that maps elements of pixel_keys to keyword arguments passed to the
self.render()
method.pixel_keys – Optional custom string specifying the pixel observation’s key in the OrderedDict of observations. Defaults to (pixels,).
- Raises:
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.ValueError – If
env
’s observation already contains any of the specifiedpixel_keys
.TypeError – When an unexpected pixel type is used
- class gymnasium.wrappers.ResizeObservation(env: Env, shape: Union[tuple, int])#
Resize the image observation.
This wrapper works on environments with image observations (or more generally observations of shape AxBxC) and resizes the observation to the shape given by the 2-tuple
shape
. The argumentshape
may also be an integer. In that case, the observation is scaled to a square of side-lengthshape
.Example
>>> import gymnasium as gym >>> env = gym.make('CarRacing-v1') >>> env.observation_space.shape (96, 96, 3) >>> env = ResizeObservation(env, 64) >>> env.observation_space.shape (64, 64, 3)
Resizes image observations to shape given by
shape
.- Parameters:
env – The environment to apply the wrapper
shape – The shape of the resized observations
- class gymnasium.wrappers.TimeAwareObservation(env: Env)#
Augment the observation with the current time step in the episode.
The observation space of the wrapped environment is assumed to be a flat
Box
. In particular, pixel observations are not supported. This wrapper will append the current timestep within the current episode to the observation.Example
>>> import gymnasium as gym >>> env = gym.make('CartPole-v1') >>> env = TimeAwareObservation(env) >>> env.reset() array([ 0.03810719, 0.03522411, 0.02231044, -0.01088205, 0. ]) >>> env.step(env.action_space.sample())[0] array([ 0.03881167, -0.16021058, 0.0220928 , 0.28875574, 1. ])
Initialize
TimeAwareObservation
that requires an environment with a flatBox
observation space.- Parameters:
env – The environment to apply the wrapper