Nodes/segment anything/SAMModelLoader (segment anything)
ComfyUI Node Runs on cloud

SAMModelLoader (segment anything)

SAMModelLoader (segment anything)

By storyicon·Created 3 years ago·Updated 2 years ago· 1,114
SAMModelLoader (segment anything)
    • SAM_MODEL
    model_name

    The other loader in the pair. Where GroundingDinoModelLoader loads the "find the object" model, this one loads the "trace its exact outline" model - SAM, Meta's Segment Anything Model. It's a one-input node whose entire job is to pick a SAM checkpoint and load it, then hand it to the segment node. Small node, but it's where you make the one real tradeoff in this pack: quality versus size.

    Why you'd reach for it

    If you're using GroundingDinoSAMSegment, you need this. GroundingDINO gives you a rough box around "the dog"; SAM is what turns that box into a mask that actually hugs the dog's fur instead of a rectangle. Our masking notes call SAM Meta's "universal segmentation model" - give it any image and a hint (a point, a box, a prompt) and it segments the object precisely. This node loads the weights that do that. It's separate from the segment node so the checkpoint loads once and gets reused across your whole graph rather than reloading every run.

    How it works

    SAM is a promptable segmenter: it was trained on a massive mask dataset to produce a clean mask from a minimal cue. In this pack the cue is the bounding box that GroundingDINO produced, but the model itself is general. This node reads the .pth checkpoint, builds the SAM model, and outputs a handle. The heavy lifting - the actual mask prediction - happens downstream inside the segment node; here you're just choosing and loading which SAM to use.

    The inputs and outputs that matter

    One input, model_name, and this is the whole decision. The dropdown offers seven checkpoints, which fall into three families:

    • sam_vit_h / sam_vit_l / sam_vit_b - the original SAM in Huge (2.56GB), Large (1.25GB), and Base (375MB). Bigger is more accurate and heavier. vit_h is the reference-quality pick; vit_b is the budget one.
    • sam_hq_vit_h / _l / _b - "HQ" variants trained for sharper, higher-fidelity edges. If your masks come out a little loose around fine boundaries, HQ is the upgrade to try - roughly the same sizes as their plain counterparts.
    • mobile_sam (39MB) - the tiny one. Fast, light, and genuinely fine for clear subjects where you don't need surgical edges. Great for batch jobs on modest hardware.

    Sensible defaults: reach for sam_vit_h when quality matters and you've got the VRAM, mobile_sam when you're batch-processing or tight on memory, and an HQ variant when edges are the thing letting you down.

    The single output is SAM_MODEL, which plugs into the sam_model input of GroundingDinoSAMSegment. Nowhere else.

    How to install it

    Ships with the pack, no separate step. ComfyUI Manager: search segment anything (the storyicon pack), install, restart. Or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/storyicon/comfyui_segment_anything
    

    then pip install -r requirements.txt and restart. SAM weights auto-download the first time you select one, or pre-place them in models/sams under your ComfyUI root, keeping the original filenames. Heads up on the download: sam_vit_h is 2.56GB, so first use of the big checkpoint isn't instant.

    Common issues & troubleshooting

    Out of memory when segmenting. The SAM checkpoint you pick here is loaded into VRAM alongside GroundingDINO and whatever else your workflow is running. If you're hitting OOM, step down from vit_h to vit_l, or drop to mobile_sam - for a lot of masking jobs the difference is invisible and the memory savings are large.

    Masks look loose or blobby. That's a model-quality ceiling, not a bug. Move up the ladder - vit_b to vit_l to vit_h - or switch to an sam_hq variant, which exists specifically for cleaner edges. SAM in general is strong but not flawless on very fine structures like stray hair.

    Download stalls. Grab the file by hand from the pack README's link and drop it in models/sams, or set HTTP_PROXY/HTTPS_PROXY if the host is slow to you.

    One thing this loader can't fix: if the detection is wrong - SAM masked the wrong object - that's a GroundingDINO problem, not a SAM one. Tune the prompt and threshold at the segment node. SAM only draws the outline of whatever box it was handed.

    Categorysegment_anything

    Inputs (1)

    NameTypeDefaultDescription
    model_nameCOMBO7 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

    Outputs (1)

    NameTypeDescription
    SAM_MODELSAM_MODEL