Segment Anything knowledge graph

Explore Segment Anything as a knowledge graph: 905 files, symbols and docs including dependencies, devDependencies, scripts, compilerOptions — mapped with Lumvise.

What the graph contains

905 elements connected by 986 relationships.

  • 308 parameter
  • 143 markdown_section
  • 128 function
  • 110 field
  • 52 block
  • 49 file
  • 31 heading
  • 19 class

Reports and notes

Set the image once, reuse its embedding

Note

segment_anything/predictor.py

`set_torch_image` validates the BCHW input and resized long side, resets previous image state, stores original/input dimensions, preprocesses pixels, and runs the image encoder. The resulting features remain on the predictor for subsequent prompt queries. The higher-level `set_image` accepts HWC uint8 pixels, handles RGB/BGR ordering, resizes with `ResizeLongestSide`, and delegates here. Changing the image must replace the cached embedding; a prediction without a set image is rejected. Evidence: [segment_anything/predictor.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fpredictor.py%3Afile%3Apredictor.py%3A).

Prompt coordinates, mask scores, and refinement

Definition

segment_anything/predictor.py

`predict_torch` consumes batched prompts already transformed into the model's input frame. Points carry foreground/background labels; boxes use XYXY coordinates; a previous low-resolution mask can guide refinement. It encodes prompts, combines them with cached image features in the mask decoder, restores masks to original image dimensions, and optionally thresholds logits. Results include masks, predicted quality scores, and low-resolution logits. With ambiguous prompts, multimask output provides alternatives; scores help the caller choose a mask. The NumPy-facing `predict` performs coordinate conversion before delegating to this method. Evidence: [segment_anything/predictor.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fpredictor.py%3Afile%3Apredictor.py%3A).

Sam owns three learned components

Definition

segment_anything/modeling/sam.py

`Sam` composes `ImageEncoderViT`, `PromptEncoder`, and `MaskDecoder`. Its batched forward method preprocesses images, creates image embeddings, encodes each image's prompts, decodes masks, and postprocesses them to their original size. It stores pixel mean/std as buffers and uses an RGB image convention. Output records include binary masks, mask-quality predictions, and low-resolution logits. Direct batched forward is useful when prompts are known in advance; the predictor supports repeated prompt interactions with cached image features. Evidence: [segment_anything/modeling/sam.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fmodeling%2Fsam.py%3Afile%3Asam.py%3A).

Automatic masks: sample, filter, deduplicate

Summary

segment_anything/automatic_mask_generator.py

`SamAutomaticMaskGenerator` uses a `SamPredictor` plus point grids to generate masks without a user supplying every prompt. Options control sampling density, batches, optional crop layers, predicted-IoU and stability thresholds, and non-maximum suppression. `generate` can remove small disconnected regions/holes and returns records with segmentation, area, XYWH box, predicted IoU, sample points, stability score, and crop box. Output can be binary masks or run-length encodings. More samples and crop layers increase inference work; the code remains separate from the learned model. Evidence: [segment_anything/automatic_mask_generator.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fautomatic_mask_generator.py%3Afile%3Aautomatic_mask_generator.py%3A).

Interactive segmentation walkthrough

Guide

notebooks/predictor_example.ipynb

Follow the predictor example alongside `SamPredictor`: load a model/checkpoint, prepare an image, call `set_image`, supply point or box prompts to `predict`, and inspect the returned masks and scores. Feed selected low-resolution logits into a later query to refine an ambiguous result. Keep coordinate spaces explicit: the high-level predictor accepts prompts in the original image frame and transforms them; `predict_torch` expects already transformed inputs. The notebook was indexed through converted text. No checkpoint-dependent notebook or image segmentation was executed during export. Evidence: [notebooks/predictor_example.ipynb](lumvise://element/filesystem%3A5149158d1c46f576%3Anotebooks%2Fpredictor_example.ipynb%3Afile%3Apredictor_example.ipynb%3A), [segment_anything/predictor.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fpredictor.py%3Afile%3Apredictor.py%3A).

Start here: segment-anything knowledge graph

Guide

README.md

# segment-anything source tour This demo combines the complete published semantic index with selected explanations attached to real files, folders, classes, and functions. Start with architecture, then follow the core concepts: 1. [Segment Anything architecture: reusable image features and prompts](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Aarchitecture) 2. [Set the image once, reuse its embedding](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Acache) 3. [Prompt coordinates, mask scores, and refinement](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Aprompts) 4. [Automatic masks: sample, filter, deduplicate](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Aautomatic) 5. [Sam owns three learned components](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Amodel) 6. [Interactive segmentation walkthrough](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Awalkthrough) 7. [Source snapshot, index coverage, and validation scope](lumvise://artifact/popular-demo-20260928%3Asegment-anything%3Aprovenance) Select an artifact to inspect its owning semantic element. Evidence links point to indexed source. The source snapshot and coverage report records the exact scope and parser limitations.

Segment Anything architecture: reusable image features and prompts

Report

segment_anything

# Segment Anything architecture SAM predicts object masks from an image and prompts. The `Sam` model combines an image encoder, prompt encoder, and mask decoder. `SamPredictor` provides an interactive API that caches image features; `SamAutomaticMaskGenerator` samples prompts across an image and filters the resulting masks. ```text image → image encoder → cached image features points / boxes / prior mask → prompt encoder both → mask decoder → full-size masks + scores ``` The model builder assembles the encoder/decoder configuration and optionally loads a checkpoint. The predictor owns image-specific state; the model owns learned components. This separation allows many prompt queries against one expensive image embedding. Checkpoint weights are separate from this source/index export. Evidence: [segment_anything/modeling/sam.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fmodeling%2Fsam.py%3Afile%3Asam.py%3A), [segment_anything/predictor.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fpredictor.py%3Afile%3Apredictor.py%3A), [segment_anything/automatic_mask_generator.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fautomatic_mask_generator.py%3Afile%3Aautomatic_mask_generator.py%3A), [segment_anything/build_sam.py](lumvise://element/filesystem%3A5149158d1c46f576%3Asegment_anything%2Fbuild_sam.py%3Afile%3Abuild_sam.py%3A).

Source snapshot, index coverage, and validation scope

Report

README.md

# Export provenance Upstream: [facebookresearch/segment-anything](https://github.com/facebookresearch/segment-anything). This graph was generated on 2026-09-28 from the existing local source folder. The folder has no Git metadata, so an exact upstream commit is unknown; no branch or commit is guessed. It was not updated from upstream during export. Source snapshot fingerprint: `65fc31ee018f7731d4d005d59a904691232ba789ddf4e5e1fa2b4cc4df4ddb96` (SHA-256 over sorted relative paths, NUL separators, and raw file SHA-256 digests; excludes Git/runtime/generated cache directories and symlinks). Regular source files: 59. Indexed semantic elements: 905. File/text parser records: 49 (6 plain_text, 42 parsed, 1 unsupported); images have separate semantic kinds. Coverage details: - `demo/src/assets/index.html`: unsupported; project scan `demo/src/assets/index.html` failed: expected non-empty converted Markdown. Static extraction is best effort. Unresolved dynamic calls are not evidence that dependencies are absent. Knowledge explanations were checked against selected local source; upstream test suites, notebooks, model inference, and model downloads were not run. The task validates index/export contents and readability.

Documentation topics

  • Contributing to segment-anything — CONTRIBUTING.md
  • Contributor License Agreement ("CLA") — CONTRIBUTING.md
  • Issues — CONTRIBUTING.md
  • License — CONTRIBUTING.md
  • Pull Requests — CONTRIBUTING.md
  • <a name="GettingStarted"></a>Getting Started — README.md
  • <a name="Models"></a>Model Checkpoints — README.md
  • Citing Segment Anything — README.md
  • Contributing — README.md
  • Contributors — README.md
  • Dataset — README.md
  • Installation — README.md
  • Latest updates -- SAM 2: Segment Anything in Images and Videos — README.md
  • License — README.md
  • ONNX Export — README.md
  • Segment Anything — README.md
  • Web demo — README.md
  • Export the ONNX model — demo/README.md
  • Export the image embedding — demo/README.md
  • ONNX multithreading with SharedArrayBuffer — demo/README.md
  • Run the app — demo/README.md
  • Segment Anything Simple Web demo — demo/README.md
  • Structure of the app — demo/README.md
  • Update the image, embedding, model in the app — demo/README.md

Types and modules

  • dependencies — demo/package.json
  • devDependencies — demo/package.json
  • scripts — demo/package.json
  • compilerOptions — demo/tsconfig.json
  • exclude — demo/tsconfig.json
  • include — demo/tsconfig.json
  • SamAutomaticMaskGenerator — segment_anything/automatic_mask_generator.py
  • LayerNorm2d — segment_anything/modeling/common.py
  • MLPBlock — segment_anything/modeling/common.py
  • Attention — segment_anything/modeling/image_encoder.py
  • Block — segment_anything/modeling/image_encoder.py
  • ImageEncoderViT — segment_anything/modeling/image_encoder.py
  • PatchEmbed — segment_anything/modeling/image_encoder.py
  • MLP — segment_anything/modeling/mask_decoder.py
  • MaskDecoder — segment_anything/modeling/mask_decoder.py
  • PositionEmbeddingRandom — segment_anything/modeling/prompt_encoder.py
  • PromptEncoder — segment_anything/modeling/prompt_encoder.py
  • Sam — segment_anything/modeling/sam.py
  • Attention — segment_anything/modeling/transformer.py
  • TwoWayAttentionBlock — segment_anything/modeling/transformer.py
  • TwoWayTransformer — segment_anything/modeling/transformer.py
  • SamPredictor — segment_anything/predictor.py
  • MaskData — segment_anything/utils/amg.py
  • SamOnnxModel — segment_anything/utils/onnx.py
  • ResizeLongestSide — segment_anything/utils/transforms.py

Functions

  • App — demo/src/App.tsx
  • initModel — demo/src/App.tsx
  • loadImage — demo/src/App.tsx
  • loadNpyTensor — demo/src/App.tsx
  • runONNX — demo/src/App.tsx
  • Stage — demo/src/components/Stage.tsx
  • getClick — demo/src/components/Stage.tsx
  • Tool — demo/src/components/Tool.tsx
  • fitToPage — demo/src/components/Tool.tsx
  • arrayToImageData — demo/src/components/helpers/maskUtils.tsx
  • imageDataToCanvas — demo/src/components/helpers/maskUtils.tsx
  • imageDataToImage — demo/src/components/helpers/maskUtils.tsx
  • onnxMaskToImage — demo/src/components/helpers/maskUtils.tsx
  • modelData — demo/src/components/helpers/onnxModelAPI.tsx
  • handleImageScale — demo/src/components/helpers/scaleHelper.tsx
  • AppContextProvider — demo/src/components/hooks/context.tsx
  • get_amg_kwargs — scripts/amg.py
  • main — scripts/amg.py
  • write_masks_to_folder — scripts/amg.py
  • run_export — scripts/export_onnx_model.py
  • to_numpy — scripts/export_onnx_model.py
  • generate — segment_anything/automatic_mask_generator.py
  • postprocess_small_regions — segment_anything/automatic_mask_generator.py
  • build_sam_vit_b — segment_anything/build_sam.py
  • build_sam_vit_h — segment_anything/build_sam.py
  • build_sam_vit_l — segment_anything/build_sam.py
  • forward — segment_anything/modeling/common.py
  • add_decomposed_rel_pos — segment_anything/modeling/image_encoder.py
  • forward — segment_anything/modeling/image_encoder.py
  • get_rel_pos — segment_anything/modeling/image_encoder.py
  • window_partition — segment_anything/modeling/image_encoder.py
  • window_unpartition — segment_anything/modeling/image_encoder.py
  • forward — segment_anything/modeling/mask_decoder.py
  • predict_masks — segment_anything/modeling/mask_decoder.py
  • forward — segment_anything/modeling/prompt_encoder.py
  • forward_with_coords — segment_anything/modeling/prompt_encoder.py
  • get_dense_pe — segment_anything/modeling/prompt_encoder.py
  • device — segment_anything/modeling/sam.py
  • forward — segment_anything/modeling/sam.py
  • postprocess_masks — segment_anything/modeling/sam.py

How things connect

  • App.tsx uses_type modelScaleProps
  • App.tsx uses_type onnxMaskToImage
  • runONNX calls onnxMaskToImage
  • Stage.tsx uses_type modelInputProps
  • Tool.tsx uses_type ToolProps
  • imageDataToImage calls imageDataToCanvas
  • onnxMaskToImage calls imageDataToImage
  • onnxMaskToImage calls arrayToImageData
  • onnxModelAPI.tsx uses_type modeDataProps
  • context.tsx uses_type modelInputProps
  • createContext.tsx uses_type modelInputProps
  • amg.py calls main
  • main instantiates SamAutomaticMaskGenerator
  • main calls get_amg_kwargs
  • main calls write_masks_to_folder
  • export_onnx_model.py calls run_export
  • run_export instantiates SamOnnxModel
  • run_export calls to_numpy
  • __init__ instantiates SamPredictor
  • __init__ calls build_all_layer_point_grids
  • _generate_masks calls _process_crop
  • _generate_masks calls generate_crop_boxes
  • _generate_masks instantiates MaskData
  • _process_batch instantiates MaskData

Folders

  • demo
  • assets
  • scripts
  • demo/src
  • notebooks
  • demo/configs
  • demo/src/assets
  • notebooks/images
  • segment_anything
  • demo/src/components
  • demo/configs/webpack
  • demo/src/assets/data
  • demo/src/assets/scss
  • segment_anything/utils
  • demo/src/components/hooks
  • segment_anything/modeling

Files

  • .flake8
  • setup.py
  • README.md
  • linter.sh
  • setup.cfg
  • .gitignore
  • demo/README.md
  • scripts/amg.py
  • CONTRIBUTING.md
  • demo/src/App.tsx
  • demo/package.json
  • demo/src/index.tsx
  • demo/tsconfig.json
  • demo/postcss.config.js
  • demo/tailwind.config.js
  • demo/src/assets/index.html
  • demo/configs/webpack/dev.js
  • demo/configs/webpack/prod.js
  • demo/src/components/Tool.tsx
  • scripts/export_onnx_model.py
  • segment_anything/__init__.py
  • demo/src/assets/scss/App.scss
  • demo/src/components/Stage.tsx
  • segment_anything/build_sam.py
  • segment_anything/predictor.py
  • segment_anything/utils/amg.py
  • demo/configs/webpack/common.js
  • segment_anything/utils/onnx.py
  • segment_anything/modeling/sam.py
  • notebooks/predictor_example.ipynb
  • notebooks/onnx_model_example.ipynb
  • segment_anything/utils/__init__.py
  • segment_anything/modeling/common.py
  • segment_anything/utils/transforms.py
  • demo/src/components/hooks/context.tsx
  • segment_anything/modeling/__init__.py