SAM ONNX models
Collection
Models for NimbusImage
•
1 item
•
Updated
The segment anything tool is uses the Segment Anything Model (SAM). This folder contains the ONNX files that represent the SAM and are used by the browser to compute segmentations.
The SAM is provided by Facebook in the form of a pytorch model: a .pth
file.
There are three types of model. They are, from the biggest to the smallest, vit_h
, vit_l
and vit_b
.
It is advised to use vit_b
as it is smaller and faster, at the cost of a lower quality segmentation.
Facebook provides a script to convert the decoder to ONNX but not the encoder. The maintainers refuse to merge PRs adding this feature. You can use the script given in one of these PRs, but the chosen solution is to use the samexporter.
checkpoint.pth
model downloaded in the previous step.mkdir temp
cd temp
git clone git@github.com:vietanhdev/samexporter.git
git clone git@github.com:facebookresearch/segment-anything.git
pip install -e ./segment-anything
pip install torchvision==0.16.1 onnx==1.15.0 onnxruntime==1.15.1 timm==0.9.12
cd samexporter
python -m samexporter.export_encoder --checkpoint ../checkpoint.pth --output ../encoder.onnx --model-type vit_b
python -m samexporter.export_decoder --checkpoint ../checkpoint.pth --output ../decoder.onnx --model-type vit_b --return-single-mask
vit_b
)cd $NIMUS_IMAGE_DIR
cp encoder.onnx public/onnx-models/sam/$MODEL_NAME
cp decoder.onnx public/onnx-models/sam/$MODEL_NAME