fractional_maxpool3d_demo.py
import svetoviz_webgpu as sv
# 1. Prepare 3D MRI volume (32 slices, 128x128)
ds = load_dataset("g4m3r/T1w_MRI_Brain_Slices", split="train", streaming=True)
slices = [np.array(ex['image'].resize((128, 128)).convert("L")).astype(np.float32) / 255.0 for ex in ds.skip(2).take(32)]
input_volume = torch.from_numpy(np.stack(slices)).unsqueeze(0).unsqueeze(0)
# 2. Define FractionalMaxPool3d
# Targeting a 0.6 reduction ratio across Depth, Height, and Width
frac_pool3d = nn.FractionalMaxPool3d(kernel_size=3, output_ratio=(0.6, 0.6, 0.6), return_indices=True)
def terminal_callback(buffer, message, images, files):
# Forward pass returns both the pooled tensor and the indices
output, indices = frac_pool3d(input_volume)
buffer.send_system_message(f"Input Voxel Grid: {input_volume.shape[2:]}")
buffer.send_system_message(f"Fractional Output Volume: {output.shape[2:]}")
buffer.send_system_message(f"Output Ratio applied: 0.6")
# 4. Start the interactive session
sv.pytorch_web(module=frac_pool3d, terminal_callback=terminal_callback)