Per class score threshold

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

Per class score threshold is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Apply class-specific confidence thresholds by inferring the dominant class per image and indexing into a per-class threshold array

How to use this skill

Skill file

Per-Class Score Threshold

Overview

Different object classes have different score distributions — small dense cells score lower than large isolated ones. A single global threshold under-filters easy classes and over-filters hard ones. Infer the dominant class per image (mode of predicted classes), then apply that class's optimized threshold. Common in cell segmentation where cell types have very different morphologies.

Quick Start

import torch
import numpy as np

THRESHOLDS = [0.15, 0.35, 0.55]  # per class, tuned on validation
MIN_PIXELS = [75, 150, 75]        # per class minimum area

def filter_predictions(predictions):
    scores = predictions['scores']
    classes = predictions['pred_classes']
    masks = predictions['pred_masks']

    # Infer dominant class for this image
    dominant_class = torch.mode(classes)[0].item()

    # Apply class-specific threshold
    keep = scores >= THRESHOLDS[dominant_class]
    return masks[keep], scores[keep], dominant_class

masks, scores, cls = filter_predictions(output['instances'])

Workflow

  1. Run inference to get per-instance scores, classes, and masks
  2. Compute the mode of predicted classes to determine dominant image class
  3. Index into per-class threshold and min-area arrays
  4. Filter predictions by the class-specific threshold
  5. Optionally apply class-specific min-area filtering post-overlap-resolution

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.