https://github.com/zz990099/sam2_cpp
SAM2 Track implementation with TensorRT & OnnxRuntime
https://github.com/zz990099/sam2_cpp
cpp onnxruntime sam sam2 segment-anything-2 tensorrt track
Last synced: 3 months ago
JSON representation
SAM2 Track implementation with TensorRT & OnnxRuntime
- Host: GitHub
- URL: https://github.com/zz990099/sam2_cpp
- Owner: zz990099
- License: apache-2.0
- Created: 2025-04-28T03:45:59.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2025-05-23T09:06:41.000Z (8 months ago)
- Last Synced: 2025-06-27T16:48:09.955Z (7 months ago)
- Topics: cpp, onnxruntime, sam, sam2, segment-anything-2, tensorrt, track
- Language: C++
- Homepage:
- Size: 4.36 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sam2_cpp
## About This Project
该项目是`SAM2`算法的c++实现,包括`TensorRT`、`OnnxRuntime`三种硬件平台(推理引擎),使用[SAM2Export](https://github.com/Aimol-l/SAM2Export)导出onnx模型,并对`MemoryAttention`模块进行了优化。
## Demo
|
|
|
|
|:----------------------------------------:|:----:|:----:|
| **image** | **masks** | **track_result** |
以下带有**opt**标志的代表在模型导出工程[SAM2Export](https://github.com/Aimol-l/SAM2Export)基础上,优化`MemoryAttention`模型结构后导出的onnx模型,具体请查看[pr_link](https://github.com/Aimol-l/SAM2Export/pull/10).
| jetson-orin-nx-16GB | qps | cpu |
|:---------:|:---------:|:----------------:|
| sam2_track(fp16) - origin | 0.98 | 4% |
| sam2_track(fp16) - **opt** | **4.25** | 17% |
## Usage
### Download Project
下载git项目
```bash
git clone git@github.com:zz990099/sam2_cpp.git
cd sam2_cpp
git submodule init && git submodule update
```
### Build Enviroment
使用docker构建工作环境
```bash
cd sam2_cpp
bash easy_deploy_tool/docker/easy_deploy_startup.sh
# Select `jetson` -> `trt10_u2204`/`trt8_u2204`
bash easy_deploy_tool/docker/into_docker.sh
```
### Compile Codes
在docker容器内,编译工程. 使用 `-DENABLE_*`宏来启用某种推理框架,可用的有: `-DENABLE_TENSORRT=ON`、`-DENABLE_ORT=ON`,可以兼容。
```bash
cd /workspace
mdkir build && cd build
cmake .. -DBUILD_TESTING=ON -DENABLE_TENSORRT=ON
make -j
```
### Convert Model
1. 从[google driver](https://drive.google.com/drive/folders/1EBDUN793q9mJwNC1NA5s2nfq0SxMTx4b?usp=drive_link)中下载模型,放到`/workspace/models/`下
2. 在docker容器内,运行模型转换脚本
```bash
cd /workspace
bash tools/cvt_onnx2trt.sh
```
### Run Test Cases
1. 下载测试数据[link](https://drive.google.com/drive/folders/13PwIl8TBYT54YhSAPmuKI98IB99GElOj?usp=drive_link),放到`/workspace/test_data`下,文件夹名为`golf`
2. 运行测试用例,具体测试用例请参考代码。
```bash
cd /workspace/build
./bin/test_sam_sam2 --gtest_filter=*correctness
# 限制GLOG输出
GLOG_minloglevel=1 ./bin/test_sam_sam2 --gtest_filter=*speed
# 使用ctest查看可用测试用例
ctest -N
# 直接使用ctest测试全部用例
ctest
```
## References
- [sam2](https://github.com/facebookresearch/sam2)
- [SAM2Export](https://github.com/Aimol-l/SAM2Export)
- [EasyDeployTool](https://github.com/zz990099/EasyDeployTool)