feat: add sam3 - #8
Open
chonk-lain wants to merge 4 commits into
Open
Conversation
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 |
There was a problem hiding this comment.
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) withfrom_origin,predict, andprocessAPIs, plus extensive offline-focused tests. - Adds shared pipeline utilities (
predict,cutout,post_process_alpha_matting,refine_foreground) and extendsBiRefNetwith the samepredict/processsurface. - 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 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 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 |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.