Class density threshold patch sampling

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

Class density threshold patch sampling is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Sample training patches from large images using per-class area-fraction thresholds to ensure each patch contains meaningful object coverage

How to use this skill

Skill file

Class-Density Threshold Patch Sampling

Overview

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.

Quick Start

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)

Workflow

  1. Define per-class area-fraction thresholds based on class frequency
  2. Randomly crop a candidate patch from the full image
  3. Check if any class exceeds its threshold in the patch
  4. Accept the patch if yes, reject and retry if no
  5. Apply augmentation to accepted patches before saving

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.