prompted-segmentation
Use SAM's SamPredictor for prompt-based masks from points, labels, boxes, iterative low-res mask refinement, image embeddings, and CPU/GPU checkpoint setup.
- License 许可
- Apache-2.0
- 最近更新
- 2026-09-03
- Stars
- ★ 351
- Forks
- 27
01 前言 Front Matter
- name
- prompted-segmentation
- license
- Apache 2.0
- metadata
- { "disco-role": "operating" }
- description
- Use SAM's SamPredictor for prompt-based masks from points, labels, boxes, iterative low-res mask refinement, image embeddings, and CPU/GPU checkpoint setup.
- disable-model-invocation
- true
02 资源文件 4
- 参考文档references/api-reference.md5.5 KB
- 参考文档references/troubleshooting.md5.0 KB
- 参考文档references/workflows.md5.2 KB
- 脚本scripts/predictor_prompt_template.py7.5 KB
03 SKILL.md 正文
Prompted Segmentation
Use this sub-skill when the user wants Segment Anything (SAM) masks for a specific object or region using prompts such as foreground/background points, boxes, prior masks, or repeated prompts on the same image.
When to Use
- Segment an object from a point prompt, box prompt, or combined box plus foreground/background points.
- Reuse one image embedding for multiple prompt attempts with
SamPredictor.set_image(...)followed by repeatedpredict(...)calls. - Refine a mask interactively by passing the best previous
low_res_masksslice back asmask_input. - Debug prompt shape, image format, checkpoint, model type, or device mismatch failures.
- Extract image embeddings for an ONNX/browser handoff with
get_image_embedding(); route ONNX export and browser runtime work to../onnx-and-browser/.
For full-image proposal generation without prompts, use ../automatic-mask-generation/ instead.
Core Pattern
- Choose a model type from
default,vit_h,vit_l, orvit_b, and pass the matching user-provided checkpoint path tosam_model_registry[model_type](checkpoint=...). - Move the SAM model to the selected device before creating or using the predictor:
sam.to(device=device). - Create
predictor = SamPredictor(sam)and callpredictor.set_image(image, image_format="RGB")once per image. - Pass prompts in original image pixel coordinates: points as
Nx2(x, y), point labels as lengthNwith1foreground and0background, and boxes asXYXY. - Use
multimask_output=Truefor ambiguous single-click prompts; useFalsefor boxes or multiple prompts when one mask is desired. - Read outputs as
(masks, scores, low_res_masks), wheremasksisCxHxW,scoresis lengthC, andlow_res_masksisCx256x256logits for refinement.
Required Details
- API signatures, return shapes, and direct
Sambatch notes:references/api-reference.md. - Prompting workflows, embedding reuse, and iterative refinement:
references/workflows.md. - Diagnosis for common runtime failures and shape errors:
references/troubleshooting.md. - Safe command-line helper template:
scripts/predictor_prompt_template.py.
Minimal Prompt Example
import numpy as np
import torch
from segment_anything import SamPredictor, sam_model_registry
model_type = "vit_b"
device = "cuda" if torch.cuda.is_available() else "cpu"
sam = sam_model_registry[model_type](checkpoint="/path/to/sam_vit_b.pth")
sam.to(device=device)
predictor = SamPredictor(sam)
predictor.set_image(image_rgb_uint8_hwc, image_format="RGB")
point_coords = np.array([[500, 375]], dtype=np.float32)
point_labels = np.array([1], dtype=np.int32)
masks, scores, low_res_masks = predictor.predict(
point_coords=point_coords,
point_labels=point_labels,
multimask_output=True,
)
best_mask = masks[int(np.argmax(scores))]
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).