I’ve been fiddling around with Detectron2 for the last few days and thought it may be worth documenting some immediate thoughts on it.

I started working with the tutorial dataset made available by Venelin Valkov and I’d also recommend his videos on Youtube. It’s actually the thing that got me curious about Detectron2.

First impressions, are that it doesn’t feel like any work at all. It’s all wired together by configuration and it just works. If you’ve experienced Fastai get_annotations with the ObjectItemList then this may feel very familiar.

I first ran the COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml which is a huge model on Google Colab and it breezed through the whole training process in 46 minutes.

I tried to replicate the whole thing on a Sagemaker P2 instance with 12 Gigs on GPU and it completely crapped out. I reverted to a slightly less intense model COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml and things seemed to move along a lot more comfortably. It still took well over an hour but the results were satisfying.

Here’s the entire notebook.

I struggled with the installation a bit and so I thought I’d document that as well. Here’s what I did.

I was running Cuda 10.1 so it’s essential to find the right detectron installation for that from their documentation

Ensure you’re installing this in your conda environment. Remember to restart your kernel once you’re done.

Install Cython, Pyyam and nvidial-ml-py3

$ pip install --upgrade pip
	
$ pip install -q cython pyyaml==5.1 nvidia-ml-py3

Install Pycocotools


$ pip install -U git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI

Check CUDA version

$ nvcc --version
(pytorch_p36) [ec2-user@ip-172-16-30-5 ObjectDetection_Detectron]$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018

In this case it’s CUDA 10.1

Find the corresponding version of detectron2 prebuilt installation and run

$ pip install detectron2 -f  https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.4/index.html

Detect installation environment for detectron once installed

 python -m detectron2.utils.collect_env.
/home/ec2-user/anaconda3/envs/pytorch_p36/bin/python: Error while finding module specification for 'detectron2.utils.collect_env.' (AttributeError: module 'detectron2.utils.collect_env' has no attribute '__path__')
(pytorch_p36) [ec2-user@ip-172-16-30-5 ObjectDetection_Detectron]$ python -m detectron2.utils.collect_env
----------------------  ----------------------------------------------------------------------------------------
sys.platform            linux
Python                  3.6.10 |Anaconda, Inc.| (default, Mar 25 2020, 23:51:54) [GCC 7.3.0]
numpy                   1.18.1
detectron2              0.1.3 @/home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/detectron2
detectron2._C           failed to import
DETECTRON2_ENV_MODULE   <not set>
PyTorch                 1.4.0 @/home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torch
PyTorch debug build     False
CUDA available          True
GPU 0                   Tesla K80
CUDA_HOME               /usr/local/cuda-10.1
NVCC                    Cuda compilation tools, release 10.1, V10.1.243
Pillow                  7.0.0
torchvision             0.5.0 @/home/ec2-user/anaconda3/envs/pytorch_p36/lib/python3.6/site-packages/torchvision
torchvision arch flags  sm_35, sm_50, sm_60, sm_70, sm_75
fvcore                  0.1.1.post20200608
cv2                     4.2.0
----------------------  ----------------------------------------------------------------------------------------
PyTorch built with:
  - GCC 7.3
  - Intel(R) Math Kernel Library Version 2020.0.0 Product Build 20191122 for Intel(R) 64 architecture applications
  - Intel(R) MKL-DNN v0.21.1 (Git Hash 7d2fd500bc78936d1d648ca713b901012f470dbc)
  - OpenMP 201511 (a.k.a. OpenMP 4.5)
  - NNPACK is enabled
  - CUDA Runtime 10.1
  - NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_75,code=sm_75;-gencode;arch=compute_37,code=compute_37
  - CuDNN 7.6.3
  - Magma 2.5.1
  - Build settings: BLAS=MKL, BUILD_NAMEDTENSOR=OFF, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Wno-stringop-overflow, DISABLE_NUMA=1, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=ON, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF,

Try

import torch, torchvision
import detectron2
from detectron2.utils.logger import setup_logger
setup_logger()
from detectron2 import model_zoo