SAM2 Segmentation (RMBG)
SAM2 Segmentation (RMBG)
- image
- background_color
- IMAGE
- MASK
- MASK_IMAGE
SAM2Segment is text-prompted object selection: type what you want, get a mask of it. Say handbag or the person in front, and this node finds it and cuts it out. It's the middle child of the pack's segmentation lineup - newer and faster than the original Segment node, one generation behind SAM3. Like the others, this is targeted masking, not background removal. You use it to grab a named object and hand the mask to inpainting, a detailer, or a compositing step, not to strip a background (RMBG does that better and cheaper).
The way it works is a two-model handoff. GroundingDINO reads your text prompt and turns it into bounding boxes; SAM2 (Meta's Segment Anything 2) turns those boxes into precise masks. This node runs both for you, which is why you'll see two model dropdowns. That's also why it's more capable than a plain background remover - it understands language and can pick one object out of a busy scene - and why it's heavier to run.
Inputs that matter: prompt is your text query (multiline, so get specific). sam2_model picks the SAM2 size - sam2.1_hiera_tiny (default, fast, smallest download) through small, base_plus, and large (best quality, biggest). Tiny is genuinely fine for most masking; only reach for large when tiny is missing detail. dino_model chooses the detector: GroundingDINO_SwinT_OGC (694MB, the default) or the bigger GroundingDINO_SwinB (938MB) for tougher detection. threshold (0.05–0.95, default 0.35) is your detection confidence - the KB's rule of thumb is roughly 0.25–0.35 for broad detection and 0.45–0.55 when you want precision and fewer false grabs. The rest are the standard finishers: mask_blur, mask_offset, invert_output, background / background_color, and a device toggle. Outputs are IMAGE, MASK, and MASK_IMAGE; MASK is the one you route downstream.
Install is the pack routine: "Comfyui-RMBG" in ComfyUI Manager, or cd ComfyUI/custom_nodes && git clone https://github.com/1038lab/ComfyUI-RMBG, then pip install -r requirements.txt and restart. Both the SAM2 weights and the GroundingDINO models auto-download on first use - SAM2 into ComfyUI/models/sam2/, GroundingDINO into ComfyUI/models/grounding-dino/ - so the first run of a given model combo pauses for the downloads, then caches.
The failure everyone hits first: a 401 error while initializing GroundingDINO. It's not a permissions problem with the repo - these are public and download anonymously. It's a stale Hugging Face token getting in the way. Delete the cached token (%USERPROFILE%\.cache\huggingface\token, and the .huggingface\token one if it exists), clear any HF_TOKEN / HUGGINGFACE_TOKEN env var, and re-run. The README calls this out explicitly because it trips up a lot of people.
When to use SAM2 versus its siblings: if SAM3Segment loads on your machine, it's usually the nicer experience - it does text-to-mask without the separate DINO step and tends to be more accurate. But SAM3 leans on Triton, which is a headache on Windows, so SAM2Segment is the reliable fallback that works everywhere without that dependency. And if you just want a clean foreground cutout, skip all of these and use RMBG or BiRefNet - pulling out the SAM+DINO machinery to remove a plain background is doing a lot of work for a job a one-model remover already nails.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| prompt | STRING | Enter text description of object to segment | |
| sam2_model | COMBO | sam2.1_hiera_tiny | SAM2 model size: Tiny (fastest) to Large (best quality) |
| dino_model | COMBO | GroundingDINO_SwinT_OGC (694MB) | GroundingDINO model for text-to-box detection |
| device | COMBO | Auto | Auto: smart detection, CPU: force CPU, GPU: force GPU |
| thresholdopt | FLOAT | 0.350.05–0.95 | Detection threshold (higher = more strict) |
| mask_bluropt | INT | 00–64 | Blur mask edges (0 = disabled) |
| mask_offsetopt | INT | 0-64–64 | Expand/shrink mask (positive = expand) |
| invert_outputopt | BOOLEAN | false | Invert the mask output |
| backgroundopt | COMBO | Alpha | Background type |
| background_coloropt | COLORCODE | #222222 | Background color (when not Alpha) |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |
| MASK | MASK | — |
| MASK_IMAGE | IMAGE | — |