Sparse event temporal window expansion

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

Sparse event temporal window expansion is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Turn sparse single-frame event labels (impacts, collisions, goals) into a usable detector training set by stamping the positive label onto a ±k-frame window around each event, then dropping any frame that contains no positives — gives the detector enough positive samples without changing the origina

How to use this skill

Skill file

Overview

Video event datasets are brutal for object detectors: a "helmet impact" might be marked on exactly one frame in a 600-frame clip, leaving a 1:600 positive ratio that no detector can train on. Worse, the labeled frame may not even be the visually clearest one — events span 4-8 frames in reality. The fix is temporal window expansion: for every positive frame, mark frames t-k…t+k (typically k=4) as positive too, using the same bounding box. Then filter the dataset to keep only frames that contain at least one positive — discarding the boring 590 negative frames per clip. The detector now sees ~9x more positive samples without you having to relabel anything.

Quick Start

import numpy as np
import pandas as pd

video_labels = pd.read_csv('train_labels.csv').fillna(0)
positives = video_labels[video_labels['impact'] > 0]

K = 4   # +/- frames around each event
offsets = np.array([-4, -3, -2, -1, 1, 2, 3, 4])

for vid, frame, lbl in positives[['video', 'frame', 'label']].values:
    nbr_frames = offsets + frame
    mask = (
        (video_labels['video']  == vid) &
        (video_labels['frame'].isin(nbr_frames)) &
        (video_labels['label']  == lbl)
    )
    video_labels.loc[mask, 'impact'] = 1

# drop frames with no positives at all
video_labels = video_labels[
    video_labels.groupby('image_name')['impact'].transform('sum') > 0
].reset_index(drop=True)

# remap to 1=non-impact, 2=impact for 2-class detector
video_labels['impact'] = video_labels['impact'].astype(int) + 1

Workflow

  1. Identify rows with positive event labels in the original annotations
  2. For each positive (video, frame, label), mark frames in [frame-k, frame+k] of the same (video, label) as positive too — match the bounding box of the existing tracked object
  3. Group by image_name and drop any frame whose total positive count is zero
  4. (Optional) Remap labels so the detector learns "background class" vs "event class" instead of binary
  5. Cache the expanded label CSV — never expand on the fly during training, it's slow and non-deterministic

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.