Skip to content
Back to skills

Per Class Score Threshold

ASecurity

Apply class-specific confidence thresholds by inferring the dominant class per image and indexing into a per-class threshold array

  • 61 stars
  • 0 votes
  • 0 copies
  • 3 views
  • Added September 12, 2026
documentationpython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill per-class-score-threshold --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Per Class Score Threshold?

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

Security grade badge for Per Class Score Threshold
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-per-class-score-threshold/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-per-class-score-threshold)

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-per-class-score-threshold
description: Apply class-specific confidence thresholds by inferring the dominant class per image and indexing into a per-class threshold array
---

# 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

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

- **Mode vs majority**: `torch.mode` is simple; for mixed-class images, consider per-instance thresholds instead
- **Threshold tuning**: sweep thresholds per class on validation set, optimize for mAP@IoU
- **Homogeneous assumption**: works best when images contain mostly one class; for mixed scenes, apply per-instance class thresholds
- **Min-area coupling**: pair with per-class min_pixels to also filter by class-specific size

## References

- [Positive score with Detectron 3/3 - Inference](https://www.kaggle.com/code/slawekbiel/positive-score-with-detectron-3-3-inference)

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…