Numpy实现UpSampling2D(上采样)

Posted AI浩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Numpy实现UpSampling2D(上采样)相关的知识,希望对你有一定的参考价值。

class UpSampling2D(Layer):
    """ Nearest neighbor up sampling of the input. Repeats the rows and
    columns of the data by size[0] and size[1] respectively.

    Parameters:
    -----------
    size: tuple
        (size_y, size_x) - The number of times each axis will be repeated.
    """
    def __init__(self, size=(2,2), input_shape=None):
        self.prev_shape = None
        self.trainable = True
        self.size = size
        self.input_shape = input_shape

    def forward_pass(self, X, training=True):
        self.prev_shape = X.shape
        # Repeat each axis as specified by size
        X_new = X.repeat(self.size[0], axis=2).repeat(self.size[1], axis=3)
        return X_new

    def backward_pass(self, accum_grad):
        # Down sample input to previous shape
        accum_grad = accum_grad[:, :, ::self.size[0], ::self.size[1]]
        return accum_grad

    def output_shape(self):
        channels, height, width = self.input_shape
        return channels, self.size[0] * height, self.size[1] * width

以上是关于Numpy实现UpSampling2D(上采样)的主要内容,如果未能解决你的问题,请参考以下文章

重新采样一个 numpy 数组

对一维 numpy 数组进行下采样

重新采样表示图像的 numpy 数组

对 numpy 数组进行二次采样/平均

对numpy数组中的每个第n个条目进行二次采样

如何在numpy的二维矩阵中随机采样