segment-anything
Use Meta Segment Anything (SAM) for prompt-based segmentation, automatic mask generation, ONNX export, browser deployment, checkpoints, optional dependencies, and troubleshooting.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- segment-anything
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Use Meta Segment Anything (SAM) for prompt-based segmentation, automatic mask generation, ONNX export, browser deployment, checkpoints, optional dependencies, and troubleshooting.
- disable-model-invocation
- true
02 资源文件 6
- 参考文档references/data-formats.md2.8 KB
- 参考文档references/repo-provenance.md1.7 KB
- 参考文档references/repo-routing-metadata.json343 B
- 参考文档references/setup-and-checkpoints.md2.9 KB
- 参考文档references/troubleshooting.md3.1 KB
- 脚本scripts/check_environment.py2.6 KB
03 SKILL.md 正文
Segment Anything
Use this repo skill when a user asks for help with the original Segment Anything (SAM) Python package, including checkpoint setup, promptable image segmentation, automatic mask generation, ONNX export, or browser deployment.
Start Here
- Install the package and required backend dependencies. SAM requires Python 3.8+, PyTorch, and TorchVision; CUDA is recommended for real workloads but CPU is usable for small inspections and examples.
- Choose a checkpoint/model pair from
references/setup-and-checkpoints.mdbefore writing code or commands. - Run
scripts/check_environment.py --check-optionalto verify imports and optional packages before troubleshooting deeper issues. - Route the user request to the narrowest sub-skill below.
Route by Task
- Use
sub-skills/prompted-segmentation/forSamPredictor, point/box prompts, repeated prompts on one image, iterative low-resolution mask refinement, and image embedding extraction. - Use
sub-skills/automatic-mask-generation/forSamAutomaticMaskGenerator, all-object masks, folder runs, PNG/CSV outputs, COCO RLE JSON, and AMG CLI tuning. - Use
sub-skills/onnx-and-browser/for ONNX export, quantization, ONNXRuntime inputs, browser asset wiring, image embeddings for web demos, and SharedArrayBuffer/header issues.
Installation Pattern
For package users, prefer the public install path plus explicit backend packages:
pip install git+https://github.com/facebookresearch/segment-anything.git
pip install torch torchvision
For local development against a checkout:
pip install -e .
Optional workflows need extra packages:
pip install opencv-python pycocotools matplotlib onnx onnxruntime
Install only the extras needed for the selected workflow: OpenCV for image I/O and small-region cleanup, pycocotools for COCO RLE, matplotlib for notebook-style visualization, and onnx/onnxruntime for export, validation, and quantization.
Minimal Import Check
from segment_anything import SamPredictor, SamAutomaticMaskGenerator, sam_model_registry
print(sorted(sam_model_registry.keys())) # ['default', 'vit_b', 'vit_h', 'vit_l']
default is equivalent to vit_h. Checkpoint files are not bundled with the package; the user must provide the matching .pth checkpoint.
Shared References and Scripts
- Read
references/setup-and-checkpoints.mdbefore selecting a model type, checkpoint, device, or optional dependency set. - Read
references/data-formats.mdwhen handling prompt arrays, mask outputs, annotation records, or SA-1B-style JSON. - Read
references/troubleshooting.mdfor install/import, checkpoint, optional dependency, backend, and cross-workflow failures. - Read
references/repo-provenance.mdbefore deciding whether this generated skill is stale for a current checkout. - Run
scripts/check_environment.pyfor a safe import and optional dependency diagnostic.
Common Decisions
- Use
vit_bfor lower memory and faster experiments,vit_lfor a larger backbone, andvit_hordefaultfor the largest documented model. - Prefer
device="cuda"for real segmentation throughput when CUDA PyTorch is correctly installed; usedevice="cpu"for parser checks, tiny examples, or machines without GPU access. - Use prompted segmentation when the user has object hints; use automatic mask generation when the user wants proposal masks for every object.
- Use ONNX only for the lightweight prompt/mask-decoder path; image embeddings still come from the SAM image encoder in Python and must match the exported checkpoint/model type.
Self-Containment Notes
This skill distills repository evidence into bundled references and scripts. Future agents should not need to open original notebooks, demo files, or repo scripts to answer normal SAM usage questions; use the bundled sub-skills and helpers instead.
04 同仓库更多 Skills
passnet-triton-opt
Make a single PassNet Triton kernel fast AND numerically faithful: performance model (when a replacement can win at all), block/grid/warp tuning, autotune policy, launch overhead, and per-op numeric recipes to pass the dtype baseline tolerances. Use when a pass matches and is correct but speedup below expected, or when correctness fails by small numeric margins.
passnet-solve
END-TO-END playbook for solving one PassNet sample: analyze the computation graphs, decide the optimization strategy (what to fuse, what to replace, what to leave alone), drive the iteration loop, and maximize the sample score. This is the ENTRY skill — invoke it first for any PassNet optimization task; it tells you when to use passnet-pattern-fusion, passnet-triton-opt and passnet-feedback.
passnet-skill
PassNet GPU kernel optimization via compiler passes. Design and implement Triton-based optimization passes, create pass files under ./pass_dir/, self-evaluate with pass_evaluator, and iterate to maximize GPU speedup.
passnet-pattern-fusion
Author PassNet pass files: write patterns that actually MATCH the FX graph, pick fusion regions (which ops to absorb into one Triton kernel), and structure multi-pass submissions with the mandatory shared-dispatch architecture. Use when creating or fixing pattern()/replacement_args()/replacement_func() files, when a pass "failed to match", or when deciding how to fuse multiple kernels/ops.
passnet-orchestrate
Round/batch planning and decision support for PassNet graph optimization. Use for multi-sample triage, worker allocation, eval-budget policy, and post-eval keep/revert decisions. For solving one concrete sample end to end, use passnet-solve as the entry skill; consult this skill only when you need broader planning or a specific decision gate.
passnet-feedback
Fast, deterministic feedback for PassNet work: pre-flight pattern/match verification WITHOUT burning a GPU evaluation, per-node bottleneck analysis of a sample's graphs, and eval-log parsing into per-variant status + estimated score + failure classification. Use BEFORE every GPU evaluation (check_pattern), at the START of a sample (analyze_graph), and AFTER every evaluation (parse_eval_log).