# The Big Idea: Segment Anything

Author: Ishwar Jangid
Canonical URL: https://ishwarj.com/papers/sam/part-1-the-big-idea/
Updated: 2026-10-17
Source: https://github.com/ishwar6/ishwar-books/blob/main/content/papers/sam/part-1-the-big-idea.md


On 5 April 2023, twelve researchers at Meta AI Research (FAIR) put a paper with a short title on arXiv: *Segment Anything*. The paper gives a model that cuts out an object in a picture when you show it the object. You click on the object, or you draw a box around it. The model then returns the exact pixels of the object. The model is the **Segment Anything Model (SAM)**. The team also released 1.1 billion masks on 11 million pictures, the largest segmentation dataset at that time.

Each piece below shows the exact lines of the paper, then explains them.

> [!DEFINITION] Definition boxes
> A yellow box like this explains one technical word, the first time that the word occurs. If you know the word, skip the box.

## The title {§Title}

> [!PAPER] · Title · page 1
> [![The title of the paper: Segment Anything](https://ishwarj.com/img/papers/sam/p1-title.pngrs/sam/p1-title.png)](/img/papers/sam/p1-title.png)
>
> **What it says:** "Segment Anything."
>
> **What it means:**
>
> Imagine a photo and a pair of scissors. You want to cut out one thing: the cat, only its ear, or the cup on the table. "Segment" means this cut, but on a computer. For each dot of the photo, the computer must say "inside" or "outside".
>
> "Anything" is the difficult word. Before this paper, a model learned a fixed list, for example 80 types of object. It could cut out a "dog", but not the dog's collar if "collar" was not on the list. The title promises a model with no list: you show it a thing, and it cuts out that thing. The thing can be a cat, a button on a remote control, a cell in a microscope picture or a bag in an X-ray picture.
>
> Also see what the title does not say. It does not say "*Recognize* Anything". SAM cuts out the object, but it does not tell you the name of the object.

> [!DEFINITION] Pixel
> One dot of a digital picture. A picture of 640 × 480 pixels has 480 rows of 640 dots, which is 307,200 dots. Each dot holds a colour as three numbers: red, green and blue.

> [!DEFINITION] Segmentation
> The job to find which pixels of a picture belong to which object. Classification gives one label for the whole picture. Segmentation gives an answer for each pixel.

> [!DEFINITION] Mask
> A grid with one number for each pixel of the picture. The number is 1 where the object is and 0 where the object is not. A model usually makes a score for each pixel first. A threshold then changes the scores into 1s and 0s.

Computer vision has many types of output for one picture. The figure shows four of them on a small toy picture with two cats on a sofa.

<figure class="fig"><svg viewBox="0 0 760 214" role="img" aria-label="Four ways to describe one picture with two cats on a sofa: one label, boxes, a class per pixel, and one mask per cat."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="97.0" y="24.0" text-anchor="middle">Classification</text><rect class="box-ghost" x="22.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="37.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="52.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="67.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="82.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="112.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="127.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="142.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="157.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="37.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="52.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="67.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="82.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="112.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="127.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="142.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="157.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="37.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="52.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="67.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="82.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="112.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="127.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="142.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="157.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="37.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="52.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="67.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="82.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="112.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="127.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="142.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="157.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="37.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="52.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="67.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="82.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="112.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="127.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="142.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="157.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="37.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="52.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="67.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="82.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="112.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="127.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="142.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="157.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="37.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="52.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="67.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="82.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="112.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="127.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="142.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="157.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="22.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="37.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="52.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="67.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="82.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="97.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="112.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="127.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="142.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="157.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><text class="t-note" x="97.0" y="105.0" text-anchor="middle">"cat"</text><text class="t-muted" x="97.0" y="182.0" text-anchor="middle">one label for the</text><text class="t-muted" x="97.0" y="198.0" text-anchor="middle">whole picture: "cat"</text><text class="t-title" x="283.0" y="24.0" text-anchor="middle">Detection</text><rect class="box-ghost" x="208.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="223.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="238.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="253.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="268.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="298.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="313.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="328.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="343.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="223.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="238.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="253.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="268.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="298.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="313.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="328.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="343.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="223.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="238.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="253.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="268.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="298.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="313.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="328.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="343.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="223.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="238.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="253.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="268.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="298.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="313.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="328.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="343.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="223.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="238.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="253.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="268.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="298.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="313.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="328.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="343.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="223.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="238.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="253.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="268.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="298.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="313.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="328.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="343.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="223.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="238.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="253.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="268.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="s4" x="298.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="313.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="s4" x="328.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.35"/><rect class="box-ghost" x="343.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="208.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="223.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="238.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="253.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="268.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="283.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="298.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="313.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="328.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="box-ghost" x="343.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:1.00"/><rect class="edge-on" x="221" y="53" width="49" height="94" rx="2" style="fill:none"/><rect class="edge-on" x="296" y="68" width="49" height="79" rx="2" style="fill:none"/><text class="t-muted" x="283.0" y="182.0" text-anchor="middle">one box per</text><text class="t-muted" x="283.0" y="198.0" text-anchor="middle">object</text><text class="t-title" x="469.0" y="24.0" text-anchor="middle">Semantic segmentation</text><rect class="s4" x="394.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="409.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="424.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="439.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="454.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="484.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="499.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="514.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="529.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="409.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="424.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="439.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="454.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="484.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="499.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="514.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="529.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="409.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="424.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="439.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="454.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="484.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="499.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="514.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="529.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="409.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="424.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="439.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="454.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="484.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="499.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="514.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="529.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="409.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="424.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="439.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="454.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="484.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="499.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="514.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="529.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="409.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="424.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="439.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="454.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="484.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="499.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="514.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="529.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="409.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="424.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="439.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="454.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="484.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="499.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s1" x="514.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.80"/><rect class="s4" x="529.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="394.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="409.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="424.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="439.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="454.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="469.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="484.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="499.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="514.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="529.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><text class="t-muted" x="469.0" y="182.0" text-anchor="middle">a class for every</text><text class="t-muted" x="469.0" y="198.0" text-anchor="middle">pixel; cats not split</text><text class="t-title" x="655.0" y="24.0" text-anchor="middle">Instance segmentation</text><rect class="s4" x="580.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="595.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="610.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="625.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="640.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="670.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="685.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="700.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="715.8" y="40.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="595.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="610.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="625.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="640.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="670.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="685.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="700.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="715.8" y="55.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="595.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="610.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="625.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="640.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="670.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s3" x="685.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="700.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="715.8" y="70.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="595.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="610.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="625.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="640.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s3" x="670.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="685.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="700.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="715.8" y="85.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="595.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="610.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="625.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="640.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s3" x="670.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="685.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="700.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="715.8" y="100.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="595.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="610.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="625.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="640.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s3" x="670.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="685.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="700.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="715.8" y="115.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s1" x="595.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="610.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="625.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="640.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s3" x="670.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="685.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s3" x="700.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="715.8" y="130.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="580.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="595.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="610.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="625.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="640.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="655.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="670.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="685.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="700.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><rect class="s4" x="715.8" y="145.8" width="13.5" height="13.5" rx="2" style="fill-opacity:0.25"/><text class="t-muted" x="655.0" y="182.0" text-anchor="middle">a separate mask</text><text class="t-muted" x="655.0" y="198.0" text-anchor="middle">for every object</text></svg><figcaption>Four types of output for one picture. Classification gives one label. Detection gives one box for each object. Semantic segmentation gives a class to each pixel, but it does not separate the two cats. Instance segmentation gives a separate mask for each cat.</figcaption></figure>

> [!DEFINITION] Semantic and instance segmentation
> **Semantic segmentation** gives each pixel a class from a fixed list, for example "cat", "sofa" or "sky". Two cats get the same class, so the model does not separate them. **Instance segmentation** gives a separate mask to each object, so "cat 1" and "cat 2" are different. SAM makes masks of single objects, so its output is like instance segmentation. But SAM does not give a class name to the mask.

Think of the photo as a sheet of squared paper, with one square for each pixel. Take a pen. Write 1 in each square that the object covers, and write 0 in all the other squares. This sheet of numbers is the mask.

<figure class="fig"><svg viewBox="0 0 760 298" role="img" aria-label="A mask is a grid with one number per pixel: 1 where the object is and 0 elsewhere."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="152.0" y="22.0" text-anchor="middle">The picture area, cut into pixels</text><rect class="s4" x="20.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="64.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="86.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="108.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="130.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="152.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="174.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="196.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="218.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="240.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="262.8" y="36.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="64.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="86.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="108.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="218.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="240.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="262.8" y="58.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="64.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="86.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="108.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="218.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="240.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="262.8" y="80.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="64.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="86.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="108.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="218.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="240.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="262.8" y="102.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="64.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="86.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="108.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="218.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="240.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="262.8" y="124.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="64.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="86.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="108.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="218.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="240.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="262.8" y="146.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="64.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="86.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="108.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="218.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="240.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="262.8" y="168.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="64.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="86.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s1" x="108.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="130.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="152.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="174.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s1" x="196.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.85"/><rect class="s4" x="218.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="240.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="262.8" y="190.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="20.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="42.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="64.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="86.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="108.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="130.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="152.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="174.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="196.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="218.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="240.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><rect class="s4" x="262.8" y="212.8" width="20.5" height="20.5" rx="2" style="fill-opacity:0.22"/><text class="t-title" x="492.0" y="22.0" text-anchor="middle">The mask: one number per pixel</text><text class="t-mono t-muted" x="371.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="415.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-on" x="437.0" y="52.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="459.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="481.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="503.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="525.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-on" x="547.0" y="52.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="569.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="591.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="613.0" y="52.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="74.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="74.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="415.0" y="74.0" text-anchor="middle">0</text><text class="t-mono t-on" x="437.0" y="74.0" text-anchor="middle">1</text><text class="t-mono t-on" x="459.0" y="74.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="74.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="74.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="74.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="74.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="569.0" y="74.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="591.0" y="74.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="613.0" y="74.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="96.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="96.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="415.0" y="96.0" text-anchor="middle">0</text><text class="t-mono t-on" x="437.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-on" x="459.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-on" x="569.0" y="96.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="591.0" y="96.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="613.0" y="96.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="118.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="118.0" text-anchor="middle">0</text><text class="t-mono t-on" x="415.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="437.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="459.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="569.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-on" x="591.0" y="118.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="613.0" y="118.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="140.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="140.0" text-anchor="middle">0</text><text class="t-mono t-on" x="415.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="437.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="459.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="569.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-on" x="591.0" y="140.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="613.0" y="140.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="162.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="162.0" text-anchor="middle">0</text><text class="t-mono t-on" x="415.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="437.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="459.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="569.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-on" x="591.0" y="162.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="613.0" y="162.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="184.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="184.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="415.0" y="184.0" text-anchor="middle">0</text><text class="t-mono t-on" x="437.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-on" x="459.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-on" x="569.0" y="184.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="591.0" y="184.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="613.0" y="184.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="415.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="437.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-on" x="459.0" y="206.0" text-anchor="middle">1</text><text class="t-mono t-on" x="481.0" y="206.0" text-anchor="middle">1</text><text class="t-mono t-on" x="503.0" y="206.0" text-anchor="middle">1</text><text class="t-mono t-on" x="525.0" y="206.0" text-anchor="middle">1</text><text class="t-mono t-on" x="547.0" y="206.0" text-anchor="middle">1</text><text class="t-mono t-muted" x="569.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="591.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="613.0" y="206.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="371.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="393.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="415.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="437.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="459.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="481.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="503.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="525.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="547.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="569.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="591.0" y="228.0" text-anchor="middle">0</text><text class="t-mono t-muted" x="613.0" y="228.0" text-anchor="middle">0</text><text class="t-note" x="20.0" y="262.0" text-anchor="start">54 of the 108 pixels belong to the object: area = 54/108 = 0.50</text><text class="t-muted" x="20.0" y="284.0" text-anchor="start">SAM's first mask for the ear click: 427 ones among 640 × 480 = 307,200 numbers</text></svg><figcaption>A mask is a grid of numbers with the same size as the picture: 1 where the object is and 0 elsewhere. The area of the mask is the number of 1s. SAM's smallest mask for a cat's ear (below) has 427 ones in a grid of 307,200 numbers.</figcaption></figure>

Mathematics writes the same sheet in one line. For a picture with $$H$$ rows and $$W$$ columns of pixels:

$$
M \in \{0, 1\}^{H \times W}, \qquad M_{ij} = \begin{cases} 1 & \text{if pixel } (i, j) \text{ is part of the object} \\ 0 & \text{if it is not} \end{cases}
$$

Read the line from left to right:

- $$M$$ is the name of the sheet: the mask.
- $$\in$$ means "is a". So the first part says: "$$M$$ is a grid of $$H$$ rows and $$W$$ columns, and each square holds 0 or 1". For our cat photo, $$H = 480$$ and $$W = 640$$.
- $$M_{ij}$$ is the number in row $$i$$ and column $$j$$. The large bracket gives the rule for this number: 1 inside the object, 0 outside.

Here is a very small example: a mask of 4 rows and 5 columns. The object is the group of 1s.

$$
M = \begin{bmatrix} 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 \end{bmatrix}, \qquad M_{2,3} = 1, \qquad M_{1,1} = 0
$$

$$M_{2,3} = 1$$ because the square in row 2, column 3 is part of the object. $$M_{1,1} = 0$$ because the top-left square is not.

How large is the object? Count the 1s. Each 1 is one pixel of the object, so the count is the area:

$$
\text{area}(M) = \sum_{i=1}^{H} \sum_{j=1}^{W} M_{ij}
$$

The symbol $$\sum$$ (the Greek capital letter sigma) means "add all of these". There are two of them, because the grid has two directions. The inner sum adds the numbers along one row, from column 1 to column $$W$$. The outer sum does this for each row, from row 1 to row $$H$$. For the small example, the four rows give:

$$
\text{area}(M) = \underbrace{0}_{\text{row 1}} + \underbrace{2}_{\text{row 2}} + \underbrace{3}_{\text{row 3}} + \underbrace{0}_{\text{row 4}} = 5
$$

For SAM's smallest mask of the cat's ear (below), the same count gives 427.

How did the field get here, and how is SAM different from a detector such as YOLO? The [Background page](https://ishwarj.com/papers/sam/background-the-road-to-sam/) tells that story.

## Figure 1: the project in one picture {§Figure 1}

> [!PAPER] · Figure 1 · page 1
> [![Figure 1 of the paper in three panels. (a) Task: promptable segmentation. Points, a box, a mask or the text cat with black ears go into a model with an image, and a valid mask comes out. (b) Model: SAM. An image encoder and a prompt encoder feed a lightweight mask decoder that outputs a valid mask. (c) Data: a loop where the model annotates data and the data trains the model, and the dataset Segment Anything 1B with 1+ billion masks on 11 million privacy respecting, licensed images](https://ishwarj.com/img/papers/sam/p1-figure1.png/sam/p1-figure1.png)](/img/papers/sam/p1-figure1.png)
>
> **What it says:** "We aim to build a foundation model for segmentation by introducing three interconnected components: a **promptable segmentation task**, a **segmentation model (SAM)** that powers data annotation and enables zero-shot transfer to a range of tasks via prompt engineering, and a **data engine for collecting SA-1B**, our dataset of over 1 billion masks."
>
> **What it means:**
>
> Read the figure as three answers to three questions.
>
> - **(a) What must the model learn?** Different hints go in: dots, a box, a rough shape or the words "cat with black ears". The picture also goes in, and one mask comes out. The hint is the **prompt**.
> - **(b) What does the model look like?** A large part reads the picture (the **image encoder**). A small part reads the hint (the **prompt encoder**). A third small part joins the two and draws the mask (the **mask decoder**). It is "lightweight" because it must be fast: you click, and you see the mask at once.
> - **(c) Where do the training masks come from?** Two arrows make a circle. The model helps people to label pictures, and the labelled pictures train the model.
>
> The circle in (c) is the new idea of the paper. Think of a student who helps to write the textbook, and then learns from that textbook. A first, weak SAM helps people to draw masks quickly. The new masks train a better SAM, the better SAM helps more, and the circle continues. The result is at the bottom of (c): **SA-1B**, more than 1 billion masks.

<figure class="fig"><svg viewBox="0 0 760 240" role="img" aria-label="The three linked parts of the project: the task defines what to learn, the model learns it, the data engine uses the model to collect more data, which trains a better model."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><rect class="box-1" x="30.0" y="30.0" width="200.0" height="130.0" rx="12"/><text class="t-title" x="130.0" y="56.0" text-anchor="middle">1. Task</text><text class="t-note" x="130.0" y="80.0" text-anchor="middle">promptable segmentation:</text><text class="t-note" x="130.0" y="98.0" text-anchor="middle">any prompt in,</text><text class="t-note" x="130.0" y="116.0" text-anchor="middle">a valid mask out</text><text class="t-muted" x="130.0" y="150.0" text-anchor="middle">paper §2</text><rect class="box-2" x="280.0" y="30.0" width="200.0" height="130.0" rx="12"/><text class="t-title" x="380.0" y="56.0" text-anchor="middle">2. Model</text><text class="t-note" x="380.0" y="80.0" text-anchor="middle">SAM: image encoder,</text><text class="t-note" x="380.0" y="98.0" text-anchor="middle">prompt encoder,</text><text class="t-note" x="380.0" y="116.0" text-anchor="middle">mask decoder</text><text class="t-muted" x="380.0" y="150.0" text-anchor="middle">paper §3</text><rect class="box-3" x="530.0" y="30.0" width="200.0" height="130.0" rx="12"/><text class="t-title" x="630.0" y="56.0" text-anchor="middle">3. Data</text><text class="t-note" x="630.0" y="80.0" text-anchor="middle">data engine: SAM helps</text><text class="t-note" x="630.0" y="98.0" text-anchor="middle">people label; the labels</text><text class="t-note" x="630.0" y="116.0" text-anchor="middle">train SAM; SA-1B</text><text class="t-muted" x="630.0" y="150.0" text-anchor="middle">paper §4, §5</text><line class="edge" x1="234.0" y1="95.0" x2="276.0" y2="95.0" marker-end="url(#ah)"/><line class="edge" x1="484.0" y1="95.0" x2="526.0" y2="95.0" marker-end="url(#ah)"/><path class="edge" d="M630,164 C630,206 380,206 380,166" marker-end="url(#ah)"/><text class="t-muted" x="505.0" y="226.0" text-anchor="middle">more data makes a better model; a better model makes labelling faster</text></svg><figcaption>The three linked parts of the project. The task tells what to learn. The model learns it. The data engine uses the model to collect more masks, and the new masks train a better model.</figcaption></figure>

> [!DEFINITION] Annotation (labelling)
> The work to add the correct answer to training data by hand. For segmentation, a person draws the outline of each object, and the outline becomes a mask. A person that does this work is an **annotator**. Annotation is slow and costs money, so segmentation datasets were small before SAM.

## The abstract, sentence by sentence {§Abstract}

The abstract has five sentences. We read it in two halves.

> [!PAPER] · Abstract · page 1
> [![The first half of the abstract: We introduce the Segment Anything (SA) project: a new task, model, and dataset for image segmentation. Using our efficient model in a data collection loop, we built the largest segmentation dataset to date (by far), with over 1 billion masks on 11M licensed and privacy respecting images](https://ishwarj.com/img/papers/sam/p1-abstract-1.pngm/p1-abstract-1.png)](/img/papers/sam/p1-abstract-1.png)
>
> **What it says:** the Segment Anything (SA) project is "a new task, model, and dataset for image segmentation". The model works "in a data collection loop". The result is "the largest segmentation dataset to date (by far), with over 1 billion masks on 11M licensed and privacy respecting images".
>
> **What it means:**
>
> Most papers give one new thing, for example a better model for an old dataset. This paper gives three new things at the same time: a task, a model and a dataset. All three are new because each one needs the other two.
>
> The model is "efficient", which means fast. So a person can use it during the labelling work, in the "loop" of Figure 1(c). The result is the largest set of masks "by far": about 400 times more masks than the largest dataset before it. "Licensed" means that the team paid a photo company for the pictures. "Privacy respecting" means that faces and car number plates are blurred.
>
> Why does the abstract name the dataset before the results of the model? Because the dataset is the main result. Other people can copy a model design. To make 1.1 billion masks again is very difficult and very expensive.

> [!DEFINITION] Dataset
> A collection of examples to train or to test a model. A segmentation dataset holds pictures and, for each picture, the masks of its objects.

> [!PAPER] · Abstract · page 1
> [![The second half of the abstract: The model is designed and trained to be promptable, so it can transfer zero-shot to new image distributions and tasks. We evaluate its capabilities on numerous tasks and find that its zero-shot performance is impressive, often competitive with or even superior to prior fully supervised results. We are releasing the Segment Anything Model (SAM) and corresponding dataset (SA-1B) of 1B masks and 11M images at segment-anything.com to foster research into foundation models for computer vision](https://ishwarj.com/img/papers/sam/p1-abstract-2.pngm/p1-abstract-2.png)](/img/papers/sam/p1-abstract-2.png)
>
> **What it says:** the model is "designed and trained to be promptable, so it can transfer zero-shot to new image distributions and tasks". Its zero-shot results are "often competitive with or even superior to prior fully supervised results". The team releases the model, the **Segment Anything Model (SAM)**, and the dataset, **SA-1B**.
>
> **What it means:**
>
> "Promptable" means that SAM gets a hint as an input, together with the picture. In training, it always gets a hint, and it must give the mask that matches the hint. "Transfer zero-shot" means that you use the model on new things with no more training. Think of a person who learned to ride a bicycle on one road. That person can ride on a new road with no new lessons. For SAM, the new roads are new types of pictures (microscope, underwater, X-ray) and new jobs (for example, "find all the edges").
>
> The result: other models had training on the exact test, and SAM did not. But SAM's score is near their score, and sometimes higher. Think of a student who never saw the old exam papers, and still gets a mark near the students who practised them.
>
> Look at the word "often" in "often competitive". It is an honest word, and it means "not always". Part 5 shows the cases where SAM is worse than the specialist models.

Four words in this half are new. The yellow boxes explain them.

> [!DEFINITION] Prompt and promptable
> A **prompt** is an input that tells the model what to do. For a language model, the prompt is text. For SAM, the prompt tells *which object* to segment: a point on the object, a box around it, a rough mask, or a short text. A **promptable** model takes a prompt as part of its input.

> [!DEFINITION] Zero-shot transfer
> Use a trained model on a new dataset or a new task with **no** more training. "Zero-shot" means "zero training examples of the new task". The opposite is **fine-tuning**, where you train the model more on examples of the new task.

> [!DEFINITION] Image distribution
> The type of pictures in a dataset: for example, holiday photos, microscope pictures, X-ray pictures of bags or paintings. A model that works on holiday photos can fail on X-ray pictures. A "new image distribution" is a type of picture that the model did not see in training.

> [!DEFINITION] Fully supervised
> A model that has training on labelled examples of the exact task and dataset of the test. It is the strong baseline that a zero-shot model must approach.

### Let us run it

Before we read the introduction, we look at the model in operation. We loaded the released SAM with the largest image encoder, ViT-H (`facebook/sam-vit-huge`). The paper uses this model in its experiments. We gave the model one picture, the test photo from the `huggingface/cats-image` dataset, and two clicks.

> [!TIP] Try it
> Click on this photo yourself in the [live demo](https://ishwarj.com/papers/sam/demo). The real SAM ViT-H answers each click with three masks and their scores.

<figure><img src="/img/papers/sam/p1-sample.png" alt="The sample picture: two tabby cats on a pink sofa with two remote controls. Two green dots mark the two clicks, one on the ear of the left cat and one on the buttons of the left remote control" loading="lazy" width="560" /><figcaption>The picture of this part: 640 × 480 pixels. The green dots are the two clicks: one on the left cat's ear at pixel (275, 62), and one on the buttons of the left remote control at pixel (80, 90).</figcaption></figure>

The code is in [`sam_part1.py`](https://github.com/ishwar6/ishwar-books/blob/main/code/papers/sam/sam_part1.py). These are its main lines:

```python
proc = SamProcessor.from_pretrained('facebook/sam-vit-huge')
sam = SamModel.from_pretrained('facebook/sam-vit-huge').to('mps').eval()
inp = proc(images=image, return_tensors='pt')
emb = sam.get_image_embeddings(inp['pixel_values'].to('mps'))       # the heavy part: once per picture

p = proc(images=image, input_points=[[[[275, 62]]]], input_labels=[[[1]]], return_tensors='pt')
out = sam(image_embeddings=emb, input_points=p['input_points'].float().to('mps'),
          input_labels=p['input_labels'].to('mps'), multimask_output=True)   # the light part: once per prompt
masks = proc.image_processor.post_process_masks(out.pred_masks.cpu(), p['original_sizes'], p['reshaped_input_sizes'])
```

```text
== the input pipeline (facebook/sam-vit-huge) ==
resize: longest side 640 -> 1024, scale 1.6000; picture becomes 1024 x 768
pad: 1024 x 768 -> 1024 x 1024 (zeros added below: 256 rows)
pixel tensor: (1, 3, 1024, 1024) = 3,145,728 numbers
image embedding: (1, 256, 64, 64) = 1,048,576 numbers (each of 64 x 64 = 4,096 cells covers 16 x 16 pixels)

== one click at (275, 62) on the left cat's ear ==
low-resolution mask logits from the decoder: (1, 1, 3, 256, 256)  (batch, prompts, 3 masks, 256, 256)
  mask 1: predicted IoU 0.946       427 pixels =   0.14% of the picture
  mask 2: predicted IoU 0.944       470 pixels =   0.15% of the picture
  mask 3: predicted IoU 0.980    49,520 pixels =  16.12% of the picture
  SAM ranks mask 3 first (highest predicted IoU)
```

Each line of that output is simple arithmetic. We do the arithmetic once by hand.

**Step 1: resize.** SAM's image encoder accepts only one size: a square of 1024 × 1024 pixels. Our photo is 640 × 480. SAM must make the photo larger, but it must not change its shape, because a stretched cat is a different picture. So SAM multiplies both sides by the same number $$s$$. It chooses $$s$$ so that the longer side becomes exactly 1024:

$$
s = \frac{1024}{\max(W, H)} = \frac{1024}{640} = 1.6, \qquad (W', H') = (s W, s H) = (1024, 768)
$$

Read it in three steps:

- $$W = 640$$ is the width and $$H = 480$$ is the height of the photo. $$\max(W, H)$$ means "the larger of the two numbers". Here $$\max(640, 480) = 640$$.
- $$s = 1024 / 640 = 1.6$$. Each side becomes 1.6 times longer.
- The new width is $$W' = 1.6 \times 640 = 1024$$ and the new height is $$H' = 1.6 \times 480 = 768$$. The shape does not change, because $$640 / 480 = 1024 / 768$$.

Why the longer side? If SAM made the shorter side 1024, the longer side would become $$640 \times 1024 / 480 \approx 1365$$. That is too wide for the square.

**Step 2: pad.** The picture is now 1024 pixels wide but only 768 pixels high. A square needs 1024 rows, so SAM adds $$1024 - 768 = 256$$ rows of zeros at the bottom. Think of a photo in a square frame, with a black strip below it. The input then has 3 colour numbers for each of the $$1024 \times 1024$$ pixels: $$3 \times 1024 \times 1024 = 3{,}145{,}728$$ numbers.

**Step 3: encode.** Put a grid of $$64 \times 64$$ squares over the picture, as a large chessboard. Each square covers $$1024 / 64 = 16$$ pixels on each side. The image encoder writes a list of 256 numbers for each square. The list describes what is in that square and around it: fur, the edge of an ear, the sofa. All the lists together are the **image embedding**: $$256 \times 64 \times 64 = 1{,}048{,}576$$ numbers.

**Step 4: decode.** The prompt and the image embedding go into the mask decoder. The decoder makes **three** masks of 256 × 256 and three scores. SAM then scales the masks back to the size of the photo, 640 × 480.

<figure class="fig"><svg viewBox="0 0 760 310" role="img" aria-label="SAM on our photo: the 640 by 480 photo is resized to 1024 by 768 and padded to 1024 by 1024, the image encoder makes a 256 by 64 by 64 embedding once, and the prompt encoder and mask decoder turn each prompt into three masks and three scores."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><rect class="box" x="14.0" y="40.0" width="120.0" height="74.0" rx="10"/><text class="t-note" x="74.0" y="62.0" text-anchor="middle">photo</text><text class="t-tick" x="74.0" y="82.0" text-anchor="middle">640 × 480</text><text class="t-tick" x="74.0" y="99.0" text-anchor="middle">× 3 colours</text><rect class="box" x="150.0" y="40.0" width="120.0" height="74.0" rx="10"/><text class="t-note" x="210.0" y="62.0" text-anchor="middle">resize + pad</text><text class="t-tick" x="210.0" y="82.0" text-anchor="middle">1024 × 768</text><text class="t-tick" x="210.0" y="99.0" text-anchor="middle">→ 1024 × 1024</text><rect class="box-2" x="286.0" y="40.0" width="120.0" height="74.0" rx="10"/><text class="t-note" x="346.0" y="62.0" text-anchor="middle">image encoder</text><text class="t-tick" x="346.0" y="82.0" text-anchor="middle">ViT-H/16</text><text class="t-tick" x="346.0" y="99.0" text-anchor="middle">runs once</text><rect class="box-ghost" x="422.0" y="40.0" width="120.0" height="74.0" rx="10"/><text class="t-note" x="482.0" y="62.0" text-anchor="middle">image embedding</text><text class="t-tick" x="482.0" y="82.0" text-anchor="middle">256 × 64 × 64</text><text class="t-tick" x="482.0" y="99.0" text-anchor="middle">1,048,576 numbers</text><line class="edge" x1="135.0" y1="77.0" x2="149.0" y2="77.0" marker-end="url(#ah)"/><line class="edge" x1="271.0" y1="77.0" x2="285.0" y2="77.0" marker-end="url(#ah)"/><line class="edge" x1="407.0" y1="77.0" x2="421.0" y2="77.0" marker-end="url(#ah)"/><rect class="box-1" x="422.0" y="160.0" width="120.0" height="54.0" rx="10"/><text class="t-note" x="482.0" y="182.0" text-anchor="middle">prompt</text><text class="t-tick" x="482.0" y="200.0" text-anchor="middle">click (275, 62)</text><line class="edge" x1="542.0" y1="187.0" x2="570.0" y2="187.0" marker-end="url(#ah)"/><line class="edge" x1="542.0" y1="77.0" x2="570.0" y2="120.0" marker-end="url(#ah)"/><rect class="box-3" x="574.0" y="96.0" width="170.0" height="116.0" rx="10"/><text class="t-note" x="659.0" y="120.0" text-anchor="middle">prompt encoder</text><text class="t-note" x="659.0" y="138.0" text-anchor="middle">+ mask decoder</text><text class="t-tick" x="659.0" y="160.0" text-anchor="middle">runs per prompt</text><text class="t-tick" x="659.0" y="178.0" text-anchor="middle">3 masks of 256 × 256</text><text class="t-tick" x="659.0" y="196.0" text-anchor="middle">+ 3 scores</text><line class="edge" x1="659.0" y1="216.0" x2="659.0" y2="244.0" marker-end="url(#ah)"/><rect class="box-on" x="540.0" y="248.0" width="204.0" height="46.0" rx="10"/><text class="t-note" x="642.0" y="268.0" text-anchor="middle">resize back to 640 × 480</text><text class="t-tick" x="642.0" y="285.0" text-anchor="middle">3 masks, 3 scores</text><text class="t-title" x="14.0" y="150.0" text-anchor="start">Heavy, once per picture</text><text class="t-muted" x="14.0" y="170.0" text-anchor="start">ViT-H: 637M weights</text><text class="t-title" x="14.0" y="268.0" text-anchor="start">Light, once per prompt</text><text class="t-muted" x="14.0" y="288.0" text-anchor="start">decoder: 4.1M weights</text><line class="edge-dim" x1="290.0" y1="140.0" x2="290.0" y2="300.0"/></svg><figcaption>SAM on our photo. The photo is resized to 1024 × 768 and padded to 1024 × 1024. The image encoder makes the 256 × 64 × 64 image embedding once. For each prompt, the prompt encoder and the mask decoder make three masks and three scores.</figcaption></figure>

> [!DEFINITION] Embedding
> A list of numbers (a vector) that describes something in a form that a network can use. A word, a patch of a picture, or a click can each become an embedding. SAM's **image embedding** is a grid of 64 × 64 vectors, with 256 numbers in each vector.

> [!DEFINITION] Encoder and decoder
> An **encoder** changes an input into an embedding. A **decoder** changes embeddings into the output that you want. SAM has two encoders (one for the picture, one for the prompt) and one decoder (for the mask).

Now look at the three masks. One click on the ear gave three different answers.

<figure><img src="/img/papers/sam/p1-ear-masks.png" alt="Three zoomed panels of the left cat's head with the click on its ear. Mask 1 in yellow covers the ear, score 0.946, 427 pixels. Mask 2 in blue covers a slightly larger ear area, score 0.944, 470 pixels. Mask 3 in dark blue covers the whole cat, score 0.980, 49,520 pixels" loading="lazy" /><figcaption>One click on the ear gives three masks. Mask 1 and mask 2 are the ear (427 and 470 pixels). Mask 3 is the whole cat (49,520 pixels). SAM's own score is highest for the whole cat, 0.980.</figcaption></figure>

All three masks are correct answers. The click was on the ear, but a person who clicks there can want the ear or the cat. The paper calls this **ambiguity**.

SAM does not choose one answer. It gives a small set of valid answers and a **score** for each one. The score is SAM's own estimate of the quality of the mask. Part 3 explains how SAM learns the score.

> [!DEFINITION] Ambiguous prompt
> A prompt that fits more than one object. One point on an ear fits the ear, the head and the whole cat. The paper's example is "a point on a shirt": the person can want the shirt or the person who wears it.

The second click shows why the score is important. The click is on the buttons of a remote control.

<figure><img src="/img/papers/sam/p1-remote-masks-full.png" alt="Three panels of the full picture with the click on the remote control. Mask 1 in yellow covers some buttons, score 0.656. Mask 2 in blue covers the whole remote control, score 0.989. Mask 3 covers the back of the sofa with many holes, score 0.793" loading="lazy" /><figcaption>One click on the buttons of the remote control. Mask 1 is a group of buttons (score 0.656). Mask 2 is the whole remote control (0.989). Mask 3 is a large region of the sofa back with many holes (0.793). The scores put the whole remote first and the bad mask second, but the bad mask has the lowest quality.</figcaption></figure>

Here, the three masks are not all valid. Mask 3 covers the back of the sofa and has many holes. SAM gives it the score 0.793, which is lower than the score 0.989 of the good mask. So the score helps an application to choose. An application can keep the mask with the highest score, or it can show all three masks to a person. The score is not perfect: here, the bad mask 3 has a higher score than the good but small mask 1.

To compare two masks, the paper uses a number called **IoU**. You will see it on almost every page of the paper, so we build it slowly.

Draw one shape on a sheet of tracing paper, and a second shape on a second sheet. Put one sheet on the other and hold them up to a lamp. Some area is dark on both sheets: this is the **overlap**. Some area is dark on at least one sheet: this is the **total area** of the two shapes together. IoU asks one question: which fraction of the total area is overlap?

> [!DEFINITION] IoU (intersection over union)
> A number from 0 to 1 that tells how much two masks overlap. Count the pixels that are in both masks (the intersection). Divide by the pixels that are in at least one mask (the union). IoU = 1 means that the two masks are identical. IoU = 0 means that they have no pixel in common.

Why do we divide by the total area, and not by the size of the object? Imagine a lazy model that marks the *whole* picture as the cat. Its mask covers the cat completely. If we divided by the size of the cat, the lazy mask would get a perfect score. With IoU, the total area is now the whole picture, so the score is low: for our cat, $$49{,}520 / 307{,}200 = 0.16$$. IoU punishes missing pixels, and it also punishes extra pixels.

Now the formula. For two masks $$A$$ and $$B$$:

$$
\text{IoU}(A, B) = \frac{|A \cap B|}{|A \cup B|} = \frac{\sum_{i,j} A_{ij} B_{ij}}{\sum_{i,j} \max(A_{ij}, B_{ij})}
$$

Read the middle part first:

- $$A \cap B$$ (say "A and B") is the overlap: the pixels that are in both masks.
- $$A \cup B$$ (say "A or B") is the pixels that are in at least one mask.
- The bars $$| \ldots |$$ mean "count the pixels".

The right part gives the same counts with the 0s and 1s of the two masks. It looks at each pixel $$(i, j)$$ one at a time. There are only four possible cases:

| $$A_{ij}$$ | $$B_{ij}$$ | $$A_{ij} B_{ij}$$: in both? | $$\max(A_{ij}, B_{ij})$$: in at least one? |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 1 |
| 0 | 1 | 0 | 1 |
| 1 | 1 | 1 | 1 |

The product is 1 only in the last row, where the pixel is in both masks. The maximum is 1 in the last three rows, where the pixel is in at least one mask. So the top sum counts the overlap, and the bottom sum counts the total area.

First, a small example. Mask $$A$$ has 4 pixels and mask $$B$$ has 4 pixels. 2 pixels are in both masks. The total area is not $$4 + 4 = 8$$, because then we count the 2 shared pixels two times: once in $$A$$ and once in $$B$$. So we subtract them one time:

$$
\text{IoU}(A, B) = \frac{2}{4 + 4 - 2} = \frac{2}{6} \approx 0.33
$$

Now the real masks. We computed the IoU of the two ear masks:

$$
\text{IoU}(\text{mask 1}, \text{mask 2}) = \frac{424}{473} = 0.896
$$

Mask 1 has 427 pixels and mask 2 has 470 pixels. 424 pixels are in both masks, and 473 pixels are in at least one mask. The check: $$427 + 470 - 424 = 473$$, because the union counts each pixel in the intersection once, not two times.

<figure class="fig"><svg viewBox="0 0 760 250" role="img" aria-label="IoU is the number of pixels in both masks divided by the number of pixels in at least one mask. For the two ear masks it is 424 divided by 473, which is 0.896."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">Intersection over union (IoU)</text><circle class="s1" cx="130" cy="120" r="62" style="fill-opacity:0.45"/><circle class="s3" cx="190" cy="120" r="62" style="fill-opacity:0.45"/><text class="t-note" x="92.0" y="124.0" text-anchor="middle">A</text><text class="t-note" x="228.0" y="124.0" text-anchor="middle">B</text><text class="t-muted" x="160.0" y="206.0" text-anchor="middle">two masks A and B</text><text class="t-note" x="400.0" y="64.0" text-anchor="middle">intersection A ∩ B</text><text class="t-muted" x="400.0" y="84.0" text-anchor="middle">pixels in both</text><line class="edge" x1="320.0" y1="104.0" x2="480.0" y2="104.0"/><text class="t-note" x="400.0" y="128.0" text-anchor="middle">union A ∪ B</text><text class="t-muted" x="400.0" y="148.0" text-anchor="middle">pixels in at least one</text><text class="t-tick" x="560.0" y="64.0" text-anchor="start">ear mask 1 vs ear mask 2</text><text class="t-tick" x="560.0" y="92.0" text-anchor="start">A = 427 pixels, B = 470 pixels</text><text class="t-tick" x="560.0" y="116.0" text-anchor="start">both: 424 pixels</text><text class="t-tick" x="560.0" y="140.0" text-anchor="start">either: 473 pixels</text><text class="t-on" x="560.0" y="168.0" text-anchor="start">IoU = 424 / 473 = 0.896</text><text class="t-muted" x="20.0" y="236.0" text-anchor="start">IoU = 1 means the two masks are identical. IoU = 0 means they share no pixel.</text></svg><figcaption>IoU is the number of pixels in both masks divided by the number of pixels in at least one mask. For the two ear masks, IoU = 424 / 473 = 0.896.</figcaption></figure>

How do you read an IoU value? 1.0 means that the two masks are identical. Two careful people who draw the same object usually agree at an IoU of 0.85 to 0.91 (§5 of the paper). So above 0.9, two masks are "the same" for most uses. 0.5 means that only half of the total area is overlap. 0 means that the masks have no pixel in common.

Now we can understand SAM's scores. They are **predicted IoU** values. To compute a real IoU, you need the correct mask. SAM does not have it: it has only the picture and the click. So SAM *estimates* the IoU of its own mask.

A score of 0.98 means: "I think that my mask and the object that you want overlap with an IoU of about 0.98". Think of a student who writes an answer and also writes how sure they are. Part 3 explains how SAM learns to make this estimate.

A box is a less ambiguous prompt than a point. A box around the right cat gives one mask:

<figure><img src="/img/papers/sam/p1-box-mask.png" alt="The full picture with a green box around the right cat. The mask in blue covers the right cat. Score 0.996, 58,209 pixels" loading="lazy" width="480" /><figcaption>A box prompt from (255, 20) to (640, 360) around the right cat. SAM gives one mask with the score 0.996: 58,209 pixels, 18.95% of the picture.</figcaption></figure>

More points also remove the ambiguity. With two clicks, one on the ear and one on the body, SAM gives one mask of the whole cat (50,214 pixels, score 0.969). Part 2 explains these cases.

The output also shows where the weights of SAM are. We counted the weights of the three released models:

```text
model                  image encoder  prompt encoder  mask decoder           total
facebook/sam-vit-base      89,670,912           6,476     4,058,340      93,735,728
facebook/sam-vit-large    308,278,272           6,476     4,058,340     312,343,088
facebook/sam-vit-huge     637,026,048           6,476     4,058,340     641,090,864
```

<figure class="fig"><svg viewBox="0 0 760 222" role="img" aria-label="The image encoder holds 95.7 to 99.4 percent of the weights; the mask decoder has 4.06 million and the prompt encoder only 6,476."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">Weights in each part of SAM (bar length = number of weights)</text><text class="t-tick" x="100.0" y="70.0" text-anchor="end">SAM ViT-B</text><g class="mark"><title>SAM ViT-B image encoder: 89,670,912</title><rect class="s2" x="110" y="56" width="55.9" height="22" rx="3"/></g><g class="mark"><title>SAM ViT-B mask decoder: 4,058,340</title><rect class="s3" x="166.9" y="56" width="2.5" height="22" rx="3"/></g><text class="t-val" x="176.5" y="72.0" text-anchor="start">93.7M in total; encoder 95.7%</text><text class="t-tick" x="100.0" y="118.0" text-anchor="end">SAM ViT-L</text><g class="mark"><title>SAM ViT-L image encoder: 308,278,272</title><rect class="s2" x="110" y="104" width="192.3" height="22" rx="3"/></g><g class="mark"><title>SAM ViT-L mask decoder: 4,058,340</title><rect class="s3" x="303.3" y="104" width="2.5" height="22" rx="3"/></g><text class="t-val" x="312.9" y="120.0" text-anchor="start">312.3M in total; encoder 98.7%</text><text class="t-tick" x="100.0" y="166.0" text-anchor="end">SAM ViT-H</text><g class="mark"><title>SAM ViT-H image encoder: 637,026,048</title><rect class="s2" x="110" y="152" width="397.5" height="22" rx="3"/></g><g class="mark"><title>SAM ViT-H mask decoder: 4,058,340</title><rect class="s3" x="508.5" y="152" width="2.5" height="22" rx="3"/></g><text class="t-val" x="518.0" y="168.0" text-anchor="start">641.1M in total; encoder 99.4%</text><rect class="s2" x="110" y="199" width="10" height="10" rx="2"/><text class="t-tick" x="126.0" y="208.0" text-anchor="start">image encoder</text><rect class="s3" x="241.60000000000002" y="199" width="10" height="10" rx="2"/><text class="t-tick" x="257.6" y="208.0" text-anchor="start">mask decoder (4.06M)</text><rect class="s1" x="423.6" y="199" width="10" height="10" rx="2"/><text class="t-tick" x="439.6" y="208.0" text-anchor="start">prompt encoder: 6,476 weights, too small to see</text></svg><figcaption>The image encoder holds 95.7% (ViT-B) to 99.4% (ViT-H) of all weights. The mask decoder has 4.06 million weights and the prompt encoder has only 6,476. The three models differ only in the image encoder.</figcaption></figure>

> [!DEFINITION] Parameter (weight)
> One learned number in a model. SAM with ViT-H has about 641 million parameters. Training sets their values, and after training they do not change.

The three models have the same prompt encoder and the same mask decoder. Only the image encoder changes: ViT-B (Base), ViT-L (Large) or ViT-H (Huge).

The paper's Figure 13 gives the sizes 91M, 308M and 636M. Our counts of the released image encoders are 89.7M, 308.3M and 637.0M. The small differences can come from which layers each count includes. In all three models, almost all the weights are in the image encoder. This split is the reason for the "amortized real-time" claim in §1, as we will see.

## Foundation models in language {§1}

Now we read the introduction. It starts with language models.

> [!PAPER] · §1 Introduction · page 1
> [![The first paragraph of the introduction: Large language models pre-trained on web-scale datasets are revolutionizing NLP with strong zero-shot and few-shot generalization. These foundation models can generalize to tasks and data distributions beyond those seen during training. This capability is often implemented with prompt engineering in which hand-crafted text is used to prompt the language model to generate a valid textual response for the task at hand](https://ishwarj.com/img/papers/sam/p1-intro-llm-1.png/p1-intro-llm-1.png)](/img/papers/sam/p1-intro-llm-1.png)
>
> **What it says:** large language models, trained on text from the web, show "strong zero-shot and few-shot generalization". The paper calls them "foundation models". You use them with "prompt engineering": you write a text that makes the model give "a valid textual response for the task at hand".
>
> **What it means:**
>
> Models such as GPT-3 first read a very large amount of text from the internet. This is the "pre-training". After that, they do new tasks with no examples (zero-shot) or with a few examples in the prompt (few-shot). The paper calls them "foundation models": one model is the base for many tasks, as the foundation of a building carries many rooms. To use one, a person writes the prompt carefully, by hand. This is "prompt engineering".
>
> Why does a paper about pictures start with language models? Because the authors want to copy their recipe: much data, one large training, and then a prompt for each new task. The rest of the paper uses this recipe for pictures.
>
> Remember the word "valid" in "a valid textual response". A question can have many good answers. The model must give *one* good answer, not *the* one exact answer. §2 uses the same word for masks.

> [!DEFINITION] Large language model (LLM)
> A very large neural network that has training on text from the internet. Its job in training is to predict the next word. GPT-3 is an example. The abbreviation NLP means **natural language processing**, the field that works with text.

> [!DEFINITION] Few-shot
> Give the model a few examples of the task in the prompt, and no more training. For example: "cat → chat, dog → chien, bird → ?". Zero-shot gives no example. Few-shot gives a small number of examples.

> [!DEFINITION] Prompt engineering
> The work to find a prompt that makes a trained model do the task that you want. For language models, you change the words of the prompt. For SAM, you change the points, boxes or masks that you give it, or you build a program that makes them.

The paragraph continues in the right column:

> [!PAPER] · §1 Introduction · page 1
> [![The end of the first paragraph: When scaled and trained with abundant text corpora from the web, these models' zero and few-shot performance compares surprisingly well to (even matching in some cases) fine-tuned models. Empirical trends show this behavior improving with model scale, dataset size, and total training compute](https://ishwarj.com/img/papers/sam/p1-intro-llm-2.png/p1-intro-llm-2.png)](/img/papers/sam/p1-intro-llm-2.png)
>
> **What it says:** zero-shot and few-shot results of large models are near to fine-tuned models, and they become better "with model scale, dataset size, and total training compute".
>
> **What it means:**
>
> A large model with no training for a task is almost as good as a model with special training for that task. It also says that this becomes better with three things: a larger model, more data and more computing power. "Empirical" means that people saw this in experiments. It is an observation, not a proof.
>
> Now ask: which of the three can the SAM team increase? A larger model is easy: use a larger network. More computing power is easy for Meta: use more GPUs. More data is not possible, because no large set of masks exists. So the one blocked control is the data. This is why half of the paper is about data.

The paper cites GPT-3 here as reference [10]. The GPT-3 abstract states the zero-shot and few-shot idea clearly:

> [!PAPER] Brown et al. (2020), GPT-3 · Abstract · page 1
> [![The GPT-3 abstract: we train GPT-3, an autoregressive language model with 175 billion parameters, and test its performance in the few-shot setting. For all tasks, GPT-3 is applied without any gradient updates or fine-tuning, with tasks and few-shot demonstrations specified purely via text interaction with the model](https://ishwarj.com/img/papers/sam/p1-gpt3-abstract.png1-gpt3-abstract.png)](/img/papers/sam/p1-gpt3-abstract.png)
>
> **What it says:** GPT-3 has "175 billion parameters". It does each task "without any gradient updates or fine-tuning". The task is "specified purely via text interaction with the model".
>
> **What it means:**
>
> GPT-3 has 175 billion learned numbers. SAM ViT-H has 641 million, about 270 times fewer. "Without any gradient updates" means that these numbers do not change. A gradient update is the step of training that changes the numbers, so no update means no training. You give the task only with words.
>
> Think of a calculator that you never program again. You only type different questions. This is GPT-3 with prompts. SAM wants the same property: its weights stay fixed, and only the clicks and the boxes change.

The figure puts the two kinds of prompt side by side.

<figure class="fig"><svg viewBox="0 0 760 262" role="img" aria-label="Left: a language model gets a text prompt and returns text. Right: SAM gets a point, box, mask or text prompt plus a picture, and returns a mask."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="190.0" y="24.0" text-anchor="middle">Language model</text><text class="t-title" x="570.0" y="24.0" text-anchor="middle">SAM</text><line class="edge-dim" x1="380.0" y1="12.0" x2="380.0" y2="260.0"/><rect class="box-1" x="40.0" y="44.0" width="300.0" height="54.0" rx="8"/><text class="t-tick" x="190.0" y="66.0" text-anchor="middle">prompt (text):</text><text class="t-note" x="190.0" y="86.0" text-anchor="middle">"Translate to French: cat"</text><line class="edge" x1="190.0" y1="102.0" x2="190.0" y2="128.0" marker-end="url(#ah)"/><rect class="box-on" x="110.0" y="132.0" width="160.0" height="40.0" rx="8"/><text class="t-note" x="190.0" y="157.0" text-anchor="middle">model</text><line class="edge" x1="190.0" y1="176.0" x2="190.0" y2="202.0" marker-end="url(#ah)"/><rect class="box-3" x="40.0" y="206.0" width="300.0" height="40.0" rx="8"/><text class="t-note" x="190.0" y="231.0" text-anchor="middle">answer (text): "chat"</text><rect class="box-1" x="420.0" y="44.0" width="70.0" height="54.0" rx="8"/><text class="t-tick" x="455.0" y="90.0" text-anchor="middle">point</text><rect class="box-1" x="500.0" y="44.0" width="70.0" height="54.0" rx="8"/><text class="t-tick" x="535.0" y="90.0" text-anchor="middle">box</text><rect class="box-1" x="580.0" y="44.0" width="70.0" height="54.0" rx="8"/><text class="t-tick" x="615.0" y="90.0" text-anchor="middle">mask</text><rect class="box-1" x="660.0" y="44.0" width="70.0" height="54.0" rx="8"/><text class="t-tick" x="695.0" y="90.0" text-anchor="middle">text</text><circle class="s3" cx="455" cy="66" r="6"/><rect class="edge-on" x="516" y="54" width="38" height="24" rx="2" style="fill:none"/><ellipse class="s1" cx="615" cy="66" rx="20" ry="11" style="fill-opacity:0.7"/><text class="t-tick" x="695.0" y="70.0" text-anchor="middle">"cat"</text><line class="edge" x1="570.0" y1="102.0" x2="570.0" y2="128.0" marker-end="url(#ah)"/><rect class="box-on" x="490.0" y="132.0" width="160.0" height="40.0" rx="8"/><text class="t-note" x="570.0" y="157.0" text-anchor="middle">SAM + the picture</text><line class="edge" x1="570.0" y1="176.0" x2="570.0" y2="202.0" marker-end="url(#ah)"/><rect class="box-3" x="400.0" y="206.0" width="340.0" height="40.0" rx="8"/><text class="t-note" x="570.0" y="231.0" text-anchor="middle">answer: a valid mask (and a score)</text></svg><figcaption>Left: a language model gets a text prompt and gives text. Right: SAM gets a point, a box, a mask or a text prompt, together with a picture, and it gives a valid mask and a score.</figcaption></figure>

## Foundation models in vision {§1}

> [!PAPER] · §1 Introduction · page 1
> [![The second paragraph of the introduction: Foundation models have also been explored in computer vision, albeit to a lesser extent. Perhaps the most prominent illustration aligns paired text and images from the web. For example, CLIP and ALIGN use contrastive learning to train text and image encoders that align the two modalities. Once trained, engineered text prompts enable zero-shot generalization to novel visual concepts and data distributions. Such encoders also compose effectively with other modules to enable downstream tasks, such as image generation (e.g., DALL·E). While much progress has been made on vision and language encoders, computer vision includes a wide range of problems beyond this scope, and for many of these, abundant training data does not exist](https://ishwarj.com/img/papers/sam/p1-intro-clip.pngm/p1-intro-clip.png)](/img/papers/sam/p1-intro-clip.png)
>
> **What it says:** in vision, the best known foundation models are CLIP and ALIGN. They "use contrastive learning to train text and image encoders". Text prompts then give "zero-shot generalization to novel visual concepts". These encoders also work as parts of larger systems, for example image generation with DALL·E. But for many vision problems, "abundant training data does not exist".
>
> **What it means:**
>
> In vision, the best known foundation models are CLIP and ALIGN. The web has many pictures with text next to them, for example captions. These pairs are free training data, and CLIP learns which caption goes with which picture. After the training, you write "a photo of a dog", and CLIP finds the pictures of dogs with no more training. CLIP can also work as one part of a larger system, as a brick works as one part of a wall. DALL·E uses it in this way.
>
> The most important sentence is the last one: for many vision problems, "abundant training data does not exist". CLIP worked because its data was free. Almost every picture on the web has some words near it. But nobody draws the exact outline of each object on web photos. So the method of CLIP, "collect the data from the web", does not work for masks.

> [!DEFINITION] Contrastive learning
> A training method that puts matching pairs near each other and non-matching pairs far apart. CLIP gets pictures with their captions. It learns to give a picture and its own caption similar embeddings, and to give a picture and a wrong caption different embeddings.

> [!DEFINITION] Modality
> One kind of data: text, pictures, sound or video. "Align the two modalities" means: make the embeddings of text and of pictures comparable, so that a picture of a dog and the text "a dog" give similar vectors.

The CLIP abstract gives the method in one sentence:

> [!PAPER] Radford et al. (2021), CLIP · Abstract · page 1
> [![Part of the CLIP abstract: the simple pre-training task of predicting which caption goes with which image is an efficient and scalable way to learn state-of-the-art image representations from scratch on a dataset of 400 million (image, text) pairs collected from the internet. After pre-training, natural language is used to reference learned visual concepts, enabling zero-shot transfer of the model to downstream tasks](https://ishwarj.com/img/papers/sam/p1-clip-abstract.png1-clip-abstract.png)](/img/papers/sam/p1-clip-abstract.png)
>
> **What it says:** CLIP learns by "predicting which caption goes with which image", on "400 million (image, text) pairs collected from the internet". After training, "natural language is used to reference learned visual concepts", and this gives "zero-shot transfer".
>
> **What it means:**
>
> CLIP learns with a matching game. It gets a large group of pictures and their captions, mixed, and it must find the correct caption for each picture. The game is cheap, and it becomes better with more data. Nobody made the 400 million labels for CLIP: the captions already existed on the web.
>
> Compare this with SAM. A CLIP label cost almost nothing. A SAM label is a mask, and even with SAM's help, a person needed 14 to 34 seconds for one mask (Part 4). At 14 seconds for each mask, 1.1 billion masks take about 4.3 million hours of work. SAM must find a different way.

The paragraph also says that CLIP "composes" with other modules: DALL·E uses CLIP as one part of an image generator. Composition is an important idea in SAM. In Part 2 and Part 5 you will see SAM as one part of a larger system. For example, a detector finds the boxes, and SAM makes the masks.

## The goal: a foundation model for segmentation {§1}

> [!PAPER] · §1 Introduction · page 1
> [![The goal paragraph: In this work, our goal is to build a foundation model for image segmentation. That is, we seek to develop a promptable model and pre-train it on a broad dataset using a task that enables powerful generalization. With this model, we aim to solve a range of downstream segmentation problems on new data distributions using prompt engineering. The success of this plan hinges on three components: task, model, and data. To develop them, we address the following questions about image segmentation: 1. What task will enable zero-shot generalization? 2. What is the corresponding model architecture? 3. What data can power this task and model?](https://ishwarj.com/img/papers/sam/p1-intro-goal.pngm/p1-intro-goal.png)](/img/papers/sam/p1-intro-goal.png)
>
> **What it says:** "our goal is to build a foundation model for image segmentation". The model is "promptable". It has pre-training "on a broad dataset", and then it solves new segmentation problems "using prompt engineering". The plan has "three components: task, model, and data", and three questions.
>
> **What it means:**
>
> The goal is one model for many segmentation jobs. First, the model has one large training on many types of pictures. Then it solves new jobs, on new types of pictures, with prompts. The difficult part is the training job.
>
> For GPT-3, the job "predict the next word" teaches grammar, facts and some reasoning. Which job teaches a general skill for pictures? This is the open question.
>
> This paragraph is the recipe of the language paragraph, with some words changed. "Language model" becomes "segmentation model", and "text prompt" becomes "segmentation prompt". The three questions are also the plan of the paper. §2 answers question 1, §3 answers question 2, and §4 and §5 answer question 3.

The term "foundation model" comes from a 2021 report from Stanford. The SAM paper cites it as [8] and uses its definition in §8:

> [!PAPER] Bommasani et al. (2021), On the Opportunities and Risks of Foundation Models · §1 · page 3
> [![The definition from the foundation models report: A foundation model is any model that is trained on broad data (generally using self-supervision at scale) that can be adapted (e.g., fine-tuned) to a wide range of downstream tasks](https://ishwarj.com/img/papers/sam/p1-foundation-def.png-foundation-def.png)](/img/papers/sam/p1-foundation-def.png)
>
> **What it says:** a foundation model is "trained on broad data (generally using self-supervision at scale)" and it "can be adapted (e.g., fine-tuned) to a wide range of downstream tasks".
>
> **What it means:**
>
> A foundation model learns from broad data: many types of data, not one small type. Usually, the data gives its own labels. For text, the next word is the label, and nobody writes it by hand. This is "self-supervision". After the training, you can change the model a little for many later jobs.
>
> SAM fits most of this definition. Its data is broad, and it adapts to many jobs with prompts. But SAM does not use self-supervision. It learns from masks that a person or SAM drew, and these masks are labels. The paper says this itself in §8 (Part 6).

> [!DEFINITION] Foundation model
> One large model, trained once on broad data, that you can use for many different tasks. You adapt it with a prompt or with a small amount of more training. You do not train a new model for each task.

> [!DEFINITION] Downstream task
> A task that you do with a model after its main training. For SAM, downstream tasks include edge detection, object proposals and instance segmentation (Part 5). "Downstream" means "later in the process".

The three questions are:

1. **What task will enable zero-shot generalization?** The task must teach a skill that is useful for many other tasks. For language, the task is "predict the next word". For SAM, the task is "give a valid mask for any prompt" (§2, Part 2).
2. **What is the corresponding model architecture?** The model must accept many types of prompt. It must be fast enough for a person to use it, and it must deal with ambiguity (§3, Part 3).
3. **What data can power this task and model?** The data must be very large and very diverse. No such data existed, so the team made it (§4 and §5, Part 4).

## The three answers depend on each other {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The paragraph at the top of page 2: These questions are entangled and require a comprehensive solution. We start by defining a promptable segmentation task that is general enough to provide a powerful pre-training objective and to enable a wide range of downstream applications. This task requires a model that supports flexible prompting and can output segmentation masks in real-time when prompted to allow for interactive use. To train our model, we need a diverse, large-scale source of data. Unfortunately, there is no web-scale data source for segmentation; to address this, we build a data engine, i.e., we iterate between using our efficient model to assist in data collection and using the newly collected data to improve the model](https://ishwarj.com/img/papers/sam/p1-intro-entangled.pngintro-entangled.png)](/img/papers/sam/p1-intro-entangled.png)
>
> **What it says:** "These questions are entangled". The task needs a model that can "output segmentation masks in real-time". The model needs a lot of data, but "there is no web-scale data source for segmentation". So the team builds "a data engine": the model helps to collect data, and the new data makes the model better.
>
> **What it means:**
>
> "Entangled" means that the questions are tied together, as strings in a knot. You cannot pull one string without moving the others. Follow the knot from one end to the other:
>
> 1. The **task** says: any hint must give a valid mask.
> 2. To learn this task, the **model** needs very many masks.
> 3. These masks do not exist, so people must make them, with the model as a tool.
> 4. People can use the model as a tool only if it is fast. So the **model must be fast**.
>
> So the speed is not mainly for the final users. It is a requirement of the data plan: the team's own annotators need it.

> [!DEFINITION] Real-time and interactive
> **Real-time** means that the answer comes so fast that a person does not wait for it. For a click, this is about 50 milliseconds or less. **Interactive** means that a person and the model work together in a loop. The person clicks, and the model shows a mask. Then the person clicks again to correct the mask.

## The task {§1}

The next paragraphs of §1 give a short preview of each section of the paper. The first preview is the task.

> [!PAPER] · §1 Introduction · page 2
> [![The Task paragraph: In NLP and more recently computer vision, foundation models are a promising development that can perform zero-shot and few-shot learning for new datasets and tasks often by using prompting techniques. Inspired by this line of work, we propose the promptable segmentation task, where the goal is to return a valid segmentation mask given any segmentation prompt. A prompt simply specifies what to segment in an image, e.g., a prompt can include spatial or text information identifying an object. The requirement of a valid output mask means that even when a prompt is ambiguous and could refer to multiple objects (for example, a point on a shirt may indicate either the shirt or the person wearing it), the output should be a reasonable mask for at least one of those objects. We use the promptable segmentation task as both a pre-training objective and to solve general downstream segmentation tasks via prompt engineering](https://ishwarj.com/img/papers/sam/p1-intro-task.pngm/p1-intro-task.png)](/img/papers/sam/p1-intro-task.png)
>
> **What it says:** the task is to "return a valid segmentation mask given any segmentation prompt". A prompt "simply specifies what to segment". When a prompt is ambiguous, for example "a point on a shirt may indicate either the shirt or the person wearing it", the output must be "a reasonable mask for at least one of those objects". The same task is "both a pre-training objective" and the way to solve other tasks.
>
> **What it means:**
>
> The task is simple to say: a hint goes in, and a mask comes out. "Any" is a strong word here. It means any type of hint, on any picture. The hint can be a position (a point or a box) or words.
>
> The difficult case is an ambiguous hint, a hint with two or more meanings. You touch a shirt in a photo and you say "this". Do you mean the shirt or the person? Both are fair answers, so the paper accepts a mask of either one. But the rule is not empty: a mask of half the shirt and half the wall is not reasonable.
>
> Why does the paper say "valid" and not "correct"? A "correct" mask needs one true answer. For an ambiguous click, there is no one true answer, so a strict teacher would punish good answers. "Valid" makes the job fair. Part 3 shows how training does this: SAM makes three masks, and only the best of the three gets the error signal.

Our ear click is an example. "The ear" and "the cat" are both valid masks. A mask of half the ear and half the sofa is not valid. SAM gave the ear and the cat, so its answer is valid for this click.

## The model {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The Model paragraph: the promptable segmentation task and the goal of real-world use impose constraints on the model architecture. The model must support flexible prompts, needs to compute masks in amortized real-time to allow interactive use, and must be ambiguity-aware. A simple design satisfies all three constraints: a powerful image encoder computes an image embedding, a prompt encoder embeds prompts, and then the two information sources are combined in a lightweight mask decoder that predicts segmentation masks. By separating SAM into an image encoder and a fast prompt encoder and mask decoder, the same image embedding can be reused with different prompts. Given an image embedding, the prompt encoder and mask decoder predict a mask from a prompt in about 50ms in a web browser. To make SAM ambiguity-aware, it predicts multiple masks for a single prompt](https://ishwarj.com/img/papers/sam/p1-intro-model.png/p1-intro-model.png)](/img/papers/sam/p1-intro-model.png)
>
> **What it says:** the model has three constraints. It must "support flexible prompts", compute masks "in amortized real-time", and be "ambiguity-aware". The design has three blocks: an "image encoder", a "prompt encoder" and a "lightweight mask decoder". The image embedding is computed once and "reused" for different prompts. The prompt encoder and the mask decoder take "∼50ms in a web browser". For ambiguity, SAM can "predict multiple masks for a single prompt".
>
> **What it means:**
>
> The task gives three rules for the design, and each rule gives one part of the model. The model must accept many types of hint, so it has a **prompt encoder** that changes a click or a box into numbers. The model must be fast, so the work is in two pieces.
>
> A large **image encoder** reads the picture once and writes a summary of it as numbers. A small **mask decoder** then uses this summary to answer each click. The same summary serves every click on that picture. A hint can have more than one meaning, so the decoder gives three masks, as you saw on the cat's ear.
>
> Read "Given an image embedding" carefully. The 50 ms (one twentieth of a second) is the time of the small parts only. The image encoder must run first, and it is much slower. We measure both times below.

The word **amortized** needs an explanation. The image encoder is slow, but it runs only one time for each picture. The decoder is fast, and it runs one time for each prompt. When a person clicks many times on the same picture, the cost of the encoder is shared among all the clicks.

> [!DEFINITION] Amortized cost
> A large cost that you pay one time and then share among many uses. If a tool costs 100 and you use it 100 times, its amortized cost is 1 for each use. For SAM, the encoder time is shared among all the prompts on the same picture.

We measured the two times on our computer:

```text
facebook/sam-vit-base: image encoder   756.2 ms (GPU)    1444.3 ms (CPU)   prompt encoder + mask decoder   5.7 ms (GPU)   17.6 ms (CPU)
facebook/sam-vit-huge: image encoder  3069.4 ms (GPU)    6809.4 ms (CPU)   prompt encoder + mask decoder   5.8 ms (GPU)   17.5 ms (CPU)
```

For ViT-H, the image encoder takes 3,069 ms on the laptop GPU. The prompt encoder and the mask decoder take 17.5 ms on the CPU. The paper's number is "∼50 ms in a web browser", also on a CPU. A web browser is slower than native PyTorch, so the two numbers agree well.

Think of a coffee machine. You pay a large amount one time for the machine: this is the encoder, 3,069 ms for each picture. Then each cup costs a small amount: this is the decoder, 17.5 ms for each click. If you make only one cup, that cup costs the full machine plus 17.5. If you make 100 cups, the 100 cups share the cost of the machine.

With $$k$$ prompts on one picture, the time for each prompt is the total time divided by $$k$$:

$$
T_{\text{prompt}}(k) = \frac{T_{\text{enc}} + k \, T_{\text{dec}}}{k} = \frac{T_{\text{enc}}}{k} + T_{\text{dec}}
$$

where:

- $$T_{\text{enc}}$$ is the time of the image encoder, which runs once (3,069 ms);
- $$T_{\text{dec}}$$ is the time of the prompt encoder and the mask decoder, which run once for each prompt (17.5 ms);
- $$k$$ is the number of prompts on the same picture.

Read the right side. The first part, $$T_{\text{enc}} / k$$, is the share of the encoder for one prompt. It becomes smaller when $$k$$ becomes larger. The second part, $$T_{\text{dec}}$$, does not change. So the time for each prompt comes near to 17.5 ms after many clicks, but it never goes below 17.5 ms.

With our numbers:

$$
\begin{aligned}
T_{\text{prompt}}(1) &= 3069 + 17.5 \approx 3087 \text{ ms} \\
T_{\text{prompt}}(10) &= 306.9 + 17.5 \approx 324 \text{ ms} \\
T_{\text{prompt}}(100) &= 30.7 + 17.5 \approx 48 \text{ ms}
\end{aligned}
$$

<figure class="fig"><svg viewBox="0 0 760 224" role="img" aria-label="Time per prompt when one image embedding is reused for 1, 10 and 100 prompts: about 3,087, 324 and 48 milliseconds."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">Milliseconds per prompt, ViT-H (log scale)</text><line class="grid" x1="170.0" y1="44.0" x2="170.0" y2="170.0"/><text class="t-muted" x="170.0" y="186.0" text-anchor="middle">10 ms</text><line class="grid" x1="256.1" y1="44.0" x2="256.1" y2="170.0"/><text class="t-muted" x="256.1" y="186.0" text-anchor="middle">30 ms</text><line class="grid" x1="350.5" y1="44.0" x2="350.5" y2="170.0"/><text class="t-muted" x="350.5" y="186.0" text-anchor="middle">100 ms</text><line class="grid" x1="436.7" y1="44.0" x2="436.7" y2="170.0"/><text class="t-muted" x="436.7" y="186.0" text-anchor="middle">300 ms</text><line class="grid" x1="531.1" y1="44.0" x2="531.1" y2="170.0"/><text class="t-muted" x="531.1" y="186.0" text-anchor="middle">1,000 ms</text><line class="grid" x1="617.2" y1="44.0" x2="617.2" y2="170.0"/><text class="t-muted" x="617.2" y="186.0" text-anchor="middle">3,000 ms</text><text class="t-tick" x="160.0" y="72.0" text-anchor="end">1 prompt per picture</text><g class="mark"><title>1 prompt: 3086.9 ms per prompt</title><rect class="s2" x="170" y="58" width="449.0" height="20" rx="3"/></g><text class="t-val" x="627.0" y="73.0" text-anchor="start">3,086.9 ms</text><text class="t-tick" x="160.0" y="112.0" text-anchor="end">10 prompts per picture</text><g class="mark"><title>10 prompts: 324.4 ms per prompt</title><rect class="s2" x="170" y="98" width="272.8" height="20" rx="3"/></g><text class="t-val" x="450.8" y="113.0" text-anchor="start">324.4 ms</text><text class="t-tick" x="160.0" y="152.0" text-anchor="end">100 prompts per picture</text><g class="mark"><title>100 prompts: 48.2 ms per prompt</title><rect class="s2" x="170" y="138" width="123.3" height="20" rx="3"/></g><text class="t-val" x="301.3" y="153.0" text-anchor="start">48.2 ms</text><line class="edge-on" x1="296.2" y1="36.0" x2="296.2" y2="172.0"/><text class="t-on" x="302.2" y="36.0" text-anchor="start">the paper's ~50 ms</text><text class="t-muted" x="20.0" y="212.0" text-anchor="start">(encoder 3,069 ms on the GPU, once) / k  +  decoder 17.5 ms on the CPU, per prompt</text></svg><figcaption>Time for each prompt when one image embedding is reused for 1, 10 and 100 prompts on our computer: about 3,087, 324 and 48 milliseconds. The encoder time is shared; the decoder time stays at 17.5 ms.</figcaption></figure>

This is the meaning of "amortized real-time". The first click on a new picture is slow. All the next clicks are fast. In the annotation tool of the paper, the server computes the image embedding before the annotator starts. The annotator then sees only the decoder time. This is possible because 99.4% of the weights are in the encoder, and the 4.1-million-weight decoder is very small.

> [!NOTE] What "real-time" does not mean here
> The full SAM with ViT-H is not real-time for a new picture. The encoder takes seconds on a laptop and about half a second on a large server GPU. The paper states this limit in §8. Only the prompt step is real-time.

## The data engine {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The Data engine paragraph: To achieve strong generalization to new data distributions, we found it necessary to train SAM on a large and diverse set of masks, beyond any segmentation dataset that already exists. While a typical approach for foundation models is to obtain data online, masks are not naturally abundant and thus we need an alternative strategy. Our solution is to build a data engine, i.e., we co-develop our model with model-in-the-loop dataset annotation. Our data engine has three stages: assisted-manual, semi-automatic, and fully automatic. In the first stage, SAM assists annotators in annotating masks, similar to a classic interactive segmentation setup. In the second stage, SAM can automatically generate masks for a subset of objects by prompting it with likely object locations and annotators focus on annotating the remaining objects,](https://ishwarj.com/img/papers/sam/p1-intro-data-1.pngp1-intro-data-1.png)](/img/papers/sam/p1-intro-data-1.png)
>
> **What it says:** SAM needs "a large and diverse set of masks, beyond any segmentation dataset that already exists". Foundation models usually get their data online, but "masks are not naturally abundant". The solution is "model-in-the-loop dataset annotation" in "three stages: assisted-manual, semi-automatic, and fully automatic".
>
> **What it means:**
>
> The team learned from experiments that SAM needs very many masks. Part 5 shows one of them. With 0.1 million pictures, SAM is clearly worse. With 1 million pictures, it is almost as good as with all 11 million. So "large" means millions of pictures, not thousands.
>
> GPT-3 and CLIP got their data online, but nobody puts masks on the web. So the team made the masks, and the model and the data grew together.
>
> Think of a new cook in a restaurant kitchen. First, the chef cooks, and the new cook helps (assisted-manual). Then the new cook makes the simple dishes, and the chef makes the difficult ones (semi-automatic). At the end, the new cook makes all the dishes alone (fully automatic). SAM is the new cook, and after each stage it has more training and becomes better.

> [!PAPER] · §1 Introduction · page 2
> [![The end of the Data engine paragraph: helping increase mask diversity. In the final stage, we prompt SAM with a regular grid of foreground points, yielding on average about 100 high-quality masks per image](https://ishwarj.com/img/papers/sam/p1-intro-data-2.pngp1-intro-data-2.png)](/img/papers/sam/p1-intro-data-2.png)
>
> **What it says:** in the last stage, SAM gets "a regular grid of foreground points", and it gives "on average ∼100 high-quality masks per image".
>
> **What it means:**
>
> A "regular grid" is a set of points at equal distances over the full picture, as the holes in a net. Imagine a person who clicks once on each point of a 32 × 32 net over the photo. That is 1,024 clicks. Each click gives three masks, so there are up to 3,072 masks. Many are bad, and many are copies of each other. SAM keeps the good ones, and about 100 stay.
>
> No person does these clicks: a program does them. This is how one model can label 11 million pictures.

The three stages, in short (Part 4 gives the details):

1. **Assisted-manual.** A person clicks, SAM proposes a mask, and the person corrects it. 4.3 million masks on 120,000 pictures.
2. **Semi-automatic.** SAM first adds the masks that it is sure of. The person then labels the objects that SAM missed. 5.9 million more masks on 180,000 pictures.
3. **Fully automatic.** SAM gets a grid of 32 × 32 points on each picture and makes all the masks. 1.1 billion masks on 11 million pictures.

> [!DEFINITION] Model-in-the-loop
> A way to make data where a model helps the people who label the data. The model proposes an answer, and a person accepts or corrects it. The corrected data then trains a better model.

## The dataset {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The Dataset paragraph: Our final dataset, SA-1B, includes more than 1B masks from 11M licensed and privacy-preserving images. SA-1B, collected fully automatically using the final stage of our data engine, has 400 times more masks than any existing segmentation dataset, and as we verify extensively, the masks are of high quality and diversity. Beyond its use in training SAM to be robust and general, we hope SA-1B becomes a valuable resource for research aiming to build new foundation models](https://ishwarj.com/img/papers/sam/p1-intro-dataset.png1-intro-dataset.png)](/img/papers/sam/p1-intro-dataset.png)
>
> **What it says:** SA-1B has "more than 1B masks from 11M licensed and privacy-preserving images". It was "collected fully automatically", and it has "400× more masks than any existing segmentation dataset".
>
> **What it means:**
>
> No person drew the released masks. SAM drew all 1.1 billion of them, about 100 on each picture. The largest dataset before SA-1B had about 2.7 million masks, so SA-1B has about 400 times more.
>
> But SAM wrote its own textbook. If SAM makes a type of error, the dataset contains the same error. So the team checked the masks. People corrected the masks on 500 pictures, and the team compared each SAM mask with its corrected mask. For 94% of the pairs, the overlap was more than 90% (Part 4 explains this check).

The paper's Figure 6 gives the size of the other datasets. We put them on one scale:

<figure class="fig"><svg viewBox="0 0 760 254" role="img" aria-label="SA-1B has 1,129 million masks on 11 million images, against 2.7 million masks for Open Images, the largest earlier set."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><text class="t-title" x="20.0" y="24.0" text-anchor="start">Masks in each dataset, in millions (log scale)</text><line class="grid" x1="120.0" y1="48.0" x2="120.0" y2="224.0"/><text class="t-muted" x="120.0" y="240.0" text-anchor="middle">0.1M</text><line class="grid" x1="193.7" y1="48.0" x2="193.7" y2="224.0"/><text class="t-muted" x="193.7" y="240.0" text-anchor="middle">1M</text><line class="grid" x1="267.4" y1="48.0" x2="267.4" y2="224.0"/><text class="t-muted" x="267.4" y="240.0" text-anchor="middle">10M</text><line class="grid" x1="341.1" y1="48.0" x2="341.1" y2="224.0"/><text class="t-muted" x="341.1" y="240.0" text-anchor="middle">100M</text><line class="grid" x1="414.8" y1="48.0" x2="414.8" y2="224.0"/><text class="t-muted" x="414.8" y="240.0" text-anchor="middle">1B</text><text class="t-tick t-strong" x="110.0" y="74.0" text-anchor="end">SA-1B</text><g class="mark"><title>SA-1B: 1129M masks on 11M images</title><rect class="s3" x="120" y="60" width="298.7" height="20" rx="3"/></g><text class="t-val" x="426.7" y="75.0" text-anchor="start">1,129M masks · 11M images · 103 per image</text><text class="t-tick" x="110.0" y="108.0" text-anchor="end">Open Images</text><g class="mark"><title>Open Images: 2.7M masks on 1M images</title><rect class="s1" x="120" y="94" width="105.5" height="20" rx="3"/></g><text class="t-val" x="233.5" y="109.0" text-anchor="start">2.7M masks · 1M images · 3 per image</text><text class="t-tick" x="110.0" y="142.0" text-anchor="end">LVIS v1</text><g class="mark"><title>LVIS v1: 1.5M masks on 0.12M images</title><rect class="s1" x="120" y="128" width="86.7" height="20" rx="3"/></g><text class="t-val" x="214.7" y="143.0" text-anchor="start">1.5M masks · 0.12M images · 12 per image</text><text class="t-tick" x="110.0" y="176.0" text-anchor="end">COCO</text><g class="mark"><title>COCO: 0.9M masks on 0.123M images</title><rect class="s1" x="120" y="162" width="70.3" height="20" rx="3"/></g><text class="t-val" x="198.3" y="177.0" text-anchor="start">0.9M masks · 0.123M images · 7 per image</text><text class="t-tick" x="110.0" y="210.0" text-anchor="end">ADE20K</text><g class="mark"><title>ADE20K: 0.7M masks on 0.028M images</title><rect class="s1" x="120" y="196" width="62.3" height="20" rx="3"/></g><text class="t-val" x="190.3" y="211.0" text-anchor="start">0.7M masks · 0.028M images · 25 per image</text></svg><figcaption>Masks in each dataset, on a log scale. SA-1B has 1,129 million masks on 11 million pictures, about 103 masks for each picture. Open Images, the largest earlier dataset, has 2.7 million masks on 1 million pictures.</figcaption></figure>

The ratios are simple divisions:

$$
\frac{1129 \text{M masks}}{2.7 \text{M masks}} \approx 418, \qquad \frac{11 \text{M images}}{1 \text{M images}} = 11, \qquad \frac{1129 \text{M}}{11 \text{M}} \approx 103 \text{ masks per image}
$$

The paper rounds the first ratio down to "400×". It also says that SA-1B has "36× more masks per image" than Open Images. With the rounded numbers of the figure we get $$103 / 2.7 \approx 38$$. The small difference comes from the rounding of the dataset sizes.

> [!DEFINITION] Log scale
> An axis where each step multiplies the value by the same number, for example by 10: 1, 10, 100, 1,000. A log scale lets you show 0.7 million and 1,129 million on the same chart. On a normal axis, the small bars would be too short to see.

Page 3 of the paper shows sample pictures of SA-1B with all their masks:

> [!PAPER] · Figure 2 · page 3
> [![Figure 2 of the paper: a full page of photos with coloured masks over them, in rows grouped by the number of masks per image, from fewer than 50 masks to more than 500 masks. The caption says that SA-1B contains 11M diverse, high-resolution, licensed, and privacy protecting images and 1.1B high-quality segmentation masks, annotated fully automatically by SAM](https://ishwarj.com/img/papers/sam/p1-figure2.png/sam/p1-figure2.png)](/img/papers/sam/p1-figure2.png)
>
> **What it says:** SA-1B has "11M diverse, high-resolution, licensed, and privacy protecting images and **1.1B high-quality segmentation masks**". The masks "were **annotated fully automatically by SAM**". The rows group the pictures by the number of masks, from fewer than 50 to more than 500. On average there are "∼100 masks per image".
>
> **What it means:**
>
> Each coloured shape is one mask, and SAM made all of them. The pictures are in rows by the number of masks, from fewer than 50 to more than 500. The original pictures are large: 3,300 × 4,950 pixels on average, about 50 times more pixels than our cat photo.
>
> Look at the bottom rows, with more than 500 masks. The masks cover small things: windows, leaves, stones and pieces of food. A person does not label 500 objects in one picture, because it takes hours. Also look closely: a mask of a window can be inside a mask of a house. One pixel can be in many masks. This is different from semantic segmentation, where each pixel has only one class.

## Responsible AI {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The Responsible AI paragraph: We study and report on potential fairness concerns and biases when using SA-1B and SAM. Images in SA-1B span a geographically and economically diverse set of countries and we found that SAM performs similarly across different groups of people. Together, we hope this will make our work more equitable for real-world use cases. We provide model and dataset cards in the appendix](https://ishwarj.com/img/papers/sam/p1-intro-rai.pngam/p1-intro-rai.png)](/img/papers/sam/p1-intro-rai.png)
>
> **What it says:** the team studies "potential fairness concerns and biases". The pictures come from "a geographically and economically diverse set of countries". SAM "performs similarly across different groups of people".
>
> **What it means:**
>
> The team asks two questions. Does the data come mostly from some places? Does the model work less well for some people? The pictures come from many countries, rich and poor. The team also tested masks of people in groups by perceived gender, age and skin tone. "Model and dataset cards" are short standard documents that tell what a model or a dataset is for, and what its limits are.
>
> The paper says "similarly", not "identically", and this is a careful word. In Appendix C, the team finds one bias. With one click, SAM segments the clothing of people who look masculine better than the clothing of people who look feminine. With three clicks, the difference closes. Part 4 shows the numbers.

> [!DEFINITION] Bias (in data)
> A dataset has a bias when some groups or some situations occur much more than in the real world. A model that has training on biased data can work less well for the groups that occur less.

## The experiments {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The Experiments paragraph: We extensively evaluate SAM. First, using a diverse new suite of 23 segmentation datasets, we find that SAM produces high-quality masks from a single foreground point, often only slightly below that of the manually annotated ground truth. Second, we find consistently strong quantitative and qualitative results on a variety of downstream tasks under a zero-shot transfer protocol using prompt engineering, including edge detection, object proposal generation, instance segmentation, and a preliminary exploration of text-to-mask prediction. These results suggest that SAM can be used out-of-the-box with prompt engineering to solve a variety of tasks involving object and image distributions beyond SAM's training data. Nevertheless, room for improvement remains, as we discuss in section 8](https://ishwarj.com/img/papers/sam/p1-intro-exp.pngam/p1-intro-exp.png)](/img/papers/sam/p1-intro-exp.png)
>
> **What it says:** the team tests SAM on "23 segmentation datasets". From "a single foreground point", SAM's masks are "often only slightly below" the masks that people drew. SAM also does "edge detection, object proposal generation, instance segmentation, and a preliminary exploration of text-to-mask prediction", all zero-shot.
>
> **What it means:**
>
> The team tests SAM on 23 datasets with very different pictures: underwater, cells, X-ray pictures, paintings and more. With only one click, SAM's masks got a rating from people only a little lower than masks that people drew. Then SAM does three jobs that it never had training for: edge detection, object proposals and instance segmentation (the table below explains each one). It also gets a first, incomplete test with text prompts, and these results are weaker.
>
> The trick in each experiment is the prompt. For each job, the team writes a small program that makes the right hints, and the model stays the same. This is the real test of the claim "foundation model".

> [!DEFINITION] Ground truth
> The correct answer for a test example, usually made by people. For segmentation, the ground truth is the mask that an annotator drew. A model's mask is compared to it, for example with IoU.

<figure class="fig"><svg viewBox="0 0 760 210" role="img" aria-label="Zero-shot transfer: SAM is trained once on SA-1B and then used without more training on new kinds of pictures such as underwater, microscope, X-ray and painting images."><defs><marker id="ah" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="arrow" d="M0,0L10,5L0,10z"/></marker><marker id="ah-on" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-on" d="M0,0L10,5L0,10z"/></marker><marker id="ah-2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path class="arrow-2" d="M0,0L10,5L0,10z"/></marker></defs><rect class="box-2" x="20.0" y="40.0" width="200.0" height="110.0" rx="12"/><text class="t-title" x="120.0" y="66.0" text-anchor="middle">Training data</text><text class="t-note" x="120.0" y="92.0" text-anchor="middle">SA-1B: 11M photos,</text><text class="t-note" x="120.0" y="112.0" text-anchor="middle">1.1B masks</text><line class="edge" x1="224.0" y1="95.0" x2="280.0" y2="95.0" marker-end="url(#ah)"/><rect class="box-on" x="284.0" y="60.0" width="150.0" height="70.0" rx="12"/><text class="t-title" x="359.0" y="92.0" text-anchor="middle">SAM</text><text class="t-tick" x="359.0" y="112.0" text-anchor="middle">no more training</text><line class="edge" x1="438.0" y1="95.0" x2="500.0" y2="34.0" marker-end="url(#ah)"/><rect class="box-1" x="504.0" y="20.0" width="236.0" height="22.0" rx="6"/><text class="t-tick" x="622.0" y="35.0" text-anchor="middle">underwater</text><line class="edge" x1="438.0" y1="95.0" x2="500.0" y2="60.0" marker-end="url(#ah)"/><rect class="box-1" x="504.0" y="46.0" width="236.0" height="22.0" rx="6"/><text class="t-tick" x="622.0" y="61.0" text-anchor="middle">microscope cells</text><line class="edge" x1="438.0" y1="95.0" x2="500.0" y2="86.0" marker-end="url(#ah)"/><rect class="box-1" x="504.0" y="72.0" width="236.0" height="22.0" rx="6"/><text class="t-tick" x="622.0" y="87.0" text-anchor="middle">X-ray of bags</text><line class="edge" x1="438.0" y1="95.0" x2="500.0" y2="112.0" marker-end="url(#ah)"/><rect class="box-1" x="504.0" y="98.0" width="236.0" height="22.0" rx="6"/><text class="t-tick" x="622.0" y="113.0" text-anchor="middle">paintings</text><line class="edge" x1="438.0" y1="95.0" x2="500.0" y2="138.0" marker-end="url(#ah)"/><rect class="box-1" x="504.0" y="124.0" width="236.0" height="22.0" rx="6"/><text class="t-tick" x="622.0" y="139.0" text-anchor="middle">drone views</text><line class="edge" x1="438.0" y1="95.0" x2="500.0" y2="164.0" marker-end="url(#ah)"/><rect class="box-1" x="504.0" y="150.0" width="236.0" height="22.0" rx="6"/><text class="t-tick" x="622.0" y="165.0" text-anchor="middle">kitchen, first-person</text><text class="t-muted" x="20.0" y="196.0" text-anchor="start">Zero-shot: the test pictures and tasks were never in the training data, and SAM gets no extra training for them.</text></svg><figcaption>Zero-shot transfer. SAM has training only on SA-1B. It is then used with no more training on new types of pictures: underwater, microscope cells, X-ray pictures of bags, paintings, drone views and first-person kitchen video.</figcaption></figure>

The five experiments go from low-level to high-level vision (Part 5):

| Experiment | Prompt | What SAM must give |
|---|---|---|
| Single point to mask | one point in the object | the object's mask |
| Edge detection | a 16 × 16 grid of points | the edges between all objects |
| Object proposals | a 64 × 64 grid of points | about 1,000 masks of possible objects |
| Instance segmentation | the boxes of a detector | one mask for each detected object |
| Text to mask | a short text such as "a wheel" | the mask of the object in the text |

## The release {§1}

> [!PAPER] · §1 Introduction · page 2
> [![The Release paragraph: We are releasing the SA-1B dataset for research purposes and making SAM available under a permissive open license (Apache 2.0) at segment-anything.com. We also showcase SAM's capabilities with an online demo](https://ishwarj.com/img/papers/sam/p1-intro-release.png1-intro-release.png)](/img/papers/sam/p1-intro-release.png)
>
> **What it says:** SA-1B is released "for research purposes". SAM is released "under a permissive open license (Apache 2.0)". There is "an online demo".
>
> **What it means:**
>
> The model and the data have different rules. You can use the SAM model in commercial products, because Apache 2.0 permits it (the yellow box below explains the licence). You can use the SA-1B dataset only for research. So a company can sell a product that uses SAM, but it cannot train its own commercial model on SA-1B. The weights that we use in this series are the released SAM weights.

> [!DEFINITION] Apache 2.0 licence
> A common open-source licence. It permits you to use, change and sell software that has the licence, also in commercial products. You must keep the copyright notice and the licence text.

## Who's who: the papers that the introduction cites

The introduction cites earlier work by number. This table puts the important references in one place. The full list is in the references at the end of this part.

| Short name | Paper | Year | What it is, in one line | Role in the SAM paper |
|---|---|---|---|---|
| GPT-3 [10] | Brown et al., [*Language Models are Few-Shot Learners*](https://arxiv.org/abs/2005.14165) | 2020 | a language model with 175 billion parameters | the zero-shot and few-shot idea |
| Foundation models [8] | Bommasani et al., [*On the Opportunities and Risks of Foundation Models*](https://arxiv.org/abs/2108.07258) | 2021 | the Stanford report that named foundation models | the definition that SAM uses |
| Scaling laws [56] | Kaplan et al., [*Scaling Laws for Neural Language Models*](https://arxiv.org/abs/2001.08361) | 2020 | loss falls as a power law of model size, data and compute | "model scale, dataset size, and total training compute" |
| CLIP [82] | Radford et al., [*Learning Transferable Visual Models From Natural Language Supervision*](https://arxiv.org/abs/2103.00020) | 2021 | an image and a text encoder trained on 400 million pairs | a vision foundation model; SAM's text encoder |
| ALIGN [55] | Jia et al., [*Scaling Up Visual and Vision-Language Representation Learning With Noisy Text Supervision*](https://arxiv.org/abs/2102.05918) | 2021 | a CLIP-like model trained on 1.8 billion noisy pairs | a vision foundation model |
| DALL·E [83] | Ramesh et al., [*Zero-Shot Text-to-Image Generation*](https://arxiv.org/abs/2102.12092) | 2021 | an image generator from text | the example of composition |
| COCO [66] | Lin et al., [*Microsoft COCO: Common Objects in Context*](https://arxiv.org/abs/1405.0312) | 2014 | 123,000 pictures with masks of 80 object types | a dataset that SA-1B is compared to |
| LVIS [44] | Gupta et al., [*LVIS: A Dataset for Large Vocabulary Instance Segmentation*](https://arxiv.org/abs/1908.03195) | 2019 | high-quality masks for more than 1,000 object types | a dataset that SA-1B is compared to |
| Open Images [60] | Kuznetsova et al., [*The Open Images Dataset V4*](https://arxiv.org/abs/1811.00982) | 2020 | 9 million pictures; 2.7 million masks in the version that SAM compares to | the largest earlier set of masks |
| ADE20K [117] | Zhou et al., [*Semantic Understanding of Scenes through the ADE20K Dataset*](https://arxiv.org/abs/1608.05442) | 2019 | 28,000 scene pictures with many masks each | the dataset with the most masks per picture before SA-1B |

> [!TAKEAWAYS] Key takeaways
> - **The goal** is a foundation model for segmentation. One model gives a mask for any object in any picture, from a prompt, with no more training.
> - **A mask** is a grid of 0s and 1s with one number for each pixel. **IoU** measures the overlap of two masks: pixels in both, divided by pixels in at least one. For our two ear masks, IoU = 424 / 473 = 0.896.
> - **Three linked parts:** a task (promptable segmentation, §2), a model (SAM, §3) and data (the data engine and SA-1B, §4 and §5). The model helps to make the data, and the data trains the model.
> - **A valid mask** is a correct mask for at least one sensible meaning of the prompt. One click on a cat's ear gave two ear masks (427 and 470 pixels, scores 0.946 and 0.944). It also gave the whole cat (49,520 pixels, score 0.980). All three masks are valid.
> - **The scores** are SAM's predicted IoU. They help to choose among the three masks. On the remote control, a bad mask got 0.793, and the good whole-remote mask got 0.989.
> - **The pipeline:** the 640 × 480 photo becomes 1024 × 768 and then a padded 1024 × 1024 square. The encoder makes 256 × 64 × 64 = 1,048,576 numbers. The decoder makes three masks of 256 × 256, which SAM scales back to 640 × 480.
> - **Amortized real-time:** the image encoder holds 99.4% of the 641 million weights of SAM ViT-H. It runs once for each picture (3,069 ms on our laptop GPU). The decoder runs once for each prompt (17.5 ms on the CPU). With 100 prompts on one picture, the time for each prompt is about 48 ms.
> - **SA-1B:** 1,129 million masks on 11 million licensed pictures, about 103 masks for each picture, and about 400 times more masks than Open Images. All released masks come from SAM itself.
> - **Licences:** the SAM model uses Apache 2.0 (commercial use is permitted). The SA-1B dataset is for research only.

**Next, in Part 2:** §2 of the paper, the task. Part 2 tells what a prompt can be and what "valid" means in exact terms. It shows why the task works as pre-training and how zero-shot transfer works with prompts. It also compares promptable segmentation with interactive and multi-task segmentation. With real runs, it shows SAM's three masks on more ambiguous clicks, and the effect of each new point.

<details>
<summary>Run it yourself</summary>

The script behind this part is [`code/papers/sam/sam_part1.py`](https://github.com/ishwar6/ishwar-books/blob/main/code/papers/sam/sam_part1.py). It counts the weights of the three models and runs the picture through SAM ViT-H. It makes the masks for the two clicks and the box. It measures the time of the encoder and the decoder, and it saves every number in `results/part1.json`.

```bash
pip install torch transformers datasets pillow
python sam_part1.py        # downloads facebook/sam-vit-base, -large and -huge (about 3.8 GB in total)
```

<figure><img src="/img/papers/sam/part1-run.png" alt="Terminal output of sam_part1.py: the parameter counts of the three SAM models, the input pipeline shapes, the three masks and scores for the ear click and the remote click, the worked IoU, the box prompt, the timing table, the amortized times and the dataset ratios" loading="lazy" /><figcaption>The full output of sam_part1.py, as it ran for this article.</figcaption></figure>

</details>

## The paper and the code

[Segment Anything](https://arxiv.org/abs/2304.02643) (ICCV 2023) · [code and weights](https://github.com/facebookresearch/segment-anything) · [our scripts for this part](https://github.com/ishwar6/ishwar-books/tree/main/code/papers/sam)

