adaptive_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 AdaptiveMaxPool3d
# Squash the complex 3D volume into a 4x4x4 cube
adap_pool3d = nn.AdaptiveMaxPool3d(output_size=(4, 4, 4))
def terminal_callback(buffer, message, images, files):
# 3. Process the volume
output = adap_pool3d(input_volume)
buffer.send_system_message(f"Input Voxel Grid: {input_volume.shape[2:]}")
buffer.send_system_message(f"Targeting fixed cube: 4x4x4")
buffer.send_system_message(f"Output Shape: {list(output.shape)}")
# 4. Start the interactive session
sv.pytorch_web(module=adap_pool3d, terminal_callback=terminal_callback)