adaptive_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 AdaptiveMaxPool2d
# Force the image into a 7x7 feature map (common for Global Average/Max Pooling)
adap_pool2d = nn.AdaptiveMaxPool2d(output_size=(7, 7))
def terminal_callback(buffer, message, images, files):
# 3. Process the image
output = adap_pool2d(input_tensor)
buffer.send_system_message(f"Original Resolution: {input_tensor.shape[2:]}")
buffer.send_system_message(f"Adaptive Output: {list(output.shape)}")
# 4. Start the interactive session
sv.pytorch_web(module=adap_pool2d, terminal_callback=terminal_callback)