BatchNorm2d

https://pytorch.org/docs/stable/generated/torch.nn.BatchNorm2d.html

batchnorm2d_demo.py
import svetoviz_webgpu as sv

# 1. Prepare 2D image data
input_tensor = torch.from_numpy(img_np).permute(2, 0, 1).unsqueeze(0)

# 2. Define BatchNorm2d
# num_features: Number of channels (e.g., 3 for RGB)
bn2d = nn.BatchNorm2d(num_features=3)

def terminal_callback(buffer, message, images, files):
    bn2d.train()
    output = bn2d(input_tensor)

    buffer.send_system_message(f"Image Resolution: {input_tensor.shape[2:]}")
    buffer.send_system_message(f"Channels Normalized: {bn2d.num_features}")
    buffer.send_system_message("Status: Each channel normalized across the spatial plane.")

# 3. Start the interactive session
sv.pytorch_web(module=bn2d, terminal_callback=terminal_callback)
Spatial Batch Normalization
BatchNorm2d activations
For 4D image tensors $(N, C, H, W)$, BatchNorm2d computes stats over $(N, H, W)$ for each channel $C$. This ensures that the activation distributions for each feature map remain consistent throughout the convolutional stack.