# yformer/efficientsam architecture

> Architecture diagram of the GitHub repository yformer/efficientsam, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/yformer/efficientsam
- Repository: https://github.com/yformer/efficientsam

## Overview

EfficientSAM is a PyTorch promptable image-segmentation model. The primary workflow builds a checkpoint-backed model, accepts an image and point prompts, encodes the image and prompts, then predicts and resizes candidate masks with quality scores. The README also highlights point/box prompting, segment-everything examples, saliency examples, and ONNX inference with separate encoder and decoder models. The graph separates model internals from the ONNX path; unsampled implementation details and the exact wiring of the additional example modes are not inferred.

Main entry points: [`efficient_sam/efficient_sam.py`](https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam.py) (EfficientSAM model), [`EfficientSAM_onnx_example.py`](https://github.com/yformer/efficientsam/blob/main/EfficientSAM_onnx_example.py) (ONNX example), [`efficient_sam/build_efficient_sam.py`](https://github.com/yformer/efficientsam/blob/main/efficient_sam/build_efficient_sam.py) (Checkpoint builder), [`efficient_sam/efficient_sam_decoder.py`](https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_decoder.py) (Mask decoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Model runtime"]
  node_example["PyTorch example"]
  node_builder["Checkpoint builder"]
  node_weights[("Model checkpoints")]
  node_model["EfficientSAM model<br/>[efficient_sam.py]"]
end

subgraph group_architecture["Model components"]
  node_encoder["Image encoder"]
  node_prompt["Prompt encoder"]
  node_maskdecoder["Mask decoder"]
  node_transformer["Two-way transformer"]
  node_mlp["MLP blocks<br/>[mlp.py]"]
end

subgraph group_onnx["ONNX inference"]
  node_onnxexample["ONNX example"]
  node_onnxmodels["ONNX model wrappers<br/>[onnx_models.py]"]
  node_onnxencoder["ONNX encoder<br/>[onnx_models.py]"]
  node_onnxdecoder["ONNX decoder<br/>[onnx_models.py]"]
  node_onnxruntime["ONNX Runtime"]
end

node_user(("User"))

node_user -->|"runs example"| node_example
node_example -->|"builds model"| node_builder
node_builder -->|"loads checkpoint"| node_weights
node_builder -->|"constructs"| node_model
node_model -->|"encodes images"| node_encoder
node_model -->|"encodes prompts"| node_prompt
node_model -->|"predicts masks"| node_maskdecoder
node_maskdecoder -->|"attends with"| node_transformer
node_maskdecoder -->|"uses heads"| node_mlp
node_transformer -->|"uses blocks"| node_mlp
node_user -->|"runs example"| node_onnxexample
node_onnxexample -->|"runs inference"| node_onnxruntime
node_onnxexample -->|"runs image encoding"| node_onnxencoder
node_onnxexample -->|"runs prompt decoding"| node_onnxdecoder
node_onnxencoder -->|"wraps encoder"| node_onnxmodels
node_onnxdecoder -->|"wraps decoder"| node_onnxmodels
node_onnxmodels -->|"delegates to"| node_model

click node_example "https://github.com/yformer/efficientsam/blob/main/EfficientSAM_example.py"
click node_builder "https://github.com/yformer/efficientsam/blob/main/efficient_sam/build_efficient_sam.py"
click node_weights "https://github.com/yformer/efficientsam/tree/main/weights"
click node_model "https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam.py"
click node_encoder "https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_encoder.py"
click node_prompt "https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_decoder.py"
click node_maskdecoder "https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_decoder.py"
click node_transformer "https://github.com/yformer/efficientsam/blob/main/efficient_sam/two_way_transformer.py"
click node_mlp "https://github.com/yformer/efficientsam/blob/main/efficient_sam/mlp.py"
click node_onnxexample "https://github.com/yformer/efficientsam/blob/main/EfficientSAM_onnx_example.py"
click node_onnxmodels "https://github.com/yformer/efficientsam/blob/main/onnx_models.py"
click node_onnxencoder "https://github.com/yformer/efficientsam/blob/main/onnx_models.py"
click node_onnxdecoder "https://github.com/yformer/efficientsam/blob/main/onnx_models.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_example,node_builder,node_weights,node_model,node_user toneBlue
class node_encoder,node_prompt,node_maskdecoder,node_transformer,node_mlp toneAmber
class node_onnxexample,node_onnxmodels,node_onnxencoder,node_onnxdecoder,node_onnxruntime toneMint
```

## Components

### Model runtime

- [PyTorch example](https://github.com/yformer/efficientsam/blob/main/EfficientSAM_example.py): `EfficientSAM_example.py`
- [Checkpoint builder](https://github.com/yformer/efficientsam/blob/main/efficient_sam/build_efficient_sam.py): `efficient_sam/build_efficient_sam.py`
- [Model checkpoints](https://github.com/yformer/efficientsam/tree/main/weights): `weights`
- [EfficientSAM model](https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam.py): `efficient_sam/efficient_sam.py`

### Model components

- [Image encoder](https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_encoder.py): `efficient_sam/efficient_sam_encoder.py`
- [Prompt encoder](https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_decoder.py): `efficient_sam/efficient_sam_decoder.py`
- [Mask decoder](https://github.com/yformer/efficientsam/blob/main/efficient_sam/efficient_sam_decoder.py): `efficient_sam/efficient_sam_decoder.py`
- [Two-way transformer](https://github.com/yformer/efficientsam/blob/main/efficient_sam/two_way_transformer.py): `efficient_sam/two_way_transformer.py`
- [MLP blocks](https://github.com/yformer/efficientsam/blob/main/efficient_sam/mlp.py): `efficient_sam/mlp.py`

### ONNX inference

- [ONNX example](https://github.com/yformer/efficientsam/blob/main/EfficientSAM_onnx_example.py): `EfficientSAM_onnx_example.py`
- [ONNX model wrappers](https://github.com/yformer/efficientsam/blob/main/onnx_models.py): `onnx_models.py`
- [ONNX encoder](https://github.com/yformer/efficientsam/blob/main/onnx_models.py): `onnx_models.py`
- [ONNX decoder](https://github.com/yformer/efficientsam/blob/main/onnx_models.py): `onnx_models.py`
- **ONNX Runtime**

### Other components

- **User**

## Connections

- User → PyTorch example: runs example (evidence: `EfficientSAM_example.py`)
- PyTorch example → Checkpoint builder: builds model (evidence: `EfficientSAM_example.py`)
- Checkpoint builder → Model checkpoints: loads checkpoint (evidence: `efficient_sam/build_efficient_sam.py`)
- Checkpoint builder → EfficientSAM model: constructs (evidence: `efficient_sam/build_efficient_sam.py`)
- EfficientSAM model → Image encoder: encodes images (evidence: `efficient_sam/efficient_sam.py`)
- EfficientSAM model → Prompt encoder: encodes prompts (evidence: `efficient_sam/efficient_sam.py`)
- EfficientSAM model → Mask decoder: predicts masks (evidence: `efficient_sam/efficient_sam.py`)
- Mask decoder → Two-way transformer: attends with (evidence: `efficient_sam/efficient_sam.py`)
- Mask decoder → MLP blocks: uses heads (evidence: `efficient_sam/efficient_sam_decoder.py`)
- Two-way transformer → MLP blocks: uses blocks (evidence: `efficient_sam/two_way_transformer.py`)
- User → ONNX example: runs example (evidence: `EfficientSAM_onnx_example.py`)
- ONNX example → ONNX Runtime: runs inference (evidence: `EfficientSAM_onnx_example.py`)
- ONNX example → ONNX encoder: runs image encoding (evidence: `EfficientSAM_onnx_example.py`)
- ONNX example → ONNX decoder: runs prompt decoding (evidence: `EfficientSAM_onnx_example.py`)
- ONNX encoder → ONNX model wrappers: wraps encoder (evidence: `onnx_models.py`)
- ONNX decoder → ONNX model wrappers: wraps decoder (evidence: `onnx_models.py`)
- ONNX model wrappers → EfficientSAM model: delegates to (evidence: `onnx_models.py`)
