Object Detection Frameworks
# Object Detection Frameworks
## Introduction & Motivation
Object detection: localize and classify objects. YOLO, R-CNN, SSD. Applications: surveillance, autonomous driving.
Motivation: Find objects in images with bounding boxes.
Applications: Real-time detection, traffic monitoring.
---
## Core Concepts & Theory
### Region-Based Detection
R-CNN family approaches.
### Single-Shot Detection
YOLO, SSD one-stage methods.
### Anchor Boxes
Pre-defined bounding box templates.
### Non-Maximum Suppression
Remove duplicate detections.
---
## Mathematical Formulation
Bounding Box Regression:
$$\Delta x = (x - x_a) / w_a, \quad \Delta y = (y - y_a) / h_a$$
$$\Delta w = \log(w / w_a), \quad \Delta h = \log(h / h_a)$$
IoU:
$$ ext{IoU} = \frac{ ext{Area}_{ ext{intersection}}}{ ext{Area}_{ ext{union}}}$$
NMS:
$$ ext{suppress if } ext{IoU} > ext{threshold}$$
---
## Advanced Theory & Extensions
### Focal Loss
Address class imbalance.
### Feature Pyramid Networks
Multi-scale feature extraction.
### Cascade Detection
Progressive refinement.
---
## Computational Considerations
Region proposal: O(H·W·K).
Classification: O(N·K·C).
NMS: O(N²).
---
## Practical Implementation Strategies
### Anchor Generation
Multi-scale anchor boxes.
### Hard Negative Mining
Focus on difficult negatives.
### Data Augmentation
Geometric and photometric transforms.
---
## Benchmark Datasets & Evaluation
COCO: 80 object categories, complex scenes.
Pascal VOC: 20 classes, 16k images.
Open Images: Large-scale diverse dataset.
---
## Key Challenges & Limitations
### Scale Variation
Objects at different sizes.
### Occlusion
Partially visible objects.
### Small Object Detection
Limited pixel information.
---
## Hyperparameter Tuning
Anchor scales: 32, 64, 128, 256, 512.
Aspect ratios: 0.5, 1.0, 2.0.
NMS threshold: 0.5-0.7.
---
## Real-World Applications & Case Studies
Traffic Monitoring: Vehicle and pedestrian detection.
Autonomous Driving: Real-time obstacle detection.
Security: Threat detection in surveillance.
---
## Integration with Other Methods
Detection + tracking for video; + segmentation for instance masks.
---
## Summary & Key Takeaways
Object detection localizes and classifies objects in images.
Principles:
1. Region proposal: Candidate generation.
2. Classification: Object identification.
3. Regression: Bounding box refinement.
4. NMS: Duplicate removal.
5. Multi-scale: Hierarchical detection.
---
## Appendix: Practical Labs
### Lab 1: Bounding Box Encoding
import numpy as np
def encode_bbox(gt_box, anchor_box):
"""Encode ground truth to anchor relative coords"""
xa, ya, wa, ha = anchor_box
xg, yg, wg, hg = gt_box
dx = (xg - xa) / wa
dy = (yg - ya) / ha
dw = np.log(wg / wa)
dh = np.log(hg / ha)
return np.array([dx, dy, dw, dh])
anchor = [100, 100, 50, 50]
gt = [110, 105, 60, 45]
encoded = encode_bbox(gt, anchor)
assert encoded.shape == (4,), "Correct encoding shape"
print("✓ Bounding box encoding working")### Lab 2: IoU Calculation
import numpy as np
def compute_iou(box1, box2):
"""Compute intersection over union"""
x1_min, y1_min, x1_max, y1_max = box1
x2_min, y2_min, x2_max, y2_max = box2
inter_xmin = max(x1_min, x2_min)
inter_ymin = max(y1_min, y2_min)
inter_xmax = min(x1_max, x2_max)
inter_ymax = min(y1_max, y2_max)
inter_area = max(0, inter_xmax - inter_xmin) * max(0, inter_ymax - inter_ymin)
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 if union_area > 0 else 0
return iou
box1 = [0, 0, 10, 10]
box2 = [5, 5, 15, 15]
iou = compute_iou(box1, box2)
assert 0 <= iou <= 1, "Valid IoU"
print(f"✓ IoU calculation working: {iou:.4f}")### Lab 3: Non-Maximum Suppression
import numpy as np
def non_maximum_suppression(detections, iou_threshold=0.5):
"""Suppress overlapping detections"""
if len(detections) == 0:
return []
detections = sorted(detections, key=lambda x: x[4], reverse=True)
keep = []
while len(detections) > 0:
keep.append(detections[0])
if len(detections) == 1:
break
remaining = []
for det in detections[1:]:
iou = compute_iou(detections[0][:4], det[:4])
if iou < iou_threshold:
remaining.append(det)
detections = remaining
return keep
def compute_iou(box1, box2):
x1_min, y1_min, x1_max, y1_max = box1
x2_min, y2_min, x2_max, y2_max = box2
inter_area = max(0, min(x1_max, x2_max) - max(x1_min, x2_min)) * max(0, min(y1_max, y2_max) - max(y1_min, y2_min))
union_area = (x1_max - x1_min) * (y1_max - y1_min) + (x2_max - x2_min) * (y2_max - y2_min) - inter_area
return inter_area / union_area if union_area > 0 else 0
detections = [
[0, 0, 10, 10, 0.9],
[1, 1, 11, 11, 0.8],
[20, 20, 30, 30, 0.7]
]
kept = non_maximum_suppression(detections)
assert len(kept) <= len(detections), "NMS reduces detections"
print("✓ Non-maximum suppression working")### Lab 4: Anchor Generation
import numpy as np
def generate_anchors(image_size, feature_size, scales=[32, 64], ratios=[0.5, 1, 2]):
"""Generate anchor boxes for feature map"""
stride = image_size // feature_size
anchors = []
for i in range(feature_size):
for j in range(feature_size):
cx = (j + 0.5) * stride
cy = (i + 0.5) * stride
for scale in scales:
for ratio in ratios:
w = scale * np.sqrt(1 / ratio)
h = scale * np.sqrt(ratio)
anchors.append([cx - w/2, cy - h/2, cx + w/2, cy + h/2])
return np.array(anchors)
anchors = generate_anchors(image_size=224, feature_size=7)
assert anchors.shape[1] == 4, "Correct anchor format"
print(f"✓ Generated {len(anchors)} anchors")---