Book Image

Hands-On Computer Vision with Detectron2

By : Van Vung Pham
5 (4)
Book Image

Hands-On Computer Vision with Detectron2

5 (4)
By: Van Vung Pham

Overview of this book

Computer vision is a crucial component of many modern businesses, including automobiles, robotics, and manufacturing, and its market is growing rapidly. This book helps you explore Detectron2, Facebook's next-gen library providing cutting-edge detection and segmentation algorithms. It’s used in research and practical projects at Facebook to support computer vision tasks, and its models can be exported to TorchScript or ONNX for deployment. The book provides you with step-by-step guidance on using existing models in Detectron2 for computer vision tasks (object detection, instance segmentation, key-point detection, semantic detection, and panoptic segmentation). You’ll get to grips with the theories and visualizations of Detectron2’s architecture and learn how each module in Detectron2 works. As you advance, you’ll build your practical skills by working on two real-life projects (preparing data, training models, fine-tuning models, and deployments) for object detection and instance segmentation tasks using Detectron2. Finally, you’ll deploy Detectron2 models into production and develop Detectron2 applications for mobile devices. By the end of this deep learning book, you’ll have gained sound theoretical knowledge and useful hands-on skills to help you solve advanced computer vision tasks using Detectron2.
Table of Contents (20 chapters)
1
Part 1: Introduction to Detectron2
4
Part 2: Developing Custom Object Detection Models
12
Part 3: Developing a Custom Detectron2 Model for Instance Segmentation Tasks
15
Part 4: Deploying Detectron2 Models into Production

Detectron2 development environments

Now, we understand the advanced CV tasks and how Detectron2 helps to develop applications for these tasks. It is time to start developing Detectron2 applications. This section provides steps to set up Detectron2 development environments on the cloud using Google Colab, a local environment, or a hybrid approach connecting Google Colab to a locally hosted runtime.

Cloud development environment for Detectron2 applications

Google Colab or Colaboratory (https://colab.research.google.com) is a cloud platform that allows you to write and execute Python code from your web browser. It enables users to start developing deep learning applications with zero configuration because most common machine learning and deep learning packages, such as PyTorch and TensorFlow, are pre-installed. Furthermore, users will have access to GPUs free of charge. Even with the free plan, users have access to a computation resource that is relatively better than a standard personal computer. Users can pay a small amount for Pro or Pro+ with higher computation resources if needed. Additionally, as its name indicates, it is relatively easy to collaborate on Google Colab, and it is easy to share Google Colab files and projects.

Deep learning models for CV tasks work with many images; thus, GPUs significantly speed up the training and inferencing time. However, by default, Google Colab does not enable GPUs' runtime. Therefore, users should enable the GPU hardware accelerator before installing Detectron2 or training Detectron2 applications. This step is to select GPU from the Hardware accelerator drop-down menu found under Runtime | Change runtime type, as shown in Figure 1.3:

Figure 1.3: Select GPU for Hardware accelerator

Figure 1.3: Select GPU for Hardware accelerator

Detectron2 has a dedicated tutorial on how to install Detectron2 on Google Colab. However, this section discusses each step and gives further details about these. First, Detectron2 is built on top of PyTorch, so we need to have PyTorch installed. By default, Google Colab runtime already installs PyTorch. So, you can use the following snippet to install Detectron2 on Google Colab:

!python -m pip install \
'git+https://github.com/facebookresearch/detectron2.git'

If you have an error message such as the following one, it is safe to ignore it and proceed:

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
flask 1.1.4 requires click<8.0,>=5.1, but you have click 8.1.3 which is incompatible.

However, if you face problems such as PyTorch versions on Google Colab, they may not be compatible with Detectron2. Then, you can install Detectron2 for specific versions of PyTorch and CUDA. You can use the following snippet to get PyTorch and CUDA versions:

import torch
TORCH_VERSION = ".".join(torch.__version__.split(".")[:2])
CUDA_VERSION = torch.__version__.split("+")[-1]
print("torch: ", TORCH_VERSION, "; cuda: ", CUDA_VERSION)

After understanding the PyTorch and CUDA versions, you can use the following snippet to install Detectron2. Please remember to replace TORCH_VERSION and CUDA_VERSION with the values found in the previous snippet:

!python -m pip install detectron2 -f \
https://dl.fbaipublicfiles.com/detectron2/wheels/{TORCH_VERSION}/{CUDA_VERSION}/index.html

Here is an example of such an installation command for CUDA version 11.3 and PyTorch version 1.10:

!python -m pip install detectron2 -f \
https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html

If you face an error such as the following, it means that there is no matching Detectron2 distribution for the current versions of PyTorch and CUDA:

ERROR: Could not find a version that satisfies the requirement detectron2 (from versions: none)
ERROR: No matching distribution found for detectron2

In this case, you can visit the Detectron2 installation page to find the distributions compatible with the current PyTorch and CUDA versions. This page is available at https://detectron2.readthedocs.io/en/latest/tutorials/install.html.

Figure 1.4 shows the current Detectron2 distributions with corresponding CUDA/CPU and PyTorch versions:

Figure 1.4: Current Detectron2 distributions for corresponding CUDA/CPU and PyTorch versions

Figure 1.4: Current Detectron2 distributions for corresponding CUDA/CPU and PyTorch versions

Suppose Detectron2 does not have a distribution that matches your current CUDA and PyTorch versions. Then, there are two options. The first option is to select the Detectron2 version with CUDA and PyTorch versions that are closest to the ones that you have. This approach should generally work. Otherwise, you can install the CUDA and PyTorch versions that Detectron2 supports.

Finally, you can use the following snippet to check the installed Detectron2 version:

import detectron2
print(detectron2.__version__)

Congratulations! You are now ready to develop CV applications using Detectron2 on Google Colab. Read on if you want to create Detectron2 applications on a local machine. Otherwise, you can go to Chapter 2 to start developing Detectron2 CV applications.

Local development environment for Detectron2 applications

Google Colab is an excellent cloud environment to quickly start building deep learning applications. However, it has several limitations. For instance, the free Google Colab plan may not have enough RAM and GPU resources for large projects. Another limitation is that your runtime may terminate if your kernel is idle for a while. Even in the purchased Pro+ plan, a Google Colab kernel can only run for 24 hours, after which it is terminated. That said, if you have a computer with GPUs, it is better to install Detectron2 on this local computer for development.

Important note – resume training option

Due to time limitations, Google Colab may terminate your runtime before your training completes. Therefore, you should train your models with a resumable option so that the Detectron2 training process can pick up the stored weights from its previous training run. Fortunately, Detectron2 supports a resumable training option so that you can do this easily.

At the time of writing this book, Detectron2 supports Linux and does not officially support Windows. You may refer to its installation page for some workarounds at https://detectron2.readthedocs.io/en/latest/tutorials/install.html if you want to install Detectron2 on Windows. This section covers the steps to install Detectron2 on Linux. Detectron2 is built on top of PyTorch. Therefore, the main installation requirement (besides Python itself) is PyTorch. Please refer to PyTorch’s official page at https://pytorch.org/ to perform the installation. Figure 1.5 shows the interface to select appropriate configurations for your current system and generate a PyTorch installation command at the bottom.

Figure 1.5: PyTorch installation command generator (https://pytorch.org)

Figure 1.5: PyTorch installation command generator (https://pytorch.org)

The next installation requirement is to install Git to install Detectron2 from source. Git is also a tool that any software developer should have. Especially since we are developing relatively complex CV applications, this tool is valuable. You can use the following steps to install and check the installed Git version from the Terminal:

$ sudo apt-get update
$ sudo apt-get install git
$ git --version

Once PyTorch and Git are installed, the steps to install Detectron2 on a local computer are the same as those used to install Detectron2 on Google Colab, described in the previous section.

Connecting Google Colab to a local development environment

There are cases where developers have developed some code with Google Colab, or they may want to use files stored on Google Drive or prefer to code with the Google Colab interface more than the standard Jupyter notebook on a local computer. In these cases, Google Colab provides an option to execute its notebook in a local environment (or other hosted runtimes such as Google Cloud instances). Google Colab has instructions for this available here: https://research.google.com/colaboratory/local-runtimes.html.

Important note – browser-specific settings

The following steps are for Google Chrome. If you are using Firefox, you must perform custom settings to allow connections from HTTPS domains with standard WebSockets. The instructions are available here: https://research.google.com/colaboratory/local-runtimes.html.

We will first need to install Jupyter on the local computer. The next step is to enable the jupyter_http_over_ws Jupyter extension using the following snippet:

$ pip install jupyter_http_over_ws
$ jupyter serverextension enable --py jupyter_http_over_ws

The next step is to start the Jupyter server on the local machine with an option to trust the WebSocket connections so that the Google Colab notebook can connect to the local runtime, using the following snippet:

$ jupyter notebook \
--NotebookApp.allow_origin=\
'https://colab.research.google.com' \
--port=8888 \
--NotebookApp.port_retries=0

Once the local Jupyter server is running, in the Terminal, there is a backend URL with an authentication token that can be used to access this local runtime from Google Colab. Figure 1.6 shows the steps to connect the Google Colab notebook to a local runtime: Connect | Connect to a local runtime:

Figure 1.6: Connecting the Google Colab notebook to a local runtime

Figure 1.6: Connecting the Google Colab notebook to a local runtime

On the next dialog, enter the backend URL generated in the local Jupyter server and click the Connect button. Congratulations! You can now use the Google Colab notebook to code Python applications using a local kernel.