• Stars
    star
    172
  • Rank 214,610 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created about 1 year ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Export Segment Anything Models to ONNX

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

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

truck

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

plants_01

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

plants_02

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.

More Repositories

1

anylabeling

Effortless AI-assisted data labeling with AI support from YOLO, Segment Anything, MobileSAM!!
Python
1,870
star
2

open-adas

An open source advanced driver assistance system (ADAS) that uses Jetson Nano as the hardware. Features: Traffic sign detection, Forward collision warning, Lane departure warning.
C++
391
star
3

tf-blazepose

BlazePose - Super fast human pose detection on Tensorflow 2.x
Python
189
star
4

shipfast

The best free SaaS base for GenAI, LLM, and other AI Services. Ship your next Generative AI startups in days.
TypeScript
165
star
5

hugo-curious

Hugo theme for AICurious blog. Based on Hugo Clarity: https://github.com/chipzoller/hugo-clarity
JavaScript
25
star
6

chess.mojo

Chess.Mojo 🔥 - The first UCI chess engine in Mojo
Python
20
star
7

This-Repo-Has-15-Stars

Yes, it's true 💔
16
star
8

blazeface_keras

BlazeFace Implementation - Keras
Python
11
star
9

autonomous-car-2020

Autonomous car source code for FPT Digital Race (Cuộc Đua Số) 2020 - Round 1 - Simulation Car. TOP 1 DHBKHN (HUST).
C++
11
star
10

awesome-autonomous

Learning resources for autonomous robot and self-driving vehicle systems
HTML
9
star
11

p8hub

Private AI Hub (P8Hub) - Host and use your own AI Services. Keep everything simple and private.
TypeScript
9
star
12

board-ar

View Raspberry Pi Pico board ports using Augmented Reality (AR)
Python
9
star
13

anylabeling-assets

Assets for AnyLabeling app
Python
8
star
14

CustomChar

Your customized AI characters on commodity hardware!
C++
7
star
15

thansohoc

Xem vận mệnh qua thần số học Pythagoras
HTML
6
star
16

trainme

Labeling and Training Toolkit for Computer Vision Projects
Python
6
star
17

facecam

Funny camera app with face decorators like Snapchat. This project uses face detection and alignment algorithms from OpenCV.
C++
6
star
18

traffic-sign-classification-uff-tensorrt

Traffic sign classification training and conversion code to uff - TensorRT
Python
5
star
19

geeky-curious

Hugo theme for AICurious - 2022
SCSS
5
star
20

ivy-ai-examples

Examples of using Ivy for training neural networks
Python
5
star
21

paper_stream

Paper Transformation and Streaming - The Runner-Up at Call For Code IBM SoICT Hackathon 2020.
Python
5
star
22

vietanhdev

4
star
23

gict-meeting

Video Conference system using linux socket connection and client-server architecture
C++
4
star
24

cpp-call-python-opencv

Call Python OpenCV function from C++
C++
3
star
25

deep-head-pose-2

Deep head pose model with ShuffleNetV2, EfficientNet backbonds and Landmark estimation.
Python
3
star
26

unet-uff-tensorrt

Training code for UNet segmentation model which can be converted to TensorRT engine
Python
3
star
27

review.aicurious.io

Review machine learning knowledge for machine learning
HTML
3
star
28

centernet-bdd-data

A fork of Centernet for Berkeley DeepDrive dataset
Python
3
star
29

algo-curious

Translation project of https://cp-algorithms.com/
JavaScript
3
star
30

course-basic-opencv

Materials for course: Basic OpenCV - AI Curious
Python
3
star
31

autonomous-car-2018

Autonomous car source code for FPT Digital Race (Cuộc Đua Số) 2018 - Round 1 - Simulation Car
C++
2
star
32

S-Editor

My configuration for Emacs: << Stark Editor >>
Emacs Lisp
2
star
33

chessai-data

Data for ChessAI Project
Python
2
star
34

SFND_Lidar_Obstacle_Detection

My implementation of LiDAR Object Detection project - Sensor Fusion Engineer Nanodegree - Udacity
C++
2
star
35

e-store-manager

Store Management Software
JavaScript
2
star
36

beNice17

A Jekyll theme for my personal website
HTML
2
star
37

online-caro

An online five-in-a-row game (also called Caro game in Vietnamese)
CSS
2
star
38

iFirewall

Implement a DDOS Protection Firewall for Flask server
HTML
2
star
39

VAEmacs-configuration

My configuration files for Emacs
Emacs Lisp
2
star
40

chessai

Chinese Chess Advanced Analytics
TypeScript
2
star
41

autonomous-car-2020-road-seg

Road segmentation - Traffic sign detection - FPT Digital Race (Cuộc Đua Số) 2020
Python
1
star
42

autonomous-car-2020-sign-detection

Traffic sign detection - FPT Digital Race (Cuộc Đua Số) 2020
Python
1
star
43

tieng-viet-moi

Chuyển tiếng Việt sang kiểu chữ mới
HTML
1
star
44

ccbot

Building a robot that can play Chinese chess (XiangQi) with humans.
Python
1
star
45

wol

Remote Wake-On-Lan from internet using Arduino and enc28j60 module
HTML
1
star
46

Data-Structure-and-Algorithms-lab

place to save files for Data Structure and Algorithms lab _ HUST
C
1
star
47

anylabeling-docs

Documentation for AnyLabeling
TypeScript
1
star
48

my-fonts

My fonts for PC
Shell
1
star
49

timelaps-movie-maker

Python
1
star
50

android-image-processing-cpp

Image processing experiments with C++ on Android
Kotlin
1
star
51

via-esp32-cam

ESP32 camera module for VIA https://via.makerviet.org
1
star
52

my-memos

Personal setup of Memos for engineering notes
1
star
53

gemo-tech-solutions

My solutions for Gemo Tech Exercises
JavaScript
1
star
54

p8hub-teachable-ai

JavaScript
1
star
55

via-gpt

VIA GPT - VIA Voice Buddy - Voice chat with ChatGPT
Python
1
star
56

hiDictionary

A dictionary using json files as database.
HTML
1
star
57

minesweeper-in-c

Minesweeper Game written in C by VietAnhDev
C
1
star
58

centernet-bdd-data-onnx-conversion

centernet-to-onnx-conversion
Python
1
star
59

moviemaker-hagiang-2023

Movie Maker for Ha Giang STEAM trip 2023
Python
1
star
60

ai-from-scratch

AI From Scratch in C++ - for educational purposes. Available now: Fully Connected Network and Convolution Neural Network.
C++
1
star