zhengziqiang / CoralSCOP

The official repository of "CoralSCOP: Segment any COral Image on this Planet". [CVPR Highlight 2024]
Other
3 stars 1 forks source link

Unable to install `detectron2` #1

Open SaiAakash opened 1 week ago

SaiAakash commented 1 week ago

Hi @zhengziqiang ! This is great work !

I tried to setup the environment and run the CoralSCOP model. There were a few issues with the installation initially which I managed and got it to work. I was able to run test.py and generate the output json files. However, when I tried to run the visualizer.py I was not able to visualize the masks and the images because of detectron2 missing from my environment.

I tried to install detectron2 but I couldn't and got the following error:

 ninja: build stopped: subcommand failed.
    Traceback (most recent call last):
      File "/home/saiaakash/.local/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 1740, in _run_ninja_build
        subprocess.run(
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/subprocess.py", line 524, 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 "<string>", line 1, in <module>
      File "/tmp/pip-req-build-ogdki4c2/setup.py", line 151, in <module>
        setup(
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/__init__.py", line 87, in setup
        return distutils.core.setup(**attrs)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 148, in setup
        return run_commands(dist)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/core.py", line 163, in run_commands
        dist.run_commands()
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 967, in run_commands
        self.run_command(cmd)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/dist.py", line 1214, in run_command
        super().run_command(command)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
        cmd_obj.run()
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/command/install.py", line 68, in run
        return orig.install.run(self)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/command/install.py", line 664, in run
        self.run_command('build')
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/dist.py", line 1214, in run_command
        super().run_command(command)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
        cmd_obj.run()
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/command/build.py", line 135, in run
        self.run_command(cmd_name)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/cmd.py", line 313, in run_command
        self.distribution.run_command(command)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/dist.py", line 1214, in run_command
        super().run_command(command)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
        cmd_obj.run()
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 79, in run
        _build_ext.run(self)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 339, in run
        self.build_extensions()
      File "/home/saiaakash/.local/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 741, in build_extensions
        build_ext.build_extensions(self)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 448, in build_extensions
        self._build_extensions_serial()
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 473, in _build_extensions_serial
        self.build_extension(ext)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/command/build_ext.py", line 202, in build_extension
        _build_ext.build_extension(self, ext)
      File "/home/saiaakash/anaconda3/envs/coralscop/lib/python3.10/site-packages/setuptools/_distutils/command/build_ext.py", line 528, in build_extension
        objects = self.compiler.compile(sources,
      File "/home/saiaakash/.local/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 562, in unix_wrap_ninja_compile
        _write_ninja_file_and_compile_objects(
      File "/home/saiaakash/.local/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 1419, in _write_ninja_file_and_compile_objects
        _run_ninja_build(
      File "/home/saiaakash/.local/lib/python3.10/site-packages/torch/utils/cpp_extension.py", line 1756, in _run_ninja_build
        raise RuntimeError(message) from e
    RuntimeError: Error compiling objects for extension
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/saiaakash/anaconda3/envs/coralscop/bin/python -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-req-build-ogdki4c2/setup.py'"'"'; __file__='"'"'/tmp/pip-req-build-ogdki4c2/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-a76am1xc/install-record.txt --single-version-externally-managed --compile --install-headers /home/saiaakash/anaconda3/envs/coralscop/include/python3.10/detectron2 Check the logs for full command output.

The traceback was much bigger, I have posted only the last few lines of it. Is this dependency a mandatory one or could we get around with some other package to visualize the masks ?

taiamiti commented 5 days ago

Hello, I encountered the same issue as the conda.yaml file uses old pytorch and cuda version. When compiling old cuda old gcc and g++ are required and not easy to install. For instance we need gcc-8 to compile with cuda 10.2 but it was discontinued in ubuntu 22.04 (it has gcc-11). A better way is to simply install a newer pytorch version with newer cuda. I tried with pytorch 2.3 with cuda 12.1 as I used a similar environement to test SAM2 and it worked. Here is the recipe to reproduce a working environement :

# first make sure you installed cudatoolkit 12.1 on your machine
# use sudo update-alternatives --config cuda to make sure to select cuda 12.1 when multiple cuda version are installed
# create conda env
conda create -n coralscop python=3.10
# install pytorch with cuda 12.1
pip install torch==2.3.0 torchvision==0.18.0 --index-url https://download.pytorch.org/whl/cu121
pip install opencv-python

# install and compile detectron2
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2

Using this environement I was able to execute both inference and visualization scripts

taiamiti commented 5 days ago

In general, using the conda.yaml file to reproduce a working env is not ideal and has many caveats. A better way is to provide installation instructions and major package dependencies. A docker build is also a nice to have. I can ake a pull request to add one if needed.

D-Barradas commented 5 days ago

@taiamiti That would be nice to have, in my case, I have to edit the coral.yml file because mmcv-full is unable to detect the $CUDA_HOME variable so I removed that and then I had to change the pycocotools to 2.0.8. The tricky parts of installing could be avoided with a dockerfile BTW the author should provide a yml file with builds and prefixes

zhengziqiang commented 2 days ago

I have tested the coral.yaml file in Ubuntu 20.04.

It usually leads to some errors when installing the pycocotools in Windows. I have tested pycocotools-windows==2.0.0 in Windows and it works for me.

As for the detectron2, we used that visualization codes to better visualize the boundary. You can skip this dependence and could just utilize the pycocotools to generate the coral reef masks instead using following codes:

import pycocotools.mask as mask
coral_mask=mask.decode(ann["segmentation"])

I will upload corresponding visualization scripts soon.

zhengziqiang commented 2 days ago

In general, using the conda.yaml file to reproduce a working env is not ideal and has many caveats. A better way is to provide installation instructions and major package dependencies. A docker build is also a nice to have. I can ake a pull request to add one if needed.

Thank you for your help! I will also try to solve this problem. I used an older pytorch version since I used pytorch-lightning for training. Please feel free to add your pull request.

SaiAakash commented 1 day ago

Thanks for all the suggestions ! I was able to run CoralSCOP by making some changes on the lines of what @D-Barradas and @taiamiti suggested. For me things were slightly different because, apart from the pycocotools error, my conda environment was unable to detect the cuda-toolkit installed inside my environment (which doesn't generally contain things like nvcc). However, I fixed this by installing cudatoolkit-dev with conda and set my CUDA_HOME env variable to the nvcc inside the conda environment.

This solved the issue for me. It's also a general solution to safely play with multiple cuda-toolkit versions if that's an unavoidable requirement.