Skip to navigation

Serve SAM2

Runs on: gpu, cpu Dockerfile
First run can take longer while the worker pulls the configured Docker image.

Overview

Segments objects with SAM 2.1, a model that stays loaded in memory and answers over HTTP. Start it from the Deployments page or place it on the canvas as a deploy node. It keeps running until you stop it.

A running deployment is what the Smart Tool in the labeling tool calls. Draw a box around an object, add a few clicks on it or next to it, and a mask comes back. Every running SAM2 deployment in the workspace shows up in the Smart Tool model list.

The same deployment also answers your own code: send a box and clicks for one object and get the mask back as a Supervisely bitmap.

Prerequisites

RequirementWhat you need
GPU workerA worker with a free GPU. SAM 2 also runs on a CPU, but a click there takes seconds.
Images in the workspaceThe images to segment, already uploaded. Calls name images by asset id.
ModelOne of the four pretrained SAM 2.1 sizes copied from the catalog, or SAM 2.1 weights of your own.
API token (optional)A personal token created under Settings → Developer, for calls from your own code. The labeling tool needs none.

How it works

1

Fetches the model’s weights - a catalog model’s from its authors, checked against a pinned SHA-256 - and loads them onto the device once at startup.

2

Reports itself as not ready until the weights are in memory, then starts accepting calls.

3

Downloads the image a call names and encodes it once, so every later click on that image skips both the download and the encoding.

4

Turns the box and the clicks into one object mask. A new click on the same box refines the previous mask instead of starting over.

5

Answers with the mask, or with an empty mask when the model is not confident enough.

FAQ

Start the deployment and wait until it shows as running. Open a labeling queue, pick the Smart Tool, and choose the deployment in the model list if the workspace runs more than one. A deployment that is stopped or still starting doesn’t appear in the list.

Open the deployment on the Deployments page. It shows the base URL and a ready-made command for each route. Calls go through OvalBee, which checks your API token and forwards them to the container.

/segment segments one object on a workspace image from a box and clicks, and /smart_segmentation is the same call under another name. /segment/upload does the same for an image you upload with the call instead of naming an asset. /encode_image downloads an image ahead of the first click, DELETE /images drops an image from memory, and /info reports which weights are loaded.

The catalog lists four SAM 2.1 sizes, from Hiera tiny to Hiera large. Large is the most accurate. Tiny answers a click fastest and is the one to try if the Smart Tool feels slow. Copy the one you want into your workspace from the model catalog, then select it in Model.

An image asset id, plus a box, clicks, or both. Box corners are inclusive and clicks are in image pixels. Positive clicks mark the object and negative clicks mark what isn’t part of it. A request with neither a box nor a click is rejected.

Either the best mask scored below confidence_threshold, or a click lies outside the box. The second case is common when you shrink the box after clicking. Neither is an error, so the response still reports success.

As a Supervisely bitmap: the top-left corner of the mask in image pixels, and the mask itself as a base64 string of a compressed PNG. The response also carries the model’s score for that mask.

One click is ambiguous: it may mean a shoulder, a person, or a whole crowd. The model proposes several masks and returns the one it scores highest. Each further click on the same box refines that mask. Going back to one click starts a new object.

The model always sees the whole image and treats the box as a hint. When the setting is on, the mask is trimmed to the box, so nothing outside the box you drew comes back. Turn it off to keep parts of the object that spill past the box.

Yes, as long as its model version says which SAM 2.1 architecture it was trained with: the model’s config.json names it, or else the version’s architecture or base model, such as sam2.1_hiera_large. A version that names none is refused at startup rather than loaded under a guessed architecture, which would load without complaint and then predict noise. With the safe-weights policy on, only safetensors weights load; a .pt checkpoint has to be converted first.

No. This deployment answers on single images. SAM 2’s video predictor isn’t part of it.

Inputs

Model
modelRequired

SAM 2.1 weights the deployment loads and keeps in memory: one of the four pretrained sizes from the catalog, downloaded from their authors when the deployment starts, or weights of your own. The version has to name its size, since the weights do not say which SAM 2.1 architecture they belong to. With the safe-weights policy on, only safetensors load. Key: MODEL.

Config
yamlRequired

YAML defaults for every request. A request may override confidence_threshold and crop_output_to_bbox per call. Key: CONFIG.

Outputs

Deployment
deployment

The running SAM 2 deployment, set once it answers requests. Wire it into Evaluate interactive instance segmentation to measure it on your own data. Key: DEPLOYMENT.

Runtime

The deployment runs in its own container. A catalog model’s weights are downloaded from their authors when the deployment starts, through the workspace’s model cache when the worker has one. On a GPU worker it holds the GPU from the moment it starts until you stop it, idle or not, so stop a deployment you’re done with.

JSON config

Machine-readable node interface for automation and advanced usage.

{
"name": "Serve SAM2",
"description": "Loads SAM 2.1 onto the GPU once and keeps answering segmentation requests until the deployment is stopped. It powers the Smart Tool in the labeling tool. Holds the GPU for its whole lifetime.\n",
"category": "Models",
"namespace": "ovalbee",
"templateKey": "models/sam2/serve_sam2",
"version": "v1",
"inputs": [
{
"key": "MODEL",
"label": "Model",
"type": "model",
"description": "SAM 2.1 weights the deployment loads and keeps in memory: one of the four pretrained sizes from the catalog, downloaded from their authors when the deployment starts, or weights of your own. The version has to name its size, since the weights do not say which SAM 2.1 architecture they belong to. With the safe-weights policy on, only safetensors load.\n",
"required": true,
"default": null,
"visibleWhen": null,
"options": {
"framework": "sam2",
"task_type": "instance_segmentation"
}
},
{
"key": "CONFIG",
"label": "Config",
"type": "yaml",
"description": "YAML defaults for every request. A request may override `confidence_threshold` and `crop_output_to_bbox` per call.\n",
"required": true,
"default": "confidence_threshold: 0.5\ncrop_output_to_bbox: true\nimage_cache_size: 8\n",
"visibleWhen": null
}
],
"outputs": [
{
"key": "DEPLOYMENT",
"label": "Deployment",
"type": "deployment",
"description": "The running SAM 2 deployment, set once it answers requests. Wire it into Evaluate interactive instance segmentation to measure it on your own data.\n",
"artifact": false,
"badge": false,
"badgeOpens": null,
"hidden": false
}
],
"runtime": {
"type": "docker",
"devices": [
"gpu",
"cpu"
],
"dockerImage": "ovalbee/node-sam2:0.0.16"
}
}