AvgPool3D

Description

Info

Parent class: Pool3D

Derived classes: -

This module implements the operation of three-dimensional average pooling. For a detailed theoretical description, see Pool3D.

For an input tensor of shape (N, C, D_{in}, H_{in}, W_{in}) and an output of shape (N, C, D_{out}, H_{out}, W_{out}) the operation is performed as follows (we consider the i-th element of the batch, the j-th map of the output

out(N_i, C_j, d, h, w) = \frac{1}{k_dk_hk_w}\sum_{t=0}^{k_d-1}\sum_{m=0}^{k_h-1}\sum_{n=0}^{k_w-1}(input(N_i, C_j, stride_d \times d + t, stride_h \times h + m, stride_w \times w + n))

where

N - size of the batch;

C - number of maps in the tensor;

H - size of the maps in height;

W - size of the maps in width;

stride_d, stride_h, stride_w - pooling step along the depth, the height and the width of the cards, respectively;

k_d, k_h, k_w - size of the pooling kernel in depth, height and width, respectively.

Initializing

def __init__(self, size=2, stride=2, pad=0, includePad=True, name=None):

Parameters

Parameter Allowed types Description Default
size Union[int, tuple] Kernel size 2
stride Union[int, tuple] Pooling stride 2
pad Union[int, tuple] Padding of the input maps 0
includePad bool Flag for including the edge filling values when calculating the average value True
name str Layer name None

Explanations

size - possible to specify either a single size of the pooling kernel, in which case it will be cubic, or a tuple (size_d, size_h, size_w), where size_d is the depth of the pooling kernel, size_h is the height of the pooling kernel, and size_w is its width.


stride - possible to specify either a single value of the pooling stride along all map axes, or a tuple (stride_d, stride_h, stride_w), where stride_d is the value of the pooling stride along the depth of the map, stride_h is the size of the pooling stride along the map height, and stride_w is the pooling stride along the width.


pad - possible to specify either a single padding value for all sides of the maps, or a tuple (pad_d, pad_h, pad_w), where pad_d is the padding value on each side along the depth of the map, pad_h is along the height of the map, and pad_w is along the width.

Important

If you set any of the size, stride or pad parameters different for the map axes, then the values of these parameters must be explicitly specified in a three-element tuple, otherwise an error will occur.


includePad - if the pad parameter was set to nonzero and new elements were added to the original tensor along the edges, then when includePad is set, their values will also affect the result of the pooling operation.

Examples


Necessary imports.

import numpy as np
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Modules import AvgPool3D

Info

gpuarray is required to properly place the tensor in the GPU.

For this module, the examples are given in a simplified version. For a more visual presentation, you can take a look at the AvgPool2D examples.

np.random.seed(123)
batchsize, maps, d, h, w = 1, 1, 6, 6, 6
data = gpuarray.to_gpu(np.random.randn(batchsize, maps, d, h, w).astype(np.float32))

Let us initialize the module with default parameters (size=2, stride=2, pad=0, includePad=True):

pool = AvgPool3D()
print(pool(data))

Let us leave all parameters the same size:

pool = AvgPool3D(size=4)
pool(data)

The size parameter can be set different for each axis of the map:

pool = AvgPool3D(size=(2, 4, 2))
pool(data)

The stride and the pad parameters can also be set different for each axis of the map:

pool = AvgPool3D(size=4, stride=(1, 4, 4), pad=(0, 1, 1))
pool(data)

As mentioned earlier, if the parameter has different values for different axes, then all these values must be specified explicitly. The following example will throw an error:

pool = AvgPool3D(stride=(1, 3))
pool(data)