Instructions to use jamiewjm/sam-tp with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sam2
How to use jamiewjm/sam-tp with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained(jamiewjm/sam-tp) 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(jamiewjm/sam-tp) 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
| from datasets import load_dataset | |
| from pathlib import Path | |
| from PIL import Image | |
| REPO = "jamiewjm/sam-tp" # change to your dataset repo id | |
| ds_imgs = load_dataset( | |
| "imagefolder", | |
| data_dir=".", | |
| data_files={"image": f"hf://datasets/{REPO}/images/**"}, | |
| split="train", | |
| ) | |
| ds_msks = load_dataset( | |
| "imagefolder", | |
| data_dir=".", | |
| data_files={"mask": f"hf://datasets/{REPO}/annotations/**"}, | |
| split="train", | |
| ) | |
| mask_index = {Path(r["image"]["path"]).name: r["image"]["path"] for r in ds_msks} | |
| row = ds_imgs[0] | |
| img_path = Path(row["image"]["path"]) | |
| msk_path = Path(mask_index[img_path.name]) | |
| print("Image:", img_path) | |
| print("Mask: ", msk_path) | |
| Image.open(img_path).show() | |
| Image.open(msk_path).show() | |