File size: 2,105 Bytes
6139499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04711d4
 
6139499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04711d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6139499
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
license: apache-2.0
pipeline_tag: mask-generation
tags:
- sam2
---

# SAM2-Hiera-small

This repository contains small 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.

```bash
pip install samv2 huggingface_hub
```

Each model requires different classes to infer. 

For prompting:

```python

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-small", filename="sam2_hiera_small.pt", local_dir = "./")

ckpt = f"./sam2_hiera_small.pt"
config = "sam2_hiera_s.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:

```python
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-small", filename="sam2_hiera_small.pt", local_dir = "./")
ckpt = f"./sam2_hiera_small.pt"
config = "sam2_hiera_s.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.

- See [image predictor example](https://github.com/facebookresearch/segment-anything-2/blob/main/notebooks/image_predictor_example.ipynb) for full example on prompting.

- See [automatic mask generation example](https://github.com/facebookresearch/segment-anything-2/blob/main/notebooks/automatic_mask_generator_example.ipynb) for generating all masks.

- See [video object segmentation example](https://github.com/facebookresearch/segment-anything-2/blob/main/notebooks/video_predictor_example.ipynb)