This code is originated from Segment Anything CPP Wrapper and implemented on the macOS app RectLabel. We customized the original code so that getMask() uses the previous mask result and retain the previous mask array for undo/redo actions.
Download a SAM model folder.
- MobileSAM
- EdgeSAM
- EdgeSAM-3x
- Tiny EfficientSAM
- Small EfficientSAM
- ViT-Base HQ-SAM
- ViT-Large HQ-SAM
- ViT-Huge HQ-SAM
- ViT-Base SAM
- ViT-Large SAM
- ViT-Huge SAM
Download an ONNX Runtime folder.
- onnxruntime-osx-universal2-1.17.1.tgz for macOS
- onnxruntime-linux-x64-1.17.1.tgz for Ubuntu CPU
- onnxruntime-linux-x64-gpu-1.17.1.tgz for Ubuntu GPU
For Ubuntu, install gflags and opencv through vcpkg.
git clone https://github.com/microsoft/vcpkg.git
./vcpkg/bootstrap-vcpkg.sh
./vcpkg/vcpkg install gflags
./vcpkg/vcpkg install opencv
For Ubuntu GPU, install cuda and cudnn.
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt update
sudo apt install cuda-drivers
reboot
nvidia-smi
sudo apt install cuda-toolkit-11-8
vi ~/.bashrc
export PATH="/usr/local/cuda/bin${PATH:+:${PATH}}"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
source ~/.bashrc
which nvcc
nvcc --version
apt list libcudnn8 -a
cudnn_version=8.9.7.29
cuda_version=cuda11.8
sudo apt install libcudnn8=${cudnn_version}-1+${cuda_version}
sudo apt install libcudnn8-dev=${cudnn_version}-1+${cuda_version}
sudo apt install libcudnn8-samples=${cudnn_version}-1+${cuda_version}
Build and run.
# macOS
cmake -S . -B build -DONNXRUNTIME_ROOT_DIR=/Users/ryo/Downloads/onnxruntime-osx-universal2-1.17.1
# Ubuntu CPU
cmake -S . -B build -DONNXRUNTIME_ROOT_DIR=/root/onnxruntime-linux-x64-1.17.1 -DCMAKE_TOOLCHAIN_FILE=/root/vcpkg/scripts/buildsystems/vcpkg.cmake
# Ubuntu GPU
cmake -S . -B build -DONNXRUNTIME_ROOT_DIR=/root/onnxruntime-linux-x64-gpu-1.17.1 -DCMAKE_TOOLCHAIN_FILE=/root/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build build
# macOS and Ubuntu CPU
./build/sam_cpp_test -encoder="mobile_sam/mobile_sam_preprocess.onnx" -decoder="mobile_sam/mobile_sam.onnx" -image="david-tomaseti-Vw2HZQ1FGjU-unsplash.jpg" -device="cpu"
# Ubuntu GPU
./build/sam_cpp_test -encoder="mobile_sam/mobile_sam_preprocess.onnx" -decoder="mobile_sam/mobile_sam.onnx" -image="david-tomaseti-Vw2HZQ1FGjU-unsplash.jpg" -device="cuda:0"
To build on the Xcode, this is our settings on the Xcode.
- General -> Frameworks, Libraries, and Embedded Content
- Build Settings
Header Search Paths
/Users/ryo/Downloads/onnxruntime-osx-universal2-1.17.1/include
Library Search Paths
/Users/ryo/Downloads/onnxruntime-osx-universal2-1.17.1/lib
- Build Phases -> Embed Libraries