ReflectionPad2d

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

reflection_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 ReflectionPad2d (15-pixel mirrored border)
pad2d = nn.ReflectionPad2d(padding=15)

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 generated by mirroring internal pixel values.")

# 3. Start the interactive session
sv.pytorch_web(module=pad2d, terminal_callback=terminal_callback)
Spatial Mirroring
ReflectionPad2d image border visualization
In 2D, the edges are reflected across the horizontal and vertical boundaries. This is widely used in image processing to avoid artifacts at the edges of the frame during convolution.