Back to paper library
arXiv 2023SegmentationPaper notes

MobileSAM

Just Swap the Engine

Original paper

Faster Segment Anything: Towards Lightweight SAM for Mobile Applications

Paper authors

Chaoning Zhang · Dongshen Han · Yu Qiao …

Show all 7 authors

Chaoning Zhang · Dongshen Han · Yu Qiao · Jung Uk Kim · Sung-Ho Bae · Seungkyu Lee · Choong Seon Hong

Publication
Publication venue not yet verified
First submitted to arXiv
arXiv:2306.14289
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{segmentationmobilesam2023,
  title = {{Faster Segment Anything: Towards Lightweight SAM for Mobile Applications}},
  author = {Chaoning Zhang and Dongshen Han and Yu Qiao and Jung Uk Kim and Sung-Ho Bae and Seungkyu Lee and Choong Seon Hong},
  year = {2023},
  eprint = {2306.14289},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2306.14289}
}
On this page

After SAM came out, many people wanted to squeeze it into phones, browsers or annotation tools, and they quickly got stuck in the same place.

SAM is built in two stages. A ViT image encoder turns the whole image into features, and a very small mask decoder then cuts out a mask based on where the user clicked or the box they drew. The decoder has only 3.87M parameters and runs fine in a browser; the image encoder, however, is a ViT-H with 632 million parameters that takes nearly half a second per image on a GPU.

To swap in a small encoder, the most direct route is to retrain it with SAM's original pipeline. But as the SAM paper states plainly, the ViT-H version took 256 A100s for 68 hours, and even ViT-B still needs 128 GPUs for several days.

Less than three months after SAM's release, Chaoning Zhang and colleagues at Kyung Hee University in South Korea proposed MobileSAM. Their idea was not to retrain SAM at all, but just to teach a small encoder to imitate the large encoder's output.

Defining the Problem

The authors treat "training a SAM with a small encoder" as knowledge distillation. The 1.1 billion masks in SA-1B were generated by SAM itself in the first place, so training a smaller SAM on them is essentially having a student imitate a teacher.

The problem is that in the official pipeline, the small encoder and the decoder are trained together. The authors see this coupled optimization as the very source of the difficulty. How well the encoder learns depends on the decoder, and how well the decoder learns depends on the encoder; when both start out poor, it's hard to pull them into a good state together.

Solving the Problem

Diagram drawn for this note, comparing three ways to distill SAM into a small encoder in three columns. (a) Coupled: Image + prompt goes into a small encoder and then a mask decoder, both trained, and the output mask is matched to the teacher's masks; the note reads "Both learn from a bad start." (b) Semi-coupled: the same pipeline, but the mask decoder is copied from SAM and frozen; the note reads "Random prompts, moving target." (c) Decoupled (MobileSAM): the image goes separately into a frozen ViT-H teacher and a TinyViT student being trained, both output 64×64×256 features, and MSE aligns the two; the student's features connect to a mask decoder reused as is, and the note reads "Features only: no masks, no prompts."
Drawn for this note from the three distillation setups in Section 3.2 of the paper.

The authors undo this coupling one step at a time.

The first step is semi-coupled distillation. SAM's decoder is already small and doesn't need redesigning, so it's copied straight from the original and frozen, and only the small encoder is trained. That way, at least the decoder can't be dragged down by an encoder that's still bad.

But the authors say this is still hard to train. The prompts given during training are random every time; on the same image, one time it's a click on the left, the next a box on the right, so the mask the decoder outputs keeps changing, and so does the target the encoder has to chase.

The second step is the decoupled distillation that MobileSAM adopts. The decoder is bypassed entirely, and the student encoder directly imitates ViT-H's output features. SAM's image features are a 64×64 map with 256 channels, and the student only has to bring its own output as close as possible to the teacher's map, so the loss is the simplest possible mean squared error:

L=∥Estudent(x)−EViT-H(x)∥22\mathcal{L} = \big\| E_{\text{student}}(x) - E_{\text{ViT-H}}(x) \big\|_2^2

xx is the input image and EE an encoder. This objective has nothing to do with prompts and needs no mask annotations, only images.

Once the student is trained, it plugs straight into SAM's original decoder and works. The decoder was trained on ViT-H's features to begin with, so if the student's features are close enough to ViT-H's, the decoder naturally understands them. The authors say fine-tuning the decoder afterward would help further, but this version of the paper skips that step.

A First Try with ViT-B

The authors first used ViT-B as the student and compared it with the ViT-B version SAM officially released. Scoring gives both the same point prompt, compares each one's mask with the mask from ViT-H SAM, and computes the mIoU between them, averaged over 200 images:

Official SAM ViT-BDecoupled ViT-B
mIoU vs. ViT-H masks0.720.75
GPUs1282
Batch size1284
Iterations180k55k
Training images11M11K

With only 0.1% of the images and less than 1% of the compute, it agrees with the teacher slightly better. Note, though, that the official ViT-B was trained on SA-1B's masks, not specifically to imitate ViT-H's output; scoring by "how much it resembles ViT-H" naturally favors a method that imitates ViT-H directly.

Switching to TinyViT

Diagram drawn for this note. A 1024×1024 image takes two paths, one above the other. The upper path is SAM's original ViT-H encoder, 632M parameters and 452 ms on one GPU, outputting 64×64×256. The lower path is TinyViT: two convs, each stride 2, give 256²×64; stage 1 is two MBConv blocks, 256²×64; stage 2 is two attention layers, 128²×128; stage 3 is six attention layers, 64²×160; stage 4 is two attention layers, 64²×320; a conv neck then turns this into 64×64×256, the same shape as above. TinyViT has 5.78M parameters and takes 8 ms on one GPU, and the stride of its last downsampling goes from 2 to 1. Both paths lead to the same SAM mask decoder, 3.87M parameters and about 4 ms.
Drawn for this note from Section 4.1 and Table 3 of the paper and the TinyViT configuration in the official code.

ViT-B is still too big for a phone, so the student in the final version is TinyViT. It's a hybrid architecture with four stages of progressively lower resolution. Two stride-2 conv layers at the start turn the 1024×1024 image into 256×256; the first stage uses MobileNet-V2's inverted residual convolutions, and the last three stages are window-attention Transformers.

One modification is necessary here. The original TinyViT halves the resolution between every pair of stages, ending with a 32×32 output, but SAM's decoder expects 64×64 features. The authors change the stride of the last downsampling from 2 to 1 and add a conv neck that turns 320 channels into 256, and the output shape then matches ViT-H exactly.

The encoder goes from 632M parameters to 5.78M, more than a hundred times fewer, and on a single GPU from 452 ms to 8 ms. The whole MobileSAM, decoder included, has 9.66M parameters.

Training uses 1% of SA-1B's images, about 100,000, for 8 epochs on a single GPU, in under a day. Since the teacher needs far more compute than the student, the authors follow TinyViT's practice of computing and storing all the ViT-H features in advance, so training only has to run the student.

Discussion

How Close to the Teacher

The paper has only one kind of quantitative evaluation, mIoU against the original SAM's output. The longer the training, the closer the match. On a single GPU, going from batch 4 to 8 and from 50k to 100k iterations raises mIoU from 0.7057 to 0.7447. Examples with point and box prompts are shown as images, and the authors' verdict is that the results are "on par" with the original SAM.

This paper doesn't evaluate on any dataset with ground-truth annotations. More complete numbers turn up instead in the HQ-SAM paper from the same month, whose authors measured MobileSAM along the way while building Light HQ-SAM. With boxes from a detector trained on COCO, MobileSAM's mask AP is 44.3, against 44.4 for SAM ViT-B and 48.9 for SAM ViT-H; on the four datasets with extremely fine annotation, its boundary mBIoU is 58.8, against 62.3 for ViT-B and 68.3 for ViT-H.

So the more accurate statement is that MobileSAM, at TinyViT's size and speed, matches SAM ViT-B's quality on COCO, falls somewhat short on boundary detail, and remains some distance from the ViT-H it imitates. Section 4.2 of the paper is titled "MobileSAM performs on par with the original SAM," while the README adds a parenthetical, "at least visually" (as long as you only eyeball it).

Compared with FastSAM

FastSAM was another method for speeding up SAM from the same period, and it took a completely different route. It uses a YOLOv8 segmentation model to cut out everything in the image first, then picks one mask from that pile according to the user's prompt. The authors draw a careful line between two tasks. What SAM is built for is segment anything, one prompt, one object; cutting out everything first is segment everything, which is essentially generating candidate masks.

FastSAMMobileSAM
Params68M9.66M
Time on one GPU64 ms12 ms

MobileSAM is about 7 times smaller and about 5 times faster. Elsewhere the text gives FastSAM 40 ms and MobileSAM 10 ms, 4 times faster; the abstract says MobileSAM takes about 10 ms, 8 ms for the encoder and 4 ms for the decoder (8 plus 4 makes about 10, presumably in mobile arithmetic). The README finally settled on 12 ms.

The quality comparison uses two points as the prompt, one foreground point and one background point, 100 to 500 pixels apart. FastSAM's mIoU against the original SAM is only 0.27 to 0.41, while MobileSAM stays between 0.71 and 0.74. The paper's reason is that "FastSAM is suggested to predict the mask with multiple points."

Three days after the paper went up on arXiv, a reader pointed out in GitHub issue #13 that FastSAM does in fact support a single point. The authors reread FastSAM's code, admitted it could, and added single-point results. With the point at the center of the image, averaged over 100 images, FastSAM gets about 0.43 and MobileSAM about 0.74. The gap is still large and the conclusion stands; it's just that the v2 paper, which went online three days later, still kept the two-point justification.

Training Code

MobileSAM's README says "The training code will be available soon."

On June 27, 2023, someone asked for the training code in issue #6. The authors replied the same day that it would come "soon after we complete other stuff that has a higher priority," adding that the existing weights were already close enough to the original SAM. That issue is still open, with a steady stream of "looking forward to it," "+1" and "please let me know when it's out" underneath. Every few months since, someone has opened a new issue asking the same thing; the most recent was in October 2025, with no reply at all. As of November 2026, this "soon" has lasted three years and five months (a "soon" like this is probably measured in geological time). In October 2023, on the other hand, a third-party team wrote their own training code and posted it in issue #111 for everyone's reference.

Fortunately, the decoupled distillation pipeline itself is very simple, storing the teacher's features and having the student do MSE regression, so reproducing it yourself isn't too hard. The trickier part is the decoder fine-tuning that the paper says would help but leaves out. In issue #49 a user reported that fine-tuning MobileSAM's decoder directly with box prompts made the masks start falling apart after one epoch, while the same procedure worked fine on ViT-B, L and H.

Conclusion

MobileSAM's way of solving the problem is pragmatic. SAM is slow in the encoder, so only the encoder is swapped; retraining the whole thing is too expensive, so the small encoder just imitates the large encoder's output features. No masks, no prompts, less than a day on a single GPU, and the result is a model sixty times smaller than the original, dozens of times faster, and compatible with every downstream SAM tool.

That compatibility is its greatest value. Grounded-SAM, AnyLabeling and the segmentation extension for Stable Diffusion WebUI soon all added a MobileSAM option, because apart from the line that loads the weights, no other code has to change; HQ-SAM's lightweight version also reuses its TinyViT directly.

When reading the numbers, remember that every "accuracy" in the paper is agreement with ViT-H SAM. On datasets with ground truth, it's roughly at ViT-B's level, with slightly weaker boundaries.