Image understanding, generation, and 3D reconstruction · Object detection · VIS009 · Architecture
Mask R-CNN
Mask R-CNN adds per-instance segmentation to a region-based object detector. Its RoIAlign operation avoids coarse coordinate quantization so that region features remain spatially aligned for mask prediction.
In CodeSplash
A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.
- Instance segmentation (Mask R-CNN, COCO) Experimental
Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.
- Availability
- Runs in CodeSplash (Experimental)
- 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
Mask R-CNN adds per-instance segmentation to a region-based object detector. Its RoIAlign operation avoids coarse coordinate quantization so that region features remain spatially aligned for mask prediction.
Question / hypotheses
Can aligned region features produce accurate separate object masks in addition to boxes and classes? This is a modeling/evaluation question, not a formal null-hypothesis test.
Architecture / computational structure
A Faster-R-CNN-style backbone/RPN generates regions. RoIAlign supplies fixed-resolution features to class/box heads and a parallel fully convolutional mask head. Masks are predicted for each instance rather than only one semantic label per pixel.
Assumptions
Bounding-box and mask annotation conventions must agree. Crowded/occluded instances and tiny regions challenge assignment; a well-localized box does not imply an accurate boundary. RoIAlign alignment flags vary between implementations.
Formula / computational definition
. For a positive region of true class k, is pixelwise binary cross-entropy on that class’s mask. RoIAlign evaluates continuous feature coordinates by bilinear interpolation before pooling.
Parameters and notation
Notation: L_cls and L_box are detector classification/localization losses. L_mask is a binary per-pixel mask loss for a matched positive region; k chooses the labeled class’s mask channel. Continuous feature coordinates are obtained by the declared image-to-feature scaling convention.
Learned or estimated: Backbone/RPN, class and box heads, mask decoder and any feature-pyramid parameters.
Hyperparameters and effects: Mask resolution, RoIAlign alignment/sampling settings, region matching, class-specific versus declared agnostic masks, loss weights and detection thresholds. Small masks can lose narrow boundaries.
Fitting, fine-tuning, and adaptation
Replace class/box/mask heads for new categories, train them with the backbone initially frozen, then unfreeze higher stages at lower rates. Use target mask annotations and validate boundary behavior. Retain source-category checks and audit resizing/coordinate transforms when exporting or changing implementations.
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 both box and mask AP, boundary quality, performance by object size/occlusion, and instance-count errors. Evaluate mask metrics separately from semantic IoU and include proposal/postprocessing costs.
Example application
Segment individual cells or objects while retaining separate identities for touching instances. 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: Continuous bilinear sampling used inside RoIAlign and binary mask cross-entropy for the selected class; not a full multi-bin RoIAlign implementation. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.
Full-model route: Python: official or verified detection frameworks. R: native torch reduced components or bridge. The example illustrates continuous RoI sampling and class-selected binary mask loss, not the complete detector.
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 — Continuous bilinear sampling used inside RoIAlign and binary mask cross-entropy for the selected class; not a full multi-bin RoIAlign implementation. 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])
feature=np.arange(16,dtype=float).reshape(4,4)
sample=bilinear(feature,1.25,1.75)
mask_prob=np.array([.9,.2,.7,.1]); mask_target=np.array([1.,0.,1.,0.])
bce=-np.mean(mask_target*np.log(mask_prob)+(1-mask_target)*np.log1p(-mask_prob))
quantized=feature[1,2]
assert np.isclose(sample,6.75) and not np.isclose(sample,quantized)
print('continuous sample:',sample,'quantized:',quantized,'selected-class mask BCE:',bce)
Component demo, not a trained model — Continuous bilinear sampling used inside RoIAlign and binary mask cross-entropy for the selected class; not a full multi-bin RoIAlign implementation. 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])
}
feature<-matrix(0:15,4,byrow=TRUE)
sample<-bilinear(feature,1.25,1.75)
mask_prob<-c(.9,.2,.7,.1); mask_target<-c(1,0,1,0)
bce<--mean(mask_target*log(mask_prob)+(1-mask_target)*log1p(-mask_prob))
quantized<-feature[2,3]
stopifnot(abs(sample-6.75)<1e-12,abs(sample-quantized)>1e-12)
print(list(continuous_sample=sample,quantized=quantized,selected_class_mask_BCE=bce))
Sources
- Source 1arxiv.org
- Source 2github.com
From the CodeSplash reference, entry VIS009.