ComfyUI-segment-anything-2
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
Available Nodes
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.safetensorssam2_hiera_large.safetensorssam2_hiera_small.safetensorssam2_hiera_tiny.safetensorssam2.1_hiera_base_plus.safetensorssam2.1_hiera_large.safetensorssam2.1_hiera_small.safetensorssam2.1_hiera_tiny.safetensors
-
Segmentor: The segmentor type to be used with the model. Options include:
single_imagevideoautomaskgenerator
-
Device: Specifies the hardware to execute the model on. Options include:
cudafor NVIDIA GPUscpufor general processingmpsfor Apple devices supporting Metal Performance Shaders
-
Precision: Determines the compute precision level. Options include:
fp16(default)bf16fp32
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-fp16adjustments). -
Error Management: Built-in error handling ensures that incompatible configurations (e.g., attempting to use
fp16precision 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.