LayerMask: SAM2 Ultra(Advance)
SAM2 Ultra — ComfyUI Node Guide
- image
- bboxes
- image
- mask
This is the single-image half of this pack's SAM2 integration - feed it an image plus bounding boxes telling it what to look at, and it gives back a clean segmentation mask with the same VITMatte-grade edge refinement the pack's other "Ultra" nodes use. If you're working with a video sequence instead, use SAM2VideoUltra; the README is explicit that this node only handles single images, and if you need to process multiple frames you should convert your batch to an image list first rather than expecting this one to track across frames.
SAM2, credited to kijai's ComfyUI-segment-anything-2 implementation in the README, is Meta's second-generation Segment Anything model - the same family that shows up across the wider masking ecosystem alongside GroundingDINO for exactly this "detect the box, then get a precise mask" pipeline. This node is that second half: it doesn't find objects itself, it needs boxes handed to it (from ObjectDetectorFL2, ObjectDetectorYOLO8, or any other bbox-producing node in this pack) and turns them into pixel-accurate masks.
The inputs that matter most:
image- your single source image.bboxes- detection box data from an upstream detector node. This is required; SAM2Ultra has no way to find objects on its own.sam2_model- sam2/sam2.1 hiera checkpoints from tiny through large; bigger generally means better segmentation at real speed cost.bbox_select+select_index- if your detector found multiple objects, chooseall,first(highest confidence), orby_indexto control which boxes actually get segmented.cache_model- keeps the model loaded between runs so repeated calls in the same session skip reload time.
The edge-refinement block matches the pattern across this pack's Ultra nodes: detail_method (VITMatte, VITMatte(local), vitmatte-base-composition-1k, PyMatting, or GuidedFilter), detail_erode/detail_dilate for how far refinement reaches from the detected edge, black_point/white_point for edge sampling thresholds, process_detail as the master toggle, device (cuda/cpu), and max_megapixels capping the resolution the refinement pass runs at.
Outputs: image (pass-through) and mask, ready for compositing, inpainting, or a further crop step.
Installing it: ComfyUI Manager, search "ComfyUI Layer Style Advance," or git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance into custom_nodes, then install_requirements.bat (or Aki variant) / pip install -r requirements.txt, then repair_dependency.bat, restart. Model files come from huggingface.co/Kijai/sam2-safetensors or BaiduNetdisk, into ComfyUI/models/sam2. If you're using a VITMatte detail method, you'll also need the VITMatte model in ComfyUI/models/vitmatte.
Where people get stuck: the most common one is skipping the detector step entirely and wondering why this node has nothing to segment - remember bboxes is required, not optional, so you need something upstream producing them first. If masks come back with rough or halo-y edges, check detail_method before assuming the SAM2 model itself is the problem; PyMatting and GuidedFilter are meaningfully cruder than the VITMatte options and are there for speed, not quality. And this pack's whole family of dependency-heavy nodes shares the same general install failure mode - if the node won't even import, that's almost always a transformers/opencv version conflict fixable with repair_dependency.bat, not something specific to SAM2 itself.
Inputs (15)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| bboxes | BBOXES | — | |
| sam2_model | COMBO | 8 options: sam2_hiera_base_plus.safetensors, sam2_hiera_large.safetensors, sam2_hiera_small.safetensors, sam2_hiera_tiny.safetensors, sam2.1_hiera_base_plus.safetensors, sam2.1_hiera_large.safetensors, +2 | |
| precision | COMBO | 3 options: fp16, bf16, fp32 | |
| bbox_select | COMBO | 3 options: all, first, by_index | |
| select_index | STRING | 0, | — |
| cache_model | BOOLEAN | false | — |
| detail_method | COMBO | 5 options: VITMatte, VITMatte(local), vitmatte-base-composition-1k, PyMatting, GuidedFilter | |
| detail_erode | INT | 61–255 | — |
| detail_dilate | INT | 41–255 | — |
| black_point | FLOAT | 0.150.01–0.98 | — |
| white_point | FLOAT | 0.990.02–0.99 | — |
| process_detail | BOOLEAN | true | — |
| device | COMBO | 2 options: cuda, cpu | |
| max_megapixels | FLOAT | 2.01–999 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |