Skip to main content

Fast Segment Anything

image

[📕Paper] [🤗HuggingFace Demo] [Colab demo] [Replicate demo & API] [Model Zoo] [BibTeX]

FastSAM Speed

The Fast Segment Anything Model(FastSAM) is a CNN Segment Anything Model trained using only 2% of the SA-1B dataset published by SAM authors. FastSAM achieves comparable performance with the SAM method at 50× higher run-time speed.

FastSAM design

🍇 Updates

Installation

Clone the repository locally:

pip install segment-anything-fast

Geospatial integration (AGPL)

Starting with 0.1.3, the geospatial wrapper formerly shipped as samgeo.fast_sam is included here as samgeo_fastsam. It retains the existing FastSAM methods and model selection. Python 3.10 or later is required for the optional geospatial dependencies.

pip install "segment-anything-fast[geo]>=0.1.3"
from samgeo_fastsam import SamGeo

sam = SamGeo(model="FastSAM-x.pt")
sam.set_image("image.tif", device="cpu")
sam.everything_prompt(output="masks.tif")
# Other existing methods: point_prompt, box_prompt, text_prompt,
# save_masks, raster_to_vector and show_anns.

Replace from samgeo.fast_sam import SamGeo in existing notebooks with the new import above. SamGeo's [fast] extra and old import are being removed; [all] and [extra] will no longer install FastSAM. There is no forwarding module in SamGeo. Serialized objects that refer to the old module path need separate migration. The agent CLI's generic generate/predict interface is not supported by this wrapper; use the Python API.

The geo extra supports existing SamGeo utilities, including older versions. After the corrected SamGeo release is available, upgrade segment-geospatial as well to remove the old adapted wrapper from an existing environment.

The two checkpoint download URLs and segmentation implementation are unchanged. No checkpoint binaries are bundled. Retaining a download link does not resolve the conflicting historical model-license record; see NOTICE.

Getting Started

First download a model checkpoint.

Then, you can run the scripts to try the everything mode and three prompt modes.

# Everything mode
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg
# Text prompt
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg  --text_prompt "the yellow dog"
# Box prompt (xywh)
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg --box_prompt "[[570,200,230,400]]"
# Points prompt
python Inference.py --model_path ./weights/FastSAM.pt --img_path ./images/dogs.jpg  --point_prompt "[[520,360],[620,300]]" --point_label "[1,0]"

You can use the following code to generate all masks, make mask selection based on prompts, and visualize the results.

from fastsam import FastSAM, FastSAMPrompt

model = FastSAM('./weights/FastSAM.pt')
IMAGE_PATH = './images/dogs.jpg'
DEVICE = 'cpu'
everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=True, imgsz=1024, conf=0.4, iou=0.9,)
prompt_process = FastSAMPrompt(IMAGE_PATH, everything_results, device=DEVICE)

# everything prompt
ann = prompt_process.everything_prompt()

# bbox default shape [0,0,0,0] -> [x1,y1,x2,y2]
ann = prompt_process.box_prompt(bbox=[[200, 200, 300, 300]])

# text prompt
ann = prompt_process.text_prompt(text='a photo of a dog')

# point prompt
# points default [[0,0]] [[x1,y1],[x2,y2]]
# point_label default [0] [1,0] 0:background, 1:foreground
ann = prompt_process.point_prompt(points=[[620, 360]], pointlabel=[1])

prompt_process.plot(annotations=ann,output_path='./output/dog.jpg',)

You are also welcomed to try our Colab demo: FastSAM_example.ipynb.

Different Inference Options

We provide various options for different purposes, details are in MORE_USAGES.md.

Web demo

Gradio demo

  • We also provide a UI for testing our method that is built with gradio. You can upload a custom image, select the mode and set the parameters, click the segment button, and get a satisfactory segmentation result. Currently, the UI supports interaction with the 'Everything mode' and 'points mode'. We plan to add support for additional modes in the future. Running the following command in a terminal will launch the demo:
# Download the pre-trained model in "./weights/FastSAM.pt"
python app_gradio.py

HF_Everyhting HF_Points

Replicate demo

  • Replicate demo has supported all modes, you can experience points/box/text mode.

Replicate-1 Replicate-2 Replicate-3

Model Checkpoints

Two model versions of the model are available with different sizes. Click the links below to download the checkpoint for the corresponding model type.

Results

All result were tested on a single NVIDIA GeForce RTX 3090.

1. Inference time

Running Speed under Different Point Prompt Numbers(ms).

method params 1 10 100 E(16x16) E(32x32*) E(64x64)
SAM-H 0.6G 446 464 627 852 2099 6972
SAM-B 136M 110 125 230 432 1383 5417
FastSAM 68M 40 40 40 40 40 40

2. Memory usage

Dataset Method GPU Memory (MB)
COCO 2017 FastSAM 2608
COCO 2017 SAM-H 7060
COCO 2017 SAM-B 4670

3. Zero-shot Transfer Experiments

Edge Detection

Test on the BSDB500 dataset.

method year ODS OIS AP R50
HED 2015 .788 .808 .840 .923
SAM 2023 .768 .786 .794 .928
FastSAM 2023 .750 .790 .793 .903

Object Proposals

COCO
method AR10 AR100 AR1000 AUC
SAM-H E64 15.5 45.6 67.7 32.1
SAM-H E32 18.5 49.5 62.5 33.7
SAM-B E32 11.4 39.6 59.1 27.3
FastSAM 15.7 47.3 63.7 32.2
LVIS

bbox AR@1000

method all small med. large
ViTDet-H 65.0 53.2 83.3 91.2
zero-shot transfer methods
SAM-H E64 52.1 36.6 75.1 88.2
SAM-H E32 50.3 33.1 76.2 89.8
SAM-B E32 45.0 29.3 68.7 80.6
FastSAM 57.1 44.3 77.1 85.3

Instance Segmentation On COCO 2017

method AP APS APM APL
ViTDet-H .510 .320 .543 .689
SAM .465 .308 .510 .617
FastSAM .379 .239 .434 .500

4. Performance Visualization

Several segmentation results:

Natural Images

Natural Images

Text to Mask

Text to Mask

5.Downstream tasks

The results of several downstream tasks to show the effectiveness.

Anomaly Detection

Anomaly Detection

Salient Object Detection

Salient Object Detection

Building Extracting

Building Detection

License

This distribution contains AGPL-covered code; it is not Apache-only. See the GNU AGPL v3, retained Apache-2.0 and SamGeo MIT texts, and NOTICE for provenance. Original permissive notices remain applicable to their respective contributions. Model checkpoints are separate downloads. Historical Apache model statements conflict with the original project's AGPL correction; this code packaging change provides no new grant over model weights. Review the checkpoint publisher's terms before redistribution or deployment.

Acknowledgement

Contributors

Our project wouldn't be possible without the contributions of these amazing people! Thank you all for making this project better.

Citing FastSAM

If you find this project useful for your research, please consider citing the following BibTeX entry.

@misc{zhao2023fast,
      title={Fast Segment Anything},
      author={Xu Zhao and Wenchao Ding and Yongqi An and Yinglong Du and Tao Yu and Min Li and Ming Tang and Jinqiao Wang},
      year={2023},
      eprint={2306.12156},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

Star History Chart

Metadata

Release files for segment-anything-fast 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for segment-anything-fast 0.1.3
File Size Uploaded Provenance Actions
segment_anything_fast-0.1.3.tar.gz 36.2 kB Without provenance Details for segment_anything_fast-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for segment-anything-fast 0.1.3
File Interpreter ABI Platform Actions
segment_anything_fast-0.1.3-py3-none-any.whl Python 3 none any Details for segment_anything_fast-0.1.3-py3-none-any.whl

Total release size: 74.8 kB

Release files / segment_anything_fast-0.1.3.tar.gz

Download URL segment_anything_fast-0.1.3.tar.gz
Size 36.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f6fbba44e02030a099aeecc40ed0f82f30acefef0891aac37c5aeb024def1d98
BLAKE2b-256 checksum
How to use checksums
0acdadef364e04f3c9e2c91e4a7ee9c4d4be6f401be70da88db13202cdb309d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / segment_anything_fast-0.1.3-py3-none-any.whl

Download URL segment_anything_fast-0.1.3-py3-none-any.whl
Size 38.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ccf4ef39b4d9b9d8e6e1620ff037563e14991aaed6c9c064dfad36c7ed7abde0
BLAKE2b-256 checksum
How to use checksums
10acd2c06a2e89ce9a8b8334439fde0f2f72b9b438ed2883da5dd60ecbe30c7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for segment-anything-fast 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page