Object Detection Yolo Faster R-CNN Ssd

# Object Detection: YOLO, Faster R-CNN & SSD

## Introduction & Motivation

Object detection: localize and classify objects. YOLO: single-shot regression; real-time speed. Faster R-CNN: region-based; high accuracy. SSD: multi-scale predictions; balanced speed-accuracy. Applications: surveillance, autonomous driving, robotics.

Motivation: Classification insufficient; need localization. Real-time detection crucial for applications.

Applications: Autonomous driving, surveillance, robotics.

---

## Core Concepts & Theory

### Bounding Box Regression

Predict offsets; localization refinement.

### Region Proposal Networks

Generate candidate regions; RPN efficiency.

### Anchor Boxes

Prior boxes; multi-scale coverage.

---

## Mathematical Formulation

Bounding box loss:
$$L_{ ext{loc}} = \sum_{ij} x_{ij}^p ext{SmoothL1}(l_i^p - g_i^p)$$

where x = confidence, l = predicted, g = ground truth.

Objectness loss:
$$L_{ ext{obj}} = \sum_i (c_i^p - c_i^*)^2$$

Classification loss:
$$L_{ ext{cls}} = \sum_c L_{ ext{softmax}}(p_c, q_c)$$

---

## Advanced Theory & Extensions

### Feature Pyramid Network

Multi-scale features; FPN.

### Focal Loss

Address foreground-background imbalance; RetinaNet.

### Non-Maximum Suppression

Remove duplicate detections; NMS.

---

## Computational Considerations

YOLO: O(S²·B·(5+C)) single pass; S = grid size, B = boxes, C = classes.

Faster R-CNN: O(RPN + ROI) two-stage; slower but more accurate.

SSD: O(M·pyramids) multi-scale; balanced.

---

## Practical Implementation Strategies

### Anchor Design

Aspect ratios, scales; data-dependent.

### Hard Negative Mining

Focus on background false positives.

### Input Normalization

Standardize pixel values; training stability.

---

## Benchmark Datasets & Evaluation

COCO: Large-scale benchmark; multiple metrics.

Pascal VOC: Classical benchmark; mAP metric.

ImageNet: Object detection challenge.

---

## Key Challenges & Limitations

### Small Object Detection

Difficult; feature resolution loss.

### Background Clutter

False positives; foreground-background imbalance.

### Real-Time Inference

Latency constraints; model compression.

---

## Hyperparameter Tuning

Anchor scales: [128, 256, 512]; data-dependent.

Aspect ratios: [0.5, 1, 2]; coverage.

NMS threshold: 0.5 typical; overlap tolerance.

---

## Real-World Applications & Case Studies

Autonomous Driving: Real-time detection; YOLO deployed.

Surveillance: Multi-object tracking; Faster R-CNN.

Medical Imaging: Lesion detection; SSD variants.

---

## Integration with Other Methods

Detection + Tracking → MOT (multi-object tracking).

Detection + Segmentation → Instance seg.

---

## Summary & Key Takeaways

Object detection via YOLO, Faster R-CNN, and SSD provides localization and classification through single-stage, region-based, and multi-scale approaches.

Principles:
1. YOLO: single-shot; real-time.
2. Faster R-CNN: region-based; accurate.
3. SSD: multi-scale; balanced.
4. Anchors: prior boxes.
5. NMS: duplicate removal.

---

---

## Appendix: Practical Labs

### Lab 1: Bounding Box Regression

import numpy as np

def compute_iou(box1, box2):
 """Compute IoU between two boxes"""
 x1_min, y1_min, x1_max, y1_max = box1
 x2_min, y2_min, x2_max, y2_max = box2
 
 intersection = max(0, min(x1_max, x2_max) - max(x1_min, x2_min)) * \
 max(0, min(y1_max, y2_max) - max(y1_min, y2_min))
 
 area1 = (x1_max - x1_min) * (y1_max - y1_min)
 area2 = (x2_max - x2_min) * (y2_max - y2_min)
 union = area1 + area2 - intersection
 
 return intersection / (union + 1e-8)

def bbox_regression_loss(predicted_box, true_box):
 """SmoothL1 loss for bbox regression"""
 diff = predicted_box - true_box
 loss = np.where(np.abs(diff) < 1, 0.5 * diff**2, np.abs(diff) - 0.5)
 return loss.mean()

# Test
np.random.seed(42)
box1 = np.array([10, 10, 50, 50])
box2 = np.array([15, 15, 55, 55])

iou = compute_iou(box1, box2)
assert 0 <= iou <= 1, "IoU in [0,1]"

loss = bbox_regression_loss(box1, box2)
assert np.isfinite(loss), "Loss finite"
print("✓ Bbox regression working")

if __name__ == "__main__":
 print("Lab 1: BboxRegression - PASSED")

### Lab 2: Anchor Boxes

import numpy as np

def generate_anchor_boxes(feature_map_size=7, anchor_scales=[128, 256, 512], 
 anchor_ratios=[0.5, 1, 2], img_size=224):
 """Generate anchor boxes"""
 stride = img_size // feature_map_size
 
 anchors = []
 
 for i in range(feature_map_size):
 for j in range(feature_map_size):
 cx = (j + 0.5) * stride
 cy = (i + 0.5) * stride
 
 for scale in anchor_scales:
 for ratio in anchor_ratios:
 w = scale * np.sqrt(ratio)
 h = scale / np.sqrt(ratio)
 
 box = [cx - w/2, cy - h/2, cx + w/2, cy + h/2]
 anchors.append(box)
 
 return np.array(anchors)

# Test
np.random.seed(42)
anchors = generate_anchor_boxes(feature_map_size=7, anchor_scales=[128, 256],
 anchor_ratios=[0.5, 1], img_size=224)

expected_num = 7 * 7 * 2 * 2 # feature_map² × scales × ratios
assert anchors.shape[0] == expected_num, "Num anchors"
assert anchors.shape[1] == 4, "Box format"
print("✓ Anchor box generation working")

if __name__ == "__main__":
 print("Lab 2: AnchorBoxes - PASSED")

### Lab 3: Non-Maximum Suppression

import numpy as np

def nms(boxes, scores, iou_threshold=0.5):
 """Non-maximum suppression"""
 if len(boxes) == 0:
 return []
 
 # Sort by score
 sorted_indices = np.argsort(-scores)
 
 keep = []
 
 while len(sorted_indices) > 0:
 # Keep highest score
 current = sorted_indices[0]
 keep.append(current)
 
 if len(sorted_indices) == 1:
 break
 
 # Compute IoU with remaining
 current_box = boxes[current]
 remaining_boxes = boxes[sorted_indices[1:]]
 
 ious = []
 for other_box in remaining_boxes:
 x1_min, y1_min, x1_max, y1_max = current_box
 x2_min, y2_min, x2_max, y2_max = other_box
 
 intersection = max(0, min(x1_max, x2_max) - max(x1_min, x2_min)) * \
 max(0, min(y1_max, y2_max) - max(y1_min, y2_min))
 
 area1 = (x1_max - x1_min) * (y1_max - y1_min)
 area2 = (x2_max - x2_min) * (y2_max - y2_min)
 union = area1 + area2 - intersection
 
 iou = intersection / (union + 1e-8)
 ious.append(iou)
 
 # Keep low IoU
 keep_indices = [i for i, iou in enumerate(ious) if iou <= iou_threshold]
 sorted_indices = sorted_indices[1:][keep_indices]
 
 return keep

# Test
np.random.seed(42)
boxes = np.array([[10, 10, 50, 50], [15, 15, 55, 55], [100, 100, 150, 150]])
scores = np.array([0.9, 0.8, 0.85])

keep_indices = nms(boxes, scores)

assert len(keep_indices) <= len(boxes), "NMS reduced"
print("✓ Non-maximum suppression working")

if __name__ == "__main__":
 print("Lab 3: NMS - PASSED")

### Lab 4: Objectness Loss

import numpy as np

def compute_objectness_loss(predicted_conf, true_conf, positive_weight=1.0):
 """Binary cross-entropy for objectness"""
 # Handle positive and negative samples differently
 loss = np.zeros_like(predicted_conf)
 
 # Positive samples
 positive_mask = true_conf == 1
 loss[positive_mask] = positive_weight * (-(np.log(predicted_conf[positive_mask] + 1e-8)))
 
 # Negative samples
 negative_mask = true_conf == 0
 loss[negative_mask] = 0.1 * (-(np.log(1 - predicted_conf[negative_mask] + 1e-8)))
 
 return loss.mean()

# Test
np.random.seed(42)
predicted = np.random.rand(100)
true = np.random.randint(0, 2, 100)

loss = compute_objectness_loss(predicted, true)

assert np.isfinite(loss), "Loss finite"
assert loss > 0, "Loss positive"
print("✓ Objectness loss working")

if __name__ == "__main__":
 print("Lab 4: ObjectnessLoss - PASSED")

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account