Image understanding, generation, and 3D reconstruction · Semantic, instance and promptable segmentation · VIS015 · Model
Segment Anything Model (SAM, original image model)
Original SAM is a promptable image segmentation model: points, boxes or masks condition one or several candidate masks.
In CodeSplash
Documented in the Academy only. There's no Set up button.
- Availability
- Explained here
- Code example
- Component demo, not a trained model
- Who ran it
- CodeSplash's run and the reference's record
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Original SAM is a promptable image segmentation model: points, boxes or masks condition one or several candidate masks. It provides reusable visual features and an interactive segmentation interface, but does not automatically assign semantic class names or replace the distinct SAM2 video system.
Question / hypotheses
Given a reproducible prompt, which candidate mask matches the intended object, and how reliably can the model select it? This is a modeling/evaluation question, not a formal null-hypothesis test.
Architecture / computational structure
A substantial image encoder computes reusable image embeddings. A prompt encoder represents sparse points/boxes and dense masks; a lightweight mask decoder combines prompt and image features and emits candidate masks with predicted quality scores. Multiple outputs address ambiguous prompts.
Assumptions
Prompt meaning, coordinate transforms and object ambiguity must be explicit. Predicted quality is not a calibrated probability of clinical, geographic or scientific correctness. Domain shift and very small, transparent or ambiguous regions can impair masks.
Formula / computational definition
. Training combines focal and Dice mask losses plus a quality-prediction loss; for ambiguity-aware outputs, the best matching candidate receives the relevant mask supervision. Here is an image, a prompt and predicted mask quality.
Parameters and notation
Notation: E_I and E_P are image and prompt encoders, D_theta the learned decoder, K the number of candidates, M-hat_k a mask and q-hat_k its predicted quality. Ground-truth IoU is available for evaluation/training, not for automatic deployment-time oracle selection.
Learned or estimated: Image/prompt encoder weights, mask-decoder attention and mask-output heads, mask tokens and quality head. A user prompt is an input, not a learned model parameter.
Hyperparameters and effects: Checkpoint size, image resizing, prompt type/count, multimask selection, mask threshold and adaptation learning rates. Prompt placement can dominate outcome; increasing prompts trades annotation effort for constraints and does not ensure semantic correctness.
Fitting, fine-tuning, and adaptation
Benchmark the frozen checkpoint with a reproducible prompt protocol first. For target adaptation, train mask-decoder parameters or an explicitly implemented adapter while freezing the image encoder; only unfreeze encoder blocks at lower rates if target validation supports it. Train with target masks and prompt simulations matching deployment; monitor source-domain prompt performance for forgetting.
How to use and calibrate
Split by independent image source, scene, subject or capture sequence before augmentation; keep near-duplicate frames and crops in the same split. Report IoU/Dice versus prompt count and placement, boundary quality, failure rate and per-domain performance. Separate oracle selection using ground truth from selection by the model quality head; oracle best-of-K results are not deployable performance.
Example application
Assist an annotator in outlining objects from point or box prompts on still images. This is a proposed use case, not a claim of validated performance on supplied real data.
Interpretation / pitfalls
Watch out
A training loss, confidence score or attention map is not a p-value or causal explanation. Confidence calibration, annotation quality and distribution-shift performance need separate assessment. A correct component calculation does not validate the full architecture, checkpoint or deployment pipeline.
Python and R code examples
Scope: SAM-style selection among ambiguous candidate masks using predicted quality, contrasted with ground-truth oracle selection; no SAM weights or prompt decoder. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.
Full-model route: Python: official Segment Anything implementation. R: labeled reticulate bridge for the real checkpoint, or custom native torch. The example demonstrates candidate-mask quality selection versus oracle IoU; it does not emulate the pretrained encoder.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Component demo, not a trained model — SAM-style selection among ambiguous candidate masks using predicted quality, contrasted with ground-truth oracle selection; no SAM weights or prompt decoder. Use the shared setup.
Shared setup for this chapter
import numpy as np
from itertools import permutations
def softmax(x):
x=np.asarray(x,dtype=float)
e=np.exp(x-x.max(axis=-1,keepdims=True))
return e/e.sum(axis=-1,keepdims=True)
def bilinear(a,y,x):
y=float(np.clip(y,0,a.shape[0]-1)); x=float(np.clip(x,0,a.shape[1]-1))
y0=int(np.floor(y)); x0=int(np.floor(x))
y1=min(y0+1,a.shape[0]-1); x1=min(x0+1,a.shape[1]-1)
dy=y-y0; dx=x-x0
return (1-dy)*((1-dx)*a[y0,x0]+dx*a[y0,x1])+dy*((1-dx)*a[y1,x0]+dx*a[y1,x1])
masks=np.array([[1,1,0,0],[1,0,0,0],[1,1,1,0]],bool)
truth=np.array([1,1,0,0],bool)
quality=np.array([.75,.8,.6])
iou=np.logical_and(masks,truth).sum(axis=1)/np.logical_or(masks,truth).sum(axis=1)
selected=int(quality.argmax()); oracle=int(iou.argmax())
assert selected!=oracle and iou[selected]<iou[oracle]
print('predicted quality:',quality,'actual IoU:',iou,'selected/oracle (zero-based):',selected,oracle)
Component demo, not a trained model — SAM-style selection among ambiguous candidate masks using predicted quality, contrasted with ground-truth oracle selection; no SAM weights or prompt decoder. Use the shared setup.
Shared setup for this chapter
softmax <- function(x) {
if(is.null(dim(x))) {e<-exp(x-max(x)); return(e/sum(e))}
t(apply(x,1,function(v) {e<-exp(v-max(v)); e/sum(e)}))
}
bilinear <- function(a,y,x) {
y<-min(max(y,0),nrow(a)-1); x<-min(max(x,0),ncol(a)-1)
y0<-floor(y); x0<-floor(x); y1<-min(y0+1,nrow(a)-1); x1<-min(x0+1,ncol(a)-1)
dy<-y-y0; dx<-x-x0
(1-dy)*((1-dx)*a[y0+1,x0+1]+dx*a[y0+1,x1+1])+dy*((1-dx)*a[y1+1,x0+1]+dx*a[y1+1,x1+1])
}
masks<-matrix(c(1,1,0,0,1,0,0,0,1,1,1,0),3,byrow=TRUE)>0
truth<-c(TRUE,TRUE,FALSE,FALSE); quality<-c(.75,.8,.6)
iou<-apply(masks,1,function(m) sum(m&truth)/sum(m|truth))
selected<-which.max(quality); oracle<-which.max(iou)
stopifnot(selected!=oracle,iou[selected]<iou[oracle])
print(list(predicted_quality=quality,actual_IoU=iou,selected_oracle_zero_based=c(selected,oracle)-1))
Sources
- Source 1arxiv.org
- Source 2github.com
From the CodeSplash reference, entry VIS015.