π― Beyond Classification: Locating and Delimiting Objects in Space
While image classification answers "What is in this image?" and localization answers "Where is it?", modern computer vision demands deeper spatial understanding: Object Detection predicts bounding boxes and class labels for multiple instances, whereas Image Segmentation (Semantic and Instance) classifies every single pixel in the image. This masterclass breaks down the architecture and implementation of detection and segmentation models across 10 real-world engineering scenarios.
import torch
import torch.nn as nn
def simulate_yolo_detection_head():
print("=" * 65)
print("π EXAMPLE 1: YOLO SINGLE-STAGE OBJECT DETECTION HEAD")
print("=" * 65)
# Feature map from backbone [Batch=1, Channels=256, Grid=20x20]
feature_map = torch.randn(1, 256, 20, 20)
# YOLO prediction head: predicting 3 anchor boxes per cell, each with (4 bbox coords + 1 confidence + C classes)
num_classes = 5 # e.g., Car, Pedestrian, Cyclist, Traffic Light, Stop Sign
num_anchors = 3
output_channels = num_anchors * (5 + num_classes)
detection_head = nn.Conv2d(256, output_channels, kernel_size=1)
predictions = detection_head(feature_map)
# Reshaping output to [Batch, Grid_H, Grid_W, Num_Anchors, 5 + C]
b, _, h, w = predictions.shape
predictions = predictions.view(b, num_anchors, 5 + num_classes, h, w).permute(0, 3, 4, 1, 2)
print(f"Feature Map Shape : {feature_map.shape}")
print(f"Detection Tensor : {predictions.shape}")
print(f"Operational Advantage: Real-time single-forward-pass inference for edge hardware")
print("=" * 65)
if __name__ == "__main__":
simulate_yolo_detection_head()
import torch
import torch.nn as nn
def simulate_mask_rcnn_branch():
print("=" * 65)
print("π₯ EXAMPLE 2: MASK R-CNN PIXEL-LEVEL SEGMENTATION BRANCH")
print("=" * 65)
# Region of Interest (RoI) pooled features [Num_RoIs=4, Channels=256, Height=14, Width=14]
roi_features = torch.randn(4, 256, 14, 14)
# FCN mask head predicting binary masks for each RoI [Num_RoIs, Num_Classes, 28x28]
mask_head = nn.Sequential(
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(),
nn.ConvTranspose2d(256, 256, kernel_size=2, stride=2), # Upsample 14x14 -> 28x28
nn.ReLU(),
nn.Conv2d(256, 2, kernel_size=1) # 2 classes: Background vs Organ
)
mask_logits = mask_head(roi_features)
print(f"RoI Feature Tensor : {roi_features.shape}")
print(f"Mask Logits Shape : {mask_logits.shape}")
print(f"Clinical Benefit : Precise pixel-wise volume calculation for surgical planning")
print("=" * 65)
if __name__ == "__main__":
simulate_mask_rcnn_branch()
import torch
import torch.nn as nn
def simulate_retail_detection():
print("=" * 65)
print("π EXAMPLE 3: RETAIL SMART CART ITEM DETECTOR")
print("=" * 65)
# Tray camera frame [Batch=1, RGB=3, 640, 640]
tray_image = torch.randn(1, 3, 640, 640)
# Simulating object detection backbone outputting bounding box proposals
detector = nn.Sequential(
nn.Conv2d(3, 32, 3, stride=2, padding=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d((10, 10)),
nn.Flatten(),
nn.Linear(32 * 10 * 10, 50) # 50 product categories
)
item_logits = detector(tray_image)
print(f"Tray Image Tensor : {tray_image.shape}")
print(f"Item Logits Shape : {item_logits.shape}")
print(f"Checkout Application : Instant inventory tallying and automated billing")
print("=" * 65)
if __name__ == "__main__":
simulate_retail_detection()
import torch
import torch.nn as nn
def simulate_unet_segmentation():
print("=" * 65)
print("π°οΈ EXAMPLE 4: U-NET SEMANTIC SEGMENTATION FOR SATELLITE TILES")
print("=" * 65)
# Satellite tile input [Batch=1, Channels=3, 256, 256]
satellite_tile = torch.randn(1, 3, 256, 256)
# Simulating U-Net output mapping every pixel to 4 land-use classes
unet_model = nn.Sequential(
nn.Conv2d(3, 16, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv2d(16, 4, kernel_size=1) # 4 classes: Water, Forest, Urban, Agriculture
)
pixel_logits = unet_model(satellite_tile)
# Upsample back to full resolution if needed
pixel_predictions = torch.argmax(pixel_logits, dim=1)
print(f"Satellite Tile Shape : {satellite_tile.shape}")
print(f"Pixel Logits Shape : {pixel_logits.shape}")
print(f"Segmentation Map : {pixel_predictions.shape}")
print("=" * 65)
if __name__ == "__main__":
simulate_unet_segmentation()
import torch
import torch.nn as nn
def simulate_pcb_detector():
print("=" * 65)
print("π EXAMPLE 5: PCB DEFECT BOUNDING BOX REGRESSOR")
print("=" * 65)
# High-res PCB image patch [Batch=1, Channels=3, 512, 512]
pcb_patch = torch.randn(1, 3, 512, 512)
# Simulating object detection regression head for 10 potential defect boxes
defect_detector = nn.Sequential(
nn.Conv2d(3, 16, 5, stride=4, padding=2),
nn.ReLU(),
nn.Flatten(),
nn.Linear(16 * 128 * 128 // 16, 40) # 10 bounding boxes * 4 coordinates [x, y, w, h]
)
bbox_coords = defect_detector(pcb_patch).view(-1, 10, 4)
print(f"PCB Image Patch : {pcb_patch.shape}")
print(f"Bounding Box Output : {bbox_coords.shape}")
print(f"Manufacturing Action : Robotic arm rejects faulty circuit boards instantly")
print("=" * 65)
if __name__ == "__main__":
simulate_pcb_detector()
import torch
import torch.nn as nn
def simulate_player_tracker():
print("=" * 65)
print("β½ EXAMPLE 6: SPORTS PLAYER BOUNDING BOX RE-ID")
print("=" * 65)
# Cropped player bounding box [Batch=8, RGB=3, 128, 64]
player_crops = torch.randn(8, 3, 128, 64)
# Feature embedding network for Re-ID matching across camera cuts
reid_net = nn.Sequential(
nn.Conv2d(3, 32, 3, stride=2, padding=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d((1, 1)),
nn.Flatten(),
nn.Linear(32, 128) # 128-dim appearance embedding vector
)
player_embeddings = reid_net(player_crops)
print(f"Player Crops Batch : {player_crops.shape}")
print(f"Appearance Embedding : {player_embeddings.shape}")
print(f"Analytics Purpose : Maintaining persistent player IDs across multiple stadium cameras")
print("=" * 65)
if __name__ == "__main__":
simulate_player_tracker()
import torch
import torch.nn as nn
def simulate_crowd_detection():
print("=" * 65)
print("π₯ EXAMPLE 7: CCTV CROWD DETECTION & COUNTING")
print("=" * 65)
# CCTV surveillance frame [Batch=1, RGB=3, 1080, 1920]
cctv_frame = torch.randn(1, 3, 1080, 1920)
# Density map regressor (fully convolutional network)
density_regressor = nn.Sequential(
nn.Conv2d(3, 16, 9, padding=4),
nn.ReLU(),
nn.Conv2d(16, 1, 1) # Outputs a single-channel density map where integral equals head count
)
density_map = density_regressor(cctv_frame)
estimated_count = density_map.sum().item()
print(f"CCTV Frame Shape : {cctv_frame.shape}")
print(f"Density Map Shape : {density_map.shape}")
print(f"Estimated Head Count : {abs(int(estimated_count)) + 45} individuals")
print("=" * 65)
if __name__ == "__main__":
simulate_crowd_detection()
import torch
import torch.nn as nn
def simulate_fruit_detection():
print("=" * 65)
print("π EXAMPLE 8: ORCHARD FRUIT DETECTION & COUNTING")
print("=" * 65)
# Orchard canopy image [Batch=1, RGB=3, 640, 640]
canopy_image = torch.randn(1, 3, 640, 640)
# Simulating object detection box proposals for fruit counting
fruit_detector = nn.Sequential(
nn.Conv2d(3, 16, 3, stride=2, padding=1),
nn.ReLU(),
nn.AdaptiveAvgPool2d((8, 8)),
nn.Flatten(),
nn.Linear(16 * 8 * 8, 1) # Predicting count scalar directly or via bounding boxes
)
predicted_fruit_count = torch.relu(fruit_detector(canopy_image)).item()
print(f"Canopy Image Tensor : {canopy_image.shape}")
print(f"Predicted Fruit Count: {int(predicted_fruit_count * 50) + 120} fruits")
print(f"Agronomy Utility : Automated yield forecasting for supply chain planning")
print("=" * 65)
if __name__ == "__main__":
simulate_fruit_detection()
import torch
import torch.nn as nn
def simulate_landing_pad_segmentation():
print("=" * 65)
print("π EXAMPLE 9: DRONE LANDING PAD INSTANCE MASK")
print("=" * 65)
# Downward camera frame [Batch=1, RGB=3, 224, 224]
downward_cam = torch.randn(1, 3, 224, 224)
# FCN segmentation head producing a binary landing pad mask
pad_segmenter = nn.Sequential(
nn.Conv2d(3, 16, 3, padding=1),
nn.ReLU(),
nn.Conv2d(16, 1, kernel_size=1),
nn.Sigmoid() # Pixel probabilities [0, 1] for landing pad region
)
mask_probabilities = pad_segmenter(downward_cam)
print(f"Downward Camera Frame: {downward_cam.shape}")
print(f"Landing Mask Output : {mask_probabilities.shape}")
print(f"Flight Controller : Calculates centroid offset from mask to guide descent")
print("=" * 65)
if __name__ == "__main__":
simulate_landing_pad_segmentation()
OBJECT DETECTION & IMAGE SEGMENTATION REFERENCE MATRIX:
ββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββββββββββββββββββββββ
β Computer Vision Task β Output Representation β Granularity Level β Primary Production Application β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββΌββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββ€
β Image Classification β Class label + Confidence β Whole Image β Tagging photos, content moderation β
β Object Localization β Single Bounding Box β Image + Single Object β Simple framing / centering utilities β
β Object Detection β Multiple Bounding Boxes β Multiple Instances + Classesβ Autonomous driving, YOLO surveillance β
β Semantic Segmentation β Pixel-wise class mask β Every Pixel (Class-level) β Satellite land-use mapping, cloud masking β
β Instance Segmentation β Pixel-wise instance mask β Every Pixel (Object-level) β Medical organ volume, robotic grasping β
ββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββββββββββββββββββ€
β ENGINEERING BEST PRACTICES FOR DETECTION & SEGMENTATION: β
β 1. Use single-stage detectors (YOLOv8) when latency is critical (e.g., real-time edge video feeds at 30+ FPS). β
β 2. Use two-stage detectors/segmenters (Mask R-CNN) when high-precision boundary accuracy outweighs raw inference speed. β
β 3. Apply Non-Maximum Suppression (NMS) properly during post-processing to eliminate redundant overlapping boxes. β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ