Summary: SAM 3.1 multiplex video tracking for the OSS sam3 repository. Features: - Multiplex architecture: batch N objects into fixed-capacity buckets for efficient multi-object tracking (16 objects per bucket) - Unified API: build_sam3_predictor(version="sam3.1") with auto-download from HuggingFace (facebook/sam3.1) - Flash Attention 3, torch.compile support (~31 FPS compiled on H100) - Text prompts, point prompts, box prompts, object removal, refinement - Clean inheritance: Sam3MultiplexBase extends Sam3VideoBase - Shared base predictor (Sam3BasePredictor) for both SAM3 and SAM3.1 Performance (H100, 5 objects, compiled): - SAM 3.1: ~31 FPS - Memory: ~17.7 GB Usage: from sam3 import build_sam3_predictor predictor = build_sam3_predictor() # auto-downloads SAM 3.1 from HF Pull Request resolved: https://github.com/facebookresearch/sam3/pull/503 Reviewed By: josephgreer Differential Revision: D98500763 Pulled By: arpitkalla fbshipit-source-id: e2b74568f24bb4ecfb3144a4e8ec559aae6db5e8
20 KiB
20 KiB
In [ ]:
# Copyright (c) Meta Platforms, Inc. and affiliates.In [ ]:
using_colab = FalseIn [ ]:
if using_colab:
import torch
import torchvision
print("PyTorch version:", torch.__version__)
print("Torchvision version:", torchvision.__version__)
print("CUDA is available:", torch.cuda.is_available())
import sys
!{sys.executable} -m pip install opencv-python matplotlib scikit-learn
!{sys.executable} -m pip install 'git+https://github.com/facebookresearch/sam3.git'In [ ]:
!nvidia-smiIn [ ]:
import os
import sam3
import torch
sam3_root = os.path.join(os.path.dirname(sam3.__file__), "..")In [ ]:
from sam3.model_builder import build_sam3_multiplex_video_predictor
predictor = build_sam3_multiplex_video_predictor()In [ ]:
import glob
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image
from sam3.visualization_utils import (
load_frame,
prepare_masks_for_visualization,
visualize_formatted_frame_output,
)
plt.rcParams["axes.titlesize"] = 12
plt.rcParams["figure.titlesize"] = 12
def propagate_in_video(predictor, session_id):
outputs_per_frame = {}
for response in predictor.handle_stream_request(
request=dict(
type="propagate_in_video",
session_id=session_id,
)
):
outputs_per_frame[response["frame_index"]] = response["outputs"]
return outputs_per_frame
def abs_to_rel_coords(coords, IMG_WIDTH, IMG_HEIGHT, coord_type="point"):
"""Convert absolute coordinates to relative coordinates (0-1 range)
Args:
coords: List of coordinates
coord_type: 'point' for [x, y] or 'box' for [x, y, w, h]
"""
if coord_type == "point":
return [[x / IMG_WIDTH, y / IMG_HEIGHT] for x, y in coords]
elif coord_type == "box":
return [
[x / IMG_WIDTH, y / IMG_HEIGHT, w / IMG_WIDTH, h / IMG_HEIGHT]
for x, y, w, h in coords
]
else:
raise ValueError(f"Unknown coord_type: {coord_type}")In [ ]:
# "video_path" needs to be either a JPEG folder or a MP4 video file
video_path = f"{sam3_root}/assets/videos/0001"In [ ]:
# load "video_frames_for_vis" for visualization purposes (they are not used by the model)
if isinstance(video_path, str) and video_path.endswith(".mp4"):
cap = cv2.VideoCapture(video_path)
video_frames_for_vis = []
while True:
ret, frame = cap.read()
if not ret:
break
video_frames_for_vis.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
cap.release()
else:
video_frames_for_vis = glob.glob(os.path.join(video_path, "*.jpg"))
try:
video_frames_for_vis.sort(
key=lambda p: int(os.path.splitext(os.path.basename(p))[0])
)
except ValueError:
print(
f'frame names are not in "<frame_index>.jpg" format: {video_frames_for_vis[:5]=}, '
f"falling back to lexicographic sort."
)
video_frames_for_vis.sort()In [ ]:
response = predictor.handle_request(
request=dict(
type="start_session",
resource_path=video_path,
)
)
session_id = response["session_id"]In [ ]:
# note: in case you already ran one text prompt and now want to switch to another text prompt
# it's required to reset the session first (otherwise the results would be wrong)
_ = predictor.handle_request(
request=dict(
type="reset_session",
session_id=session_id,
)
)In [ ]:
prompt_text_str = "person"
frame_idx = 0 # add a text prompt on frame 0
response = predictor.handle_request(
request=dict(
type="add_prompt",
session_id=session_id,
frame_index=frame_idx,
text=prompt_text_str,
)
)
out = response["outputs"]
plt.close("all")
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[prepare_masks_for_visualization({frame_idx: out})],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# we pick id 2, which is the dancer in the front
obj_id = 2
response = predictor.handle_request(
request=dict(
type="remove_object",
session_id=session_id,
obj_id=obj_id,
)
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
sample_img = Image.fromarray(load_frame(video_frames_for_vis[0]))
IMG_WIDTH, IMG_HEIGHT = sample_img.sizeIn [ ]:
# let's add back the dancer via point prompts.
# we will use a single positive click to add the dancer back.
frame_idx = 0
obj_id = 2
points_abs = np.array(
[
[760, 550], # positive click
]
)
# positive clicks have label 1, while negative clicks have label 0
labels = np.array([1])In [ ]:
# convert points and labels to tensors; also convert to relative coordinates
points_tensor = torch.tensor(
abs_to_rel_coords(points_abs, IMG_WIDTH, IMG_HEIGHT, coord_type="point"),
dtype=torch.float32,
)
points_labels_tensor = torch.tensor(labels, dtype=torch.int32)
response = predictor.handle_request(
request=dict(
type="add_prompt",
session_id=session_id,
frame_index=frame_idx,
points=points_tensor,
point_labels=points_labels_tensor,
obj_id=obj_id,
)
)
out = response["outputs"]
plt.close("all")
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[prepare_masks_for_visualization({frame_idx: out})],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
points_list=[points_abs],
points_labels_list=[labels],
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
# For the dancer in the front, suppose now we only want to segment her T-shirt instead of her whole body
# we will use 2 positive clicks and 2 negative clicks to select her shirt.
refine_object_3 = True
if refine_object_3:
frame_idx = 0
obj_id = 3
points_abs = np.array(
[
[800, 135], # positive click
[800, 180], # negative click
]
)
# positive clicks have label 1, while negative clicks have label 0
labels = np.array([1, 0])
else:
frame_idx = 0
obj_id = 2
points_abs = np.array(
[
[740, 450], # positive click
[760, 630], # negative click
[840, 640], # negative click
[760, 550], # positive click
]
)
# positive clicks have label 1, while negative clicks have label 0
labels = np.array([1, 0, 0, 1])In [ ]:
# convert points and labels to tensors; also convert to relative coordinates
points_tensor = torch.tensor(
abs_to_rel_coords(points_abs, IMG_WIDTH, IMG_HEIGHT, coord_type="point"),
dtype=torch.float32,
)
points_labels_tensor = torch.tensor(labels, dtype=torch.int32)
response = predictor.handle_request(
request=dict(
type="add_prompt",
session_id=session_id,
frame_index=frame_idx,
points=points_tensor,
point_labels=points_labels_tensor,
obj_id=obj_id,
)
)
out = response["outputs"]
plt.close("all")
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[prepare_masks_for_visualization({frame_idx: out})],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
points_list=[points_abs],
points_labels_list=[labels],
)In [ ]:
# now we propagate the outputs from frame 0 to the end of the video and collect all outputs
outputs_per_frame = propagate_in_video(predictor, session_id)
# finally, we reformat the outputs for visualization and plot the outputs every 60 frames
outputs_per_frame = prepare_masks_for_visualization(outputs_per_frame)
vis_frame_stride = 60
plt.close("all")
for frame_idx in range(0, len(outputs_per_frame), vis_frame_stride):
visualize_formatted_frame_output(
frame_idx,
video_frames_for_vis,
outputs_list=[outputs_per_frame],
titles=["SAM 3.1 Dense Tracking outputs"],
figsize=(6, 4),
)In [ ]:
_ = predictor.handle_request(
request=dict(
type="close_session",
session_id=session_id,
)
)