SAM2ModelLoader (segment anything2)
SAM2ModelLoader (segment anything2)
- SAM2_MODEL
The other loader in the pair. Where GroundingDinoModelLoader loads the "find the object" model, this one loads the "trace its exact outline" model - SAM 2, Meta's Segment Anything 2. It's a one-input node: pick a SAM 2 checkpoint, load it, hand it to the segment node. Tiny footprint, but it's where you make the one real quality-versus-size tradeoff in this pack.
Why you'd reach for it
If you're using GroundingDinoSAM2Segment, you need this. GroundingDINO gives you a rough box around "the dog"; SAM 2 is what turns that box into a mask that actually hugs the dog's fur instead of a rectangle. Our masking notes describe SAM as Meta's universal segmentation model - hand it an image and a cue (a point, a box, a prompt) and it returns a precise mask. SAM 2 is the newer generation: better accuracy than the original, and it added video, though this pack only uses the image side. This node loads the weights that do the tracing. It's a separate node so the checkpoint loads once and gets reused across your whole graph rather than reloading every run.
How it works
SAM 2 is a promptable segmenter built on a hierarchical vision-transformer image encoder - that's the "hiera" you see in every checkpoint name. It was trained to produce a clean mask from a minimal cue, and in this pack the cue is the bounding box GroundingDINO handed over. This node reads the checkpoint, builds the model, and outputs a handle. The actual mask prediction happens downstream inside the segment node; here you're only choosing and loading which SAM 2 to use.
The inputs and outputs that matter
One input, model_name, and this is the whole decision. The dropdown lists eight checkpoints - four sizes across two releases:
- sam2 hiera tiny / small / base_plus / large - the original SAM 2 (July 2024).
tinyis 38.9MB and fast;largeis 224.4MB and the most accurate of this set. - sam2.1 hiera tiny / small / base_plus / large - the improved SAM 2.1 checkpoints (Sept 2024). Same four sizes;
sam2.1_hiera_largeis the 856MB top pick.
The size ladder is the tradeoff: tiny is the batch-and-modest-hardware choice, large is what you use when edges matter and you've got the VRAM, small / base_plus sit in between. Default to a sam2.1 checkpoint - it's the newer, better-tuned release and there's rarely a reason to pick the older 2.0 weights unless you already have them on disk. So: sam2.1_hiera_small for a good balance, step up to sam2.1_hiera_large when fine boundaries are the thing letting you down.
The single output is SAM2_MODEL, which plugs into the sam_model input of GroundingDinoSAM2Segment. Nowhere else.
How to install it
Ships with the pack, no separate step. ComfyUI Manager: search ComfyUI SAM2 (or "Segment Anything 2"), install, restart. Or:
cd ComfyUI/custom_nodes
git clone https://github.com/neverbiasu/ComfyUI-SAM2
then pip install -r requirements.txt and restart. SAM 2 weights auto-download the first time you select one, or pre-place them in models/sam2 under your ComfyUI root - keep the original filenames, since the pack matches checkpoints by name. sam2.1_hiera_large is 856MB, so first use of the big one isn't instant; set HTTP_PROXY / HTTPS_PROXY if the download is slow.
Common issues & troubleshooting
Out of memory when segmenting. The SAM 2 checkpoint you pick here loads into VRAM alongside GroundingDINO and the rest of your workflow. If you're hitting OOM, step down a size - large to base_plus to small to tiny - and at the segment node keep keep_model_loaded off so the weights release between runs. For a lot of masking jobs the drop in mask quality is invisible and the memory savings are large.
Masks look loose or blobby. That's a model-quality ceiling, not a bug. Move up the ladder toward large, or jump from a 2.0 checkpoint to its 2.1 equivalent. SAM 2 is strong but still imperfect on very fine structures like stray hair.
Download stalls. Grab the checkpoint by hand from the README's links, drop it in models/sam2 with its original name, or set the proxy env vars.
One thing this loader can't fix: if SAM 2 masked the wrong object, that's a detection problem, not a segmentation one. SAM 2 only outlines whatever box it was handed - tune the prompt and threshold over at the segment node.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| model_name | COMBO | 8 options: sam2_hiera_tiny, sam2_hiera_small.pt, sam2_hiera_base_plus.pt, sam2_hiera_large.pt, sam2_1_hiera_tiny.pt, sam2_1_hiera_small.pt, +2 |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| SAM2_MODEL | SAM2_MODEL | — |