skills/cv/class-density-threshold-patch-sampling/SKILL.md
Sample training patches from large images using per-class area-fraction thresholds to ensure each patch contains meaningful object coverage
npx skillsauth add wenmin-wu/ds-skills cv-class-density-threshold-patch-samplingInstall 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.
Large satellite or histopathology images are too big to feed directly into a CNN. Random cropping produces mostly empty patches for rare classes. Instead, set a minimum area-fraction threshold per class — a patch is accepted only if at least one class exceeds its threshold. Rare classes get lower thresholds (0.1%) to capture sparse features; common classes get higher thresholds (40%) to filter noise.
import numpy as np
import random
def sample_patches(image, mask, patch_size, n_patches, thresholds):
h, w = image.shape[:2]
patches_img, patches_msk = [], []
area = patch_size * patch_size
attempts = 0
while len(patches_img) < n_patches and attempts < n_patches * 20:
x = random.randint(0, h - patch_size)
y = random.randint(0, w - patch_size)
msk_patch = mask[x:x+patch_size, y:y+patch_size]
for cls_idx, thresh in enumerate(thresholds):
if msk_patch[:, :, cls_idx].sum() / area > thresh:
patches_img.append(image[x:x+patch_size, y:y+patch_size])
patches_msk.append(msk_patch)
break
attempts += 1
return patches_img, patches_msk
thresholds = [0.4, 0.1, 0.1, 0.15, 0.3, 0.05, 0.1, 0.05, 0.001, 0.005]
imgs, msks = sample_patches(image, mask, 256, 5000, thresholds)
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