Frame differencing temporal encoding

From wenmin-wu/ds-skills · View interactive version

Frame differencing temporal encoding is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Encode motion and velocity by computing per-channel pixel differences between consecutive frames instead of stacking raw frames for RL visual observations

How to use this skill

Skill file

Frame Differencing Temporal Encoding

Overview

Frame stacking (concatenating the last N frames) is the standard way to give RL agents temporal information from visual observations. Frame differencing is a lighter alternative: subtract the previous frame from the current one to produce a motion-only image. Moving objects appear as non-zero pixels while static backgrounds cancel out, giving the network explicit velocity signals without doubling the input channels.

Quick Start

import numpy as np
from collections import deque

class FrameDiffWrapper:
    def __init__(self, env, n_channels=4):
        self.env = env
        self.n_channels = n_channels
        self.buffer = deque(maxlen=2)

    def reset(self):
        obs = self.env.reset()
        frame = obs / 255.0
        self.buffer.append(frame)
        self.buffer.append(np.zeros_like(frame))
        return self._diff()

    def step(self, action):
        obs, reward, done, info = self.env.step(action)
        self.buffer.append(obs / 255.0)
        return self._diff(), reward, done, info

    def _diff(self):
        diff = np.empty_like(self.buffer[1])
        for c in range(diff.shape[-1]):
            diff[..., c] = self.buffer[1][..., c] - self.buffer[0][..., c]
        return diff

Workflow

  1. Maintain a deque of the last 2 frames (normalized to [0, 1])
  2. At each step, compute per-channel difference: current - previous
  3. Feed the difference image to the policy network instead of stacked frames
  4. Moving objects have large positive/negative values; static regions are ~0
  5. Optionally combine: stack one raw frame + one diff frame for position + velocity

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.