fractional_maxpool2d_demo.py
import svetoviz_webgpu as sv
# 1. Prepare 2D image data
img = Image.open("sample_image.jpg").convert("RGB")
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 FractionalMaxPool2d
# output_ratio: The ratio of output size to input size (0 < ratio < 1)
frac_pool2d = nn.FractionalMaxPool2d(kernel_size=3, output_ratio=(0.5, 0.5), return_indices=True)
def terminal_callback(buffer, message, images, files):
# Forward pass returns both the pooled tensor and the indices
output, indices = frac_pool2d(input_tensor)
buffer.send_system_message(f"Input Image Shape: {input_tensor.shape[2:]}")
buffer.send_system_message(f"Fractional Output Shape (0.5 ratio): {output.shape[2:]}")
buffer.send_system_message("Note: Pooling regions are stochastically generated.")
# 4. Start the interactive session
sv.pytorch_web(module=frac_pool2d, terminal_callback=terminal_callback)