Facebook AI Research recently released Detectron2, their next generation software system that implements state-of-the-art object detection algorithms.
Although it is built using a Windows pipeline, there is no official support for it on Windows. There are versions of Detectron2 available for Windows, but at the time of writing, these are older versions of the code and have been modified to remove Linux specific code and replace it with Windows specific code. This means you lose out on later features, bug fixes, improvements, etc. that Facebook include.
This tutorial shows how to get the latest version of Detectron2 working on Windows using WSL2 (Windows Subsystem for Linux), and includes steps for running this on the latest NVidia RTX30xx cards.
In addition, I'll show you how to call Detectron2 running inside WSL2 from Anaconda running natively within Windows.
This tutorial has been tested on the following PC build:
- AMD Ryzen 9 CPU
- NVidia RTX3070
- Windows 10 Home Build 21301.rs_prerelease.210123-1645
For this, we will install:
- Ubuntu 20.04LTS
- CUDA 11.2
- Pytorch 1.7.1
- PyCocoTools 2.0.2
Most of this information is available on the Microsoft and Nvidia websites, with some information buried in responses to issues on a number of forums, so for those of you wanting to do this, here's all the information in one place.
Note that for this tutorial, I'm assuming that you already have a good understanding of Windows, Linux and Python.
There are now 2 approches for installing below:
- An in-depth manual approach using Windows 10, WSL2, and Ubuntu 20.04 LTS
- Directly with Windows 10 and Anaconda
There's also a section on calling Detectron2 using a RESTful API.
If you have any issues with this, or over time the instructions change, please raise an issue stating the following:
- Your PC build (CPU, Windows version, GPU and anything else relevant)
- What the errors or issues you experienced are
- Any fixes you've tried or implemented
NOTE: Currently the WSL2 CUDA drivers aren't available on the NVIDIA site (17th March 2021) so please check before progressing with this section.
- Make sure that you backup your PC first. Some of these changes can be breaking (for example, after this I found that some games didn't work).
- Join the Windows Insider Program, download and install the latest development release of Windows 10 and configure WSL2 (see https://docs.microsoft.com/en-us/windows/win32/direct3d12/gpu-cuda-in-wsl). I've tested with Ubuntu 20.04LTS, feel free to try others Linux distributions if you want. Note, for those of you with RTX cards (including RTX30xx GPUs), you need to install the GeForce driver from https://developer.nvidia.com/cuda/wsl/download. For this step, you'll need to create an NVidia developers account.
- Once you have Ubuntu installed, launch it so you have a command prompt.
- Optional Install Anaconda. You don't need to do this, but I prefer to ensure that I keep versions of Python and libraries seperate for different projects. I also mix and match between PyTorch and Tensorflow development, so this helps significantly.
- Optional Once Anaconda is installed, create and activate a virtual environment. I'm using Python 3.8.5 here.
- If you're not using Anaconda, please ensure that you have a recent version of Python installed (I'm using Python 3.8.5 here). You may also need to install other Python libraries to complete the rest of this tutorial.
- You can now test whether your install is working by running
nvidia-smi
from within Ubuntu. You should see the usage statistics for your GPU. - Within your Python environment, install the following packages (
conda
instructions below, usepip
if you need to): - Install pytorch and associated libraries:
conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch
- Install PyCocoTools using
conda install -c conda-forge pycocotools
- From Python, check the install by running the following commands. If you get any errors here, please revisit the above steps, and check with the relevant forums for any errors you might get:
import torch
torch.cuda.is_available()
should returnTrue
. If you get false, please revisit the above steps.import pycocotools
- You can either install Detectron2 straight from https://github.com/facebookresearch/detectron2 or clone the repo locally and install from there. In either case, follow the instructions here to build Detectron2 from source.
- Now check that Detectron2 has installed by running
import detectron2
from within Python.
If you've got this far, then Detectron2 is installed and ready for you to use.
Detectron2, at the time of writing, does not have a native API. This is simple to setup using Flask. You can of course import detectron2
into your code directly, but if you want to move to an API based approach, here's an example of how to get this working.
Optional If you want to run Anaconda in Windows and call Detectron2 in Ubuntu, then you'll need to get the internal IP address of your Ubuntu container using ipconfig
. On my system, I used the IP address of the eth0
adapter, which was of the format 172.x.x.x
. Note this IP address can change between reboots.
I've provided an example script to get you started. Note that this isn't production ready, it's purely for development and test purposes. This example code isn't designe to handle multiple concurrent requests, etc.
To call the API, please see this notebook. For information on the output format of the API, please read the Detectron2 documentation.
You'll notice that these scripts use the Blosc library. This is to facilitate transfering binary (image) data over HTTP inside a JSON request/response.
Note that you will need Cuda 11.2 or later and Visual Studio 2017-2019 (Inclusive)
A conda YML file for install directly into Conda on Windows can be found here:
facebookresearch/detectron2#9 (comment)
- How to Run the New StyleGAN2 ADA for PyTorch on Windows without Docker with Ampere/30xx or 20xx.
- Detectron2: The base basic end-to-end tutorial
Thanks to @b_pronod for helping with the WSL2 and CUDA setup.