← See All Custom Node Packs

ComfyUI-segment-anything-2

1206

Run ComfyUI Easily with InstaSD

Skip the complex setup and run ComfyUI online. InstaSD helps creative professionals build workflows and deploy them to the world:

  • One-click deployment
  • Any model, any node
  • Powerful GPUs for rapid iteration
Get Started

DownloadAndLoadSAM2Model

DownloadAndLoadSAM2Model Node Documentation

Overview

The DownloadAndLoadSAM2Model node is an integral component of the ComfyUI workflows designed to manage the retrieval and loading of SAM2 models. It provides a streamlined method for downloading and preparing these models for use on various devices, including CUDA GPUs, CPUs, and MPS (Metal Performance Shaders) devices. The node supports multiple model architectures and configurations, accommodating various precision settings to optimize performance on supported hardware.

Functionality

The primary function of the DownloadAndLoadSAM2Model node is to check the availability of specified SAM2 models locally and download them if necessary. Once downloaded, the model is loaded into the appropriate format for execution based on the user's selected configurations. The node is responsible for ensuring compatibility between the model, the device it's executed on, and the precision level desired by the user.

Inputs

The node accepts the following inputs:

  • Model: The specific model variant to be loaded. Options include multiple versions of SAM2, such as:

    • 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
    • sam2.1_hiera_small.safetensors
    • sam2.1_hiera_tiny.safetensors
  • Segmentor: The segmentor type to be used with the model. Options include:

    • single_image
    • video
    • automaskgenerator
  • Device: Specifies the hardware to execute the model on. Options include:

    • cuda for NVIDIA GPUs
    • cpu for general processing
    • mps for Apple devices supporting Metal Performance Shaders
  • Precision: Determines the compute precision level. Options include:

    • fp16 (default)
    • bf16
    • fp32

Outputs

The DownloadAndLoadSAM2Model node outputs the following:

  • SAM2MODEL: A dictionary-like object encapsulating the loaded model that's ready for subsequent operations or processing in the workflow. This object contains the model's architecture, device allocation, and precision settings alongside additional metadata, including the segmentor type and SAM2 version.

Usage in ComfyUI Workflows

Within ComfyUI workflows, the DownloadAndLoadSAM2Model node serves as an initialization step, ensuring that the desired SAM2 model is correctly prepared and accessible for downstream tasks such as image or video segmentation. By defining the necessary inputs regarding model version, device, and computational precision at this node, users can seamlessly integrate SAM2-based operations into more complex image processing pipelines without manually handling model files or configurations.

Special Features and Considerations

  • Automatic Download: This node simplifies the process of model management by automatically downloading the specified model if it's not found locally, reducing manual overhead for users.

  • Compatibility Handling: The node includes logic to ensure model and device compatibility, such as adjusting for precision differences when using CPU devices or enabling Tensor Float 32 (TF32) on compatible NVIDIA GPUs for enhanced performance.

  • Versioning Support: The node distinguishes between different versions of SAM2 (e.g., 2.0 vs. 2.1) and adjusts its configurations accordingly to ensure proper functionality (sam2.1_xxx-fp16 adjustments).

  • Error Management: Built-in error handling ensures that incompatible configurations (e.g., attempting to use fp16 precision on CPU devices) are flagged with appropriate error messages, guiding users to corrective actions.

This node is fundamental for users looking to leverage advanced segmentation models within ComfyUI, providing both a high level of automation and customizability to meet specific processing needs.

Want to try ComfyUI-segment-anything-2 without installing anything? Run ComfyUI online on InstaSD's cloud GPUs — no setup required.