HQ-SAM (Segment Anything in High Quality)
repository·main·Indexed 26 days ago
https://github.com/syscv/sam-hqAn enhanced version of the Segment Anything Model (SAM) designed for high-quality, zero-shot image segmentation with higher precision in mask generation. The repository includes HQ-SAM, HQ-SAM 2 for static images and video prediction, and integration with Grounding DINO for open-set object detection. It supports multiple model types including vit_b, vit_l, vit_h, and a real-time optimized vit_tiny.
What's inside HQ-SAM
- HQ-SAM (Segment Anything in High Quality) is an upgrade to the original SAM (Segment Anything Model) designed for high-quality zero-shot segmentation. It was proposed by researchers from ETH Zurich & HKUST and presented at NeurIPS 2023. It aims to improve the precision and quality of segmentation masks compared to the standard SAM model.
Skip the SAM 2 CUDA extension during installation
mainIf you want to skip building the SAM 2 CUDA extension (which provides post-processing to remove small holes and sprinkles in output masks), set the
SAM2_BUILD_CUDAenvironment variable to0. This will not affect core segmentation results in most cases.SAM2_BUILD_CUDA=0 pip install -e ".[notebooks]"Set up the SegInW environment
mainIf you are working with the
seginwfolder for Segmentation in the Wild benchmarks, you must installGroundingDINOin editable mode.cd seginw python -m pip install -e GroundingDINOcd seginw python -m pip install -e GroundingDINOInstall HQ-SAM 2 with Jupyter and Matplotlib
mainTo use the HQ-SAM 2 predictor and run the provided example notebooks, install the
[notebooks]extra via pip.pip install -e ".[notebooks]"Install Grounding DINO
mainInstall Grounding DINO in editable mode using pip. If you have a CUDA environment, ensure the
CUDA_HOMEenvironment variable is set to enable GPU acceleration. If no CUDA is available, the package will be compiled in CPU-only mode.pip install -e .Train HQ-SAM
mainRun the training process using
torch.distributed.launch. You must specify the checkpoint path, the model type, and the output directory.Arguments:
--checkpoint: Path to the initialization checkpoint.--model-type: The ViT backbone type (e.g.,vit_b,vit_l,vit_h).--output: Path to the directory where training results will be saved.--nproc_per_node: Number of GPUs to use.
python -m torch.distributed.launch --nproc_per_node=<num_gpus> train.py --checkpoint <path/to/checkpoint> --model-type <model_type> --output <path/to/output>Prepare HQSeg-44K dataset for training
mainTo train HQ-SAM, you must prepare the HQSeg-44K dataset. The dataset can be downloaded from Hugging Face. The directory structure must follow this pattern:
data |____DIS5K |____cascade_psp | |____DUTS-TE | |____DUTS-TR | |____ecssd | |____fss_all | |____MSRA_10K |____thin_object_detection | |____COIFT | |____HRSOD | |____ThinObject5KDownload HQ-SAM 2 Checkpoints
mainDownload all model checkpoints using the provided shell script in the
checkpointsdirectory, or download the HQ-SAM 2 large checkpoint individually from Hugging Face.cd checkpoints && \ ./download_ckpts.sh && \ cd ..Prepare SegInW evaluation data
mainDownload and unzip the SegInW dataset into the
datadirectory.cd data wget https://huggingface.co/sam-hq-team/SegInW/resolve/main/seginw.zip unzip seginw.zipThe expected structure is
data/seginw/<category_name>/containing various subfolders likeAirplane-Parts,Bottles, etc.cd data wget https://huggingface.co/sam-hq-team/SegInW/resolve/main/seginw.zip unzip seginw.zipForce build the SAM 2 CUDA extension
mainTo ensure the CUDA extension is built and to catch any errors during the process, use the
SAM2_BUILD_ALLOW_ERRORS=0environment variable. This is useful if you want to enable the mask post-processing step and want the installation to fail explicitly if the build fails.pip uninstall -y SAM-2 && \ rm -f ./sam2/*.so && \ SAM2_BUILD_ALLOW_ERRORS=0 pip install -v -e ".[notebooks]"Download pretrained checkpoints
mainDownload the required checkpoints for GroundingDINO, SAM, and HQ-SAM into the
pretrained_checkpointdirectory.cd pretrained_checkpoint wget https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth wget https://huggingface.co/sam-hq-team/sam-hq-training/resolve/main/pretrained_checkpoint/sam_vit_h_4b8939.pth wget https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_h.pthcd pretrained_checkpoint wget https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth wget https://huggingface.co/sam-hq-team/sam-hq-training/resolve/main/pretrained_checkpoint/sam_vit_h_4b8939.pth wget https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_h.pthInstall HQ-SAM via pip
mainFor a quick setup, install the
segment-anything-hqpackage using pip. You can then use thesam_model_registryto load models directly in Python.pip install segment-anything-hq from segment_anything_hq import sam_model_registry model_type = "<model_type>" # e.g., "vit_l", "vit_b", "vit_h", or "vit_tiny" sam_checkpoint = "<path/to/checkpoint>" sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)