MobileSAM, converted for mobilesam

MobileSAM (Zhang, Han, Qiao, Kim, Bae, Lee, Hong: Faster Segment Anything: Towards Lightweight SAM for Mobile Applications, 2023): Segment Anything's prompt encoder and mask decoder, with a TinyViT-5M image encoder distilled from SAM's ViT-H. Converted for the mobilesam Rust crate: MLX (Apple silicon GPU) and ONNX Runtime (CPU, elsewhere). The networks are unchanged; only their form is.

Source: weights/mobile_sam.pt of ChaoningZhang/MobileSAM at commit f706ad9c4eb7f219c00d9050e46328518ffb65d2 (SHA-256 6dbb90523a35330fedd7f1d3dfc66f995213d81b29a5ca8108dbcdd4e37d6c2f), Apache-2.0, by the paper's authors; its prompt encoder and mask decoder are Segment Anything's (Kirillov et al., 2023; Meta, Apache-2.0).

File Bytes SHA-256
mobilesam.mlx.safetensors 48296872 d027b7f8af2918c104ce636dd5441cb697dbda9358f06daec0dbaf04f0aa12d1
mobilesam-encoder.onnx 28016135 bcd6fc16f5269977dd195606296946ae0d138bd25962bc2407ec9d37d8e73f64
mobilesam-decoder.onnx 16493124 1b85e480f8ff176e19ff56ba6c21fbf9047c770e497291ccd1a50b05f6b3633c

What the files are

The image encoder takes the image as SAM's SamPredictor prepares it: resized so its longest side is 1024 (Pillow's bilinear), normalised with SAM's mean [123.675, 116.28, 103.53] and deviation [58.395, 57.12, 57.375] (in 0..255), and zero-padded at the right and bottom to 1024x1024. It gives the image's embedding, 256 x 64 x 64.

The decoder (prompt encoder and mask decoder) takes the embedding and a batch of prompts, each a sequence of tokens: a point's coordinates in the resized image, shifted to the pixel's centre (+0.5) and divided by 1024, with its label (-1 padding, 0 background, 1 foreground, 2 and 3 a box's top-left and bottom-right corners); a prompt without a box ends with a padding point, as in SAM. Optionally a previous 256x256 mask's logits per prompt. It gives all four of the decoder's masks (256x256 logits: the single-mask output, then the three of the multimask output) and their predicted IoUs. Selecting masks and upscaling them to the image are left to the caller.

  • mobilesam.mlx.safetensors: both networks in float32 for the mobilesam crate's Swift engine: batch norm folded into the encoder's convolutions, each attention's bias gathered into (heads, N, N), the prompt encoder's dense positional encoding computed once (prompt_encoder.dense_pe, 64 x 64 x 256), convolutions in MLX's (out, kh, kw, in) layout and transposed convolutions as (in, kh, kw, out).
  • mobilesam-encoder.onnx: opset 17, input image (1, 3, 1024, 1024), output embedding (1, 256, 64, 64).
  • mobilesam-decoder.onnx: opset 17, inputs embedding (1, 256, 64, 64), points (prompts, tokens, 2), labels (prompts, tokens), mask_input (prompts, 1, 256, 256), has_mask_input (1); outputs low_res_logits (prompts, 4, 256, 256) and iou (prompts, 4).

How they were checked

conversion/convert.py in the crate's repository made them and recorded what MobileSAM's own Python makes of three test images: embeddings, the decoder's outputs for five prompts each (points, a box, both, and a mask fed back), the predictor's masks, and the automatic mask generator's. Against those, on ONNX Runtime and on MLX (float32, Apple M5 GPU): embeddings within 7e-6, mask logits within 4e-4, scores within 5e-6; from the same decoder outputs, the crate's automatic mask generator gives SAM's masks.

Downloads last month

-

Downloads are not tracked for this model. How to track
MLX
Hardware compatibility
Log In to add your hardware

Quantized

Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support