Object Detection Model Architectures and Implementation Details in Microsoft AI-For-Beginners

The ObjectDetection.ipynb notebook demonstrates two distinct object detection approaches—a sliding-window VGG-16 heat-map classifier and a dense regression network for synthetic data—while pointing learners toward production-grade RetinaNet implementations.

The Microsoft AI-For-Beginners repository provides an educational progression through computer vision concepts, with the object detection lesson located at lessons/4-ComputerVision/11-ObjectDetection/ObjectDetection.ipynb. This notebook explores fundamental object detection model architectures ranging from naive classification-based localization to modern anchor-based detectors. The implementations progress from interpretable heat-map visualizations using pre-trained CNNs to lightweight fully-connected networks trained on synthetic geometric data.

VGG-16 Sliding-Window Heat-Map Approach

The first architecture repurposes the VGG-16 model pre-trained on ImageNet as a feature extractor to generate spatial activation maps. Rather than modifying the network for regression, the notebook treats detection as a dense classification problem across image tiles.

Architecture Overview

The implementation loads keras.applications.vgg16.VGG16 with weights='imagenet' and preserves the original classification head. The model processes 224 × 224 pixel inputs and outputs 1,000-class ImageNet probabilities. For the cat detection demonstration, the notebook aggregates probabilities for feline classes (indices 281–293) to produce a single confidence score per tile.

Implementation Steps

The sliding-window pipeline operates through three core functions defined in the notebook:

  1. predict(img) – Resizes an input tile to 224 × 224, applies keras.applications.vgg16.preprocess_input, and runs vgg.predict(). It returns the sum of probabilities for cat-related classes.

  2. predict_map(img, n) – Divides the source image into an n × n grid. It extracts tiles using array slicing (img[dx*i:dx*(i+1), dx*j:dx*(j+1)]) and aggregates predict() outputs into a 2D heat-map array.

  3. Visualization – The heat-map is overlaid with the original image using matplotlib to visualize spatial confidence distributions.

import cv2, numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt

# Load pre-trained VGG-16

vgg = keras.applications.vgg16.VGG16(weights='imagenet')

def predict(img):
    """Return summed probability of ImageNet cat classes (281-293)."""
    im = cv2.resize(img, (224, 224))
    im = keras.applications.vgg16.preprocess_input(im)
    probs = vgg.predict(np.expand_dims(im, axis=0))[0]
    return np.sum(probs[281:294])

def predict_map(img, n):
    """Create an n×n heat-map of cat probabilities."""
    dx = img.shape[0] // n
    res = np.zeros((n, n), dtype=np.float32)
    for i in range(n):
        for j in range(n):
            tile = img[dx*i:dx*(i+1), dx*j:dx*(j+1)]
            res[i, j] = predict(tile)
    return res

Dense Regression Network for Synthetic Data

The second approach demonstrates bounding-box regression using a fully-connected network trained on synthetically generated rectangles. This architecture eschews convolutional layers in favor of dense connections to predict four continuous values representing bounding box coordinates.

Model Architecture

The keras.Sequential model accepts 8 × 8 binary images and outputs a 4-dimensional vector [x, y, w, h]. The architecture consists of:

  • Flatten layer – Converts 8 × 8 inputs into 64-dimensional vectors
  • Dense(200, activation='relu') – Hidden layer with ReLU activation
  • Dropout(0.2) – Regularization to prevent overfitting
  • Dense(4) – Linear output layer predicting normalized bounding box parameters

Training Pipeline

The notebook implements a complete synthetic data workflow:

  1. Data Generation – generate_images(num_imgs, img_size=8) creates random white rectangles on black backgrounds and returns corresponding [x, y, w, h] labels.

  2. Normalization – Input images are standardized using zero-mean, unit-variance normalization: (imgs - np.mean(imgs)) / np.std(imgs).

  3. Training – The model compiles with SGD optimizer and MSE loss (model.compile('sgd', 'mse')), then trains for 30 epochs with bounding boxes normalized by dividing by 8.0.

  4. Evaluation – The IOU(bbox1, bbox2) function calculates Intersection-over-Union by computing overlap width (wI) and height (hI) between predicted and ground-truth boxes, achieving approximately 0.71 mean IoU on held-out test data.

import numpy as np
from tensorflow import keras

def generate_images(num_imgs, img_size=8, min_object_size=1, max_object_size=4):
    """Create synthetic images with a single white rectangle."""
    bboxes = np.zeros((num_imgs, 4))
    imgs = np.zeros((num_imgs, img_size, img_size))
    for i in range(num_imgs):
        w, h = np.random.randint(min_object_size, max_object_size, size=2)
        x = np.random.randint(0, img_size - w)
        y = np.random.randint(0, img_size - h)
        imgs[i, x:x+w, y:y+h] = 1.0
        bboxes[i] = [x, y, w, h]
    return imgs, bboxes

# Build the dense model

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(8, 8)),
    keras.layers.Dense(200, activation='relu'),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(4)
])
model.compile('sgd', loss='mse')

# Training with normalization

imgs, bboxes = generate_images(100000)
imgs_norm = (imgs - np.mean(imgs)) / np.std(imgs)
model.fit(imgs_norm, bboxes / 8.0, epochs=30)

# IoU calculation

def IOU(b1, b2):
    """Intersection-over-Union for boxes [x, y, w, h]."""
    x1, y1, w1, h1 = b1
    x2, y2, w2, h2 = b2
    wI = min(x1 + w1, x2 + w2) - max(x1, x2)
    hI = min(y1 + h1, y2 + h2) - max(y1, y2)
    if wI <= 0 or hI <= 0:
        return 0.0
    inter = wI * hI
    union = w1 * h1 + w2 * h2 - inter
    return inter / union

Production-Grade Detection with RetinaNet

While the notebook focuses on educational implementations, it directs learners toward RetinaNet for real-world applications. The notebook references the official Keras examples and the third-party keras-retinanet library as the standard approach for production object detection pipelines. This architecture addresses the class imbalance problem in dense detection through focal loss and utilizes feature pyramid networks (FPN) for multi-scale object detection.

Summary

  • VGG-16 Heat-Map Method – Repurposes ImageNet pre-trained weights for sliding-window classification, generating interpretable spatial confidence maps without network retraining.
  • Dense Regression Model – Demonstrates end-to-end bounding-box prediction using fully-connected layers on synthetic 8×8 binary images, achieving ~0.71 mean IoU.
  • Evaluation Metric – Intersection-over-Union (IoU) serves as the primary accuracy measure for comparing predicted [x, y, w, h] vectors against ground truth.
  • Production Pathway – The notebook explicitly recommends RetinaNet implementations for scalable, real-world object detection tasks beyond educational contexts.

Frequently Asked Questions

What is the sliding window approach used in the VGG-16 heat-map method?

The sliding window approach divides the input image into an n × n grid of tiles using the predict_map() function. Each tile is resized to 224 × 224 pixels and fed through the pre-trained VGG-16 network to extract class probabilities. The notebook aggregates the probabilities for cat-related ImageNet classes (indices 281–293) to create a spatial heat-map showing where the model detects feline features.

How does the simple synthetic object detector work?

The synthetic detector uses a fully-connected neural network to regress bounding box coordinates [x, y, w, h] from 8 × 8 binary images containing random white rectangles. The generate_images() function creates training data programmatically, while the model architecture—comprising a Flatten layer, Dense(200) with ReLU, Dropout(0.2), and Dense(4) output—learns to predict rectangle locations through mean squared error minimization over 30 epochs.

Why does the notebook recommend RetinaNet for real-world applications?

RetinaNet addresses the foreground-background class imbalance inherent in dense object detection through its focal loss function, while feature pyramid networks enable detection across multiple scales. The notebook cites RetinaNet as the appropriate next step because the educational examples (VGG-16 heat-maps and dense regression) demonstrate concepts but lack the architectural sophistication—such as anchor boxes and multi-scale feature extraction—required for robust detection in complex visual environments.

What metric is used to evaluate the dense regression model?

The notebook employs Intersection-over-Union (IoU) to evaluate bounding box accuracy. The IOU() function calculates the ratio of overlap area to union area between predicted and ground-truth boxes. On a held-out test set of 500 synthetic images, the trained model achieves approximately 0.71 mean IoU, indicating substantial overlap between predicted and actual rectangle locations.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →