Skip to content

MaxUnpool2D

Description

Info

Parent class: Module

Derived classes: -

This module calculates the partial inverse value of the two-dimensional max pooling MaxPool2D.

The pooling operation is not completely reversible, since non-maximum values are lost during processing.

This module accepts the output data of MaxPool2D as input data, including the maximum value indexes, and calculates the partial inversion of the candidate tensor, placing its elements in the output tensor according to the maximum value indexes. All non-maximum values of the new tensor will be equal to zero.

Initializing

def __init__(self, maxpool2d, name=None):

Parameters

Parameter Allowed types Description Default
maxpool2d Module MaxPool2D class instance -
name str Layer name None

Explanations

-

Examples

Necessary imports.

import numpy as np
from PuzzleLib.Backend import gpuarray
from PuzzleLib.Modules import MaxPool2D, MaxUnpool2D

Info

gpuarray is required to properly place the tensor in the GPU

For the sake of simplicity, let us assume that the size of the batch and the number of maps are equal to 1:

np.random.seed(123)
batchsize, maps, h, w = 1, 1, 6, 6
indata = gpuarray.to_gpu(np.random.randint(0, 9, (batchsize, maps, h, w)).astype(np.float32))
print(indata)
[[[[2. 2. 6. 1. 3. 6.]
   [1. 0. 1. 0. 0. 3.]
   [4. 0. 0. 4. 1. 7.]
   [3. 2. 4. 7. 2. 4.]
   [8. 0. 7. 3. 4. 6.]
   [1. 5. 6. 2. 1. 8.]]]]

We initialize the instance of the MaxPool2D class with the default parameters (size=2, stride=2, pad=0). As mentioned above, an instance of the MaxUnpool2D class must accept a MaxPool2D object as an input:

maxpool2d = MaxPool2D()
maxunpool2d = MaxUnpool2D(maxpool2d)
print(maxpool2d(indata))
[[[[2. 6. 6.]
   [4. 7. 7.]
   [8. 7. 8.]]]]

Let us create a candidate tensor and perform an operation on it:

data = data = gpuarray.to_gpu(np.random.randint(0, 9, maxpool2d.data.shape).astype(np.float32))
print(data)
[[[[3. 5. 0.]
   [2. 6. 2.]
   [4. 4. 6.]]]]
print(maxunpool2d(data))
[[[[3. 0. 5. 0. 0. 0.]
   [0. 0. 0. 0. 0. 0.]
   [2. 0. 0. 0. 0. 2.]
   [0. 0. 0. 6. 0. 0.]
   [4. 0. 4. 0. 0. 0.]
   [0. 0. 0. 0. 0. 6.]]]]