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 ]

💻 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)