Skip to content

Commit f1262a6

Browse files
JuliajrentainheShuoShenDekwikwagInfro
authored
Sync fork with upstream main (#5)
* support custom video tracking demo with local gd1.0 model * update dockerfile (IDEA-Research#47) * feat: grounded_sam2_hf_mode cli arguments (IDEA-Research#52) * [New Feature] Support SAM 2.1 (IDEA-Research#59) * support sam 2.1 * refine config path and ckpt path * update README * refine config * fix hyper link for florence-2 demo * update README * update running info * support slice inference on gd1.5 sam2 demo * update README for SAHI inference * refine README * refine README * refine README * refine README * refine useless code * fix: zero object detection error (IDEA-Research#64) * update dockerfile * fix: zero object detection error * fix: zero object detection error * support box threshold in GD 1.5 demos * Fixed Type on MORE_DETAILED_CAPTION (IDEA-Research#68) * Add DINO-X arXiv link * support DINO-X with SAM 2 for detection and segmentation * add dino-x sam2 tracking demo * Fix typo * update dds-cloudapi-sdk to 0.3.3 * update README * update setuptools build requirement to fix build error (IDEA-Research#91) * Add torch2.6 support for ms_deform_attn_cuda (IDEA-Research#94) * update DINO-X api usage to dds v2 * update DINO-X api usage to dds v2 * update DINO-X api to V2 * Pinned setuptools in Dockerfile (IDEA-Research#99) Co-authored-by: Sami Haidar Wehbe <sami@autoenhance.ai> * feat:add grounded_sam2_tracking_camera_with_continuous_id.py (closes … (IDEA-Research#97) * feat:add grounded_sam2_tracking_camera_with_continuous_id.py (closes IDEA-Research#74) * update README * Change default output dir for HF demo (IDEA-Research#105) * Fix demos for CPU inference (IDEA-Research#104) * Fix unsorted `all_class_names` mismatch (IDEA-Research#112) `all_class_names` was not sorted together along with `all_object_ids` and `all_object_boxes`. This causes issue when drawing boxes on multi-class grounding detection. * support 5090 (IDEA-Research#115) * Handle (N,K,H,W) masks when multimask_output=True by picking the top-scoring candidate (IDEA-Research#118) * Update setup.py support 5090 * Update grounded_sam2_local_demo.py fix: handle (N,K,H,W) masks when multimask_output=True by picking the top-scoring candidate When multimask_output=True, the predictor returns masks with shape (N,K,H,W). The previous code called squeeze(1) assuming K==1 and crashed (“cannot select an axis to squeeze out…”). This patch selects the best candidate per instance (argmax over scores) to reduce it to (N,H,W). Note: similar assumptions may exist in other files, but I haven't tested them, so this change is scoped to this file only. * Replaced 'box_threshold' argument with 'threshold' to processor.post_process_grounded_object_detection for compatibility with the latest version of transformers (IDEA-Research#119) * fix: allow compilation with lower cuda version (IDEA-Research#122) * fix: add CUDA version check for <12.8 compatibility (IDEA-Research#123) Co-authored-by: jieun-b <jieun-b> --------- Co-authored-by: rentainhe <596106517@qq.com> Co-authored-by: Susan Shen <31400000+ShuoShenDe@users.noreply.github.com> Co-authored-by: kwikwag <sadan.yuval@gmail.com> Co-authored-by: Ren Tianhe <48727989+rentainhe@users.noreply.github.com> Co-authored-by: John Heilman <1735575+Infro@users.noreply.github.com> Co-authored-by: Reuben Feinman <rfeinman16@gmail.com> Co-authored-by: Andrew Choi <neffneff4@gmail.com> Co-authored-by: Sami Haidar <47423350+sami-haidar@users.noreply.github.com> Co-authored-by: Sami Haidar Wehbe <sami@autoenhance.ai> Co-authored-by: Embodied Learner <157788460+HITSZ-Robotics@users.noreply.github.com> Co-authored-by: will ye <williamyeny@gmail.com> Co-authored-by: Meng-Jiun Chiou <coldmanck@gmail.com> Co-authored-by: Freax Ruby <limyoonaxi@gmail.com> Co-authored-by: Ooosn <namewangyy@gmail.com> Co-authored-by: Hamza Adnan <42165451+hamzaadnan@users.noreply.github.com> Co-authored-by: Zicheng Zhang <38455236+victkk@users.noreply.github.com> Co-authored-by: Jieun Bae <59651199+jieun-b@users.noreply.github.com>
1 parent ff4dcf2 commit f1262a6

File tree

350 files changed

+41075
-835
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

350 files changed

+41075
-835
lines changed

.watchmanconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

Dockerfile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ FROM pytorch/pytorch:2.3.1-cuda12.1-cudnn8-devel
44
ARG USE_CUDA=0
55
ARG TORCH_ARCH="7.0;7.5;8.0;8.6"
66

7-
ENV AM_I_DOCKER True
8-
ENV BUILD_WITH_CUDA "${USE_CUDA}"
7+
ENV AM_I_DOCKER=True
8+
ENV BUILD_WITH_CUDA="${USE_CUDA}"
99
ENV TORCH_CUDA_ARCH_LIST="${TORCH_ARCH}"
10-
ENV CUDA_HOME /usr/local/cuda-12.1/
10+
ENV CUDA_HOME=/usr/local/cuda-12.1/
1111
# Ensure CUDA is correctly set up
12-
ENV PATH /usr/local/cuda-12.1/bin:${PATH}
13-
ENV LD_LIBRARY_PATH /usr/local/cuda-12.1/lib64:${LD_LIBRARY_PATH}
12+
ENV PATH=/usr/local/cuda-12.1/bin:${PATH}
13+
ENV LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64:${LD_LIBRARY_PATH}
1414

1515
# Install required packages and specific gcc/g++
1616
RUN apt-get update && apt-get install --no-install-recommends wget ffmpeg=7:* \
@@ -27,7 +27,8 @@ WORKDIR /home/appuser/Grounded-SAM-2
2727

2828

2929
# Install essential Python packages
30-
RUN python -m pip install --upgrade pip setuptools wheel numpy
30+
RUN python -m pip install --upgrade pip "setuptools>=62.3.0,<75.9" wheel numpy \
31+
opencv-python transformers supervision pycocotools addict yapf timm
3132

3233
# Install segment_anything package in editable mode
3334
RUN python -m pip install -e .

INSTALL.md

Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
## Installation
2+
3+
### Requirements
4+
5+
- Linux with Python ≥ 3.10, PyTorch ≥ 2.3.1 and [torchvision](https://github.com/pytorch/vision/) that matches the PyTorch installation. Install them together at https://pytorch.org to ensure this.
6+
* Note older versions of Python or PyTorch may also work. However, the versions above are strongly recommended to provide all features such as `torch.compile`.
7+
- [CUDA toolkits](https://developer.nvidia.com/cuda-toolkit-archive) that match the CUDA version for your PyTorch installation. This should typically be CUDA 12.1 if you follow the default installation command.
8+
- If you are installing on Windows, it's strongly recommended to use [Windows Subsystem for Linux (WSL)](https://learn.microsoft.com/en-us/windows/wsl/install) with Ubuntu.
9+
10+
Then, install SAM 2 from the root of this repository via
11+
```bash
12+
pip install -e ".[notebooks]"
13+
```
14+
15+
Note that you may skip building the SAM 2 CUDA extension during installation via environment variable `SAM2_BUILD_CUDA=0`, as follows:
16+
```bash
17+
# skip the SAM 2 CUDA extension
18+
SAM2_BUILD_CUDA=0 pip install -e ".[notebooks]"
19+
```
20+
This would also skip the post-processing step at runtime (removing small holes and sprinkles in the output masks, which requires the CUDA extension), but shouldn't affect the results in most cases.
21+
22+
### Building the SAM 2 CUDA extension
23+
24+
By default, we allow the installation to proceed even if the SAM 2 CUDA extension fails to build. (In this case, the build errors are hidden unless using `-v` for verbose output in `pip install`.)
25+
26+
If you see a message like `Skipping the post-processing step due to the error above` at runtime or `Failed to build the SAM 2 CUDA extension due to the error above` during installation, it indicates that the SAM 2 CUDA extension failed to build in your environment. In this case, **you can still use SAM 2 for both image and video applications**. The post-processing step (removing small holes and sprinkles in the output masks) will be skipped, but this shouldn't affect the results in most cases.
27+
28+
If you would like to enable this post-processing step, you can reinstall SAM 2 on a GPU machine with environment variable `SAM2_BUILD_ALLOW_ERRORS=0` to force building the CUDA extension (and raise errors if it fails to build), as follows
29+
```bash
30+
pip uninstall -y SAM-2 && \
31+
rm -f ./sam2/*.so && \
32+
SAM2_BUILD_ALLOW_ERRORS=0 pip install -v -e ".[notebooks]"
33+
```
34+
35+
Note that PyTorch needs to be installed first before building the SAM 2 CUDA extension. It's also necessary to install [CUDA toolkits](https://developer.nvidia.com/cuda-toolkit-archive) that match the CUDA version for your PyTorch installation. (This should typically be CUDA 12.1 if you follow the default installation command.) After installing the CUDA toolkits, you can check its version via `nvcc --version`.
36+
37+
Please check the section below on common installation issues if the CUDA extension fails to build during installation or load at runtime.
38+
39+
### Common Installation Issues
40+
41+
Click each issue for its solutions:
42+
43+
<details>
44+
<summary>
45+
I got `ImportError: cannot import name '_C' from 'sam2'`
46+
</summary>
47+
<br/>
48+
49+
This is usually because you haven't run the `pip install -e ".[notebooks]"` step above or the installation failed. Please install SAM 2 first, and see the other issues if your installation fails.
50+
51+
In some systems, you may need to run `python setup.py build_ext --inplace` in the SAM 2 repo root as suggested in https://github.com/facebookresearch/sam2/issues/77.
52+
</details>
53+
54+
<details>
55+
<summary>
56+
I got `MissingConfigException: Cannot find primary config 'configs/sam2.1/sam2.1_hiera_l.yaml'`
57+
</summary>
58+
<br/>
59+
60+
This is usually because you haven't run the `pip install -e .` step above, so `sam2` isn't in your Python's `sys.path`. Please run this installation step. In case it still fails after the installation step, you may try manually adding the root of this repo to `PYTHONPATH` via
61+
```bash
62+
export SAM2_REPO_ROOT=/path/to/sam2 # path to this repo
63+
export PYTHONPATH="${SAM2_REPO_ROOT}:${PYTHONPATH}"
64+
```
65+
to manually add `sam2_configs` into your Python's `sys.path`.
66+
67+
</details>
68+
69+
<details>
70+
<summary>
71+
I got `RuntimeError: Error(s) in loading state_dict for SAM2Base` when loading the new SAM 2.1 checkpoints
72+
</summary>
73+
<br/>
74+
75+
This is likely because you have installed a previous version of this repo, which doesn't have the new modules to support the SAM 2.1 checkpoints yet. Please try the following steps:
76+
77+
1. pull the latest code from the `main` branch of this repo
78+
2. run `pip uninstall -y SAM-2` to uninstall any previous installations
79+
3. then install the latest repo again using `pip install -e ".[notebooks]"`
80+
81+
In case the steps above still don't resolve the error, please try running in your Python environment the following
82+
```python
83+
from sam2.modeling import sam2_base
84+
85+
print(sam2_base.__file__)
86+
```
87+
and check whether the content in the printed local path of `sam2/modeling/sam2_base.py` matches the latest one in https://github.com/facebookresearch/sam2/blob/main/sam2/modeling/sam2_base.py (e.g. whether your local file has `no_obj_embed_spatial`) to indentify if you're still using a previous installation.
88+
89+
</details>
90+
91+
<details>
92+
<summary>
93+
My installation failed with `CUDA_HOME environment variable is not set`
94+
</summary>
95+
<br/>
96+
97+
This usually happens because the installation step cannot find the CUDA toolkits (that contain the NVCC compiler) to build a custom CUDA kernel in SAM 2. Please install [CUDA toolkits](https://developer.nvidia.com/cuda-toolkit-archive) or the version that matches the CUDA version for your PyTorch installation. If the error persists after installing CUDA toolkits, you may explicitly specify `CUDA_HOME` via
98+
```
99+
export CUDA_HOME=/usr/local/cuda # change to your CUDA toolkit path
100+
```
101+
and rerun the installation.
102+
103+
Also, you should make sure
104+
```
105+
python -c 'import torch; from torch.utils.cpp_extension import CUDA_HOME; print(torch.cuda.is_available(), CUDA_HOME)'
106+
```
107+
print `(True, a directory with cuda)` to verify that the CUDA toolkits are correctly set up.
108+
109+
If you are still having problems after verifying that the CUDA toolkit is installed and the `CUDA_HOME` environment variable is set properly, you may have to add the `--no-build-isolation` flag to the pip command:
110+
```
111+
pip install --no-build-isolation -e .
112+
```
113+
114+
</details>
115+
116+
<details>
117+
<summary>
118+
I got `undefined symbol: _ZN3c1015SmallVectorBaseIjE8grow_podEPKvmm` (or similar errors)
119+
</summary>
120+
<br/>
121+
122+
This usually happens because you have multiple versions of dependencies (PyTorch or CUDA) in your environment. During installation, the SAM 2 library is compiled against one version library while at run time it links against another version. This might be due to that you have different versions of PyTorch or CUDA installed separately via `pip` or `conda`. You may delete one of the duplicates to only keep a single PyTorch and CUDA version.
123+
124+
In particular, if you have a lower PyTorch version than 2.3.1, it's recommended to upgrade to PyTorch 2.3.1 or higher first. Otherwise, the installation script will try to upgrade to the latest PyTorch using `pip`, which could sometimes lead to duplicated PyTorch installation if you have previously installed another PyTorch version using `conda`.
125+
126+
We have been building SAM 2 against PyTorch 2.3.1 internally. However, a few user comments (e.g. https://github.com/facebookresearch/sam2/issues/22, https://github.com/facebookresearch/sam2/issues/14) suggested that downgrading to PyTorch 2.1.0 might resolve this problem. In case the error persists, you may try changing the restriction from `torch>=2.3.1` to `torch>=2.1.0` in both [`pyproject.toml`](pyproject.toml) and [`setup.py`](setup.py) to allow PyTorch 2.1.0.
127+
</details>
128+
129+
<details>
130+
<summary>
131+
I got `CUDA error: no kernel image is available for execution on the device`
132+
</summary>
133+
<br/>
134+
135+
A possible cause could be that the CUDA kernel is somehow not compiled towards your GPU's CUDA [capability](https://developer.nvidia.com/cuda-gpus). This could happen if the installation is done in an environment different from the runtime (e.g. in a slurm system).
136+
137+
You can try pulling the latest code from the SAM 2 repo and running the following
138+
```
139+
export TORCH_CUDA_ARCH_LIST=9.0 8.0 8.6 8.9 7.0 7.2 7.5 6.0`
140+
```
141+
to manually specify the CUDA capability in the compilation target that matches your GPU.
142+
</details>
143+
144+
<details>
145+
<summary>
146+
I got `RuntimeError: No available kernel. Aborting execution.` (or similar errors)
147+
</summary>
148+
<br/>
149+
150+
This is probably because your machine doesn't have a GPU or a compatible PyTorch version for Flash Attention (see also https://discuss.pytorch.org/t/using-f-scaled-dot-product-attention-gives-the-error-runtimeerror-no-available-kernel-aborting-execution/180900 for a discussion in PyTorch forum). You may be able to resolve this error by replacing the line
151+
```python
152+
OLD_GPU, USE_FLASH_ATTN, MATH_KERNEL_ON = get_sdpa_settings()
153+
```
154+
in [`sam2/modeling/sam/transformer.py`](sam2/modeling/sam/transformer.py) with
155+
```python
156+
OLD_GPU, USE_FLASH_ATTN, MATH_KERNEL_ON = True, True, True
157+
```
158+
to relax the attention kernel setting and use other kernels than Flash Attention.
159+
</details>
160+
161+
<details>
162+
<summary>
163+
I got `Error compiling objects for extension`
164+
</summary>
165+
<br/>
166+
167+
You may see error log of:
168+
> unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2022 (inclusive) are supported! The nvcc flag '-allow-unsupported-compiler' can be used to override this version check; however, using an unsupported host compiler may cause compilation failure or incorrect run time execution. Use at your own risk.
169+
170+
This is probably because your versions of CUDA and Visual Studio are incompatible. (see also https://stackoverflow.com/questions/78515942/cuda-compatibility-with-visual-studio-2022-version-17-10 for a discussion in stackoverflow).<br>
171+
You may be able to fix this by adding the `-allow-unsupported-compiler` argument to `nvcc` after L48 in the [setup.py](https://github.com/facebookresearch/sam2/blob/main/setup.py). <br>
172+
After adding the argument, `get_extension()` will look like this:
173+
```python
174+
def get_extensions():
175+
srcs = ["sam2/csrc/connected_components.cu"]
176+
compile_args = {
177+
"cxx": [],
178+
"nvcc": [
179+
"-DCUDA_HAS_FP16=1",
180+
"-D__CUDA_NO_HALF_OPERATORS__",
181+
"-D__CUDA_NO_HALF_CONVERSIONS__",
182+
"-D__CUDA_NO_HALF2_OPERATORS__",
183+
"-allow-unsupported-compiler" # Add this argument
184+
],
185+
}
186+
ext_modules = [CUDAExtension("sam2._C", srcs, extra_compile_args=compile_args)]
187+
return ext_modules
188+
```
189+
</details>

MANIFEST.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
4+
# This source code is licensed under the license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
recursive-include sam2 *.yaml #include all config files

0 commit comments

Comments
 (0)