Soft threshold count aggregation

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

Soft threshold count aggregation is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Aggregate patch-level count-regression predictions into image-level totals by multiplying each prediction by a boolean mask (x * (x > tau)) instead of rounding, retaining fractional evidence above threshold while killing background noise

How to use this skill

Skill file

Overview

Patch count-regression produces noisy fractional outputs everywhere — even empty patches return small positive values that sum to spurious counts over thousands of tiles. Hard rounding throws away real signal on patches with 0.4 objects. The right aggregation is soft thresholding: preds * (preds > tau) zeros out any patch below tau with a boolean mask but keeps the full fractional value for surviving patches, then sums. This is not the same as preds.clip(tau) or hard rounding — it's a masked sum that preserves the confidence signal.

Quick Start

import numpy as np

preds = model.predict(test_patches)            # (n_patches, n_classes) float
tau = 0.30                                     # tuned on validation

counts = np.sum(preds * (preds > tau), axis=0).astype('int')
# equivalent, explicit:
# counts = np.where(preds > tau, preds, 0).sum(axis=0).round().astype('int')

Workflow

  1. Run patch-level count regression on every test patch
  2. Sweep tau over [0.1, 0.2, 0.3, 0.4, 0.5] on a held-out validation set
  3. Pick the tau (per-class if needed) minimizing validation RMSE against ground-truth image counts
  4. At inference, multiply predictions by (preds > tau) and sum across patches
  5. Round the result to integer counts for the submission

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.