Install Detectron2 on Windows 10

10,014

Solution 1

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/[email protected]
  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

Solution 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

Solution 3

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

Solution 4

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

  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

Share:
10,014
Admin
Author by

Admin

Updated on June 19, 2022

Comments

  • Admin
    Admin almost 2 years

    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?

  • Ichsan
    Ichsan over 2 years
    this github shows the way for installing detectron2 in linux
  • Maryam Bahrami
    Maryam Bahrami over 2 years
    @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
  • Ichsan
    Ichsan over 2 years
    Sorry, unfortunatelly, your syntax didn't success in my computer (windows 10 education, python 3.7.10)
  • Maryam Bahrami
    Maryam Bahrami over 2 years
    @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.
  • I. Lin
    I. Lin over 2 years
    I can't either, @MaryamBahrami mind letting me know which version of PyTorch were you using? That would be really helpful. Thx.
  • Maryam Bahrami
    Maryam Bahrami over 2 years
    @I.Lin I added the versions to the answer.
  • I. Lin
    I. Lin over 2 years
    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?
  • DV82XL
    DV82XL over 2 years
    Tested with python=3.9, cudatoolkit=11.3, pytorch=1.10 and [email protected]. I also had to run mingw-get update and mingw-get upgrade. Works like a charm.
  • SagRU
    SagRU over 2 years
    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
    SagRU over 2 years
    Here's a separate question: stackoverflow.com/questions/70751751/…
  • east
    east over 2 years
    @SagRU, In step 10), You run the command: 'git clone 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
    east over 2 years
    @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.
  • SagRU
    SagRU over 2 years
    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_rotate‌​d\box_iou_rotated_cu‌​da.cu. I wonder if it comes from Microsoft Visual Studio, cython or another package?
  • east
    east over 2 years
    @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).
  • Greg7000
    Greg7000 about 2 years
    worked,like a charm. Thx
  • user2454869
    user2454869 about 2 years
    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 stackoverflow.com/questions/70751751/…
  • Rich KS
    Rich KS about 2 years
    @DV82XL Your suggestion seems to work for me; thank you.
  • Greg7000
    Greg7000 almost 2 years
    Step 3, replace '--' with '-'
  • east
    east almost 2 years
    @Greg7000, thnx.
  • Preetom Saha Arko
    Preetom Saha Arko almost 2 years
    Thanks a lot. It worked for me in python 3.10.4 (anaconda) as well.