* docs: deep audit — fix stale config keys, missing commands, and registry drift Cross-checked ~80 high-impact docs pages (getting-started, reference, top-level user-guide, user-guide/features) against the live registries: hermes_cli/commands.py COMMAND_REGISTRY (slash commands) hermes_cli/auth.py PROVIDER_REGISTRY (providers) hermes_cli/config.py DEFAULT_CONFIG (config keys) toolsets.py TOOLSETS (toolsets) tools/registry.py get_all_tool_names() (tools) python -m hermes_cli.main <subcmd> --help (CLI args) reference/ - cli-commands.md: drop duplicate hermes fallback row + duplicate section, add stepfun/lmstudio to --provider enum, expand auth/mcp/curator subcommand lists to match --help output (status/logout/spotify, login, archive/prune/ list-archived). - slash-commands.md: add missing /sessions and /reload-skills entries + correct the cross-platform Notes line. - tools-reference.md: drop bogus '68 tools' headline, drop fictional 'browser-cdp toolset' (these tools live in 'browser' and are runtime-gated), add missing 'kanban' and 'video' toolset sections, fix MCP example to use the real mcp_<server>_<tool> prefix. - toolsets-reference.md: list browser_cdp/browser_dialog inside the 'browser' row, add missing 'kanban' and 'video' toolset rows, drop the stale '38 tools' count for hermes-cli. - profile-commands.md: add missing install/update/info subcommands, document fish completion. - environment-variables.md: dedupe GMI_API_KEY/GMI_BASE_URL rows (kept the one with the correct gmi-serving.com default). - faq.md: Anthropic/Google/OpenAI examples — direct providers exist (not just via OpenRouter), refresh the OpenAI model list. getting-started/ - installation.md: PortableGit (not MinGit) is what the Windows installer fetches; document the 32-bit MinGit fallback. - installation.md / termux.md: installer prefers .[termux-all] then falls back to .[termux]. - nix-setup.md: Python 3.12 (not 3.11), Node.js 22 (not 20); fix invalid 'nix flake update --flake' invocation. - updating.md: 'hermes backup restore --state pre-update' doesn't exist — point at the snapshot/quick-snapshot flow; correct config key 'updates.pre_update_backup' (was 'update.backup'). user-guide/ - configuration.md: api_max_retries default 3 (not 2); display.runtime_footer is the real key (not display.runtime_metadata_footer); checkpoints defaults enabled=false / max_snapshots=20 (not true / 50). - configuring-models.md: 'hermes model list' / 'hermes model set ...' don't exist — hermes model is interactive only. - tui.md: busy_indicator -> tui_status_indicator with values kaomoji|emoji|unicode|ascii (not kawaii|minimal|dots|wings|none). - security.md: SSH backend keys (TERMINAL_SSH_HOST/USER/KEY) live in .env, not config.yaml. - windows-wsl-quickstart.md: there is no 'hermes api' subcommand — the OpenAI-compatible API server runs inside hermes gateway. user-guide/features/ - computer-use.md: approvals.mode (not security.approval_level); fix broken ./browser-use.md link to ./browser.md. - fallback-providers.md: top-level fallback_providers (not model.fallback_providers); the picker is subcommand-based, not modal. - api-server.md: API_SERVER_* are env vars — write to per-profile .env, not 'hermes config set' which targets YAML. - web-search.md: drop web_crawl as a registered tool (it isn't); deep-crawl modes are exposed through web_extract. - kanban.md: failure_limit default is 2, not '~5'. - plugins.md: drop hard-coded '33 providers' count. - honcho.md: fix unclosed quote in echo HONCHO_API_KEY snippet; document that 'hermes honcho' subcommand is gated on memory.provider=honcho; reconcile subcommand list with actual --help output. - memory-providers.md: legacy 'hermes honcho setup' redirect documented. Verified via 'npm run build' — site builds cleanly; broken-link count went from 149 to 146 (no regressions, fixed a few in passing). * docs: round 2 audit fixes + regenerate skill catalogs Follow-up to the previous commit on this branch: Round 2 manual fixes: - quickstart.md: KIMI_CODING_API_KEY mentioned alongside KIMI_API_KEY; voice-mode and ACP install commands rewritten — bare 'pip install ...' doesn't work for curl-installed setups (no pip on PATH, not in repo dir); replaced with 'cd ~/.hermes/hermes-agent && uv pip install -e ".[voice]"'. ACP already ships in [all] so the curl install includes it. - cli.md / configuration.md: 'auxiliary.compression.model' shown as 'google/gemini-3-flash-preview' (the doc's own claimed default); actual default is empty (= use main model). Reworded as 'leave empty (default) or pin a cheap model'. - built-in-plugins.md: added the bundled 'kanban/dashboard' plugin row that was missing from the table. Regenerated skill catalogs: - ran website/scripts/generate-skill-docs.py to refresh all 163 per-skill pages and both reference catalogs (skills-catalog.md, optional-skills-catalog.md). This adds the entries that were genuinely missing — productivity/teams-meeting-pipeline (bundled), optional/finance/* (entire category — 7 skills: 3-statement-model, comps-analysis, dcf-model, excel-author, lbo-model, merger-model, pptx-author), creative/hyperframes, creative/kanban-video-orchestrator, devops/watchers, productivity/shop-app, research/searxng-search, apple/macos-computer-use — and rewrites every other per-skill page from the current SKILL.md. Most diffs are tiny (one line of refreshed metadata). Validation: - 'npm run build' succeeded. - Broken-link count moved 146 -> 155 — the +9 are zh-Hans translation shells that lag every newly-added skill page (pre-existing pattern). No regressions on any en/ page.
526 lines
14 KiB
Markdown
526 lines
14 KiB
Markdown
---
|
||
title: "Segment Anything Model — SAM: zero-shot image segmentation via points, boxes, masks"
|
||
sidebar_label: "Segment Anything Model"
|
||
description: "SAM: zero-shot image segmentation via points, boxes, masks"
|
||
---
|
||
|
||
{/* This page is auto-generated from the skill's SKILL.md by website/scripts/generate-skill-docs.py. Edit the source SKILL.md, not this page. */}
|
||
|
||
# Segment Anything Model
|
||
|
||
SAM: zero-shot image segmentation via points, boxes, masks.
|
||
|
||
## Skill metadata
|
||
|
||
| | |
|
||
|---|---|
|
||
| Source | Bundled (installed by default) |
|
||
| Path | `skills/mlops/models/segment-anything` |
|
||
| Version | `1.0.0` |
|
||
| Author | Orchestra Research |
|
||
| License | MIT |
|
||
| Dependencies | `segment-anything`, `transformers>=4.30.0`, `torch>=1.7.0` |
|
||
| Platforms | linux, macos, windows |
|
||
| Tags | `Multimodal`, `Image Segmentation`, `Computer Vision`, `SAM`, `Zero-Shot` |
|
||
|
||
## Reference: full SKILL.md
|
||
|
||
:::info
|
||
The following is the complete skill definition that Hermes loads when this skill is triggered. This is what the agent sees as instructions when the skill is active.
|
||
:::
|
||
|
||
# Segment Anything Model (SAM)
|
||
|
||
Comprehensive guide to using Meta AI's Segment Anything Model for zero-shot image segmentation.
|
||
|
||
## When to use SAM
|
||
|
||
**Use SAM when:**
|
||
- Need to segment any object in images without task-specific training
|
||
- Building interactive annotation tools with point/box prompts
|
||
- Generating training data for other vision models
|
||
- Need zero-shot transfer to new image domains
|
||
- Building object detection/segmentation pipelines
|
||
- Processing medical, satellite, or domain-specific images
|
||
|
||
**Key features:**
|
||
- **Zero-shot segmentation**: Works on any image domain without fine-tuning
|
||
- **Flexible prompts**: Points, bounding boxes, or previous masks
|
||
- **Automatic segmentation**: Generate all object masks automatically
|
||
- **High quality**: Trained on 1.1 billion masks from 11 million images
|
||
- **Multiple model sizes**: ViT-B (fastest), ViT-L, ViT-H (most accurate)
|
||
- **ONNX export**: Deploy in browsers and edge devices
|
||
|
||
**Use alternatives instead:**
|
||
- **YOLO/Detectron2**: For real-time object detection with classes
|
||
- **Mask2Former**: For semantic/panoptic segmentation with categories
|
||
- **GroundingDINO + SAM**: For text-prompted segmentation
|
||
- **SAM 2**: For video segmentation tasks
|
||
|
||
## Quick start
|
||
|
||
### Installation
|
||
|
||
```bash
|
||
# From GitHub
|
||
pip install git+https://github.com/facebookresearch/segment-anything.git
|
||
|
||
# Optional dependencies
|
||
pip install opencv-python pycocotools matplotlib
|
||
|
||
# Or use HuggingFace transformers
|
||
pip install transformers
|
||
```
|
||
|
||
### Download checkpoints
|
||
|
||
```bash
|
||
# ViT-H (largest, most accurate) - 2.4GB
|
||
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth
|
||
|
||
# ViT-L (medium) - 1.2GB
|
||
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth
|
||
|
||
# ViT-B (smallest, fastest) - 375MB
|
||
wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth
|
||
```
|
||
|
||
### Basic usage with SamPredictor
|
||
|
||
```python
|
||
import numpy as np
|
||
from segment_anything import sam_model_registry, SamPredictor
|
||
|
||
# Load model
|
||
sam = sam_model_registry["vit_h"](https://github.com/NousResearch/hermes-agent/blob/main/skills/mlops/models/segment-anything/checkpoint="sam_vit_h_4b8939.pth")
|
||
sam.to(device="cuda")
|
||
|
||
# Create predictor
|
||
predictor = SamPredictor(sam)
|
||
|
||
# Set image (computes embeddings once)
|
||
image = cv2.imread("image.jpg")
|
||
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
||
predictor.set_image(image)
|
||
|
||
# Predict with point prompts
|
||
input_point = np.array([[500, 375]]) # (x, y) coordinates
|
||
input_label = np.array([1]) # 1 = foreground, 0 = background
|
||
|
||
masks, scores, logits = predictor.predict(
|
||
point_coords=input_point,
|
||
point_labels=input_label,
|
||
multimask_output=True # Returns 3 mask options
|
||
)
|
||
|
||
# Select best mask
|
||
best_mask = masks[np.argmax(scores)]
|
||
```
|
||
|
||
### HuggingFace Transformers
|
||
|
||
```python
|
||
import torch
|
||
from PIL import Image
|
||
from transformers import SamModel, SamProcessor
|
||
|
||
# Load model and processor
|
||
model = SamModel.from_pretrained("facebook/sam-vit-huge")
|
||
processor = SamProcessor.from_pretrained("facebook/sam-vit-huge")
|
||
model.to("cuda")
|
||
|
||
# Process image with point prompt
|
||
image = Image.open("image.jpg")
|
||
input_points = [[[450, 600]]] # Batch of points
|
||
|
||
inputs = processor(image, input_points=input_points, return_tensors="pt")
|
||
inputs = {k: v.to("cuda") for k, v in inputs.items()}
|
||
|
||
# Generate masks
|
||
with torch.no_grad():
|
||
outputs = model(**inputs)
|
||
|
||
# Post-process masks to original size
|
||
masks = processor.image_processor.post_process_masks(
|
||
outputs.pred_masks.cpu(),
|
||
inputs["original_sizes"].cpu(),
|
||
inputs["reshaped_input_sizes"].cpu()
|
||
)
|
||
```
|
||
|
||
## Core concepts
|
||
|
||
### Model architecture
|
||
|
||
<!-- ascii-guard-ignore -->
|
||
<!-- ascii-guard-ignore -->
|
||
```
|
||
SAM Architecture:
|
||
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
|
||
│ Image Encoder │────▶│ Prompt Encoder │────▶│ Mask Decoder │
|
||
│ (ViT) │ │ (Points/Boxes) │ │ (Transformer) │
|
||
└─────────────────┘ └─────────────────┘ └─────────────────┘
|
||
│ │ │
|
||
Image Embeddings Prompt Embeddings Masks + IoU
|
||
(computed once) (per prompt) predictions
|
||
```
|
||
<!-- ascii-guard-ignore-end -->
|
||
<!-- ascii-guard-ignore-end -->
|
||
|
||
### Model variants
|
||
|
||
| Model | Checkpoint | Size | Speed | Accuracy |
|
||
|-------|------------|------|-------|----------|
|
||
| ViT-H | `vit_h` | 2.4 GB | Slowest | Best |
|
||
| ViT-L | `vit_l` | 1.2 GB | Medium | Good |
|
||
| ViT-B | `vit_b` | 375 MB | Fastest | Good |
|
||
|
||
### Prompt types
|
||
|
||
| Prompt | Description | Use Case |
|
||
|--------|-------------|----------|
|
||
| Point (foreground) | Click on object | Single object selection |
|
||
| Point (background) | Click outside object | Exclude regions |
|
||
| Bounding box | Rectangle around object | Larger objects |
|
||
| Previous mask | Low-res mask input | Iterative refinement |
|
||
|
||
## Interactive segmentation
|
||
|
||
### Point prompts
|
||
|
||
```python
|
||
# Single foreground point
|
||
input_point = np.array([[500, 375]])
|
||
input_label = np.array([1])
|
||
|
||
masks, scores, logits = predictor.predict(
|
||
point_coords=input_point,
|
||
point_labels=input_label,
|
||
multimask_output=True
|
||
)
|
||
|
||
# Multiple points (foreground + background)
|
||
input_points = np.array([[500, 375], [600, 400], [450, 300]])
|
||
input_labels = np.array([1, 1, 0]) # 2 foreground, 1 background
|
||
|
||
masks, scores, logits = predictor.predict(
|
||
point_coords=input_points,
|
||
point_labels=input_labels,
|
||
multimask_output=False # Single mask when prompts are clear
|
||
)
|
||
```
|
||
|
||
### Box prompts
|
||
|
||
```python
|
||
# Bounding box [x1, y1, x2, y2]
|
||
input_box = np.array([425, 600, 700, 875])
|
||
|
||
masks, scores, logits = predictor.predict(
|
||
box=input_box,
|
||
multimask_output=False
|
||
)
|
||
```
|
||
|
||
### Combined prompts
|
||
|
||
```python
|
||
# Box + points for precise control
|
||
masks, scores, logits = predictor.predict(
|
||
point_coords=np.array([[500, 375]]),
|
||
point_labels=np.array([1]),
|
||
box=np.array([400, 300, 700, 600]),
|
||
multimask_output=False
|
||
)
|
||
```
|
||
|
||
### Iterative refinement
|
||
|
||
```python
|
||
# Initial prediction
|
||
masks, scores, logits = predictor.predict(
|
||
point_coords=np.array([[500, 375]]),
|
||
point_labels=np.array([1]),
|
||
multimask_output=True
|
||
)
|
||
|
||
# Refine with additional point using previous mask
|
||
masks, scores, logits = predictor.predict(
|
||
point_coords=np.array([[500, 375], [550, 400]]),
|
||
point_labels=np.array([1, 0]), # Add background point
|
||
mask_input=logits[np.argmax(scores)][None, :, :], # Use best mask
|
||
multimask_output=False
|
||
)
|
||
```
|
||
|
||
## Automatic mask generation
|
||
|
||
### Basic automatic segmentation
|
||
|
||
```python
|
||
from segment_anything import SamAutomaticMaskGenerator
|
||
|
||
# Create generator
|
||
mask_generator = SamAutomaticMaskGenerator(sam)
|
||
|
||
# Generate all masks
|
||
masks = mask_generator.generate(image)
|
||
|
||
# Each mask contains:
|
||
# - segmentation: binary mask
|
||
# - bbox: [x, y, w, h]
|
||
# - area: pixel count
|
||
# - predicted_iou: quality score
|
||
# - stability_score: robustness score
|
||
# - point_coords: generating point
|
||
```
|
||
|
||
### Customized generation
|
||
|
||
```python
|
||
mask_generator = SamAutomaticMaskGenerator(
|
||
model=sam,
|
||
points_per_side=32, # Grid density (more = more masks)
|
||
pred_iou_thresh=0.88, # Quality threshold
|
||
stability_score_thresh=0.95, # Stability threshold
|
||
crop_n_layers=1, # Multi-scale crops
|
||
crop_n_points_downscale_factor=2,
|
||
min_mask_region_area=100, # Remove tiny masks
|
||
)
|
||
|
||
masks = mask_generator.generate(image)
|
||
```
|
||
|
||
### Filtering masks
|
||
|
||
```python
|
||
# Sort by area (largest first)
|
||
masks = sorted(masks, key=lambda x: x['area'], reverse=True)
|
||
|
||
# Filter by predicted IoU
|
||
high_quality = [m for m in masks if m['predicted_iou'] > 0.9]
|
||
|
||
# Filter by stability score
|
||
stable_masks = [m for m in masks if m['stability_score'] > 0.95]
|
||
```
|
||
|
||
## Batched inference
|
||
|
||
### Multiple images
|
||
|
||
```python
|
||
# Process multiple images efficiently
|
||
images = [cv2.imread(f"image_{i}.jpg") for i in range(10)]
|
||
|
||
all_masks = []
|
||
for image in images:
|
||
predictor.set_image(image)
|
||
masks, _, _ = predictor.predict(
|
||
point_coords=np.array([[500, 375]]),
|
||
point_labels=np.array([1]),
|
||
multimask_output=True
|
||
)
|
||
all_masks.append(masks)
|
||
```
|
||
|
||
### Multiple prompts per image
|
||
|
||
```python
|
||
# Process multiple prompts efficiently (one image encoding)
|
||
predictor.set_image(image)
|
||
|
||
# Batch of point prompts
|
||
points = [
|
||
np.array([[100, 100]]),
|
||
np.array([[200, 200]]),
|
||
np.array([[300, 300]])
|
||
]
|
||
|
||
all_masks = []
|
||
for point in points:
|
||
masks, scores, _ = predictor.predict(
|
||
point_coords=point,
|
||
point_labels=np.array([1]),
|
||
multimask_output=True
|
||
)
|
||
all_masks.append(masks[np.argmax(scores)])
|
||
```
|
||
|
||
## ONNX deployment
|
||
|
||
### Export model
|
||
|
||
```bash
|
||
python scripts/export_onnx_model.py \
|
||
--checkpoint sam_vit_h_4b8939.pth \
|
||
--model-type vit_h \
|
||
--output sam_onnx.onnx \
|
||
--return-single-mask
|
||
```
|
||
|
||
### Use ONNX model
|
||
|
||
```python
|
||
import onnxruntime
|
||
|
||
# Load ONNX model
|
||
ort_session = onnxruntime.InferenceSession("sam_onnx.onnx")
|
||
|
||
# Run inference (image embeddings computed separately)
|
||
masks = ort_session.run(
|
||
None,
|
||
{
|
||
"image_embeddings": image_embeddings,
|
||
"point_coords": point_coords,
|
||
"point_labels": point_labels,
|
||
"mask_input": np.zeros((1, 1, 256, 256), dtype=np.float32),
|
||
"has_mask_input": np.array([0], dtype=np.float32),
|
||
"orig_im_size": np.array([h, w], dtype=np.float32)
|
||
}
|
||
)
|
||
```
|
||
|
||
## Common workflows
|
||
|
||
### Workflow 1: Annotation tool
|
||
|
||
```python
|
||
import cv2
|
||
|
||
# Load model
|
||
predictor = SamPredictor(sam)
|
||
predictor.set_image(image)
|
||
|
||
def on_click(event, x, y, flags, param):
|
||
if event == cv2.EVENT_LBUTTONDOWN:
|
||
# Foreground point
|
||
masks, scores, _ = predictor.predict(
|
||
point_coords=np.array([[x, y]]),
|
||
point_labels=np.array([1]),
|
||
multimask_output=True
|
||
)
|
||
# Display best mask
|
||
display_mask(masks[np.argmax(scores)])
|
||
```
|
||
|
||
### Workflow 2: Object extraction
|
||
|
||
```python
|
||
def extract_object(image, point):
|
||
"""Extract object at point with transparent background."""
|
||
predictor.set_image(image)
|
||
|
||
masks, scores, _ = predictor.predict(
|
||
point_coords=np.array([point]),
|
||
point_labels=np.array([1]),
|
||
multimask_output=True
|
||
)
|
||
|
||
best_mask = masks[np.argmax(scores)]
|
||
|
||
# Create RGBA output
|
||
rgba = np.zeros((image.shape[0], image.shape[1], 4), dtype=np.uint8)
|
||
rgba[:, :, :3] = image
|
||
rgba[:, :, 3] = best_mask * 255
|
||
|
||
return rgba
|
||
```
|
||
|
||
### Workflow 3: Medical image segmentation
|
||
|
||
```python
|
||
# Process medical images (grayscale to RGB)
|
||
medical_image = cv2.imread("scan.png", cv2.IMREAD_GRAYSCALE)
|
||
rgb_image = cv2.cvtColor(medical_image, cv2.COLOR_GRAY2RGB)
|
||
|
||
predictor.set_image(rgb_image)
|
||
|
||
# Segment region of interest
|
||
masks, scores, _ = predictor.predict(
|
||
box=np.array([x1, y1, x2, y2]), # ROI bounding box
|
||
multimask_output=True
|
||
)
|
||
```
|
||
|
||
## Output format
|
||
|
||
### Mask data structure
|
||
|
||
```python
|
||
# SamAutomaticMaskGenerator output
|
||
{
|
||
"segmentation": np.ndarray, # H×W binary mask
|
||
"bbox": [x, y, w, h], # Bounding box
|
||
"area": int, # Pixel count
|
||
"predicted_iou": float, # 0-1 quality score
|
||
"stability_score": float, # 0-1 robustness score
|
||
"crop_box": [x, y, w, h], # Generation crop region
|
||
"point_coords": [[x, y]], # Input point
|
||
}
|
||
```
|
||
|
||
### COCO RLE format
|
||
|
||
```python
|
||
from pycocotools import mask as mask_utils
|
||
|
||
# Encode mask to RLE
|
||
rle = mask_utils.encode(np.asfortranarray(mask.astype(np.uint8)))
|
||
rle["counts"] = rle["counts"].decode("utf-8")
|
||
|
||
# Decode RLE to mask
|
||
decoded_mask = mask_utils.decode(rle)
|
||
```
|
||
|
||
## Performance optimization
|
||
|
||
### GPU memory
|
||
|
||
```python
|
||
# Use smaller model for limited VRAM
|
||
sam = sam_model_registry["vit_b"](https://github.com/NousResearch/hermes-agent/blob/main/skills/mlops/models/segment-anything/checkpoint="sam_vit_b_01ec64.pth")
|
||
|
||
# Process images in batches
|
||
# Clear CUDA cache between large batches
|
||
torch.cuda.empty_cache()
|
||
```
|
||
|
||
### Speed optimization
|
||
|
||
```python
|
||
# Use half precision
|
||
sam = sam.half()
|
||
|
||
# Reduce points for automatic generation
|
||
mask_generator = SamAutomaticMaskGenerator(
|
||
model=sam,
|
||
points_per_side=16, # Default is 32
|
||
)
|
||
|
||
# Use ONNX for deployment
|
||
# Export with --return-single-mask for faster inference
|
||
```
|
||
|
||
## Common issues
|
||
|
||
| Issue | Solution |
|
||
|-------|----------|
|
||
| Out of memory | Use ViT-B model, reduce image size |
|
||
| Slow inference | Use ViT-B, reduce points_per_side |
|
||
| Poor mask quality | Try different prompts, use box + points |
|
||
| Edge artifacts | Use stability_score filtering |
|
||
| Small objects missed | Increase points_per_side |
|
||
|
||
## References
|
||
|
||
- **[Advanced Usage](https://github.com/NousResearch/hermes-agent/blob/main/skills/mlops/models/segment-anything/references/advanced-usage.md)** - Batching, fine-tuning, integration
|
||
- **[Troubleshooting](https://github.com/NousResearch/hermes-agent/blob/main/skills/mlops/models/segment-anything/references/troubleshooting.md)** - Common issues and solutions
|
||
|
||
## Resources
|
||
|
||
- **GitHub**: https://github.com/facebookresearch/segment-anything
|
||
- **Paper**: https://arxiv.org/abs/2304.02643
|
||
- **Demo**: https://segment-anything.com
|
||
- **SAM 2 (Video)**: https://github.com/facebookresearch/segment-anything-2
|
||
- **HuggingFace**: https://huggingface.co/facebook/sam-vit-huge
|