Few weeks ago, I had the need to test and use some custom models made with Caffe2 framework and Detectron. They are actively developed on Linux, but I needed to have them run on Windows 10 with CUDA GPU support. It is possible to build Caffe2 for Windows, and a guide is provided, but if you need to use Detectron (not supported on Windows, officially), it is a bit more complicated and some changes in the source code and in the build scripts are required.
In the previous post, I detailed all the steps required to build Caffe2 on Windows 10 with CUDA 9/10 support. You need to follow all those steps, if not already done, prior to continue with this guide, where I'll show you how to build and run Detectron on Windows.
DISCLAIMER: this guide has been written and tested in the last week of September 2018. I've tried and tested it on 3 different Windows dev machines (2 with CUDA 9.2, 1 with CUDA 10), successfully. But I can't 100% ensure it works on yours, nor I can provide you direct support in case something does not work. Please check carefully the version of packages, dependencies, git commits, etc. It is quite possible that newer releases (of any dependency, package, core or 3rd party source code, tools) may brake the build.
Step 0: prerequisites
To successfully build and run Detectron on Windows 10, the same pre-requisites for Caffe2 on Windows 10 apply. Even if you can build and run Caffe2 on CPU only, CUDA GPU support IS REQUIRED for Detectron.
Step 1: Python Virtual Environment for Detectron
We're going to prepare a virtual environment to run our Detectron experiments. You may use the same environment created to build for Caffe2, based on Python 2.7. I preferred to have a dedicated virtual environment for Detectron.
Open a x64 Native Tools Developer Command Prompt
Clone Detectron from the official GitHub repository
> mkdir c:\projects\detectron > cd c:\projects\detectron > git clone --recursive https://github.com/facebookresearch/Detectron.git
If you prefer, you can also clone the repo using TortoiseGit. Remember to check the
I built successfully using the following commits from master branch:
- d38ade04c7aed364814f27bab588d74502c85c40 (2018-09-25 23:22:59)
- 3a38b7bcfc728dbe0ea7923af8ca77d7afbdb7ba (2018-09-06 00:57:42)
Update pip to the latest version (I used v18.0)
> python -m pip install -U pip
> python -m pip install virtualenv
Create a new Python 2.7 virtual environment
> python -m virtualenv caffe2env
Activate the virtual environment
Install the required packages (refer to README in Detectron repository).
I used Intel-MKL optimized numpy. You can get it from here and install it, before any other package. I installed 1.15.2 version.
(caffe2env)> pip install PATH_TO\numpy‑1.15.2+mkl‑cp27‑cp27m‑win_amd64.whl
Then install the other dependencies:
(caffe2env)> pip install -r requirements.txt
Add missing packages from requirements.txt.:
(caffe2env)> pip install protobuf (caffe2env)> pip install future (caffe2env)> pip install pydot
Build Cython optimized
You have to apply some minor changes in order to build and use them.
setup.py, comment out ALL
extra_compile_args ['-Wno-cpp'],option (not supported by VC++)
detectron\utils\cython_nms.pyx, fix a runtime problem as described here. At line 45, replace
detectron\utils\net.py, fix a runtime problem when loading a saved Pickle file model. At line 62, replace
(caffe2env)> python setup.py build_ext install
This will build and create a package for Detectron module, with the optimized
cython_nmsfiles. Extract the Egg (zip) file you can find in the
distfolder to a temp folder, and copy the content to
\detectron\utils(overwriting existing ones, if any).
Get and install PyCocoTools for Python 2.7, for Windows:
You have to use a custom build working on Windows, because the version available using pip does not work with Python 2.7. If you prefer, you can clone the repo using TortoiseGit. Then, using the
caffe2envvirtual environment we created so far, setup and install the package.
> mkdir c:\projects\cocoapi-python27 > cd c:\projects\cocoapi-python27 > git clone --recursive https://github.com/neoglez/cocoapi (caffe2env)> PythonAPI\python setup.py build_ext install
The custom Caffe2 we have built uses a workaround to make Detectron operators working in Windows. A fix is needed to avoid loading them as an external dynamic library. Please note that custom operators are NOT supported (if you need them, a similar workaround may work, too).
\detectron\utils\env.pyin order to bypass the DLL loading (and print a warning to remember this workaround):
def get_detectron_ops_lib(): print("In Windows, Detectron ops are built-in. No need to load dynamically. Ignore the following warning.") return "" def get_custom_ops_lib(): print("In Windows, Detectron custom ops are currently not supported. Ignore the following warning.") return ""
Detectron for Windows is now ready.
Step 2: test
To verify that it works, you can test with the following commands:
> (caffe2env)> set pythonpath=C:\python27;C:\python27\DLLs;c:\Projects\pytorch\build;c:\Projects\detectron;C:\opencv\build > (caffe2env)> python detectron/tests/test_spatial_narrow_as_op.py
It should run some test using Caffe2 Detectron operators... if they all pass, your Detectron is working. If not, or any other error occurs, your Caffe2/Detectron build has some issue (i.e. wrong build configuration, missing dependencies, missing DLLs, no supported CUDA device available, etc.). Check on the Internet, on StackOverflow, GitHub repo issues for ideas and clues on how to solve them.
That's all. Another long step-by-step guide, but there should be everything to obtain Detectron working on Windows.