Object Detection - Yolo Ssd

# Object Detection - YOLO & SSD

## Introduction & Motivation

YOLO & SSD: single-stage object detection. Real-time inference without region proposals. Applications: video surveillance, autonomous driving, real-time systems.

Motivation: Detect objects without multi-stage pipeline.

Applications: Real-time detection, video analysis, autonomous systems.

---

## Core Concepts & Theory

### Single-Stage Detection

End-to-end bounding box prediction.

### Anchor Boxes

Pre-defined box templates.

### Multi-Scale Predictions

Detect objects at different scales.

### Loss Functions

Combined localization and classification.

---

## Mathematical Formulation

YOLO Loss:
$$\mathcal{L} = \lambda_{ ext{coord}} \sum_{ij} \mathbb{1}_{ij}^{ ext{obj}}[(x_i - \hat{x}_i)^2 + (y_i - \hat{y}_i)^2] + ...$$

SSD Loss:
$$\mathcal{L} = \frac{1}{N}(\mathcal{L}_{ ext{conf}} + \alpha \mathcal{L}_{ ext{loc}})$$

Localization:
$$\mathcal{L}_{ ext{loc}} = \sum_i ext{Smooth}_{L1}( ext{loc}_i - ext{gt}_i)$$

---

## Advanced Theory & Extensions

### Anchor-Free Detection

No predefined anchors.

### Feature Pyramid Networks

Multi-scale feature fusion.

### Hard Negative Mining

Focus on challenging examples.

---

## Computational Considerations

Inference: O(H·W·A·C).

NMS: O(N·\log N).

Speed: Real-time on GPU.

---

## Practical Implementation Strategies

### Anchor Design

Choose appropriate scales/ratios.

### Confidence Threshold

Filter low-confidence predictions.

### NMS Strategy

Suppress overlapping boxes.

---

## Benchmark Datasets & Evaluation

COCO: Object detection benchmark.

Pascal VOC: Classic benchmark.

ImageNet: Large-scale objects.

---

## Key Challenges & Limitations

### Small Objects

Detection difficulty.

### Class Imbalance

Background dominance.

### Speed-Accuracy Trade-off

Balance real-time vs precision.

---

## Hyperparameter Tuning

Anchor scales: 32, 64, 128, 256, 512.

Confidence threshold: 0.5-0.7.

NMS threshold: 0.45.

---

## Real-World Applications & Case Studies

Autonomous Driving: Real-time vehicle detection.

Video Surveillance: Object tracking.

Industrial Inspection: Quality control.

---

## Integration with Other Methods

YOLO/SSD + multi-scale; + hard negative mining for better learning.

---

## Summary & Key Takeaways

YOLO and SSD enable efficient single-stage detection.

Principles:
1. Single-stage: End-to-end prediction.
2. Anchor boxes: Predefined templates.
3. Multi-scale: Feature pyramids.
4. Real-time: GPU-optimized inference.
5. Efficiency: Trade-off for speed.

---

## Appendix: Practical Labs

### Lab 1: Anchor Box Generation

import numpy as np

def generate_anchors(scales, ratios, base_size=32):
 """Generate anchor boxes for single scale"""
 anchors = []
 
 for scale in scales:
 for ratio in ratios:
 w = base_size * scale * np.sqrt(1/ratio)
 h = base_size * scale * np.sqrt(ratio)
 
 anchors.append([-w/2, -h/2, w/2, h/2])
 
 return np.array(anchors)

anchors = generate_anchors([1, 2], [0.5, 1, 2])
assert anchors.shape[0] == 6
print("✓ Anchor generation working")

### Lab 2: YOLO Loss

import numpy as np

def yolo_loss(pred_boxes, pred_conf, pred_cls, gt_boxes, gt_cls, num_classes):
 """Compute YOLO loss"""
 # Localization loss
 loc_loss = np.sum((pred_boxes - gt_boxes) ** 2)
 
 # Confidence loss
 conf_loss = -np.mean(gt_boxes[:, 0] * np.log(pred_conf + 1e-8) +
 (1 - gt_boxes[:, 0]) * np.log(1 - pred_conf + 1e-8))
 
 # Classification loss
 cls_loss = -np.mean(gt_cls * np.log(pred_cls + 1e-8))
 
 total = loc_loss + 5 * conf_loss + cls_loss
 return total

np.random.seed(42)
pred_box = np.random.randn(10, 4)
pred_conf = np.random.rand(10, 1)
pred_cls = np.random.rand(10, 20)
gt_box = np.random.randn(10, 4)
gt_cls = np.eye(20)[np.random.randint(0, 20, 10)]

loss = yolo_loss(pred_box, pred_conf, pred_cls, gt_box, gt_cls, 20)
assert loss > 0
print(f"✓ YOLO loss: {loss:.2f}")

### Lab 3: Non-Maximum Suppression

import numpy as np

def nms(boxes, scores, iou_threshold=0.5):
 """Apply NMS to remove overlapping boxes"""
 indices = np.argsort(scores)[::-1]
 keep = []
 
 while len(indices) > 0:
 current = indices[0]
 keep.append(current)
 indices = indices[1:]
 
 if len(indices) == 0:
 break
 
 # Compute IoU
 ious = []
 for i in indices:
 x1 = max(boxes[current, 0], boxes[i, 0])
 y1 = max(boxes[current, 1], boxes[i, 1])
 x2 = min(boxes[current, 2], boxes[i, 2])
 y2 = min(boxes[current, 3], boxes[i, 3])
 
 inter = max(0, x2-x1) * max(0, y2-y1)
 union = (boxes[current,2]-boxes[current,0]) * (boxes[current,3]-boxes[current,1]) + \
 (boxes[i,2]-boxes[i,0]) * (boxes[i,3]-boxes[i,1]) - inter
 iou = inter / union if union > 0 else 0
 ious.append(iou)
 
 indices = indices[np.array(ious) < iou_threshold]
 
 return keep

np.random.seed(42)
boxes = np.array([[10, 10, 50, 50], [12, 12, 52, 52], [100, 100, 150, 150]])
scores = np.array([0.9, 0.8, 0.7])
keep = nms(boxes, scores)
assert len(keep) <= len(boxes)
print("✓ NMS working")

### Lab 4: mAP Computation

import numpy as np

def compute_ap(precision, recall):
 """Compute average precision"""
 mrec = np.concatenate(([0.], recall, [1.]))
 mpre = np.concatenate(([0.], precision, [0.]))
 
 for i in range(len(mpre)-1, 0, -1):
 mpre[i-1] = np.maximum(mpre[i-1], mpre[i])
 
 i = np.where(mrec[1:] != mrec[:-1])[0]
 ap = np.sum((mrec[i+1] - mrec[i]) * mpre[i+1])
 
 return ap

recall = np.linspace(0, 1, 11)
precision = 1 - 0.1 * recall
ap = compute_ap(precision, recall)
assert 0 <= ap <= 1
print(f"✓ AP: {ap:.3f}")

---

Go deeper with CFSGPT

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

Create Free Account