Skip to content

Instantly share code, notes, and snippets.

@yunho-c
Created March 18, 2026 21:47
Show Gist options
  • Select an option

  • Save yunho-c/693dd9d49e23efbdcf8180df5416a70d to your computer and use it in GitHub Desktop.

Select an option

Save yunho-c/693dd9d49e23efbdcf8180df5416a70d to your computer and use it in GitHub Desktop.
Run SAM 3
import torch
from transformers import Sam3Processor, Sam3Model
from PIL import Image
import requests
# Target your Nvidia GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
# Automatically download and load SAM 3 to the GPU
print("Loading model...")
model = Sam3Model.from_pretrained("facebook/sam3").to(device)
processor = Sam3Processor.from_pretrained("facebook/sam3")
# Load a test image
url = "https://images.unsplash.com/photo-1518791841217-8f162f1e1131"
image = Image.open(requests.get(url, stream=True).raw).convert("RGB")
# Process the image with a text prompt
print("Running inference...")
inputs = processor(images=image, text="cat", return_tensors="pt").to(device)
# Generate the segmentation masks
with torch.no_grad():
outputs = model(**inputs)
# Post-process the raw outputs into usable masks and bounding boxes
results = processor.post_process_instance_segmentation(
outputs,
threshold=0.5,
mask_threshold=0.5,
target_sizes=inputs.get("original_sizes").tolist()
)[0]
print(f"Success! Found {len(results['masks'])} objects matching the prompt.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment