Open-Set Self-Prompting for Segment Anything

Segment Anything (SAM) is a large image segmenter that draws precise object boundaries when a person gives it a click or a box. Many groups now replace the person with a small trained network that writes these prompts automatically. These self-prompters share one flaw. They assume every image contains only the classes they were trained on. When a scan contains an organ or lesion the prompter has never seen, it does not abstain. It prompts SAM on the nearest familiar-looking region, and SAM returns a confident, clean mask with the wrong label. This proposal builds a self-prompter that does three things: it writes prompts for known structures, it refuses to write a known-class prompt for regions that fall outside every known class, and it hands those unfamiliar regions to SAM as "unknown object" masks.

The central claim is about why current self-prompters behave this way, and it is testable. A prompter learns from two signals. The first is the segmentation loss, passed back through SAM's frozen mask decoder. The second is whatever keeps its embedding space class-specific. SAM's decoder does not care about classes, so we hypothesize that the segmentation gradient pushes the prompter's features toward generic "this is an object with a boundary" directions. That gradient conflicts with the gradient that keeps classes compact and well separated, and the conflict wears away the distance gap that would let the model reject unknowns. If the hypothesis holds, a simple asymmetric gradient projection inside the prompter should recover open-set rejection without costing known-class accuracy, and it should do better than detaching or reweighting the losses.

Core Claim and Positioning

Three prototype-driven SAM prompters are the closest work, and all three are closed-set. A prototype here means a class's average embedding; a query region is classified by its distance to these averages. ProtoSAM computes prototypes in DINOv2 feature space from one support image, thresholds a similarity map, and prompts SAM from the single most confident connected component. It therefore assumes the target is present and fails on multi-focal targets. PGP-SAM learns organ prototypes that it converts into dense and sparse prompts, but it trains all organs jointly on a fixed label set and never rejects anything. UNICL-SAM models uncertainty in noisy support examples and discovers part prototypes with a fixed cluster count, again without any notion of an unprompted class. These papers supply our architecture template. Our proposal differs in three ways: it adds rejection, it prompts every component, and it identifies the training conflict that we argue causes closed-set overconfidence.

Prior open-set work supplies the rejection geometry. P-ODN learns a radius around each class prototype so that empty feature space is reserved for unknowns. Kernel Null Space Methods for Novelty Detection scores novelty as the distance to the nearest collapsed class point. Neither method interacts with a promptable segmenter. Revisiting Open-Set Panoptic Segmentation shows that a class-agnostic "is this a thing" head finds unlabeled objects. We reuse that idea as a foreground head, but we let SAM produce the masks instead of retraining a full CNN.

Gradient-conflict work supplies the fix. ProGrad protects CLIP's zero-shot knowledge by removing the part of the task gradient that opposes a guidance gradient. CAGrad and PCGrad treat the competing tasks symmetrically. Our projection follows ProGrad's asymmetric form, but the quantity it protects is different: open-set geometry rather than zero-shot predictions.

Two findings motivate the conflict hypothesis without proving it. Chaitanya et al. found that global-context and local-boundary objectives interfere enough to force staged training. ProtoSAM reports that encoder fine-tuning helps CT and MRI but hurts polyps. KDPH shows that point prototypes oscillate under conflicting pulls. This matters because our prototypes may need more than one mode per class, so we allow a small number of sub-prototypes per class and ablate that number.

Research Program

The model keeps SAM's image encoder and mask decoder frozen. The trainable parts are four:

  1. A lightweight prompt generator with a shared trunk over SAM encoder features. It is AutoSAM-style, after AutoSAM, which produces continuous prompt embeddings.
  2. A per-pixel embedding head.
  3. A bank of $M$ sub-prototypes per known class, each with a learned radius.
  4. A class-agnostic foreground head trained on the union of all known masks.

At inference the model thresholds each class similarity map and splits it into connected components. Every component, not just the most confident one, is pooled into an embedding and compared with the prototypes. A component within its class radius becomes a continuous prompt for that class. A foreground component outside every radius becomes an "unknown" prompt, and SAM delineates it without assigning a class. Pixels of held-out classes appear only as background during training, so the open-set test is realistic.

Training uses three losses:

  1. A segmentation loss $L_{seg}$ (Dice plus cross-entropy through SAM's decoder on known masks).
  2. A metric loss $L_{met}$ (prototype-distance cross-entropy plus a P-ODN-style compactness term).
  3. A foreground loss $L_{fg}$.

Let $g_{seg}$ and $g_{met}$ be the gradients of $L_{seg}$ and $L_{met}$ with respect to the shared trunk parameters. When the two gradients point in opposing directions, we remove the conflicting part of the segmentation gradient:

$$ \tilde g_{seg} = g_{seg} - \frac{\min(0,\; g_{seg}\cdot g_{met})}{\lVert g_{met}\rVert^2}\, g_{met}. $$

The trunk is then updated with $\tilde g_{seg} + g_{met} + g_{fg}$, where $g_{fg}$ is the foreground-loss gradient. Heads outside the trunk receive their own gradients unchanged.

We sacrifice $g_{seg}$ rather than $g_{met}$ for a specific reason. SAM's frozen decoder already handles boundary precision, so the prompter can afford to lose the class-agnostic component of the segmentation signal. This is a hypothesis, and the program tests it against symmetric projection. Because SAM is frozen, computing the two gradients costs two backward passes through a small network and SAM's lightweight decoder.

The first deliverable is diagnostic. During ordinary joint training, we will log the per-step cosine between $g_{seg}$ and $g_{met}$ on the trunk. In parallel, we will track the open-set separation gap: the distance from unknown components to the nearest prototype minus the distance from known components to their own prototype. The hypothesis predicts two things: frequent negative cosines, and a separation gap that shrinks in step with them.

Baselines

The naive reference is the same self-prompter trained with summed losses and no rejection. It is scored for open-set behavior by thresholding its maximum class similarity, so it shows how badly closed-set prompting fails on held-out organs.

The strongest closed-set competitor is PGP-SAM, which reports 78.75% mean Dice on Synapse using 10% of 2D slices, against 73.91% for SAMed (PGP-SAM). We will retrain it on our known-organ subsets with the same SAM backbone, slice budget, and number of tuning trials. We will equip it with two post-hoc open-set scores: maximum softmax probability and an energy score. For one-shot comparisons on Abd-CT and polyps, ProtoSAM reports 73.45 mean Dice on Abd-CT and 68.06 on polyp benchmarks (ProtoSAM). We will run it with its native single-component prompting and with our all-components extension, so that we can separate the effect of multi-component prompting from the effect of rejection.

The strongest open-set competitor we can construct runs SAM's automatic grid prompting to propose all masks. It then embeds each mask with the same encoder and classifies it with P-ODN-style prototype radii. This baseline uses no learned prompter at all. For Synapse with held-out organs, no reported result was found on Lacuna, so all open-set numbers will be produced by us under shared splits.

Training-rule ablations isolate the key ingredient while keeping architecture, data, and tuning budget fixed. They are:

  1. Summed losses.
  2. The best loss weight from a grid of equal trial count.
  3. A detached variant, where the segmentation loss does not reach the trunk.
  4. Two-stage training (metric first, then segmentation).
  5. Symmetric PCGrad.
  6. CAGrad.
  7. Our asymmetric projection.

Beating the field means two things together. Known-class Dice must stay within one point of the best closed-set competitor in the full eight-organ setting, where the reference is PGP-SAM's 78.75. At the same time, component-level open-set AUROC and the rate of unknown pixels mislabeled as known organs must improve over every alternative across all held-out folds.

Why not just…?

Why not just threshold the confidence of a fine-tuned closed-set prompter? Softmax-style scores give no guarantee of unallocated space for novel inputs. Kernel Null Space Methods for Novelty Detection reports a median AUC gain of over 5% from distance-based scoring over one-vs-rest classifiers. P-ODN reports 89.50% F-score against 82.59% for OSDN. Neither result comes from segmentation, so the PGP-SAM-plus-energy baseline settles this directly.

Why not just let SAM propose every object and classify the masks afterward? This is the most serious threat, because it gets objectness from SAM for free. It has two weaknesses. AutoSAM reports that SAM with ground-truth point prompts reaches only 29.65 Dice on MoNuSeg, so SAM's raw proposals may be poor on medical images. Grid prompting is also slow. If this baseline matches us on open-set AUROC and known Dice, then the learned prompter adds only speed, and we will report that honestly along with the measured latency.

Why not just detach the segmentation loss or tune loss weights? Detaching removes the conflict trivially, but the trunk then never learns which prompts SAM delineates well. Our prediction is that known Dice drops, while weighting buys rejection only by giving up Dice. CAGrad reports that scalar weighting leaves a 5.59% multi-task drop on NYU-v2 against 0.20%, but that is a different domain. If detaching matches projection on both axes here, the gradient-routing contribution collapses to "do not backpropagate segmentation into the trunk," and we would report that simpler recipe as the finding.

Proposed Experiments

The primary benchmark is Synapse with organ hold-out. We train on five organs and treat three as unknowns, rotating over three folds. Metrics are known-organ Dice, component-level AUROC, false known-label rate on held-out organ pixels, and unknown-mask Dice. A full eight-organ closed-set run checks parity with PGP-SAM. Abd-CT and Abd-MRI test the one-shot regime against ProtoSAM. Kvasir-SEG, CVC-ClinicDB, ColonDB, and ETIS, restricted to images with several polyps, measure per-lesion recall, which is the multi-component failure ProtoSAM leaves open. As a cross-domain check, we train on COCO known classes and evaluate unseen classes on LVIS-PS with $PQ^*$, where the thing-agnostic method reports 19.80 (Revisiting Open-Set Panoptic Segmentation).

Three checks decide the outcome:

  1. Is there conflict at all? If gradient cosines on the trunk are rarely negative under joint training, the conflict hypothesis is false. The work then reduces to open-set self-prompting with ordinary training.
  2. Does conflict predict lost separation? Across seeds and loss weights, the fraction of conflicting steps should correlate negatively with the final open-set separation gap.
  3. Does the fix work as the hypothesis says? Asymmetric projection must beat detaching, best-weighted training, and symmetric projection on open-set AUROC at matched known Dice. It should also beat symmetric projection specifically, which is the evidence that the segmentation gradient is the one to sacrifice.

The number of sub-prototypes per class, from one to four, is ablated because single prototypes may oscillate in the way KDPH documents.