AdaptiveAvgPool3d

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

adaptive_avgpool3d_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 AdaptiveAvgPool3d
# Compresses the 3D volume into a 4x4x4 voxel representation via mean-averaging
adap_avg_pool3d = nn.AdaptiveAvgPool3d(output_size=(4, 4, 4))

def terminal_callback(buffer, message, images, files):
    # 3. Process the volume
    output = adap_avg_pool3d(input_volume)
    buffer.send_system_message(f"Input Voxel Grid: {input_volume.shape[2:]}")
    buffer.send_system_message(f"Targeting fixed average cube: 4x4x4")
    buffer.send_system_message(f"Output Shape: {list(output.shape)}")

# 4. Start the interactive session
sv.pytorch_web(module=adap_avg_pool3d, terminal_callback=terminal_callback)
Volumetric Mean Aggregation
AdaptiveAvgPool3d activations
3D adaptive average pooling reduces the entire volume to a compact 4x4x4 cube, effectively creating a low-resolution structural summary.