ZeroPad2d

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

zeropad2d_demo.py
import svetoviz_webgpu as sv

# 1. Prepare 2D image data (C, H, W)
img_np = np.array(img).astype(np.float32) / 255.0
input_tensor = torch.from_numpy(img_np).permute(2, 0, 1).unsqueeze(0)

# 2. Define ZeroPad2d
# padding=(left, right, top, bottom) -> Adds 10px black border
zeropad = nn.ZeroPad2d(padding=10)

def terminal_callback(buffer, message, images, files):
    output = zeropad(input_tensor)

    buffer.send_system_message(f"Original Resolution: {input_tensor.shape[2:]}")
    buffer.send_system_message(f"Padded Resolution: {output.shape[2:]}")
    buffer.send_system_message("Boundary expanded with zero-valued pixels (Black).")

# 3. Start the interactive session
sv.pytorch_web(module=zeropad, terminal_callback=terminal_callback)
Spatial Margin Expansion
ZeroPad2d black border visualization
ZeroPad2d is a specialized case of ConstantPad2d where the value is locked to zero. It is the standard method for maintaining feature map resolution across convolutional layers, effectively "framing" the input so that kernels can center on edge pixels.