facebookresearch / sam2

The repository provides code for running inference with the Meta Segment Anything Model 2 (SAM 2), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
12.38k stars 1.14k forks source link

Issue with video predictor #14

Closed MattLiutt closed 3 months ago

MattLiutt commented 3 months ago

I tried to run the example under Colab, and encountered the following error when import _C.so from sam2. It would be helpful if you could provide some ideas on this issue.

/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py in decorate_context(*args, **kwargs)
    113     def decorate_context(*args, **kwargs):
    114         with ctx_factory():
--> 115             return func(*args, **kwargs)
    116 
    117     return decorate_context

/content/segment-anything-2/sam2/sam2_video_predictor.py in add_new_points(self, inference_state, frame_idx, obj_id, points, labels, clear_old_points, normalize_coords)
    219             # Clamp the scale of prev_sam_mask_logits to avoid rare numerical issues.
    220             prev_sam_mask_logits = torch.clamp(prev_sam_mask_logits, -32.0, 32.0)
--> 221         current_out, _ = self._run_single_frame_inference(
    222             inference_state=inference_state,
    223             output_dict=obj_output_dict,  # run on the slice of a single object

/content/segment-anything-2/sam2/sam2_video_predictor.py in _run_single_frame_inference(self, inference_state, output_dict, frame_idx, batch_size, is_init_cond_frame, point_inputs, mask_inputs, reverse, run_mem_encoder, prev_sam_mask_logits)
    808         # potentially fill holes in the predicted masks
    809         if self.fill_hole_area > 0:
--> 810             pred_masks_gpu = fill_holes_in_mask_scores(
    811                 pred_masks_gpu, self.fill_hole_area
    812             )

/content/segment-anything-2/sam2/utils/misc.py in fill_holes_in_mask_scores(mask, max_area)
    221     # (background regions are those with mask scores <= 0)
    222     assert max_area > 0, "max_area must be positive"
--> 223     labels, areas = get_connected_components(mask <= 0)
    224     is_hole = (labels > 0) & (areas <= max_area)
    225     # We fill holes with a small positive mask score (0.1) to change them to foreground.

/content/segment-anything-2/sam2/utils/misc.py in get_connected_components(mask)
     59               components for foreground pixels and 0 for background pixels.
     60     """
---> 61     from sam2 import _C
     62 
     63     return _C.get_connected_componnets(mask.to(torch.uint8).contiguous())

ImportError: /content/segment-anything-2/sam2/_C.so: undefined symbol: _ZN3c1015SmallVectorBaseIjE8grow_podEPKvmm

Python version: 3.10 Environment: Google Colab with T4

MattLiutt commented 3 months ago

Issue solved.

albertfaromatics commented 3 months ago

How did you solve it?

leoisufa commented 3 months ago

How did you solve it? same question

leviome commented 3 months ago

How ?

leoisufa commented 3 months ago

I fix this problem by installing pytorch=2.1.0, while I meet this problem by using pytorch=2.3.1. After changing the pytorch version, it works well.

How did you solve it? same question

fangli333 commented 3 months ago

just conda install pytorch=2.1.0?

Squirrel-code commented 3 months ago

I noticed that there are

_REQUIREDPACKAGES = [ "torch>=2.3.1",

in setpu.py, I'm confused. X﹏X

Squirrel-code commented 3 months ago

Another solution in #22 is running: python setup.py build_ext --inplace. It workd. ( •̀ ω •́ )✧

XinrunXu commented 3 months ago

Do not work for me

(sam2) C:\Users\PS\Desktop\segment-anything-2>python setup.py clean --all CUDA_HOME in setup.py: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8 running clean removing 'build\temp.win-amd64-cpython-310' (and everything under it) 'build\lib.win-amd64-cpython-310' does not exist -- can't clean it 'build\bdist.win-amd64' does not exist -- can't clean it 'build\scripts-3.10' does not exist -- can't clean it removing 'build'

(sam2) C:\Users\PS\Desktop\segment-anything-2>python setup.py build_ext --inplace CUDA_HOME in setup.py: C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8 running build_ext C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py:384: UserWarning: Error checking compiler version for cl: [WinError 2] The system cannot find the file specified warnings.warn(f'Error checking compiler version for {compiler}: {error}') building 'sam2._C' extension creating C:\Users\PS\Desktop\segment-anything-2\build creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310 creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2 creating C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2\csrc Emitting ninja build file C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\build.ninja... Compiling objects... Allowing ninja to set a default number of workers... (overridable by setting the environment variable MAX_JOBS=N) [1/1] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin\nvcc --generate-dependencies-with-compile --dependency-output C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj.d -std=c++17 --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /wd4624 -Xcompiler /wd4067 -Xcompiler /wd4068 -Xcompiler /EHsc -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\torch\csrc\api\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\TH -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\include" -IC:\Users\PS.conda\envs\sam2\include -IC:\Users\PS.conda\envs\sam2\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt" -c C:\Users\PS\Desktop\segment-anything-2\sam2\csrc[connected_components.cu](http://connected_components.cu/) -o C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -DCUDA_NO_BFLOAT16_CONVERSIONS -DCUDA_NO_HALF2_OPERATORS --expt-relaxed-constexpr -DCUDA_HAS_FP16=1 -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -DCUDA_NO_HALF2_OPERATORS -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_C -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_80,code=sm_80 FAILED: C:/Users/PS/Desktop/segment-anything-2/build/temp.win-amd64-cpython-310/Release/sam2/csrc/connected_components.obj C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\bin\nvcc --generate-dependencies-with-compile --dependency-output C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj.d -std=c++17 --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler /wd4244 -Xcompiler /wd4267 -Xcompiler /wd4275 -Xcompiler /wd4018 -Xcompiler /wd4190 -Xcompiler /wd4624 -Xcompiler /wd4067 -Xcompiler /wd4068 -Xcompiler /EHsc -Xcudafe --diag_suppress=base_class_has_different_dll_interface -Xcudafe --diag_suppress=field_without_dll_interface -Xcudafe --diag_suppress=dll_interface_conflict_none_assumed -Xcudafe --diag_suppress=dll_interface_conflict_dllexport_assumed -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\torch\csrc\api\include -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\TH -IC:\Users\PS.conda\envs\sam2\lib\site-packages\torch\include\THC "-IC:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\include" -IC:\Users\PS.conda\envs\sam2\include -IC:\Users\PS.conda\envs\sam2\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.40.33807\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\um" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\shared" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\winrt" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\cppwinrt" -c C:\Users\PS\Desktop\segment-anything-2\sam2\csrc[connected_components.cu](http://connected_components.cu/) -o C:\Users\PS\Desktop\segment-anything-2\build\temp.win-amd64-cpython-310\Release\sam2/csrc/connected_components.obj -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -DCUDA_NO_BFLOAT16_CONVERSIONS -DCUDA_NO_HALF2_OPERATORS --expt-relaxed-constexpr -DCUDA_HAS_FP16=1 -DCUDA_NO_HALF_OPERATORS -DCUDA_NO_HALF_CONVERSIONS -DCUDA_NO_HALF2_OPERATORS -DTORCH_API_INCLUDE_EXTENSION_H -DTORCH_EXTENSION_NAME=_C -D_GLIBCXX_USE_CXX11_ABI=0 -gencode=arch=compute_80,code=sm_80 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.1\include\crt/host_config.h(153): fatal error C1189: #error: -- 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. connected_components.cu ninja: build stopped: subcommand failed. Traceback (most recent call last): File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 2107, in _run_ninja_build subprocess.run( File "C:\Users\PS.conda\envs\sam2\lib\subprocess.py", line 526, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Users\PS\Desktop\segment-anything-2\setup.py", line 68, in setup( File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools__init__.py", line 104, in setup return distutils.core.setup(**attrs) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\core.py", line 184, in setup return run_commands(dist) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\core.py", line 200, in run_commands dist.run_commands() File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\dist.py", line 969, in run_commands self.run_command(cmd) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools\dist.py", line 967, in run_command super().run_command(command) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\dist.py", line 988, in run_command cmd_obj.run() File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools\command\build_ext.py", line 91, in run _build_ext.run(self) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 359, in run self.build_extensions() File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 870, in build_extensions build_ext.build_extensions(self) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 479, in build_extensions self._build_extensions_serial() File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 505, in _build_extensions_serial self.build_extension(ext) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools\command\build_ext.py", line 252, in build_extension _build_ext.build_extension(self, ext) File "C:\Users\PS.conda\envs\sam2\lib\site-packages\setuptools_distutils\command\build_ext.py", line 560, in build_extension objects = self.compiler.compile( File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 842, in win_wrap_ninja_compile _write_ninja_file_and_compile_objects( File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 1783, in _write_ninja_file_and_compile_objects _run_ninja_build( File "C:\Users\PS.conda\envs\sam2\lib\site-packages\torch\utils\cpp_extension.py", line 2123, in _run_ninja_build raise RuntimeError(message) from e RuntimeError: Error compiling objects for extension