Skip to content

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

np.random.seed(123)
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)
[[[[2. 2. 6.]
   [1. 3. 9.]
   [6. 1. 0.]]]]
upsample = Upsample2D(scale=2, mode="nearest")
print(upsample(data))
[[[[2. 2. 2. 2. 6. 6.]
   [2. 2. 2. 2. 6. 6.]
   [1. 1. 3. 3. 9. 9.]
   [1. 1. 3. 3. 9. 9.]
   [6. 6. 1. 1. 0. 0.]
   [6. 6. 1. 1. 0. 0.]]]]

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")
print(upsample(data))
[[[[2. 2. 6.]
   [2. 2. 6.]
   [1. 3. 9.]
   [1. 3. 9.]
   [6. 1. 0.]
   [6. 1. 0.]]]]

With linear interpolation, the results will be different:

upsample = Upsample2D(scale=2, mode="linear")
print(upsample(data))
[[[[2.         2.         2.         2.8000002  4.4        6.        ]
   [1.6        1.9200001  2.2400002  3.3600004  5.28       7.2000003 ]
   [1.2        1.84       2.48       3.9200003  6.1600003  8.400001  ]
   [2.0000002  2.2400002  2.48       3.52       5.3599997  7.2       ]
   [4.         3.1200001  2.24       2.16       2.8799999  3.6       ]
   [6.         4.         2.         0.79999995 0.39999998 0.        ]]]]