> ## Documentation Index
> Fetch the complete documentation index at: https://docs.encord.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SAM 2 Segmentation & Tracking

Segment Anything Model 2 (SAM 2) allows you to automatically create labels around distinct features in all supported file formats.

***

## SAM 2 Segmentation

### Availability

SAM 2 supports labeling the following Ontology shapes.

| Ontology Shape         | Supported? |
| ---------------------- | ---------- |
| Polygon                | ✅          |
| Bounding box           | ✅          |
| Bitmask                | ✅          |
| Rotatable bounding box | ❌          |
| Keypoint               | ❌          |
| Polyline               | ❌          |
| Object Primitive       | ❌          |

SAM 2  supports labeling the following modalities.

| Modality       | Supported |
| -------------- | --------- |
| Single Image   | ✅         |
| Image Group    | ✅         |
| Image Sequence | ✅         |
| Video          | ✅         |
| Audio          | ❌         |
| Text / HTML    | ❌         |
| Document       | ❌         |

### New Instances

<iframe className="w-full aspect-video rounded-xl" src="https://www.loom.com/embed/176e7e652bff4387bbc822ee515f9898?sid=873fc5bb-fa48-4124-9494-35cbb06cb441" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

<Tip>
  Use the <kbd>Shift</kbd> + <kbd>A</kbd> keyboard shortcut to toggle SAM mode. Enabling SAM mode automatically activates SAM 2 whenever the class is selected, so there's no need to click the wand icon separately.
</Tip>

1. Click the wand icon on Ontology class you want to label with.

2. Click, or click-and-drag the cursor across the part of the image you want to label.
   The SAM popup appears.

<Tip>
  * Left-click parts of the image to include them in the label.
  * Right-click parts of the selection to exclude them from the label.
</Tip>

3. Click **Save** or press <kbd>ENTER</kbd>

### Existing Instances

Videos and image sequences can show a single object instance in multiple frames.

<iframe className="w-full aspect-video rounded-xl" src="https://www.loom.com/embed/88e9ca66b9594863bb290aa182ae0031?sid=e0013875-06bb-4e03-87e1-8b8318a85f71" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

1. Navigate to the frame you want to create the label.
2. Click the **Toggle auto-annotation** button or use the <kbd>SHIFT</kbd> + <kbd>A</kbd> keyboard shortcut.

<Tip>
  SAM mode remains active when switching to a different annotation class. Deactivate SAM 2 by clicking the **Toggle auto-annotation** button, or using the <kbd>SHIFT</kbd> + <kbd>A</kbd> keyboard shortcut again.
</Tip>

3. Click, or click-and-drag the cursor across the part of the image you want to label.
   The SAM popup appears.

<Tip>
  * Left-click parts of the image to include them in the label.
  * Right-click parts of the selection to exclude them from the label.
</Tip>

4. Click **Save** or press <kbd>ENTER</kbd>

### Panoptic Exclude Mode

When creating bitmask labels with SAM 2 single object detection, you can use panoptic exclude mode to automatically exclude existing bitmasks from new segmentations. This prevents overlapping bitmasks and ensures clean, non-overlapping annotations.

<Note>
  Panoptic exclude mode only works with bitmask annotations and is automatically applied when using SAM 2 single object detection.
</Note>

To use panoptic exclude mode:

1. Enable **SAM mode** by clicking the wand icon or pressing <kbd>Shift</kbd> + <kbd>A</kbd>
2. Select the **Bitmask** annotation class
3. In the bitmask settings panel, set **Panoptic mode** to **Exclude**
4. Use SAM 2 single object detection to create new bitmask labels

When you create a new bitmask with SAM 2, it will automatically exclude any existing bitmasks in the same area, ensuring your annotations don't overlap.

***

## SAM 2 Tracking

SAM 2 supports tracking objects forwards and backwards across multiple frames.

<Note>
  SAM 2 tracking is computationally intensive and performance might be slower than other labeling actions.
</Note>

### Availability

The following table shows the Ontology shapes for which SAM 2 forwards and backwards tracking can be used.

| **Ontology shape**     | **SAM 2 Video Tracking** |
| ---------------------- | ------------------------ |
| Bounding box           | ✅                        |
| Rotatable bounding box | ✅                        |
| Polygon                | ✅                        |
| Polyline               | ✅                        |
| Object Primitive       | ✅                        |
| Keypoint               | ✅                        |
| Bitmask                | ✅                        |

The following table shows the modalities that support SAM 2 forwards and backwards tracking.

| **Modalities**  | **SAM 2 Video Tracking** |
| --------------- | ------------------------ |
| Images          | ❌                        |
| Videos          | ✅                        |
| Image Groups    | ✅  (Forwards only)       |
| Image Sequences | ✅  (Forwards only)       |
| DICOM           | ✅  (Forwards only)       |
| Audio           | ❌                        |
| Text / HTML     | ❌                        |
| Documents       | ❌                        |

### Track While Creating Labels (Bounding Boxes, Polygons, Bitmasks)

Before you begin, set your tracking range in the [Editor Settings](/platform-documentation/Annotate/annotate-label-editor/annotate-label-editor-settings-shortcuts#object-tracking). By default, object tracking is configured to run for 30 frames, meaning SAM 2 tracking tracks the object forwards or backwards across 30 consecutive frames.

1. Click the *wand* icon next to the class you want to label, or press <kbd>Shift</kbd> + <kbd>A</kbd> to toggle SAM mode.

2. Click the instance you want to label. Adjust your label until the correct region is selected by SAM 2 if needed.

3. Specify the number of frames you want to track the instance across.

4. Click **Backwards** or **Forwards** to start the **SAM 2 tracking algorithm**.

<Note>
  You can also use keyboard shortcuts to initiate SAM 2 tracking:

  * Tracking forwards: <kbd>SHIFT</kbd> + <kbd>T</kbd>
  * Tracking backwards: <kbd>SHIFT</kbd> + <kbd>T</kbd> + <kbd>OPTION</kbd>
</Note>

5. Click **Save** when tracking completes to save the generated labels.

<iframe className="w-full aspect-video rounded-xl" src="https://www.loom.com/embed/533e5efd54674762b12954b6af91efdc?sid=6b8c7984-9b15-42cc-91f3-ca27ad8e7b68" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

### Track Existing Instances

**Method 1**:

<Note>
  The **Automated labeling** feature supports tracking forwards through data units. Specify frames after the existing frame to track.
</Note>

1. Click **Automated labeling**.

2. Expand the **Tracking and interpolation** section.

3. Select the instances you want to track.

4. Make sure the **Tracking** method is selected.

5. Set the **tracking annotation interval**. This defines how many consecutive frames an instance can be missing before the tracking algorithm stops.

6. Specify the **frame range** you want to track the selected instances across.

7. Click **Run tracking** to begin the process.

<iframe className="w-full aspect-video rounded-xl" src="https://www.loom.com/embed/ce51d5838d4d4349a9e926dbcc43d00a?sid=61a6a13e-39ac-4e1c-83c6-cb717193dd9a" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

**Method 2**:

Before you begin, set your default tracking range in the [Editor Settings](/platform-documentation/Annotate/annotate-label-editor/annotate-label-editor-settings-shortcuts#object-tracking). By default, object tracking is configured to run for 30 frames, meaning SAM 2 tracking tracks the object forwards or backwards across 30 consecutive frames.

1. Right-click the instance label you want to start tracking.

2. Click **Track forwards** or **Track backwards** to run SAM 2 tracking for 30 consecutive frames.

   <Note>
     You can also use keyboard shortcuts to initiate SAM 2 tracking:

     * Tracking forwards: <kbd>SHIFT</kbd> + <kbd>T</kbd>
     * Tracking backwards: <kbd>SHIFT</kbd> + <kbd>T</kbd> + <kbd>OPTION</kbd>
   </Note>

<iframe className="w-full aspect-video rounded-xl" src="https://www.loom.com/embed/7d7b0fd938144b84bf50a1fbad31076b?sid=68262d4d-ac3c-47d1-8cb5-14c47f6e7835" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

### Tracking Multiple Objects

To track multiple objects of any shape:

1. Click one of the objects you want to track.

2. Hold <kbd>Shift</kbd> on your keyboard and select all the other objects you want to track.

3. Right-click on one of the selected objects and click **Track forwards all** or **Track backwards all**.

<Note>
  You can also use keyboard shortcuts to initiate SAM 2 tracking on multiple object instances:

  * Track forwards all: <kbd>SHIFT</kbd> + <kbd>T</kbd>
  * Track backwards all: <kbd>SHIFT</kbd> + <kbd>T</kbd> + <kbd>OPTION</kbd>
</Note>

<iframe className="w-full aspect-video rounded-xl" src="https://www.loom.com/embed/411b0ccf5c2e4fe0aec1673c3df81093?sid=6ac915fc-42f1-416e-ac52-bd7e12818941" title="YouTube video player" frameBorder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

***

### Settings

The *Object tracking* section of the [editor settings](/platform-documentation/Annotate/annotate-label-editor/annotate-label-editor-settings-shortcuts#object-tracking) allows you to adjust the following.

**Change Tracking Range**

The range SAM 2 object tracking runs can be adjusted in the *Object tracking* section of the [editor settings](/platform-documentation/Annotate/annotate-label-editor/annotate-label-editor-settings-shortcuts#object-tracking). The default tracking range is set to 30 frames. This range includes the frame that SAM 2 tracking starts on.

**Advanced Tracking**

This toggle lets you choose between a faster, simpler tracking algorithm or a slower, more advanced one with higher accuracy. The advanced algorithm is enabled by default.
