Keypoint aware raster augmentation

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

Keypoint aware raster augmentation is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Use albumentations keypoint_params to jointly augment BEV rasters and trajectory target points so the spatial transform stays consistent

How to use this skill

Skill file

Overview

BEV rasters paired with trajectory targets (lists of (x, y) points) cannot be augmented with image-only transforms — a horizontal flip of the raster without flipping the trajectory produces a garbage training pair. Albumentations supports this natively via KeypointParams: pass the trajectory points as keypoints, and the library applies the exact same geometric transform to both the image and the points. This unlocks shift/scale/rotate/flip/cutout on motion-prediction data with zero custom code.

Quick Start

import albumentations as A
import numpy as np

tfms = A.Compose([
    A.HorizontalFlip(p=0.5),
    A.ShiftScaleRotate(shift_limit=0.05, scale_limit=0.1,
                       rotate_limit=15, p=0.7),
    A.CoarseDropout(max_holes=4, max_height=16, max_width=16, p=0.3),
], keypoint_params=A.KeypointParams(format='xy', remove_invisible=False))

def augment(sample):
    image = sample['image'].transpose(1, 2, 0)            # (C,H,W) -> (H,W,C)
    kps = sample['target_positions'].tolist()             # [(x,y), ...]
    out = tfms(image=image, keypoints=kps)
    sample['image'] = out['image'].transpose(2, 0, 1)
    sample['target_positions'] = np.array(out['keypoints'], dtype=np.float32)
    return sample

Workflow

  1. Build an A.Compose pipeline with any mix of geometric and pixel transforms
  2. Add keypoint_params=A.KeypointParams(format='xy', remove_invisible=False) — remove_invisible=False is critical so flipped points outside the image aren't silently dropped
  3. In the dataset's __getitem__, convert the raster from (C,H,W) to (H,W,C) and pass it with keypoints=target_points
  4. Albumentations returns a dict with both transformed tensors
  5. Transpose back and overwrite the sample — done

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.