Exporting Segment Anything models different formats
Project description
SAM Exporter
Exporting Segment Anything models to different formats.
The Segment Anything repository does not have a way to export encoder to ONNX format. There are some pull requests for this feature, but they have not accepted by SAM authors. Therefore, I want to create an easy tool to export Segment Anything models to different output formats as an easy option.
Supported models:
- SAM ViT-B
- SAM ViT-L
- SAM ViT-H
- MobileSAM*
Installation
From PyPi:
pip install samexporter
From source:
git clone https://github.com/vietanhdev/samexporter
cd samexporter
pip install -e .
Usage
- Download all models from Segment Anything repository (*.pth).
- Download MobileSAM from https://github.com/ChaoningZhang/MobileSAM.
original_models
+ sam_vit_b_01ec64.pth
+ sam_vit_h_4b8939.pth
+ sam_vit_l_0b3195.pth
+ mobile_sam.pt
...
- Convert encoder SAM-H to ONNX format:
python -m samexporter.export_encoder --checkpoint original_models/sam_vit_h_4b8939.pth \
--output output_models/sam_vit_h_4b8939.encoder.onnx \
--model-type vit_h \
--quantize-out output_models/sam_vit_h_4b8939.encoder.quant.onnx \
--use-preprocess
- Convert decoder SAM-H to ONNX format:
python -m samexporter.export_decoder --checkpoint original_models/sam_vit_h_4b8939.pth \
--output output_models/sam_vit_h_4b8939.decoder.onnx \
--model-type vit_h \
--quantize-out output_models/sam_vit_h_4b8939.decoder.quant.onnx \
--return-single-mask
Remove --return-single-mask
if you want to return multiple masks.
- Inference using the exported ONNX model:
python -m samexporter.inference \
--encoder_model output_models/sam_vit_h_4b8939.encoder.onnx \
--decoder_model output_models/sam_vit_h_4b8939.decoder.onnx \
--image images/truck.jpg \
--prompt images/truck_prompt.json \
--output output_images/truck.png \
--show
python -m samexporter.inference \
--encoder_model output_models/sam_vit_h_4b8939.encoder.onnx \
--decoder_model output_models/sam_vit_h_4b8939.decoder.onnx \
--image images/plants.png \
--prompt images/plants_prompt1.json \
--output output_images/plants_01.png \
--show
python -m samexporter.inference \
--encoder_model output_models/sam_vit_h_4b8939.encoder.onnx \
--decoder_model output_models/sam_vit_h_4b8939.decoder.onnx \
--image images/plants.png \
--prompt images/plants_prompt2.json \
--output output_images/plants_02.png \
--show
Short options:
- Convert all Meta's models to ONNX format:
bash convert_all_meta_sam.sh
- Convert MobileSAM to ONNX format:
bash convert_mobile_sam.sh
Tips
- Use "quantized" models for faster inference and smaller model size. However, the accuracy may be lower than the original models.
- SAM-B is the most lightweight model, but it has the lowest accuracy. SAM-H is the most accurate model, but it has the largest model size. SAM-M is a good trade-off between accuracy and model size.
AnyLabeling
This package was originally developed for auto labeling feature in AnyLabeling project. However, you can use it for other purposes.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file samexporter-0.2.0.tar.gz
.
File metadata
- Download URL: samexporter-0.2.0.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4c66a19f7598ca226bfedac94594080c5fde3dbef4919f9911def5dc98b81eb |
|
MD5 | b82aada77b7d28d05227a11f7a1e9e66 |
|
BLAKE2b-256 | b17abdfa5aecad6523714951b0f1726d060b44df8b07bafc440d1ad003d99098 |
File details
Details for the file samexporter-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: samexporter-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.8.16
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e19fa75735fcb2dcfde9aa1dc1fa4f934b81a8ded0c72510646b81ad195c1a12 |
|
MD5 | 26404f7ffe7d823742437ae06bd3cd8c |
|
BLAKE2b-256 | bacc925faa363b20a2101d2950119b414bc5255f5b573a9ceb0d7aa4d676f2ec |