Detectron2 & Python Wheels Cache. Building a Detectron2 wheel package on... | by Ian Ormesher | Towards Data Science

I’ve been recently doing some work with a custom Detectron2 model. I’ve been training my model on a Linux VM in the cloud, but I wanted to use this trained model in a Windows environment. The official line from Facebook Research about Detectron2 is that it isn’t supported on Windows. But I wasn’t going to let that stop me from trying! The good news is I was able to do it and this article describes the process I used.
In the process of explaining how I did this, I’m going to assume that you are familiar/comfortable with the following:
Python virtual environments
GIT command line
The first thing is to create a virtual environment and then activate it:
virtualenv detectron2detectron2\Scripts\activate
From your virtual environment install PyTorch and torchvision (CPU version only):
pip install torch==1.5.1+cpu torchvision==0.6.1+cpu -f https://download.pytorch.org/whl/torch_stable.html
It’s also probably worth installing fvcore and the windows version of pycocotools which is pycocotools-windows:
pip install fvcorepip install pycocotools-windows
To download the Detectron2 source we are going to clone a particular tagged release (here the tag is ‘v0.1’) using GIT command line:
git clone --depth 1 --branch v0.1 https://github.com/facebookresearch/detectron2.git
Once you’ve cloned the release change to the detectron2 folder you’ve just created with this clone from within your virtual environment (not your GIT command line). E.g. if you GIT cloned into the folder D:\Source\Python\detectron2 then change into that directory and issue the following:
pip install -U .
Don’t forget the full-stop at the end — that’s really important!
This should build the detectron2 package and if all has gone well you should see something like this at the end:
Because we cloned the v0.1 tag you’ll see that it’s built detectron2–0.1
You’ll notice that when you install a python package it checks it’s local wheel cache to see if it’s already downloaded this in the past, and if it has then it uses the cached version. To create you’re own wheel cache you need to first install the wheel package:
pip install wheel
Once you’ve done that we want to list the currently installed packages for our virtual environment and put them into a file (here it’s called requirements.txt):
pip freeze > requirements.txt
To copy/create all the wheels for the currently installed packages and put them in a cache folder (here I’m using D:\wheels_cache) do the following:
pip wheel --wheel-dir=D:\wheels_cache -r requirements.txt
One of the benefits of using a wheels cache is that in the future you can install directly using the cached wheel rather than having to build a package again. This is useful if you want to do this on a different PC where you may not have all the dependencies. You can do that like this:
pip install D:\wheels_cache\detectron2-0.1-cp38-cp38-win_amd64.whl
One of the things to note with this approach is that wheels are dependent on the target platform and Python version. This one is for Windows 64-bit and Python 3.8.
You can even install everything from the requirements.txt file, telling it to look for the wheels in your cache folder:
pip install --no-index --find-links=D:\wheels_cache -r requirements.txt
Once I had to set up a PC to be installed at a customer site and this box was to be isolated — i.e. it would not be allowed to connect to the internet or a local network. I had to be able to install Python and all the packages needed on this machine to run the machine learning software I had implemented. The approach I took was to build a wheels cache for the machine of all the packages that were needed. That way installation could happen from the cache.