Min-Area Mask Filtering

SkillAI & models

Removes predicted segmentation masks below a per-class minimum pixel area threshold to eliminate small false positive regions at inference time.

Available today. Use it from your connected AI after setup.

Connect ahel once, and every AI you use reads what you have installed.

Then ask your AI: use the Min-Area Mask Filtering skill

What this skill tells your AI

The instructions your AI receives, as published by wenmin-wu/ds-skills in skills/cv/min-area-mask-filtering/SKILL.md and read by ahel’s review.

Overview

Segmentation models often produce small spurious predictions — isolated pixel clusters that score above the confidence threshold but are too small to be real objects. Min-area filtering applies a per-class pixel count threshold: after binarizing the predicted mask, any connected component (or entire class mask) with fewer pixels than the threshold is zeroed out. This simple post-processing step typically improves Dice/IoU by 0.5-2% by eliminating false positives, especially in defect detection and medical imaging where true objects have known minimum sizes.

Quick Start

import numpy as np

def filter_small_masks(pred_masks, min_areas, thresholds):
    """Filter masks below per-class minimum area.

    Args:
        pred_masks: (H, W, C) float array of predicted probabilities
        min_areas: list of minimum pixel counts per class
        thresholds: list of binarization thresholds per class
    Returns:
        Filtered binary masks (H, W, C)
    """
    filtered = np.zeros_like(pred_masks, dtype=np.uint8)
    for c in range(pred_masks.shape[-1]):
        mask = (pred_masks[:, :, c] > thresholds[c]).astype(np.uint8)
        if mask.sum() < min_areas[c]:
            mask = np.zeros_like(mask)
        filtered[:, :, c] = mask
    return filtered

# Per-class thresholds and min areas (tune on validation)
thresholds = [0.5, 0.5, 0.5, 0.5]
min_areas = [600, 600, 1000, 2000]
clean_masks = filter_small_masks(raw_preds, min_areas, thresholds)

Workflow

  1. Binarize predicted probabilities with per-class confidence thresholds
  2. Count positive pixels per class
  3. Zero out any class mask with fewer pixels than its minimum area
  4. Encode cleaned masks for submission (e.g., RLE)

Key Decisions

  • Per-class thresholds: Different defect types have different minimum sizes — tune each independently
  • Min area values: Derive from training set statistics (smallest real annotation area)
  • Connected components: For finer control, filter individual connected components instead of the whole mask
  • Threshold search: Grid-search both confidence threshold and min-area on validation Dice/IoU

References

Signals

GitHub stars
60
Forks
4
Last commit
Apr 2026
Advanced
Catalog kind
skill
Gateway key
cv-min-area-mask-filtering
Source
github.com/wenmin-wu/ds-skills