LayerMask: Load SAM2 Model(Advance)
Load SAM2 Model — Meta's second-gen segmenter, loaded once and shared
- sam2_model
SAM2 is Meta's follow-up to the original Segment Anything Model, with real accuracy gains and - the bigger deal - video support, meaning it can track a segmented object across frames instead of only ever working on single images. This node is the loader: it doesn't segment anything itself, it just loads a SAM2 checkpoint and hands the result to SAM2Ultra or SAM2VideoUltra downstream. It exists as its own node for the same reason the pack's other model loaders got split out - so multiple SAM2-using nodes in one workflow can share a single loaded model instead of each loading their own copy. chflame163's SAM2 nodes are themselves adapted from kijai's ComfyUI-segment-anything-2 implementation.
Three inputs, all straightforward: sam2_model picks from eight checkpoint files, covering both the original SAM2 line (sam2_hiera_base_plus/large/small/tiny) and the improved SAM2.1 line (sam2.1_hiera_base_plus/large/small/tiny) - if you don't have a specific reason to use the older weights, the 2.1 versions are the newer, generally better choice, with the usual size-versus-quality tradeoff across base/large/small/tiny. precision picks fp16, bf16, or fp32 - fp16 is the standard choice for speed and memory unless you're hitting numerical issues, in which case bf16 is the usual fallback. device is cuda or cpu. Output is sam2_model, which feeds into either of the SAM2-based masking nodes.
Worth knowing before you reach for this: the pack's own README calls out that SAM2 Ultra (the single-image node this loader feeds) only supports one image at a time - if you need to process a batch, convert your image batch to an image list first rather than expecting it to iterate automatically. The video-capable sibling, SAM2VideoUltra, is the one built for multi-frame sequences and works differently, defining the recognition target on the first frame and propagating it forward.
Install the pack via ComfyUI Manager (search "ComfyUI Layer Style Advance") or git clone https://github.com/chflame163/ComfyUI_LayerStyle_Advance into custom_nodes/, restart, run the requirements installer. Model files download from the author's Baidu link or from huggingface.co/Kijai/sam2-safetensors, and go in ComfyUI/models/sam2. These are .safetensors files, so they're not subject to the same pickle-security concerns as older .pth checkpoints, but they're still a real download - the larger hiera variants are multiple gigabytes.
Because this is a relatively clean, modern loader (safetensors, no bert-base-uncased-style external tokenizer dependency), it's less prone to the exotic dependency errors that hit the original SAM/GroundingDINO nodes in this pack. The main failure mode is just picking a precision your hardware doesn't like - if you're on an older GPU without good bf16 support and inference is producing garbage or crashing, switch to fp16 or fp32 and see if that resolves it. Beyond that, general pack install issues (a bad opencv-contrib-python, stale dependencies after an update) are the more likely culprit than anything unique to SAM2 itself - repair_dependency.bat in the plugin folder is worth running first if imports fail.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| 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 | |
| device | COMBO | 2 options: cuda, cpu |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| sam2_model | LS_SAM2_MODEL | — |