<commit_before>import numpy
import warnings

from chainer.backends import cuda
from chainer import initializer

# Original code from Berkeley FCN
# https://github.com/shelhamer/fcn.berkeleyvision.org/blob/master/surgery.py


def _get_upsampling_filter(size, ndim):
    """Make a 2D bilinear kernel suitable for upsampling"""
    factor = (size + 1) // 2
    if size % 2 == 1:
        center = factor - 1.
    else:
        center = factor - 0.5
    slices = (slice(None, size),) * ndim
    og = numpy.ogrid[slices]
    filt = 1
    for og_i in og:
        filt = filt * (1. - abs(og_i - center) / factor)
    return filt


class UpsamplingDeconvFilter(initializer.Initializer):
    """Initializes array with upsampling deconvolution filter.

    The array is initialized with a standard image upsampling weight.
    This initializer is often used as inital weight for
    :func:`~chainer.links.DeconvolutionND`.

    Reference: Long et al., https://arxiv.org/abs/1411.4038

    Attributes:
        interpolation (str): Upsampling interpolation method.
        Default is 'bilinear'.

    """

    def __init__(self, interpolation='bilinear', dtype=None):
        super(UpsamplingDeconvFilter, self).__init__(dtype)
        if interpolation != 'bilinear':
            warnings.warn(
                'Unsupported interpolation method: {}'.format(interpolation))

    def __call__(self, array):
        if self.dtype is not None:
            assert array.dtype == self.dtype
        xp = cuda.get_array_module(array)

        in_c, out_c = array.shape[:2]
        assert in_c == out_c or out_c == 1

        ksize = None
        for k in array.shape[2:]:
            if ksize is None:
                ksize = k
            else:
                assert ksize == k

        filt = _get_upsampling_filter(ksize, ndim=array.ndim - 2)
        filt = xp.asarray(filt)

        array[...] = 0.
        if out_c == 1:
            array[range(in_c), 0, ...] = filt<commit_msg>rename _get_upsampling_filter -> _get_bilinear_upsampling_filter<commit_after>import numpy
import warnings

from chainer.backends import cuda
from chainer import initializer

# Original code from Berkeley FCN
# https://github.com/shelhamer/fcn.berkeleyvision.org/blob/master/surgery.py


def _get_bilinear_upsampling_filter(size, ndim):
    """Make a ND bilinear kernel suitable for upsampling"""
    factor = (size + 1) // 2
    if size % 2 == 1:
        center = factor - 1.
    else:
        center = factor - 0.5
    slices = (slice(None, size),) * ndim
    og = numpy.ogrid[slices]
    filt = 1
    for og_i in og:
        filt = filt * (1. - abs(og_i - center) / factor)
    return filt


class UpsamplingDeconvFilter(initializer.Initializer):
    """Initializes array with upsampling deconvolution filter.

    The array is initialized with a standard image upsampling weight.
    This initializer is often used as inital weight for
    :func:`~chainer.links.DeconvolutionND`.

    Reference: Long et al., https://arxiv.org/abs/1411.4038

    Attributes:
        interpolation (str): Upsampling interpolation method.
        Default is 'bilinear'.

    """

    def __init__(self, interpolation='bilinear', dtype=None):
        super(UpsamplingDeconvFilter, self).__init__(dtype)
        if interpolation != 'bilinear':
            warnings.warn(
                'Unsupported interpolation method: {}'.format(interpolation))

    def __call__(self, array):
        if self.dtype is not None:
            assert array.dtype == self.dtype
        xp = cuda.get_array_module(array)

        in_c, out_c = array.shape[:2]
        assert in_c == out_c or out_c == 1

        ksize = None
        for k in array.shape[2:]:
            if ksize is None:
                ksize = k
            else:
                assert ksize == k

        filt = _get_bilinear_upsampling_filter(ksize, ndim=array.ndim - 2)
        filt = xp.asarray(filt)

        array[...] = 0.
        if out_c == 1:
            array[range(in_c), 0, ...] = filt
{'fraction_non_alphanumeric': 0.10257693269952464, 'fraction_numerical': 0.012259194395796848, 'mean_word_length': 2.334445371142619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '668248', 'n_tokens_mistral': 1325, 'n_tokens_neox': 1242, 'n_words': 345}