MobileSAM tiny

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

MobileSAM tinyPromptable segmentation, 1024 × 1024 input, one box, multimask output. TinyViT encoder and native SAM decoder.LibreYOLOMobileSAM tinyPromptable segmentation, 1024 × 1024 input, one box, multimask output. TinyViT encoder and native SAM decoder.TinyViT image encoderRGB input after normalize/pad3 × 1,024 × 1,024Conv2d 3×3 / 23 to 32, 512 × 512BatchNorm2d32 × 512 × 512GELU32 × 512 × 512Conv2d 3×3 / 232 to 64, 256 × 256BatchNorm2d64 × 256 × 256MBConv, repeats=264 × 256 × 256Patch merge 1128 × 128 × 128TinyViT block, repeats=216,384 tokens × 128Patch merge 2160 × 64 × 64TinyViT block, repeats=64,096 tokens × 160Patch merge 3 (stride 1)320 × 64 × 64TinyViT block, repeats=24,096 tokens × 320Conv2d 1×1 + channel LN320 to 256, 64 × 64Conv2d 3×3 + channel LN256 to 256, p=1, 64 × 64Box 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.MBConvInput: 64 × 256 × 256Conv2d 1×164 to 256, no biasBatchNorm2d256 × 256 × 256GELU256 × 256 × 256Depthwise Conv2d 3×3256 to 256, g=256, p=1BatchNorm2d256 × 256 × 256GELU256 × 256 × 256Conv2d 1×1256 to 64, no biasBatchNorm2d64 × 256 × 256+GELU64 × 256 × 256TinyViT blockStage order in numeric lists: 128, 160, 320.Pad and partition windows128 pads to 133; 64 pads to 70LayerNorm128 / 160 / 320 channelsWindow self-attention4 / 5 / 10 heads, 32 channels per headReverse windows and crop128×128 / 64×64 / 64×64+Reshape NCHW128 / 160 / 320 channelsDepthwise Conv2d 3×3groups=128 / 160 / 320, p=1BatchNorm2d128 / 160 / 320 channelsFlatten spatial tokens16,384 / 4,096 / 4,096 tokensLayerNorm128 / 160 / 320 channelsLinear128 to 512 / 160 to 640 / 320 to 1,280GELU512 / 640 / 1,280 channelsLinear512 to 128 / 640 to 160 / 1,280 to 320+Patch mergingReshape input to NCHW64×256×256 / 128×128×128 / 160×64×64Conv2d 1×164 to 128 / 128 to 160 / 160 to 320BatchNorm2d128 / 160 / 320 channelsGELU128 / 160 / 320 channelsDepthwise Conv2d 3×3Strides 2 / 2 / 1, p=1BatchNorm2d128 / 160 / 320 channelsGELU128 / 160 / 320 channelsConv2d 1×1128 to 128 / 160 to 160 / 320 to 320BatchNorm2d128 / 160 / 320 channelsFlatten and transpose16,384×128 / 4,096×160 / 4,096×320No residual connection around patch merging.Encoder neck primitives:Conv2d 1×1320 to 256, bias=FalseChannel LayerNorm256 × 64 × 64Conv2d 3×3 / 1256 to 256, p=1, bias=FalseChannel LayerNorm256 × 64 × 64Window self-attentionQuery input49 / 196 / 49 × 128 / 160 / 320Key/value input49 / 196 / 49 × 128 / 160 / 320Linear Q128 / 160 / 320 to 128 / 160 / 320Reshape heads4 / 5 / 10 × 49 / 196 / 49 × 32Linear K128 / 160 / 320 to 128 / 160 / 320Reshape heads4 / 5 / 10 × 49 / 196 / 49 × 32Linear V128 / 160 / 320 to 128 / 160 / 320Reshape heads4 / 5 / 10 × 49 / 196 / 49 × 32MatMul Q K-transpose4 / 5 / 10 × 49 / 196 / 49 × 49 / 196 / 49ScaleDivide by sqrt(32)Position bias4 / 5 / 10 × 49 / 196 / 49 × 49 / 196 / 49+Softmax over keys4 / 5 / 10 × 49 / 196 / 49 × 49 / 196 / 49MatMul attention × V4 / 5 / 10 × 49 / 196 / 49 × 32Merge heads49 / 196 / 49 × 128 / 160 / 320Linear output128 / 160 / 320 to 128 / 160 / 320Attention bias uses absolute relative offsets.Stage 1: 361 windows of 7×7.Stage 2: 25 windows of 14×14.Stage 3: 100 windows of 7×7.All numeric lists are aligned by the three transformer stages. Their actual dimensions are written in the stage graph and definitions.Source: libreyolo/models/mobilesam/model.py. Revision a4d0ecc9e17f.libreyolo.com