ConstantPad2d

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

constant_pad2d_demo.py
import svetoviz_webgpu as sv

# 1. Prepare 2D image data
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 ConstantPad2d
# Adds a 25-pixel border with a specific gray value (0.5)
pad2d = nn.ConstantPad2d(padding=25, value=0.5)

def terminal_callback(buffer, message, images, files):
    output = pad2d(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("Border filled with constant value: 0.5")

# 3. Start the interactive session
sv.pytorch_web(module=pad2d, terminal_callback=terminal_callback)
Non-Zero Spatial Framing
ConstantPad2d gray border visualization
Unlike ZeroPad2d, ConstantPad2d accepts a value argument. This creates a uniform frame around the image using the specified intensity, which can be useful when working with normalized data where zero is not the mean.