9

I try to install Facebook's Detectron2 followed this official repo. Following that repo, detectron2 can only install on linux. However, I'm working on a server run on Windows operator. Anybody know how to install it on Windows?

mpx
  • 3,081
  • 2
  • 26
  • 56
Khiem Le
  • 195
  • 2
  • 2
  • 6

7 Answers7

8

Answer found through this issue: https://github.com/facebookresearch/detectron2/issues/9

These steps worked for me on my RTX 3070.

  1. Install Anaconda https://docs.anaconda.com/anaconda/install/windows/
  2. Create a environment.yml file containing the following code.
name: detectron2
channels:
  - pytorch
  - conda-forge
  - anaconda
  - defaults
dependencies:
  - python=3.8
  - numpy
  - pywin32
  - cudatoolkit=11.0
  - pytorch==1.7.1
  - torchvision
  - git
  - pip
  - pip:
    - git+https://github.com/facebookresearch/detectron2.git@v0.3
  1. Launch the Anaconda terminal, navigate to the yml file and run conda env create -f environment.yml

  2. Activate the environment conda activate detectron2

And you're good to go.

Edit: This works without issue if you run your script within the anaconda terminal but I was also having this issue ImportError: DLL load failed: The specified module could not be found. with numpy and Pillow when running the script from VS Code so if you happen to have this issue, I fixed it by uninstalling and reinstalling the troubled modules from within the anaconda terminal.

pip uninstall numpy
pip install numpy
DV82XL
  • 5,350
  • 5
  • 30
  • 59
Austin Ulfers
  • 354
  • 6
  • 17
  • 3
    Tested with `python=3.9`, `cudatoolkit=11.3`, `pytorch=1.10` and `detectron2.git@v0.6`. I also had to run `mingw-get update` and `mingw-get upgrade`. Works like a charm. – DV82XL Nov 27 '21 at 23:24
  • 1
    @DV82XL Your suggestion seems to work for me; thank you. – Rich KS Mar 27 '22 at 14:52
6

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):

  1. 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

  2. Activate detectron_env:

    conda activate detectron_env

  3. 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

  4. Install cudnn:
    (note. Don't specify the version number. It will be automatically figured out)

    conda install -c anaconda cudnn

  5. Install pywin32:

    conda install -c anaconda pywin32

  6. 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

  7. Check whether GPU is enabled for pytorch:

    Enable python, import torch and type 'torch.cuda.is_available()'

    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.

  8. 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

  9. Go to the directory where you want to install detectron2.

  10. 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

  11. Install dependencies:
    (note. Don't enter the cloned detectron_repo directory)

    pip install -q -e detectron_repo

  12. Go to detectron_repo directory:

    cd detectron_repo

  13. 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

  14. 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 <path_to_your_image_file.jpg> --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

Preetom Saha Arko
  • 2,588
  • 4
  • 21
  • 37
east
  • 73
  • 1
  • 5
  • For me your instruction failed at the step 11. When I run `pip install -q -e detectron_repo` I get the following error: `running build_ext error: [WinError 2] The system cannot find the file specified` – SagRU Jan 18 '22 at 06:51
  • Here's a separate question: https://stackoverflow.com/questions/70751751/cant-build-detectron2-on-windows-10 – SagRU Jan 18 '22 at 07:32
  • @SagRU, In step 10), You run the command: 'git clone https://github.com/facebookresearch/detectron2.git detectron_repo'. This should create a folder named 'detectron_repo' in the folder where you ran the above command (you can check). Once you confirm this, in step 11), you don't move the folder, just stay where you were in step 10) and run the command 'pip install -q -e detectron_repo'. This should go smoothly. – east Jan 19 '22 at 10:18
  • 1
    @SagRU, I just read your another post. It seems that you followed correctly.... And, I don't remember the fist Visual Studio installation procedure (Its long ago, but perhaps I struggled with VS 2019 and reinstalled VS 2015). I will check on it and get back to you as soon as I find something. – east Jan 19 '22 at 11:39
  • If I change `build_ext` to `build_ext --inplace` in `setup.py` I get a bit different output: `building 'detectron2._C' extension Don't know how to compile E:\TRON\detectron_repo\detectron2\layers\csrc\box_iou_rotated\box_iou_rotated_cuda.cu`. I wonder if it comes from Microsoft Visual Studio, cython or another package? – SagRU Jan 19 '22 at 16:42
  • @SagRU, I assume that cuda version installed in your computer is 11.X, not 10.X (since you installed cudatoolkit 11.3 in step 3... you can check by typing nvCC -V in anaconda prompt window). Perhaps... I suggest you start from step1 again by completely deleting the cloned folder 'detectron_repo' and removing the created anaconda environment 'detectron_env' (also completely deleting 'detectron_env' folder in the Anaconda3/envs folder). – east Jan 21 '22 at 03:33
  • worked,like a charm. Thx – Greg7000 Jan 26 '22 at 19:19
  • The recipe by 'east' is great. One word regarding compiler: You cannot use VS 2022 - at the moment (february 2022). That is, because VS 2022 demands CUDA 11.6, but there is currently no pytorch package on conda channel ‘pytorch’ which is built against CUDA 11.6 … So at least for now, one has to use VS 2019 and CUDA 11.3, then it works (I just built it). Note VS 2017 is too old (is not able to compile pytorch C++ code). Furthermore, at the begin I did additionally the steps (1) and (2) from https://stackoverflow.com/questions/70751751/cant-build-detectron2-on-windows-10 – user2454869 Feb 17 '22 at 18:30
  • Step 3, replace '--' with '-' – Greg7000 Jun 02 '22 at 12:13
  • @Greg7000, thnx. – east Jun 14 '22 at 15:04
  • Thanks a lot. It worked for me in python 3.10.4 (anaconda) as well. – Preetom Saha Arko Jun 17 '22 at 17:56
2

Here is the installation instructions. To install the latest version of Detectron2, following command works.

  • Windows10
  • Python = 3.7.9
  • Pytorch = 1.7.1
  • Torchvision = 0.8.2
  • Cuda = 11.0
  • detectron2 = 0.5

In the terminal:

python -m pip install git+https://github.com/facebookresearch/detectron2.git
Maryam Bahrami
  • 1,056
  • 9
  • 18
  • 1
    this github shows the way for installing detectron2 in linux – Ichsan Aug 09 '21 at 11:34
  • 1
    @Ichsan It seems you have not read and tested the command. You can install the latest version of Detectron2 using the command I stated. For the previous version you need Linux. On Windows this works – Maryam Bahrami Aug 09 '21 at 13:38
  • Sorry, unfortunatelly, your syntax didn't success in my computer (windows 10 education, python 3.7.10) – Ichsan Aug 09 '21 at 15:18
  • @Ichsan It might be because incompatible version of Pytorch or Torchvision, if you have them installed. If so, uninstall them, and run this again. If is not the case, try this: create a virtual environment, clone Detectron2, cd to the Directory and pip install -e. – Maryam Bahrami Aug 10 '21 at 06:42
  • 1
    I can't either, @MaryamBahrami mind letting me know which version of PyTorch were you using? That would be really helpful. Thx. – Ian Sep 07 '21 at 12:37
  • @I.Lin I added the versions to the answer. – Maryam Bahrami Sep 07 '21 at 14:16
  • well, thanks for the reply @Maryam Bahrami, but I still failed to build it when installed it. There are tons of error messages like: [1/11] C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.0\bin\nvcc --use-local-env -Xcompiler /MD -Xcompiler /wd4819 -Xcompiler /wd4251 -Xcompiler... Seems like something wrong with the GPU, what model were you using? – Ian Sep 07 '21 at 15:16
2

I Installed this in Windows 10 and 11 Successfully using this trick.

First of all, Install Visual Studio and Compiler, and build tools, and runtimes. Install .NET Runtime.

Then start with

conda create -n norfair python=3.9

conda install -c anaconda pywin32

conda install -c anaconda cython

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

pip install git+https://github.com/facebookresearch/detectron2.git

DONE

Hiren Namera
  • 390
  • 1
  • 10
1

Here is how I managed to use detectron2 on Windows 10:

  1. Determine how to install pytorch using https://pytorch.org/get-started/locally/ (I am using CPU only, install pytorch using the suggested command

  2. Run python -m pip install git+github.com/facebookresearch/detectron2.git

  3. conda install pywin32

Greg7000
  • 297
  • 3
  • 15
0

I build using win 10, py 3.8, cuda 11.3 following steps from @east as well as I did additionally used the steps (1) and (2) from stackoverflow.com/questions/70751751/…

Edited to include details:

1)I made a new env D/jav/learning with python 3.8.3 and activated it using conda activate D:\jav\learning

  1. conda install pytorch==1.10.1 torchvision==0.11.2 torchaudio==0.10.1 cudatoolkit=11.3 -c pytorch

  2. Set up environment variables for Microsoft Visual Studio by running vcvars64.bat

          C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat
    

4)Add new system path for cl.exe in my PATH variable:

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64
  1. Install following: conda install -c anaconda pywin32

                 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
    
  2. Then following direction of comments (9 to 14) from @east

    a) Go to the directory where you want to install detectron2.

                                                                git clone https://github.com/facebookresearch/detectron2.git    detectron_repo
    

    b)Install dependencies: pip install -q -e detectron_repo

c)Go to detectron_repo directory:

 cd detectron_repo

      Build detectron2:

      python setup.py build develop

Test:

Go to demo/ directory and run the following script python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input <path_to_your_image_file.jpg> --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

Successful, just got this warning:

PkgResourcesDeprecationWarning: is an invalid version and will not be supported in a future release

Jav
  • 43
  • 8
0

Regarding pip installations, it was a nightmare but I finally managed to create a workaround for me:

First install torch 1.10 cpu version:

pip install torch==1.10.0+cpu torchvision==0.11.0+cpu torchaudio==0.10.0 -f https://download.pytorch.org/whl/torch_stable.html --no-cache -I

Install detectron2:

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

Uninstall torch if you need newer version/cuda:

pip uninstall -y torch torchvision torchaudio

Finally delete all torch folders in your Lib/site-packages/ and install a different torch version.

m8ki
  • 1