ML Notes
Master object detection with YOLO, Faster R-CNN, SSD architectures, anchor boxes, non-maximum suppression, and real-time detection implementation.
Object detection answers two questions simultaneously: "What objects are in this image?" and "Where exactly are they?" Unlike classification which assigns one label to an entire image, detection finds multiple objects, draws bounding boxes around each one, and classifies them individually. This capability powers autonomous driving (detecting cars, pedestrians, traffic signs), surveillance systems, retail analytics, and robotics — any application where knowing both identity and location of objects matters.
From Classification to Detection
Image classification takes an image and outputs a single label. Object detection is dramatically harder because it must handle variable numbers of objects (one image might have 2 cars and 5 pedestrians, the next might have 20 cars and no pedestrians), objects at different scales and positions, overlapping and occluded objects, and the need for both classification accuracy and localization precision simultaneously.
The output of a detector for each object is: a bounding box (four numbers: x, y, width, height), a class label (what type of object), and a confidence score (how sure the model is).
Two-Stage vs One-Stage Detectors
Object detection architectures fall into two families with fundamentally different philosophies:
Two-Stage Detectors (Faster R-CNN family): First propose regions likely to contain objects, then classify each region. More accurate but slower. The Region Proposal Network generates candidate boxes, then a classifier refines each one. Typically achieves higher accuracy but runs at 5-15 FPS.
One-Stage Detectors (YOLO, SSD family): Process the entire image in a single pass, predicting all bounding boxes and classes simultaneously. Faster but traditionally less accurate. YOLO divides the image into a grid and predicts boxes directly from each cell. Modern versions (YOLOv8) have closed the accuracy gap while maintaining 30-100+ FPS.
Two-Stage (Faster R-CNN)
Image → Backbone CNN → Region Proposals → Classify each region → Refined boxes
Pros: Higher accuracy, especially for small objects
Cons: Slower (5-15 FPS)
One-Stage (YOLO)
Image → Backbone CNN → Predict boxes + classes in one shot
Pros: Real-time speed (30-100+ FPS)
Cons: Historically less accurate (gap is now minimal with YOLOv8)
YOLO: You Only Look Once
YOLO revolutionized real-time detection by treating it as a single regression problem. The image is divided into an S×S grid. Each grid cell predicts B bounding boxes, confidence scores, and class probabilities. All predictions happen in one forward pass through the network, enabling real-time performance.
Anchor Boxes and Non-Maximum Suppression
Two critical concepts enable accurate detection:
Anchor Boxes are predefined box shapes of various aspect ratios and sizes. Instead of predicting absolute box coordinates from scratch, the model predicts adjustments (offsets) to the nearest anchor box. This makes the prediction problem easier because the model only needs to learn small refinements rather than exact coordinates. Common aspect ratios include 1:1, 1:2, 2:1 for different object shapes.
Non-Maximum Suppression (NMS) handles duplicate detections. When multiple anchor boxes detect the same object, NMS keeps only the highest-confidence detection and removes all overlapping boxes with IoU above a threshold (typically 0.5). Without NMS, you would get dozens of slightly different boxes around each object.
Evaluation Metrics: mAP
Object detection uses mean Average Precision (mAP) as the primary metric. For each class, compute the precision-recall curve across all confidence thresholds, then calculate the area under that curve (Average Precision). mAP averages this across all classes. Two common variants exist: mAP@0.5 (considers a detection correct if IoU with ground truth exceeds 0.5) and mAP@0.5:0.95 (averages across IoU thresholds from 0.5 to 0.95, rewarding precise localization).
Training Custom Object Detectors
To detect custom objects, you need annotated training data with bounding box labels. Tools like LabelImg, CVAT, or Roboflow help you draw boxes around objects and export in YOLO format. You typically need 100-500 annotated images per class for good results with fine-tuning from pre-trained weights.
# Fine-tune YOLOv8 on custom data
model = YOLO('yolov8m.pt')
# Train on custom dataset (requires YAML config file)
results = model.train(
data='custom_dataset.yaml',
epochs=100,
imgsz=640,
batch=16,
patience=20, # early stopping
augment=True
)
# Evaluate on test set
metrics = model.val()
print(f"mAP@0.5: {metrics.box.map50:.4f}")
print(f"mAP@0.5:0.95: {metrics.box.map:.4f}")Choosing the Right Architecture
For real-time applications (video surveillance, autonomous driving), YOLOv8 provides the best speed-accuracy tradeoff. For maximum accuracy on difficult images (small objects, dense scenes), use Faster R-CNN or DETR (Detection Transformer). For edge deployment on mobile devices, use YOLOv8-nano or EfficientDet-Lite. Consider your FPS requirements, hardware constraints, and accuracy needs when selecting an architecture.
Performance Optimization and Best Practices
When implementing object detection solutions in production, performance optimization becomes crucial. Start by profiling your code to identify bottlenecks — often the slowest step is not where you expect. Consider batch processing multiple images simultaneously to amortize overhead costs. Use appropriate data types (uint8 for pixel values, float32 for computations) and avoid unnecessary type conversions that waste memory and CPU cycles. Pre-allocate output arrays rather than growing them dynamically, and leverage vectorized NumPy operations over Python loops wherever possible. These optimizations can improve throughput by 10-100x for real-time applications.
Common Debugging Strategies
Debugging object detection code requires visual inspection at each pipeline stage. Display intermediate results to verify that each transformation produces expected output. Check array shapes and value ranges after every operation — a common bug is accidentally normalizing values to 0-1 when your function expects 0-255. Use assertions liberally during development to catch shape mismatches early. When results look wrong, work backwards from the output to find where the pipeline first deviates from expectations. Building this systematic debugging mindset saves hours of frustration and produces more reliable systems.
Practical Implementation Considerations
When deploying object detection solutions in production environments, several practical considerations become important. First, preprocessing must be consistent between training and inference — if you normalized images to a specific range during training, the same normalization must be applied at inference time. Second, consider the computational budget available: edge devices may require model compression techniques like quantization (reducing weight precision from 32-bit to 8-bit) or pruning (removing unimportant connections). Third, monitor model performance over time since real-world data distributions shift — a model trained on summer images may degrade in winter conditions without periodic retraining on fresh data.
Debugging and Troubleshooting
When object detection models produce unexpected results, systematic debugging is essential. Start by visualizing predictions on individual examples to understand failure modes. Check that input preprocessing matches training exactly — even subtle differences in normalization or resizing can cause significant accuracy drops. Verify that your evaluation metrics match the competition or paper you are comparing against, as different IoU thresholds or averaging methods can give very different numbers. When training diverges or produces poor results, reduce learning rate, verify label correctness on a random sample, and ensure data augmentation is not too aggressive for your dataset size.
Key Takeaways
Object detection combines localization and classification to find all objects in an image with bounding boxes. One-stage detectors like YOLO provide real-time speed while two-stage detectors like Faster R-CNN maximize accuracy. Understanding anchor boxes and NMS is essential for debugging detection models. Evaluate with mAP and remember that training data quality (accurate, consistent bounding box annotations) matters more than model architecture choice for practical applications.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Object Detection.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Machine Learning topic.
Search Terms
machine-learning, machine learning, machine, learning, computer, vision, object, detection
Related Machine Learning Topics