Convolutional Neural Networks & Vision Backbones
👁️ Convolutional Neural Networks (CNNs) & Vision Backbones
In computer vision and visual game AI, Convolutional Neural Networks (CNNs) process 2D/3D pixel grids using sliding spatial filter kernels.
🏛️ The Convolution Operation
[ Input Image Patch (3x3) ] [ Kernel / Filter (3x3) ] [ Feature Map Pixel ]
[ 1 0 1 ] [ 1 0 -1 ]
[ 0 1 0 ] * [ 1 0 -1 ] = [ 4 ]
[ 1 0 1 ] [ 1 0 -1 ]
- Translation Invariance: A vertical edge or enemy character detected in the top-left of a game screen is recognized with the exact same convolutional kernel when it moves to the bottom-right.
- Pooling (Max/Avg Pooling): Downsamples feature map resolutions (
) while preserving dominant edge activations.
💻 1. Modern Vision Backbone in PyTorch
import torch
import torch.nn as nn
class GameVisionBackbone(nn.Module):
def __init__(self, in_channels: int = 3, feature_dim: int = 256):
super().__init__()
self.conv = nn.Sequential(
nn.Conv2d(in_channels, 32, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.AdaptiveAvgPool2d((1, 1)), # Global average pooling to [Batch, 128, 1, 1]
nn.Flatten(),
nn.Linear(128, feature_dim)
)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.conv(x)
🔗 Related Notes
- Machine Learning Fundamentals — Tensors, autograd, and training loops.
- Reinforcement Learning & Game AI — Feeding raw screen pixels into RL agents.
- Sensory Perception & Spatial Grids for AI — Spatial grids vs visual rendering.