Pooling

Pooling functions can be used to:

using functions that calculate values such as:

For example, let the input matrix be 3 x 3 x 4 dimensions as shown below:

[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]]]

The down-sampling factor for each dimension indicates the magnitude of reduction in a dimension, where:

down-sampling factor used =

  • 0 if the down-sampling factor given is 1

  • min(1, down-sampling factor given - 1)

So if:

  • a down-sampling factor given is 2, and the original magnitude of the dimension is 2, it will be reduced to 1

  • a down-sampling factor given is 2, and the original magnitude of the dimension is 4, it will be reduced to 3

The down-sampling factors are expressed in an array with one element for each dimension, such as:

(1, 2, 2) will use the dimensions (3, 2, 3) for the result matrix

where:

  • first dimension: the 3 row/column groups of 12 cells each

  • second dimension: the 3 row groups of 4 cells each

  • third dimension: the 4 cells in each row

and use:

  • pooling function: maximum value

  • down-sampling factors for each axis: 1, 2, 2

Applying the down-sizing function and factors to the input matrix, we get:

[[[ 5  7]
  [ 9 11]]

 [[17 19]
  [21 23]]

 [[29 31]
  [33 35]]]

Python Example

To download the code for the example below click here.

"""
pooling_with_numpy.py
creates and tests a pooling function
"""

import numpy as np
from skimage.measure import block_reduce

# Define parameters for creating a test matrix.
start = 3
stop = 3
step = 4

# Define variables for pooling types.
type_mean = 'mean'
type_max = 'max'

# Define multiple down-sampling factors for each matrix axis.
down_sampling_factors_a = (3, 3, 1)
down_sampling_factors_b = (1, 3, 4)
down_sampling_factors_c = (3, 1, 2)
down_sampling_factors_d = (1, 2, 2)
down_sampling_factors_e = (1, 1, 1)


# Define a pooling function.
def pooling(input_matrix, down_sampling_factors, function):
    result = None
    if function == type_mean:
        result = block_reduce(input_matrix, block_size=down_sampling_factors, func=np.mean)
    elif function == type_max:
        result = block_reduce(input_matrix, block_size=down_sampling_factors, func=np.max)
    else:
        return result
    print(function + ' using axis down-sampling factors ' +
          str(down_sampling_factors) + str(' :'))
    print(result)
    print('')


# Create a test matrix.
test_matrix = np.arange(start * stop * step).reshape(start, stop, step)
print('test_matrix:')
print(test_matrix)
print('')

pooling(test_matrix, down_sampling_factors_a, type_mean)
pooling(test_matrix, down_sampling_factors_b, type_max)
pooling(test_matrix, down_sampling_factors_c, type_max)
pooling(test_matrix, down_sampling_factors_d, type_max)
pooling(test_matrix, down_sampling_factors_e, type_max)

The output is below:

test_matrix:
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]]]

mean using axis down-sampling factors (3, 3, 1) :
[[[16. 17. 18. 19.]]]

max using axis down-sampling factors (1, 3, 4) :
[[[11]]

 [[23]]

 [[35]]]

max using axis down-sampling factors (3, 1, 2) :
[[[25 27]
  [29 31]
  [33 35]]]

max using axis down-sampling factors (1, 2, 2) :
[[[ 5  7]
  [ 9 11]]

 [[17 19]
  [21 23]]

 [[29 31]
  [33 35]]]

max using axis down-sampling factors (1, 1, 1) :
[[[ 0  1  2  3]
  [ 4  5  6  7]
  [ 8  9 10 11]]

 [[12 13 14 15]
  [16 17 18 19]
  [20 21 22 23]]

 [[24 25 26 27]
  [28 29 30 31]
  [32 33 34 35]]]