Upsample2D

Description

Info

Parent class: Module

Derived classes: -

Increases the dimension of two-dimensional maps by a specified number of times, filling new cells with values according to the selected mode (see parameters).

In contrast to deconvolution this layer is not trainable.

Initializing

def __init__(self, scale=2, mode="nearest", name=None):

Parameters

Parameter Allowed types Description Default
scale Union[int, tuple] Scale: a number by which the tensor dimensions will be multiplied 2
mode str New cells filling mode "nearest"
name str Layer name None

Explanations

scale - possible to specify either a single scale value in height or width, and a tuple of the form (scale_h, scale_w), where scale_h - scale value for the map height, and scale_w - for the width;


mode - possible options: "nearest" (copies the value of the nearest cell), "linear" (uses linear interpolation according to the values of nearby cells).

Examples

Necessary imports:

>>> import numpy as np
>>> from PuzzleLib.Backend import gpuarray
>>> from PuzzleLib.Modules import Upsample2D

Info

gpuarray is required to properly place the tensor in the GPU

>>> batchsize, maps, h, w = 1, 1, 3, 3
>>> data = gpuarray.to_gpu(np.random.randint(0, 10, (batchsize, maps, h, w)).astype(np.float32))
>>> print(data)
[[[[9. 8. 4.]
   [2. 7. 5.]
   [3. 8. 3.]]]]
>>> upsample = Upsample2D(scale=2, mode="nearest")
>>> upsample(data)
[[[[9. 9. 8. 8. 4. 4.]
   [9. 9. 8. 8. 4. 4.]
   [2. 2. 7. 7. 5. 5.]
   [2. 2. 7. 7. 5. 5.]
   [3. 3. 8. 8. 3. 3.]
   [3. 3. 8. 8. 3. 3.]]]]

As mentioned above, the scale can be set different for height and width (in this case, the length of the scale tuple should correspond to the number of map dimensions):

>>> upsample = Upsample2D(scale=(2, 1), mode="nearest")
>>> upsample(data)
[[[[9. 8. 4.]
   [9. 8. 4.]
   [2. 7. 5.]
   [2. 7. 5.]
   [3. 8. 3.]
   [3. 8. 3.]]]]

With linear interpolation, the results will be different:

>>> upsample = Upsample2D(scale=2, mode="linear")
>>> upsample(data)
[[[[9.        8.6       8.2       7.2       5.6       4.       ]
   [6.2000003 6.76      7.32      6.96      5.68      4.4      ]
   [3.3999999 4.92      6.44      6.7200003 5.7599998 4.8      ]
   [2.2       4.2       6.2       6.68      5.64      4.6      ]
   [2.6       4.6       6.6000004 6.84      5.3199997 3.8      ]
   [3.        5.        7.        7.        5.        3.       ]]]]