Instructions to use merve/sam2-hiera-tiny with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use merve/sam2-hiera-tiny with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained(merve/sam2-hiera-tiny) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): predictor.set_image(<your_image>) masks, _, _ = predictor.predict(<input_prompts>)# Use SAM2 with videos import torch from sam2.sam2_video_predictor import SAM2VideoPredictor predictor = SAM2VideoPredictor.from_pretrained(merve/sam2-hiera-tiny) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): state = predictor.init_state(<your_video>) # add new prompts and instantly get the output on the same frame frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>): # propagate the prompts to get masklets throughout the video for frame_idx, object_ids, masks in predictor.propagate_in_video(state): ... - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -20,7 +20,6 @@ pip install samv2 huggingface_hub
|
|
| 20 |
Each model requires different classes to infer.
|
| 21 |
|
| 22 |
```python
|
| 23 |
-
|
| 24 |
from huggingface_hub import hf_hub_download
|
| 25 |
from sam2.build_sam import build_sam2
|
| 26 |
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
@@ -41,6 +40,24 @@ masks = predictor.predict(box=box,
|
|
| 41 |
multimask_output=False)
|
| 42 |
```
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
## Resources
|
| 45 |
|
| 46 |
The team behind SAM2 made example notebooks for all tasks.
|
|
|
|
| 20 |
Each model requires different classes to infer.
|
| 21 |
|
| 22 |
```python
|
|
|
|
| 23 |
from huggingface_hub import hf_hub_download
|
| 24 |
from sam2.build_sam import build_sam2
|
| 25 |
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
|
|
| 40 |
multimask_output=False)
|
| 41 |
```
|
| 42 |
|
| 43 |
+
For automatic mask generation:
|
| 44 |
+
|
| 45 |
+
```python
|
| 46 |
+
from huggingface_hub import hf_hub_download
|
| 47 |
+
from sam2.build_sam import build_sam2
|
| 48 |
+
from sam2.automatic_mask_generator import SAM2AutomaticMaskGenerator
|
| 49 |
+
|
| 50 |
+
hf_hub_download(repo_id = "merve/sam2-hiera-tiny", filename="sam2_hiera_tiny.pt", local_dir = "./")
|
| 51 |
+
sam2_checkpoint = "../checkpoints/sam2_hiera_tiny.pt"
|
| 52 |
+
model_cfg = "sam2_hiera_t.yaml"
|
| 53 |
+
|
| 54 |
+
sam2 = build_sam2(model_cfg, sam2_checkpoint, device ='cuda', apply_postprocessing=False)
|
| 55 |
+
|
| 56 |
+
mask_generator = SAM2AutomaticMaskGenerator(sam2)
|
| 57 |
+
masks = mask_generator.generate(image)
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
|
| 61 |
## Resources
|
| 62 |
|
| 63 |
The team behind SAM2 made example notebooks for all tasks.
|