Nodes/ComfyUI-RMBG/YOLO Segmentation (RMBG)
ComfyUI Node Runs on cloud

YOLO Segmentation (RMBG)

The one node in this pack that won't remove your background

By 1038lab·Created 2 years ago·Updated 22 days ago· 2,226
YOLO Segmentation (RMBG)
  • images
  • background_color
  • IMAGE
  • ANNOTATED_IMAGE
  • MASK
  • MASK_LIST
◄modelyolo26m-seg (Medium / 54.8MB)►
◄mask_countall►
◄select_mask_indexnone►
◄conf0.25►
◄iou0.45►
◄classes►
◄annotation_modeMask & Box►
◄backgroundAlpha►
◄mask_blur0►
◄mask_offset0►
◄invert_outputfalse►
◄deviceauto►
◄max_det300►
◄retina_maskstrue►
◄agnostic_nmsfalse►
◄unload_modelfalse►

Everything else in ComfyUI-RMBG answers "what is the foreground?" This node answers a different question: "where are the objects, and which one do you want?" That's YOLO - Ultralytics' real-time detector - wrapped so its instance masks come out as a ComfyUI mask you can actually wire somewhere. If you're used to reaching for SAM whenever a mask is involved, this is the cheaper, dumber, much faster option, and for "the person in this shot" it's usually enough.

What it's actually for

The job is targeted masking, not cutout. In the wider workflow that means: isolate a subject so you can inpaint only that, or run a detailer pass on it, or drop it into a composite with the background left alone. The KB's detection doc puts the distinction bluntly - the mask is the thing, and "you can get that mask in SO many ways."

Where YOLO beats the segmentation giants is vocabulary and speed. It knows. You don't prompt it, you don't click it. It has a fixed list of 80 everyday classes (the presets here are the standard COCO-trained seg weights), runs in milliseconds, and returns one mask per detected instance. For batch work across hundreds of frames that matters more than a beautiful edge.

Where it loses: anything not in those 80 classes. Your OC, your specific garment, your product - YOLO has no idea. That's when you go to the pack's SAM3 node or Florence-2, not here.

How it works

The node loads an Ultralytics -seg checkpoint and runs it per-image over the batch, then decides which of the returned instance masks you want. Two widgets do the picking, and confusing them is the number one way people get empty output:

  • mask_count - how many detections to merge. all merges every instance; a number takes the first N.
  • select_mask_index - 1-based start point. On none it starts at the first detection.

The interaction is the fiddly part. select_mask_index on its own grabs one mask (the span defaults to 1 unless mask_count says otherwise), and select_mask_index: 3 + mask_count: 2 gives you detections three and four. Merging is a union - masks are combined with a max, so two overlapping boxes become one silhouette, never two outputs.

conf (0.25) and iou (0.45) go straight through to Ultralytics as-is. classes takes a comma list or ranges like 0,2,5-7 - class IDs, not names. annotation_mode only affects the preview image. retina_masks (on by default) is worth leaving on; it gives you high-res masks instead of postage stamps. device, max_det, agnostic_nms and unload_model are the knobs you'll only touch when something's wrong.

Inputs and outputs

Feed images - a batch, and it runs on every frame. Outputs:

  • IMAGE - the cutout, RGBA when background is Alpha, RGB when you set a Color.
  • ANNOTATED_IMAGE - boxes and/or masks drawn on a copy. Free sanity check; wire it to a preview while you tune conf.
  • MASK - the merged mask. This is the one you send to inpainting or a detailer.
  • MASK_LIST - the same MASK type, but batched: one mask item per selected detection instead of one merged blob. Use it when instances need to be handled separately.

Wire MASK into VAEEncode-with-mask or any inpaint node; wire IMAGE into Image Save. If you only need the preview, don't bother loading the mask path at all.

Install

The node ships with the pack. In ComfyUI Manager search ComfyUI-RMBG, or:

cd ComfyUI/custom_nodes
git clone https://github.com/1038lab/ComfyUI-RMBG
./ComfyUI/python_embeded/python -m pip install -r ComfyUI-RMBG/requirements.txt

Then the optional bit, which the README is explicit about:

./ComfyUI/python_embeded/python -m pip install ultralytics --no-deps

ultralytics is deliberately not in requirements.txt. Install it --no-deps unless you enjoy watching it renegotiate your torch version. The preset weights download on first use into ComfyUI/models/ultralytics; drop your own .pt files in that folder and they appear in the model dropdown alongside the built-in presets.

Where people get burned

Ultralytics is AGPL-3.0, and the pack around it is GPL-3.0. That's a different licence planet from the MIT models the rest of ComfyUI-RMBG wraps, and it matters if you're shipping a product. Ultralytics has also had one genuine supply-chain incident - a poisoned release in December 2024 that shipped a cryptominer into ComfyUI setups through a detailing pack. Pin your version and don't blindly pip install -U it.

Two smaller ones, both from the code rather than the README:

  • No detections produces a zero mask, not an error. Downstream inpainting then silently does nothing. Put ANNOTATED_IMAGE on a preview while tuning, or you'll spend twenty minutes blaming the sampler.
  • Detection order is Ultralytics' confidence order. select_mask_index is not "left to right" or "biggest to smallest" - it's whatever the model ranked first. If you need stability across frames, mask_count: all is the honest choice.

Class IDs, not names. person typed into classes parses to nothing, prints a warning, and quietly keeps every class - so you get everyone in the shot and assume the filter is broken.

Category🧪AILab/🧽RMBG

Inputs (18)

NameTypeDefaultDescription
imagesIMAGE—
modelCOMBOyolo26m-seg (Medium / 54.8MB)YOLO model weights stored under /tmp/ComfyUI/models/ultralytics (presets download automatically).
mask_countCOMBOallMerge this many detections. 'all' merges everything (or just the selected index when specified).
select_mask_indexoptCOMBOnone1-based index of the first mask to keep. Use 'none' to start from the first detection.
confoptFLOAT0.250–1Confidence threshold forwarded to Ultralytics.
iouoptFLOAT0.450–1IOU used during NMS.
classesoptSTRINGComma list or ranges of class IDs; empty keeps every class.
annotation_modeoptCOMBOMask & BoxDisplay mode for ANNOTATED_IMAGE output.
backgroundoptCOMBOAlphaBackground type for cutout IMAGE output.
background_coloroptCOLORCODE#222222Background color when background is set to Color.
mask_bluroptINT00–64Apply Gaussian blur to mask edges.
mask_offsetoptINT0-64–64Expand/Shrink mask boundary (positive = expand, negative = shrink).
invert_outputoptBOOLEANfalseInvert the mask output.
deviceoptCOMBOautoForce a device or auto-detect CUDA → MPS → CPU.
max_detoptINT3001–1000Maximum detections per image.
retina_masksoptBOOLEANtrueUse high-resolution masks (Ultralytics retina_masks flag).
agnostic_nmsoptBOOLEANfalseEnable class-agnostic NMS.
unload_modeloptBOOLEANfalseUnload model from VRAM after execution.

Outputs (4)

NameTypeDescription
IMAGEIMAGE—
ANNOTATED_IMAGEIMAGE—
MASKMASK—
MASK_LISTMASK—