Installation of detectron2 in Windows is somehow tricky. I struggled a whole week to make it work. For this, I created a new anaconda environment (to match with the version requirement for pytorch and torchvision for detectron2) and started from installing cudatoolkit and cudnn in that environment. This could be the best way not to mess up with the existing anaconda environment.
Here is the step by step procedure (verified with my laptop with Windows 10 and RTX2070 GPU):
Create an anaconda environment (say ‘detectron_env’):
(note. python 3.8 didn’t work, 3.7 worked)
conda create -n detectron_env python=3.7
Activate detectron_env:
conda activate detectron_env
Install cudatoolkit:
(note. cuda version number should match with the one installed in your computer (in my case 11.3. You can check by typing “nvcc -V” in the anaconda prompt window. For further information, refer to https://pytorch.org/)
conda install –c anaconda cudatoolkit=11.3
Install cudnn:
(note. Don’t specify the version number. It will be automatically figured out)
conda install -c anaconda cudnn
Install pywin32:
conda install -c anaconda pywin32
Install pytorch, torchvision and torchaudio:
(note. Version number of cudatoolkit should match with the one in step 3. pytorch will be automatically installed with version number equal to or higher than 1.8 that is required by detectron2)
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
Check whether GPU is enabled for pytorch:
Enable python, import torch and type ‘torch.cuda.is_avaiable()’
You should get ‘True’. However, If you find that GPU is not enabled for pytorch, go to step 1) and try again with different version numbers for cuda and/or python.
Install some packages:
(note. You should install ninja. Otherwise, set-up and build procedure will not go smoothly)
conda install -c anaconda cython
pip install opencv-python
pip install git+https://github.com/facebookresearch/fvcore
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
pip install av
conda install -c anaconda scipy
conda install -c anaconda ninja
Go to the directory where you want to install detectron2.
Git clone the following repository:
(note. The folder name for detectron2 should be different from ‘detectron2’. In my case, I used ‘detectron_repo’. Otherwise, path for pytorch will be confused)
git clone https://github.com/facebookresearch/detectron2.git detectron_repo
Install dependencies:
(note. Don’t enter the cloned detectron_repo directory)
pip install -q -e detectron_repo
Go to detectron_repo directory:
cd detectron_repo
Build detectron2:
python setup.py build develop
If the above is not successful, you may need to start again from the beginning or reinstall pytorch. If you reinstall pytocrh, you need to rebuild detectron2 again.
If the above is successful, then
Test:
Go to demo/ directory and run the following script by specyfing an input path to any of your image (say .jpg):
python demo.py --config-file …/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input
原文链接:
https://stackoverflow.com/questions/60631933/install-detectron2-on-windows-10
需要注意的是需要安装vs 2015-2019,安装2022提示无法兼容
使用python -m detectron2.utils.collect_env
查看环境各个包的版本