Summary: there are several imports within the `sam3.model` package that reference other packages within `sam3` other than `sam3` and `sam3.model` (for example [here](https://github.com/facebookresearch/sam3/blob/main/sam3/model/sam3_tracker_base.py#L15)). This fixes the package structure so that you can `pip install` the package and `import sam3` Pull Request resolved: https://github.com/facebookresearch/sam3/pull/327 Reviewed By: haithamkhedr Differential Revision: D88950127 Pulled By: lematt1991 fbshipit-source-id: 3554512d304ccdf679a9af8606bbfe1f7f2a1cfb
7.6 KiB
7.6 KiB
In [1]:
# Copyright (c) Meta Platforms, Inc. and affiliates.In [ ]:
import torch
# turn on tfloat32 for Ampere GPUs
# https://pytorch.org/docs/stable/notes/cuda.html#tensorfloat-32-tf32-on-ampere-devices
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# use bfloat16 for the entire notebook. If your card doesn't support it, try float16 instead
torch.autocast("cuda", dtype=torch.bfloat16).__enter__()
# inference mode for the whole notebook. Disable if you need gradients
torch.inference_mode().__enter__()In [ ]:
import os
SAM3_ROOT = os.path.dirname(os.getcwd())
os.chdir(SAM3_ROOT)
# setup GPU to use - A single GPU is good with the purpose of this demo
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
_ = os.system("nvidia-smi")In [ ]:
import sam3
from sam3 import build_sam3_image_model
from sam3.model.sam3_image_processor import Sam3Processor
sam3_root = os.path.dirname(sam3.__file__)
bpe_path = f"{sam3_root}/assets/bpe_simple_vocab_16e6.txt.gz"
model = build_sam3_image_model(bpe_path=bpe_path)
processor = Sam3Processor(model, confidence_threshold=0.5)In [ ]:
LLM_CONFIGS = {
# vLLM-served models
"qwen3_vl_8b_thinking": {
"provider": "vllm",
"model": "Qwen/Qwen3-VL-8B-Thinking",
},
# models served via external APIs
# add your own
}
model = "qwen3_vl_8b_thinking"
LLM_API_KEY = "DUMMY_API_KEY"
llm_config = LLM_CONFIGS[model]
llm_config["api_key"] = LLM_API_KEY
llm_config["name"] = model
# setup API endpoint
if llm_config["provider"] == "vllm":
LLM_SERVER_URL = "http://0.0.0.0:8001/v1" # replace this with your vLLM server address as needed
else:
LLM_SERVER_URL = llm_config["base_url"]In [ ]:
from functools import partial
from IPython.display import display, Image
from sam3.agent.client_llm import send_generate_request as send_generate_request_orig
from sam3.agent.client_sam3 import call_sam_service as call_sam_service_orig
from sam3.agent.inference import run_single_image_inferenceIn [ ]:
# prepare input args and run single image inference
image = "assets/images/test_image.jpg"
prompt = "the leftmost child wearing blue vest"
image = os.path.abspath(image)
send_generate_request = partial(send_generate_request_orig, server_url=LLM_SERVER_URL, model=llm_config["model"], api_key=llm_config["api_key"])
call_sam_service = partial(call_sam_service_orig, sam3_processor=processor)
output_image_path = run_single_image_inference(
image, prompt, llm_config, send_generate_request, call_sam_service,
debug=True, output_dir="agent_output"
)
# display output
if output_image_path is not None:
display(Image(filename=output_image_path))In [ ]:
In [ ]: