merve's picture
merve HF staff
Update README.md
1cfc573 verified
|
raw
history blame
2.17 kB
metadata
license: apache-2.0
pipeline_tag: mask-generation
tags:
  - sam2

SAM2-Hiera-base-plus

This repository contains base variant of SAM2 model. SAM2 is the state-of-the-art mask generation model released by Meta.

Usage

You can use it like below. First install packaged version of SAM2.

pip install samv2 huggingface_hub

Each model requires different classes to infer.

from huggingface_hub import hf_hub_download
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor

hf_hub_download(repo_id = "merve/sam2-hiera-base-plus", filename="sam2_hiera_base_plus.pt", local_dir = "./")
sam2_checkpoint = "../checkpoints/sam2_hiera_base_plus.pt"
model_cfg = "sam2_hiera_b+.yaml"

sam2_model = build_sam2(config, ckpt, device="cuda", apply_postprocessing=False)
predictor = SAM2ImagePredictor(sam2_model)

# it accepts coco format
box = [x1, y1, w, h]
predictor.set_image(image)

masks = predictor.predict(box=box,
multimask_output=False)

For automatic mask generation:

from huggingface_hub import hf_hub_download
from sam2.build_sam import build_sam2
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator

hf_hub_download(repo_id = "merve/sam2-hiera-base-plus", filename="sam2_hiera_base_plus.pt", local_dir = "./")
sam2_checkpoint = "../checkpoints/sam2_hiera_base_plus.pt"
model_cfg = "sam2_hiera_b+.yaml"

sam2 = build_sam2(model_cfg, sam2_checkpoint, device ='cuda', apply_postprocessing=False)

mask_generator = SAM2AutomaticMaskGenerator(sam2)
masks = mask_generator.generate(image)

Resources

The team behind SAM2 made example notebooks for all tasks.