facebookresearch / segment-anything-2

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
10.6k stars 840 forks source link

Cannot Build SAM v2 i have pytorch installed on GPU but build scrypt seem to create a venv pip-build-env-n0may61s that does not have the required dependencies ? #172

Open mcousine opened 1 month ago

mcousine commented 1 month ago

D:\Applfich\SAM2\segment-anything-2>pip install -e ".[demo]" Looking in indexes: https://artifactory.hydro.qc.ca/artifactory/api/pypi/global-pypi-virtual/simple Obtaining file:///D:/Applfich/SAM2/segment-anything-2 Installing build dependencies ... done Checking if build backend supports build_editable ... done Getting requirements to build editable ... error error: subprocess-exited-with-error

× Getting requirements to build editable did not run successfully. │ exit code: 1 ╰─> [32 lines of output] C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\torch_subclasses\functional_tensor.py:258: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at C:\actions-runner_work\pytorch\pytorch\builder\windows\pytorch\torch\csrc\utils\tensor_numpy.cpp:84.) cpu = _conversion_method_template(device=torch.device("cpu")) Traceback (most recent call last): File "C:\Users\dd1968\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip_vendor\pyproject_hooks_in_process_in_process.py", line 353, in main() File "C:\Users\dd1968\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip_vendor\pyproject_hooks_in_process_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\dd1968\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip_vendor\pyproject_hooks_in_process_in_process.py", line 132, in get_requires_for_build_editable return hook(config_settings) ^^^^^^^^^^^^^^^^^^^^^ File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\setuptools\build_meta.py", line 458, in get_requires_for_build_editable return self.get_requires_for_build_wheel(config_settings) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\setuptools\build_meta.py", line 327, in get_requires_for_build_wheel return self._get_build_requires(config_settings, requirements=[]) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\setuptools\build_meta.py", line 297, in _get_build_requires self.run_setup() File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\setuptools\build_meta.py", line 313, in run_setup exec(code, locals()) File "", line 70, in File "", line 51, in get_extensions File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\torch\utils\cpp_extension.py", line 1076, in CUDAExtension library_dirs += library_paths(cuda=True) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\torch\utils\cpp_extension.py", line 1214, in library_paths paths.append(_join_cuda_home(lib_dir)) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\LOCAL\TEMP\pip-build-env-n0may61s\overlay\Lib\site-packages\torch\utils\cpp_extension.py", line 2416, in _join_cuda_home raise OSError('CUDA_HOME environment variable is not set. ' OSError: CUDA_HOME environment variable is not set. Please set it to your CUDA install root. [end of output]

note: This error originates from a subprocess, and is likely not a problem with pip. error: subprocess-exited-with-error

× Getting requirements to build editable did not run successfully. │ exit code: 1 ╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

CUDA is enable pytorch is using GPU ?

D:\Applfich\SAM2\segment-anything-2>pip install torch Looking in indexes: https://artifactory.hydro.qc.ca/artifactory/api/pypi/global-pypi-virtual/simple Requirement already satisfied: torch in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (2.4.0+cu124) Requirement already satisfied: filelock in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (3.13.1) Requirement already satisfied: typing-extensions>=4.8.0 in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (4.9.0) Requirement already satisfied: sympy in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (1.12) Requirement already satisfied: networkx in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (3.2.1) Requirement already satisfied: jinja2 in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (3.1.3) Requirement already satisfied: fsspec in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (2023.10.0) Requirement already satisfied: setuptools in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from torch) (72.1.0) Requirement already satisfied: MarkupSafe>=2.0 in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from jinja2->torch) (2.1.4) Requirement already satisfied: mpmath>=0.19 in c:\users\dd1968\appdata\local\programs\python\python312\lib\site-packages (from sympy->torch) (1.3.0)

`D:\Applfich\SAM2\segment-anything-2>python Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.

import torch

if torch.cuda.is_available(): ... print("CUDA is available. PyTorch can use the GPU.") ... device = torch.device("cuda") ... device_name = torch.cuda.get_device_name(device) ... print(f"Using GPU: {device_name}") ... else: ... print("CUDA is not available. PyTorch will use the CPU.") ... device = torch.device("cpu") ... CUDA is available. PyTorch can use the GPU. Using GPU: NVIDIA RTX A2000 Laptop GPU

Example tensor on CPU

tensor = torch.randn(3, 3) print(f"Tensor is on device: {tensor.device}") Tensor is on device: cpu

Move tensor to GPU if available

tensor_gpu = tensor.to(device) print(f"Tensor is now on device: {tensor_gpu.device}") Tensor is now on device: cuda:0 `

ronghanghu commented 1 month ago

Hi, this is likely due to an error in building the SAM 2 CUDA extension. We have recently made the CUDA extension step optional (in https://github.com/facebookresearch/segment-anything-2/pull/155 and https://github.com/facebookresearch/segment-anything-2/pull/175) as a workaround to this problem.

You can pull the latest code and reinstall via

# run the line below inside the SAM 2 repo
git pull;
pip uninstall -y SAM-2;
rm -f sam2/*.so;
pip install -e ".[demo]"

which allows using SAM 2 without CUDA extension (the results should stay the same in most cases, see INSTALL.md for details).

fffionayou commented 1 month ago

I'm having the same problem as you, so can you solve it?

mcousine commented 1 month ago

Buy the way NumPy IS installed the submodule that crash refer as numpy not found and I am on windows CUDA driver for GPU is Installed and CUDA 12.6 toolkit also. Pytorch found the device and GPU capable.

Failed to initialize NumPy: No module named 'numpy'

D:\Applfich>pip install numpy Looking in indexes: https://artifactory.hydro.qc.ca/artifactory/api/pypi/global-pypi-virtual/simple Requirement already satisfied: numpy in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (1.26.4)

And the other requirement also meet Pytorch

D:\Applfich>pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124 Looking in indexes: https://download.pytorch.org/whl/cu124 Requirement already satisfied: torch in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (2.4.0+cu124) Requirement already satisfied: torchvision in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (0.19.0) Requirement already satisfied: torchaudio in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (2.4.0) Requirement already satisfied: filelock in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (3.13.1) Requirement already satisfied: typing-extensions>=4.8.0 in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (4.9.0) Requirement already satisfied: sympy in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (1.12) Requirement already satisfied: networkx in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (3.2.1) Requirement already satisfied: jinja2 in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (3.1.3) Requirement already satisfied: fsspec in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (2023.10.0) Requirement already satisfied: setuptools in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torch) (72.1.0) Requirement already satisfied: numpy<2 in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torchvision) (1.26.4) Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from torchvision) (10.2.0) Requirement already satisfied: MarkupSafe>=2.0 in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from jinja2->torch) (2.1.4) Requirement already satisfied: mpmath>=0.19 in c:\users\ABCD\appdata\local\programs\python\python312\lib\site-packages (from sympy->torch) (1.3.0)

ronghanghu commented 1 month ago

Hi @mcousine @fffionayou It seems that the CUDA extension failed to build on Windows. We have recently made it optional to build the SAM 2 CUDA extension. Could you try pulling the latest code and reinstall via

# run the line below inside the SAM 2 repo
git pull;
pip uninstall -y SAM-2;
rm -f sam2/*.so;
pip install -e ".[demo]"

which allows using SAM 2 without CUDA extension (the results should stay the same in most cases, see INSTALL.md for details).

YachiZhang commented 2 weeks ago

Same, and my environment is torch=2.3.1 cuda=12.1, the output show that it installs torch=2.4.0 in the venv

jiawei151 commented 1 day ago

ch=2.3.1 cuda=12.1

Exactly the same situation. Have you solved this problem by now, @YachiZhang ? Thanks.