AI-For-Beginners/translations/en/lessons/4-ComputerVision/11-ObjectDetection/README.md

10 KiB
Raw Blame History

Object Detection

The image classification models we've explored so far take an image as input and produce a categorical output, such as identifying the class 'number' in the MNIST dataset. However, in many scenarios, it's not enough to simply know that an image contains certain objects—we also want to pinpoint their exact locations. This is the purpose of object detection.

Pre-lecture quiz

Object Detection

Image from YOLO v2 website

A Naive Approach to Object Detection

Imagine we want to locate a cat in an image. A very simplistic approach to object detection might look like this:

  1. Divide the image into a grid of smaller tiles.
  2. Perform image classification on each tile.
  3. Identify tiles with sufficiently high activation as containing the object of interest.

Naive Object Detection

Image from Exercise Notebook

However, this method is far from ideal because it only provides a rough estimate of the object's bounding box. For more precise localization, we need to use regression to predict the bounding box coordinates—and for that, we require specialized datasets.

Regression for Object Detection

This blog post offers an excellent introduction to detecting shapes.

Datasets for Object Detection

Here are some commonly used datasets for object detection:

  • PASCAL VOC - 20 classes
  • COCO - Common Objects in Context. Includes 80 classes, bounding boxes, and segmentation masks.

COCO

Object Detection Metrics

Intersection over Union

While evaluating image classification models is straightforward, object detection requires assessing both the accuracy of the predicted class and the precision of the bounding box location. For the latter, we use Intersection over Union (IoU), which measures the overlap between two bounding boxes (or areas).

IoU

Figure 2 from this excellent blog post on IoU

The concept is simple: divide the area of intersection between two shapes by the area of their union. For two identical shapes, IoU equals 1. For completely non-overlapping shapes, IoU equals 0. Typically, we only consider bounding boxes with IoU above a certain threshold.

Average Precision

To evaluate how well a specific object class C is detected, we use the Average Precision metric, calculated as follows:

  1. Plot a Precision-Recall curve, which shows accuracy as a function of the detection threshold (ranging from 0 to 1).
  2. Depending on the threshold, the number of detected objects and the precision-recall values will vary.
  3. The resulting curve looks like this:

Image from NeuroWorkshop

The Average Precision for a class C is the area under this curve. More specifically, the Recall axis is divided into 10 segments, and Precision is averaged across these points:


AP = {1\over11}\sum_{i=0}^{10}\mbox{Precision}(\mbox{Recall}={i\over10})

AP and IoU

We only consider detections with IoU above a certain threshold. For example, the PASCAL VOC dataset typically uses \mbox{IoU Threshold} = 0.5, while COCO evaluates AP across multiple \mbox{IoU Threshold} values.

Image from NeuroWorkshop

Mean Average Precision - mAP

The primary metric for object detection is Mean Average Precision (mAP). This is the Average Precision averaged across all object classes, and sometimes across multiple \mbox{IoU Threshold} values. The process of calculating mAP is explained in detail in this blog post and here with code examples.

Different Object Detection Approaches

Object detection algorithms can be broadly categorized into two types:

  • Region Proposal Networks (R-CNN, Fast R-CNN, Faster R-CNN): These methods generate Regions of Interest (ROIs) and run a CNN over them to find the highest activations. This approach is somewhat similar to the naive method but uses more sophisticated ROI generation. A major drawback is that these methods are slow because the CNN classifier must process the image multiple times.
  • One-pass methods (YOLO, SSD, RetinaNet): These architectures predict both object classes and ROIs in a single pass.

R-CNN: Region-Based CNN

R-CNN uses Selective Search to generate a hierarchical structure of ROIs. These ROIs are passed through CNN feature extractors and SVM classifiers to determine object classes, while linear regression predicts the bounding box coordinates. Official Paper

RCNN

Image from van de Sande et al. ICCV11

RCNN-1

Images from this blog

F-RCNN - Fast R-CNN

This method is similar to R-CNN, but the regions are defined after applying convolutional layers.

FRCNN

Image from the Official Paper, arXiv, 2015

Faster R-CNN

This approach introduces a neural network to predict ROIs, known as the Region Proposal Network. Paper, 2016

FasterRCNN

Image from the official paper

R-FCN: Region-Based Fully Convolutional Network

This algorithm is faster than Faster R-CNN. The key idea is:

  1. Extract features using ResNet-101.
  2. Process features with a Position-Sensitive Score Map. Each object from C classes is divided into k\times k regions, and the network predicts parts of objects.
  3. For each part of the k\times k regions, the networks vote for object classes, and the class with the highest vote is selected.

r-fcn image

Image from official paper

YOLO - You Only Look Once

YOLO is a real-time, one-pass algorithm. The main idea is:

  • Divide the image into S\times S regions.
  • For each region, the CNN predicts n possible objects, bounding box coordinates, and confidence = probability × IoU.

YOLO

Image from official paper

Other Algorithms

✍️ Exercises: Object Detection

Continue your learning in the following notebook:

ObjectDetection.ipynb

Conclusion

In this lesson, you explored a variety of approaches to object detection!

🚀 Challenge

Explore these articles and notebooks about YOLO and try implementing them:

Post-lecture quiz

Review & Self Study

Assignment: Object Detection