Object Detection

Detecting Objects in Images

Object Detection is a fundamental task in Computer Vision that goes beyond simple image classification. While classification merely states “there is a dog in the image”, detection states “there is a dog here and a cat there”, drawing bounding boxes.

⚙️ How it works: Practical Context

The task involves two simultaneous predictions:

  1. Regression: Predicting the coordinates of the bounding box (center x, center y, width, height).
  2. Classification: Predicting the class (label) of the object inside that box.

There are two major families:

  • Two-Stage Detectors: (e.g., Faster R-CNN) Propose regions (RPN) and then classify/refine them using roi-pooling. Slower but highly accurate.
  • One-Stage Detectors: (e.g., YOLO, SSD) Treat detection as a single regression problem on a grid. Extremely fast, but with slight difficulty regarding small and clustered objects.

🧮 Mathematical Foundation (Multi-task Loss Function)

Because the model needs to optimize two distinct things at the same time, the total loss function (LtotalL_{total}) is a linear combination of the classification loss (LclsL_{cls}) and the bounding box regression loss (LlocL_{loc}):

Ltotal(p,u,t,v)=Lcls(p,u)+λ[u1]Lloc(t,v)L_{total}(p, u, t, v) = L_{cls}(p, u) + \lambda \cdot [u \ge 1] L_{loc}(t, v)

Eq. 1: Multi-task Loss function.

Where:

  • pp is the predicted probability distribution (calculated via Softmax).
  • uu is the true object class (u1u \ge 1 means it’s an object, u=0u=0 indicates background).
  • tt is the predicted coordinate vector (tx,ty,tw,th)(t_x, t_y, t_w, t_h).
  • vv is the true ground truth bounding box vector.
  • λ\lambda is the balancing hyperparameter.

1. Classification Loss (Cross-Entropy)

Usually computed with standard logarithmic cross-entropy over K+1K+1 classes (where +1+1 is the background):

Lcls(p,u)=logpuL_{cls}(p, u) = -\log p_u

Eq. 2: Cross-entropy loss for classification.

Where:

  • Lcls(p,u)L_{cls}(p, u): classification loss for the object.
  • pup_u: probability predicted by the model for the correct class uu.

2. Regression Loss (Smooth L1 / Huber Loss)

For localization, using Mean Squared Error (L2L_2) can cause gradients to explode on early outliers. Thus, we use the Smooth L1L_1 Loss, which is quadratic for small errors (smoothing convergence) and linear for large errors (outlier resistance):

Lloc(t,v)=i{x,y,w,h}smoothL1(tivi)L_{loc}(t, v) = \sum_{i \in \{x,y,w,h\}} \text{smooth}_{L_1}(t_i - v_i)

Eq. 3: Smooth L1 regression loss for bounding box.

Where:

smoothL1(x)={0.5x2if x<1x0.5otherwise\text{smooth}_{L_1}(x) = \begin{cases} 0.5 x^2 & \text{if } |x| < 1 \\ |x| - 0.5 & \text{otherwise} \end{cases}

🎯 When to use

  • Surveillance and tracking systems.
  • Autonomous vehicles (detecting pedestrians, signs).
  • Medical image analysis (identifying tumors in scans).

Related: roi-pooling · edge-detection-image-processing

Built with Eleventy · search by Lunr.js