Skip to content
Back to skills

Class Density Threshold Patch Sampling

ASecurity

Sample training patches from large images using per-class area-fraction thresholds to ensure each patch contains meaningful object coverage

  • 61 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 12, 2026
toolspython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill class-density-threshold-patch-sampling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Class Density Threshold Patch Sampling?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Class Density Threshold Patch Sampling
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-class-density-threshold-patch-sampling/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-class-density-threshold-patch-sampling)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: cv-class-density-threshold-patch-sampling
description: Sample training patches from large images using per-class area-fraction thresholds to ensure each patch contains meaningful object coverage
---

# 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

```python
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

- **Threshold per class**: rare classes need low thresholds (0.001); dominant classes need higher (0.3-0.5)
- **Max attempts**: cap retries to avoid infinite loops on empty regions
- **vs weighted sampling**: density thresholds are simpler and guarantee minimum object coverage
- **Patch size**: must be large enough to capture spatial context (128-512 typical for satellite)

## References

- [SegNet_dstl](https://www.kaggle.com/code/ksishawon/segnet-dstl)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…