Coder Social home page Coder Social logo

running-detectron2-on-windows-wsl2-rtx30xx's Introduction

How to run Detectron2 on Windows 10 CUDA 11.2 and Nvidia RTX30xx GPUs.

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:

  1. An in-depth manual approach using Windows 10, WSL2, and Ubuntu 20.04 LTS
  2. Directly with Windows 10 and Anaconda

There's also a section on calling Detectron2 using a RESTful API.

Issues

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

Windows 10, WSL2, Ubuntu 20.04 LTS

NOTE: Currently the WSL2 CUDA drivers aren't available on the NVIDIA site (17th March 2021) so please check before progressing with this section.

  1. 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).
  2. 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.
  3. Once you have Ubuntu installed, launch it so you have a command prompt.
  4. 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.
  5. Optional Once Anaconda is installed, create and activate a virtual environment. I'm using Python 3.8.5 here.
  6. 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.
  7. 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.
  8. Within your Python environment, install the following packages (conda instructions below, use pip if you need to):
  9. Install pytorch and associated libraries: conda install pytorch torchvision torchaudio cudatoolkit=11.0 -c pytorch
  10. Install PyCocoTools using conda install -c conda-forge pycocotools
  11. 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:
  12. import torch
  13. torch.cuda.is_available() should return True. If you get false, please revisit the above steps.
  14. import pycocotools
  15. 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.
  16. 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.

Calling Detectron2 from by API

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.

Conda, Windows 10

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)

Additional useful resources

Acknowledgements

Thanks to @b_pronod for helping with the WSL2 and CUDA setup.

running-detectron2-on-windows-wsl2-rtx30xx's People

Contributors

markstrefford avatar

Stargazers

Philip Young avatar ralpy.na avatar Siladittya Manna avatar Jinghong Gao avatar zhangdawei avatar  avatar  avatar lilei avatar  avatar Faizal Ebrahim avatar KangolHsu avatar Mobassir avatar

Watchers

James Cloos avatar  avatar

running-detectron2-on-windows-wsl2-rtx30xx's Issues

nvidia-smi not working

Hi @markstrefford ,

Here is my PC build.

OS: Windows 10 Pro, 64-Bit
GPU: NVIDIA GeForce GTX 1080 Ti x 4
CPU: AMD Ryzen Threadripper 1950X 16-Core Processor

I was able to successfully install WSL2 and Ubuntu 20.04. I also installed Windows Terminal to run the commands. I installed miniconda and created an environment with Python 3.8.

  1. I tried installing CUDA and it seems that did not actually work.

I downloaded the WSL drivers from: https://developer.nvidia.com/cuda/wsl/download
image

I downloaded and ran the exe: 470.76_gameready_win10-dch_64bit_international.exe

  1. I then following this user guide: https://docs.nvidia.com/cuda/wsl-user-guide/index.html#installing-nvidia-drivers

I ran:

  1. Step 2 seemed to have run successfully but when I try running nvidia-smi I get:

image

This makes me think the CUDA installation did not actually work.

DLL load failed

cuda11.1
pytorch1.7.1
rtx3060
detectron2: v0.3

File "D:\Desktop\detectron2_solov2_six\AdelaiDet-master\detectron2\data_init_.py", line 4, in
from .build import (
File "D:\Desktop\detectron2_solov2_six\AdelaiDet-master\detectron2\data\build.py", line 12, in
from detectron2.structures import BoxMode
File "D:\Desktop\detectron2_solov2_six\AdelaiDet-master\detectron2\structures_init_.py", line 6, in
from .keypoints import Keypoints, heatmaps_to_keypoints
File "D:\Desktop\detectron2_solov2_six\AdelaiDet-master\detectron2\structures\keypoints.py", line 6, in
from detectron2.layers import interpolate
File "D:\Desktop\detectron2_solov2_six\AdelaiDet-master\detectron2\layers_init_.py", line 3, in
from .deform_conv import DeformConv, ModulatedDeformConv
File "D:\Desktop\detectron2_solov2_six\AdelaiDet-master\detectron2\layers\deform_conv.py", line 10, in
from detectron2 import _C
ImportError: DLL load failed:

NameError: name 'encode_video_frame_as_base64' is not defined

when i try to run (using your calling-detectron2-flask-api-example.ipynb):

print(f'Starting API call at {datetime.now()}')
res = requests.post(url, data=encode_video_frame_as_base64(frame_rear))
response_json = json.loads(res.text)
print(f'Ending API call at {datetime.now()}')

i get this error :

Starting API call at 2021-03-15 22:59:42.023794
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-21-fe8805e46185> in <module>
      1 print(f'Starting API call at {datetime.now()}')
----> 2 res = requests.post(url, data=encode_video_frame_as_base64(frame_rear))
      3 response_json = json.loads(res.text)
      4 print(f'Ending API call at {datetime.now()}')

NameError: name 'encode_video_frame_as_base64' is not defined

is the notebook incomplete? how to use it?

also i think you have syntax error while loading the image and encode in Base64

correct code should be :

with open(IMAGE_PATH, "rb") as image_file:
    data = base64.b64encode(image_file.read())

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.