LayerMask: SegmentAnythingUltra V2(Advance)
SegmentAnythingUltra V2 — text-prompted masking that actually gets hair right
- image
- image
- mask
If you've ever wanted to type "the red dress" or just "subject" and get a clean mask back, this is that node. It pairs Grounding DINO (text-to-box object detection) with Segment Anything (box-to-mask), then runs an edge-refinement pass on top so the result doesn't look like a blunt cutout. It's chflame163's improved wrapper around storyicon's original comfyui_segment_anything, and the "V2" is specifically about that last step: V2 adds VITMatte-based detail processing, which is the difference between a mask that handles a t-shirt fine and one that survives flyaway hair.
The mechanism is two stages bolted together. Grounding DINO reads your prompt text and returns bounding boxes for anything matching it - this is the part that lets you target "the dog" instead of just getting the whole foreground. SAM then takes those boxes and produces a precise mask inside them. Everything after that (detail_method onward) is post-processing to fix the mask edge, because raw SAM output is a hard binary boundary and hair, fur, and semi-transparent stuff need something closer to matting.
The inputs worth actually tuning: sam_model and grounding_dino_model pick your model sizes - sam_vit_h is the biggest/most accurate, mobile_sam is the 39MB speed option, and the sam_hq_* variants trade a bit of speed for better mask boundaries. threshold (default 0.3) is DINO's detection confidence - raise it if it's grabbing things you didn't ask for. prompt defaults to "subject", which just means "the main foreground thing," but you can put in a specific noun phrase. detail_method is where the real quality lever is: VITMatte gives you the best hair/fur edges but is genuinely heavy on big images (the README specifically warns that anything over ~2K resolution with VITMatte eats a lot of memory), PyMatting and GuidedFilter are the lighter fallbacks. process_detail lets you skip that whole pass if you don't need it - useful when you just want a fast rough mask. Outputs are image (the cutout) and mask, which you'd feed into a compositing node or an inpaint.
Install through ComfyUI Manager (search "ComfyUI Layer Style Advance") or git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance into custom_nodes/, then restart. This particular node needs more model files than most in the pack: SAM checkpoints go in ComfyUI/models/sams, the Grounding DINO config + weights go in ComfyUI/models/grounding-dino, and - this is the one people miss - Grounding DINO's text tokenizer needs bert-base-uncased's five files (config.json, model.safetensors, tokenizer_config.json, tokenizer.json, vocab.txt) sitting in ComfyUI/models/bert-base-uncased. If you're using VITMatte as your detail method, grab the vitmatte model into ComfyUI/models/vitmatte too. All of these are bundled in chflame163's HuggingFace repo (huggingface.co/chflame163/ComfyUI_LayerStyle), which is the easiest single place to pull everything from instead of hunting five separate sources.
That bert-base-uncased dependency is also the most common way this node breaks in the wild - it's not part of the model download, it's fetched from Hugging Face at load time if it isn't cached locally, and on any sandboxed or offline deploy (people have hit this trying to host ComfyUI on Replicate, for instance) you'll get a couldn't connect to huggingface.co error the moment Grounding DINO tries to build its tokenizer. Pre-download those five files and you're fine. Beyond that, the pack-wide dependency issues apply here too: a cv2.ximgproc guidedFilter import error usually means a bad opencv-contrib-python version, and it's worth running repair_dependency.bat in the plugin folder if imports look broken after an update - it fixes the three or four package-version issues (opencv, transformers, protobuf) that account for most install pain in this pack.
Inputs (14)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| sam_model | COMBO | 7 options: sam_vit_h (2.56GB), sam_vit_l (1.25GB), sam_vit_b (375MB), sam_hq_vit_h (2.57GB), sam_hq_vit_l (1.25GB), sam_hq_vit_b (379MB), +1 | |
| grounding_dino_model | COMBO | 2 options: GroundingDINO_SwinT_OGC (694MB), GroundingDINO_SwinB (938MB) | |
| threshold | FLOAT | 0.300–1 | — |
| detail_method | COMBO | 5 options: VITMatte, VITMatte(local), vitmatte-base-composition-1k, PyMatting, GuidedFilter | |
| detail_erode | INT | 61–255 | — |
| detail_dilate | INT | 61–255 | — |
| black_point | FLOAT | 0.150.01–0.98 | — |
| white_point | FLOAT | 0.990.02–0.99 | — |
| process_detail | BOOLEAN | true | — |
| prompt | STRING | subject | — |
| device | COMBO | 2 options: cuda, cpu | |
| max_megapixels | FLOAT | 2.01–999 | — |
| cache_model | BOOLEAN | false | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |