Skip to content

feat: add sam3 - #8

Open
chonk-lain wants to merge 4 commits into
mainfrom
dev
Open

feat: add sam3#8
chonk-lain wants to merge 4 commits into
mainfrom
dev

Conversation

@chonk-lain

Copy link
Copy Markdown
Member

No description provided.

Copilot AI lite review requested due to automatic review settings August 17, 2026 18:22
Comment on lines +11 to +18
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v6
# torch, torchvision and the onnx extras live in the dev group, so the
# suite needs it — `dependencies` alone cannot run the tests.
- run: uv sync --group dev
- run: uv run pytest tests/ -q

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Meta SAM3 support and a unified end-to-end inference pipeline to nobg, including prompt/box-driven cutouts, foreground refinement, and ONNX export/runtime support, aligning the library around a consistent “matte logits → alpha → cutout” contract.

Changes:

  • Introduces Sam3 (model + config + processor) with from_origin, predict, and process APIs, plus extensive offline-focused tests.
  • Adds shared pipeline utilities (predict, cutout, post_process_alpha_matting, refine_foreground) and extends BiRefNet with the same predict/process surface.
  • Implements ONNX export/push/load via Onnx_Mixin + OnnxModel, and improves performance in metrics via vectorized/batched implementations (with equivalence tests).

Reviewed changes

Copilot reviewed 19 out of 21 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/test_sam3.py Comprehensive SAM3 config/model/from_origin/autodispatch and loss tests (offline, small model).
tests/test_mixin.py ONNX export + onnxruntime parity tests for BiRefNet and SAM3.
tests/test_metrics.py Adds equivalence tests for new vectorized E-measure and batched connected-components/connectivity error.
tests/test_image_processing_sam3.py SAM3 processor + end-to-end predict/process tests with offline tokenizer fixture.
tests/test_image_processing_birefnet.py Adds refinement and _box_blur correctness tests; adds BiRefNet predict/process tests.
src/nobg/utils.py New shared pipeline utilities: predict, cutout, post_process_alpha_matting, refine_foreground, _box_blur.
src/nobg/sam3/modeling_sam3.py New SAM3 wrapper model + config + from_origin remap/config decoding + ONNX hooks.
src/nobg/sam3/image_processing_sam3.py New Sam3Processor wrapper adding nobg matting methods and default prompt behavior.
src/nobg/sam3/init.py Adds SAM3 package module (empty marker).
src/nobg/mixin.py Major extension: Onnx_Mixin + OnnxModel runtime wrapper + ONNX metadata/model-card support.
src/nobg/metrics.py Vectorizes _e_measure_curve; batches connected-components and connectivity_error to reduce Python loops/syncs.
src/nobg/loss.py Adds SAM3 focal+dice objective and supporting focal/dice functions.
src/nobg/birefnet/modeling_birefnet.py Adds BiRefNet predict, default_processor, and process using shared utils.predict.
src/nobg/birefnet/image_processing_birefnet.py Switches to shared post_process_alpha_matting/cutout and adds refine_foreground.
src/nobg/auto.py Extends AutoModel/AutoProcessor dispatch to SAM3; adds upstream processor-type mappings.
src/nobg/init.py Exports Sam3, Sam3Processor, and OnnxModel.
README.md Documents process/predict, SAM3 usage (prompt/boxes), refinement, and ONNX export/runtime.
pyproject.toml Updates deps/groups: adds ONNX optional extra and dev deps; bumps transformers requirement.
AGENTS.md Updates architecture guidance to include SAM3 processors, shared utils pipeline, tests, and ONNX rules.
.github/workflows/test.yml Adds CI workflow using uv sync --group dev and pytest.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/nobg/auto.py
Comment on lines +87 to +99
# SAM3 also needs a text tokenizer, which no config.json describes, so
# take it from the repo alongside the size-only image processor.
from transformers import AutoTokenizer
from transformers.models.sam3.image_processing_sam3 import Sam3ImageProcessor

image_size = config.get("image_size", 1008)
return Sam3Processor(
Sam3ImageProcessor(size={"height": image_size, "width": image_size}),
AutoTokenizer.from_pretrained(
pretrained_model_name_or_path, token=kwargs.get("token")
),
default_prompt=config.get("default_prompt", DEFAULT_PROMPT),
)
Comment thread src/nobg/utils.py
Comment on lines +241 to +260
single = not isinstance(image, list | tuple)
items = [image] if single else list(image)
if not items:
return []
images = [load_img(item).convert("RGB") for item in items]

# Boxes are per-image, so normalize to one entry per image up front and slice
# them with each chunk below. A bare list of boxes is taken as this call's
# single image; nesting one level deeper gives one box list per image.
per_image_boxes = None
if boxes is not None:
if not isinstance(boxes, list | tuple) or not boxes:
raise TypeError(
"boxes must be a non-empty list of [x1, y1, x2, y2] boxes, or a "
f"list of those (one per image), got {boxes!r}"
)
depth = 0
probe = boxes
while isinstance(probe, list | tuple) and probe:
depth += 1
Comment thread pyproject.toml Outdated
chonk-lain and others added 2 commits August 19, 2026 22:39
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants