Object Detection Yolo

# Object Detection & YOLO

## Introduction & Motivation

Object Detection: localize and classify objects in images. Real-time detection; single-stage detectors. Applications: autonomous driving, surveillance, robotics.

Motivation: Fast, accurate object localization at scale.

Applications: Real-time detection, embedded systems.

---

## Core Concepts & Theory

### Anchor-Based Detection

Predefined anchor boxes at multiple scales.

### Single-Stage Detectors

YOLO, SSD: direct bbox + class prediction.

### Two-Stage Detectors

R-CNN, Faster R-CNN: region proposal + refinement.

### NMS (Non-Maximum Suppression)

Remove duplicate detections.

---

## Mathematical Formulation

YOLO Loss:
$$L = \lambda_{coord} \sum (x - \hat{x})^2 + \lambda_{obj} \sum (C - \hat{C})^2 + \sum (p - \hat{p})^2$$

IoU (Intersection over Union):
$$ ext{IoU} = \frac{ ext{Area}(B_p \cap B_g)}{ ext{Area}(B_p \cup B_g)}$$

Anchor Box:
$$ ext{bbox} = (t_x, t_y, t_w, t_h) ext{ relative to anchor}$$

---

## Advanced Theory & Extensions

### YOLOv3

Multi-scale predictions; residual connections.

### EfficientDet

Compound scaling for efficiency.

### Focal Loss

Handle class imbalance in one-stage detectors.

---

## Computational Considerations

YOLO: O(h·w·anchors).

Feature Pyramid: O(multi_scale).

NMS: O(N log N).

---

## Practical Implementation Strategies

### Multi-Scale Training

Varying input sizes for robustness.

### Anchor Design

Data-driven anchor cluster analysis via k-means.

### Loss Weighting

Balance localization, objectness, classification losses.

---

## Benchmark Datasets & Evaluation

COCO: 330K images, 80 object classes, 2.5M instances.

PASCAL VOC: 16K images, 20 classes.

ImageNet: 1K classes with bounding boxes.

---

## Key Challenges & Limitations

### Small Object Detection

Limited resolution for tiny objects.

### Class Imbalance

Dominant background class.

### Real-Time Inference

Memory and latency constraints.

---

## Hyperparameter Tuning

Anchor aspect ratios: 0.5, 1, 2, 3.

Confidence threshold: 0.3-0.5.

NMS IoU threshold: 0.3-0.5.

---

## Real-World Applications & Case Studies

Autonomous Vehicles: Pedestrian, vehicle, sign detection.

Retail: Inventory tracking, shelf monitoring.

Safety: Hard hat, safety vest detection.

---

## Integration with Other Methods

Object detection + tracking for video; + attention for focusing on relevant regions.

---

## Summary & Key Takeaways

Object Detection via single-stage detectors enables real-time localization and classification.

Principles:
1. Anchor design: Multi-scale coverage.
2. Direct regression: Coordinates + classes.
3. Loss combination: Localization + objectness + classification.
4. NMS: Duplicate removal.
5. Efficiency: Real-time inference.

---

---

## Appendix: Practical Labs

### Lab 1: IoU Calculation

import numpy as np

def compute_iou(bbox1, bbox2):
 """Compute Intersection over Union"""
 x1_min, y1_min, x1_max, y1_max = bbox1
 x2_min, y2_min, x2_max, y2_max = bbox2
 
 # Intersection
 inter_x_min = max(x1_min, x2_min)
 inter_y_min = max(y1_min, y2_min)
 inter_x_max = min(x1_max, x2_max)
 inter_y_max = min(y1_max, y2_max)
 
 inter_area = max(0, inter_x_max - inter_x_min) * max(0, inter_y_max - inter_y_min)
 
 # Union
 box1_area = (x1_max - x1_min) * (y1_max - y1_min)
 box2_area = (x2_max - x2_min) * (y2_max - y2_min)
 union_area = box1_area + box2_area - inter_area
 
 iou = inter_area / (union_area + 1e-8)
 return iou

# Test
bbox1 = (0, 0, 10, 10)
bbox2 = (5, 5, 15, 15)

iou = compute_iou(bbox1, bbox2)

assert 0 <= iou <= 1, "IoU in range"
print("✓ IoU calculation working")

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

### Lab 2: NMS (Non-Maximum Suppression)

import numpy as np

def nms(detections, iou_threshold=0.5):
 """Non-Maximum Suppression"""
 if len(detections) == 0:
 return []
 
 # Sort by confidence
 detections = sorted(detections, key=lambda x: x[4], reverse=True)
 keep = []
 
 while len(detections) > 0:
 keep.append(detections[0])
 
 if len(detections) == 1:
 break
 
 # Compute IoU with remaining
 ious = []
 for det in detections[1:]:
 bbox1 = detections[0][:4]
 bbox2 = det[:4]
 
 x1_min, y1_min, x1_max, y1_max = bbox1
 x2_min, y2_min, x2_max, y2_max = bbox2
 
 inter_x_min = max(x1_min, x2_min)
 inter_y_min = max(y1_min, y2_min)
 inter_x_max = min(x1_max, x2_max)
 inter_y_max = min(y1_max, y2_max)
 
 inter_area = max(0, inter_x_max - inter_x_min) * max(0, inter_y_max - inter_y_min)
 
 box1_area = (x1_max - x1_min) * (y1_max - y1_min)
 box2_area = (x2_max - x2_min) * (y2_max - y2_min)
 union_area = box1_area + box2_area - inter_area
 
 iou = inter_area / (union_area + 1e-8)
 ious.append(iou)
 
 # Keep detections below threshold
 detections = [detections[i+1] for i, iou in enumerate(ious) if iou < iou_threshold]
 
 return keep

# Test
detections = [(0, 0, 10, 10, 0.9), (1, 1, 11, 11, 0.8), (20, 20, 30, 30, 0.7)]

keep = nms(detections)

assert len(keep) <= len(detections), "NMS reduces detections"
print("✓ NMS working")

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

### Lab 3: YOLO Loss

import numpy as np

def yolo_loss(pred_boxes, pred_conf, pred_class, true_boxes, true_conf, true_class, lambda_coord=5, lambda_obj=1):
 """YOLO loss function"""
 # Localization loss
 loc_loss = lambda_coord * np.sum((pred_boxes - true_boxes) ** 2)
 
 # Confidence loss
 conf_loss = lambda_obj * np.sum((pred_conf - true_conf) ** 2)
 
 # Classification loss
 class_loss = np.sum((pred_class - true_class) ** 2)
 
 total_loss = loc_loss + conf_loss + class_loss
 return total_loss

# Test
np.random.seed(42)
pred_boxes = np.random.randn(7, 7, 4)
pred_conf = np.random.rand(7, 7)
pred_class = np.random.rand(7, 7, 80)

true_boxes = np.random.randn(7, 7, 4)
true_conf = np.random.rand(7, 7)
true_class = np.random.rand(7, 7, 80)

loss = yolo_loss(pred_boxes, pred_conf, pred_class, true_boxes, true_conf, true_class)

assert np.isfinite(loss), "Loss finite"
print("✓ YOLO loss working")

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

### Lab 4: Anchor Box Generation

import numpy as np

def generate_anchors(scales, aspect_ratios):
 """Generate anchor boxes"""
 anchors = []
 
 for scale in scales:
 for ar in aspect_ratios:
 w = scale * np.sqrt(ar)
 h = scale / np.sqrt(ar)
 anchors.append((w, h))
 
 return np.array(anchors)

# Test
scales = [0.5, 1, 2]
aspect_ratios = [0.5, 1, 2]

anchors = generate_anchors(scales, aspect_ratios)

assert anchors.shape[0] == len(scales) * len(aspect_ratios), "Correct anchor count"
print("✓ Anchor generation working")

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

Go deeper with CFSGPT

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

Create Free Account