SAM base

Click a block to read its description, or select it with Tab and Enter.

SAM basePromptable segmentation, 1024 × 1024 input, one box, multimask output. Image memory and video tracking are outside this graph.LibreYOLOSAM basePromptable segmentation, 1024 × 1024 input, one box, multimask output. Image memory and video tracking are outside this graph.Image encoderInput after resize/pad3 × 1,024 × 1,024Conv2d 16×16 / 1664 × 64 × 768, NHWCAdd learned absolute position64 × 64 × 768Window/global ViT blocks64 × 64 × 768, repeats=12Permute to NCHW768 × 64 × 64Conv2d 1×1768 to 256, bias=FalseChannel LayerNorm256 × 64 × 64Conv2d 3×3 / 1256 to 256, p=1, bias=FalseChannel LayerNorm256 × 64 × 64Global blocks (1-based): 3, 6, 9, 12Other blocks use 14 × 14 windows.No CLS token in the image encoder.Box prompt encoderOne box2 corner coordinates × 2Shift pixel centersAdd 0.5 to x and yNormalize coordinatesDivide by 1024, map to [-1,1]MatMul random Fourier matrix2 coords to 128 frequenciesMultiply by 2 pi2 × 128Sin2 × 128Cos2 × 128Concat sin and cos2 × 256Corner type2 learned × 256+Sparse prompt embeddings2 × 256Learned no-mask embeddingBroadcast to 256 × 64 × 64Selected input: one box, no mask prompt.Points use the same Fourier encoding, pluspositive/negative or not-a-point embeddings.Dense image position: this encoding evaluatedon the centers of the 64 × 64 feature grid.Mask decoder inputsImage embedding256 × 64 × 64Dense prompt256 × 64 × 64+Flatten image features4,096 × 256Learned output tokensIoU + 4 masks = 5 × 256Sparse prompts2 × 256Concat output and prompt tokens7 × 256Two-way block 17 queries, 4,096 image tokensTwo-way block 27 queries, 4,096 image tokensFinal token-to-image attentionQ+Q0, K+P, V=updated image+Final LayerNorm on queries7 × 256Q0 is the original token sequence.P is fixed Fourier image position.4,096 image tokens feed block 1 keys/values.Masks and quality outputsUpdated query tokens1 IoU + 4 mask tokensUpdated image tokens4,096 × 256Reshape to image256 × 64 × 64ConvTranspose2d 2×2 / 2256 to 64; 64 × 128 × 128Channel LayerNorm64 × 128 × 128GELU64 × 128 × 128ConvTranspose2d 2×2 / 264 to 32; 32 × 256 × 256GELU32 × 256 × 256Select four mask tokens4 × 256Four independent Linear layersEach 256 to 256ReLU4 × 256Four independent Linear layersEach 256 to 256ReLU4 × 256Four independent Linear layersEach 256 to 32MatMul mask coefficients × upscaled image4 × 32 times 32 × 65,536; reshape to 4 × 256 × 256Select masks 1 to 33 × 256 × 256IoU token Linear256 to 256ReLU256Linear256 to 256ReLU256Linear256 to 4 scoresSelect scores 1 to 33 quality scoresMask logits are resized to the input canvas,then unpadded/resized to the original image.multimask_output=True is drawn.False selects mask/score 0 instead.Two-way block 1Query state7 × 256Image state4,096 × 256Token self-attention8 heads, 32 channels per headFirst self-attention has no PE or residual.LayerNorm7 × 256Original tokens Q07 × 256+Image position P4,096 × 256+Token-to-image attentionQ=7, K/V=4,096; 8 heads × 16+LayerNorm7 × 256Linear256 to 2,048ReLU7 × 2,048Linear2,048 to 256+LayerNorm7 × 256Original tokens Q07 × 256+Image-to-token attentionQ=4,096, K/V=7; 8 heads × 16+LayerNorm on image state4,096 × 256Updated queries: 7 × 256Two-way block 2Query state7 × 256Image state4,096 × 256Token self-attention8 heads, 32 channels per headOriginal tokens Q07 × 256+Self-attention Q/K include Q0.+LayerNorm7 × 256Original tokens Q07 × 256+Image position P4,096 × 256+Token-to-image attentionQ=7, K/V=4,096; 8 heads × 16+LayerNorm7 × 256Linear256 to 2,048ReLU7 × 2,048Linear2,048 to 256+LayerNorm7 × 256Original tokens Q07 × 256+Image-to-token attentionQ=4,096, K/V=7; 8 heads × 16+LayerNorm on image state4,096 × 256Updated queries: 7 × 256Token self-attentionQuery input7 × 256Key/value input7 × 256Linear Q256 to 256Reshape heads8 × 7 × 32Linear K256 to 256Reshape heads8 × 7 × 32Linear V256 to 256Reshape heads8 × 7 × 32MatMul Q K-transpose8 × 7 × 7ScaleDivide by sqrt(32)Softmax over keys8 × 7 × 7MatMul attention × V8 × 7 × 32Merge heads7 × 256Linear output256 to 256Token-to-image attentionQuery input7 × 256Key/value input4096 × 256Linear Q256 to 128Reshape heads8 × 7 × 16Linear K256 to 128Reshape heads8 × 4096 × 16Linear V256 to 128Reshape heads8 × 4096 × 16MatMul Q K-transpose8 × 7 × 4096ScaleDivide by sqrt(16)Softmax over keys8 × 7 × 4096MatMul attention × V8 × 7 × 16Merge heads7 × 128Linear output128 to 256Image-to-token attentionQuery input4096 × 256Key/value input7 × 256Linear Q256 to 128Reshape heads8 × 4096 × 16Linear K256 to 128Reshape heads8 × 7 × 16Linear V256 to 128Reshape heads8 × 7 × 16MatMul Q K-transpose8 × 4096 × 7ScaleDivide by sqrt(16)Softmax over keys8 × 4096 × 7MatMul attention × V8 × 4096 × 16Merge heads4096 × 128Linear output128 to 256Decoder conventionsQ/K add positional terms; V is the raw state.Final attention repeats token-to-image attention.Its output is added to queries, then normalized.Four mask tokens have independent MLP weights.IoU token uses a separate three-layer MLP.All decoder Linear projections include bias.Prompt input for this view is one box.Dense prompt is the learned no-mask embedding.Point prompts use Fourier position with apositive, negative or padding type embedding.Encode once and cache the image features;new prompts rerun prompt encoder and decoder.Image ViT blockInput: 64 × 64 × 768LayerNorm64 × 64 × 768Window partition when localPad 64 to 70; 25 windows of 14 × 14Image self-attention12 heads; local 196 / global 4,096 tokensReverse windows and crop64 × 64 spatial grid+LayerNorm64 × 64 × 768Linear768 to 3072GELU64 × 64 × 3072Linear3072 to 768+Local image attentionQuery input196 × 768Key/value input196 × 768Linear Q768 to 768Reshape heads12 × 196 × 64Linear K768 to 768Reshape heads12 × 196 × 64Linear V768 to 768Reshape heads12 × 196 × 64MatMul Q K-transpose12 × 196 × 196ScaleDivide by sqrt(64)Position bias12 × 196 × 196+Softmax over keys12 × 196 × 196MatMul attention × V12 × 196 × 64Merge heads196 × 768Linear output768 to 768Global image attentionQuery input4096 × 768Key/value input4096 × 768Linear Q768 to 768Reshape heads12 × 4096 × 64Linear K768 to 768Reshape heads12 × 4096 × 64Linear V768 to 768Reshape heads12 × 4096 × 64MatMul Q K-transpose12 × 4096 × 4096ScaleDivide by sqrt(64)Position bias12 × 4096 × 4096+Softmax over keys12 × 4096 × 4096MatMul attention × V12 × 4096 × 64Merge heads4096 × 768Linear output768 to 768Decomposed relative positionQuery spatial gridHead width 64Relative H table27 / 127 distances × 64Gather offsets14×14 / 64×64 coordinate pairsEinsum query × HQuery-dependent axis biasRelative W table27 / 127 distances × 64Gather offsets14×14 / 64×64 coordinate pairsEinsum query × WQuery-dependent axis bias+Broadcast axis biases before addition.Per-head bias: 196×196 locally;4,096×4,096 in global blocks.Variant E n h Mbase: 768, 12, 12, 3072large: 1024, 24, 16, 4096huge: 1280, 32, 16, 5120Global indices (1-based):base: 3, 6, 9, 12large: 6, 12, 18, 24huge: 8, 16, 24, 32Source: libreyolo/models/sam/model.py. Revision a4d0ecc9e17f.libreyolo.com