Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Farama Foundation Gymnasium AtariPreprocessing

From Leeroopedia
Knowledge Sources
Domains Reinforcement_Learning, Wrappers
Last Updated 2026-02-15 03:00 GMT

Overview

Implements the standard Atari 2600 preprocessing techniques following the guidelines of Machado et al. (2018), including noop reset, frame skipping, max-pooling, grayscale conversion, resizing, and optional life-loss termination.

Description

The AtariPreprocessing wrapper applies the following preprocessing stages to Atari environments:

  • Noop Reset -- At reset, takes a random number of no-op actions (up to noop_max, default 30) to introduce stochasticity in the initial state.
  • Frame Skipping -- Repeats each action for frame_skip steps (default 4), accumulating total reward.
  • Max-Pooling -- Takes the element-wise maximum of the last two frames within the frame skip window to handle flickering sprites.
  • Terminal on Life Loss -- Optionally terminates the episode when the agent loses a life (disabled by default, as recommended by Machado et al.).
  • Resize -- Resizes the observation from the original 210x160 to a configurable size (default 84x84) using OpenCV's INTER_AREA interpolation.
  • Grayscale -- Converts RGB observations to grayscale (enabled by default), with an optional channel axis.
  • Scaling -- Optionally normalizes pixel values from [0, 255] to [0, 1) as float32.

Requires the opencv-python package. The base environment must have frameskip=1 when using frame_skip > 1.

Usage

Use this wrapper as the standard preprocessing pipeline for Atari environments in deep reinforcement learning. Combine with FrameStackObservation for frame stacking. This wrapper replaces multiple individual wrappers with a single efficient implementation.

Code Reference

Source Location

Signature

class AtariPreprocessing(gym.Wrapper, gym.utils.RecordConstructorArgs):
    def __init__(
        self,
        env: gym.Env,
        noop_max: int = 30,
        frame_skip: int = 4,
        screen_size: int | tuple[int, int] = 84,
        terminal_on_life_loss: bool = False,
        grayscale_obs: bool = True,
        grayscale_newaxis: bool = False,
        scale_obs: bool = False,
    ): ...

Import

from gymnasium.wrappers import AtariPreprocessing

I/O Contract

Inputs

Name Type Required Description
env Env Yes The Atari environment to wrap (must have frameskip=1)
noop_max int No Max number of no-op actions at reset (default 30, set 0 to disable)
frame_skip int No Number of frames to skip between agent steps (default 4)
screen_size int or tuple[int, int] No Target screen size for resizing (default 84)
terminal_on_life_loss bool No Terminate on life loss (default False)
grayscale_obs bool No Convert to grayscale (default True)
grayscale_newaxis bool No Add channel axis to grayscale (default False)
scale_obs bool No Scale pixels to [0, 1) float32 (default False)

Outputs

Name Type Description
observation ndarray Preprocessed image observation (resized, optionally grayscale/scaled)
reward float Total accumulated reward across skipped frames
terminated bool Whether the episode terminated (including life loss if enabled)
truncated bool Whether the episode was truncated
info dict Additional environment information

Usage Examples

import gymnasium as gym
import ale_py

gym.register_envs(ale_py)

env = gym.make("ALE/Pong-v5", frameskip=1)
env = AtariPreprocessing(
    env,
    noop_max=10,
    frame_skip=4,
    terminal_on_life_loss=True,
    screen_size=84,
    grayscale_obs=True,
    grayscale_newaxis=False,
    scale_obs=False,
)

obs, info = env.reset(seed=42)
# obs.shape == (84, 84), dtype == uint8

# Combine with frame stacking for DQN-style preprocessing
from gymnasium.wrappers import FrameStackObservation
env = FrameStackObservation(env, stack_size=4)
obs, info = env.reset(seed=42)
# obs.shape == (4, 84, 84)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment