skills/cv/patch-grid-count-regression/SKILL.md
Tile a large aerial image into fixed-size patches, accumulate per-class point-annotation counts into a grid tensor aligned with the tiles, and train a small CNN to regress per-class object counts per patch under MSE
npx skillsauth add wenmin-wu/ds-skills cv-patch-grid-count-regressionInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
When you only have point annotations and the target is a count (not bounding boxes or pixel masks), patch-level count regression is the lightest-weight recipe that works. Tile the image into fixed-size patches, accumulate point labels into a (grid_x, grid_y, n_classes) count tensor, then train a small CNN with a linear head to predict per-class counts per patch under MSE. At inference you sum the patch predictions over the image. It dodges the complexity of detection, segmentation, and density maps, and it's exactly what won NOAA Steller Sea Lion Population Count top kernels.
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
PATCH = 300
H, W = img.shape[:2]
grid = np.zeros((W // PATCH + 1, H // PATCH + 1, n_classes), dtype='int16')
for x, y, cls in points:
grid[x // PATCH, y // PATCH, cls] += 1
X, Y = [], []
for i in range(W // PATCH):
for j in range(H // PATCH):
X.append(img[j*PATCH:(j+1)*PATCH, i*PATCH:(i+1)*PATCH])
Y.append(grid[i, j])
X = np.array(X); Y = np.array(Y, dtype='float32')
model = Sequential([
Conv2D(32, 3, activation='relu', padding='same', input_shape=(PATCH, PATCH, 3)),
Conv2D(64, 3, activation='relu', padding='same'), MaxPooling2D(),
Flatten(), Dense(256, activation='relu'),
Dense(n_classes, activation='linear'), # linear head — count regression
])
model.compile(loss='mse', optimizer='adam')
model.fit(X, Y, epochs=20, batch_size=32)
(grid_x, grid_y, n_classes) integer tensor(N_patches, H, W, 3) and (N_patches, n_classes)data-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF