예제 #1
0
def avg_pooling(x, pool_h, pool_w, stride):
    """
    Applies average pooling over an input array.

    Args:
        x (numpy.ndarray): The input array to be average pooled.
        pool_h (int): Height of the pooling window.
        pool_w (int): Width of the pooling window.
        stride (int): The stride of the sliding window.

    Returns:
        numpy.ndarray, an output array after applying average pooling on input array.
    """
    validator.check_integer("stride", stride, 0, Rel.GT)
    num, channel, height, width = x.shape
    out_h = (height - pool_h) // stride + 1
    out_w = (width - pool_w) // stride + 1

    col = im2col(x, pool_h, pool_w, stride)
    col = col.reshape(-1, pool_h * pool_w)

    out = np.mean(col, axis=1)
    out = out.reshape((num, out_h, out_w, channel)).transpose(0, 3, 1, 2)

    return out
예제 #2
0
    def __init__(self, kernel_size, stride, pad_mode):
        name = self.__class__.__name__
        super(_PoolNd, self).__init__()
        validator.check_type('kernel_size', kernel_size, [int, tuple])
        validator.check_type('stride', stride, [int, tuple])
        self.pad_mode = validator.check_string('pad_mode', pad_mode.upper(),
                                               ['VALID', 'SAME'])

        if isinstance(kernel_size, int):
            validator.check_integer("kernel_size", kernel_size, 1, Rel.GE)
        else:
            if (len(kernel_size) != 2 or (not isinstance(kernel_size[0], int))
                    or (not isinstance(kernel_size[1], int))
                    or kernel_size[0] <= 0 or kernel_size[1] <= 0):
                raise ValueError(
                    f'The kernel_size passed to cell {name} should be an positive int number or'
                    f'a tuple of two positive int numbers, but got {kernel_size}'
                )
        self.kernel_size = kernel_size

        if isinstance(stride, int):
            validator.check_integer("stride", stride, 1, Rel.GE)
        else:
            if (len(stride) != 2 or (not isinstance(stride[0], int))
                    or (not isinstance(stride[1], int)) or stride[0] <= 0
                    or stride[1] <= 0):
                raise ValueError(
                    f'The stride passed to cell {name} should be an positive int number or'
                    f'a tuple of two positive int numbers, but got {stride}')
        self.stride = stride
예제 #3
0
def max_pool_with_argmax(x, pool_h, pool_w, stride, pad):
    """Max pooling with argmax."""
    validator.check_integer("stride", stride, 0, Rel.GT)
    num, channel, height, width = x.shape
    out_h = (height + 2*pad - pool_h)//stride + 1
    out_w = (width + 2*pad - pool_w)//stride + 1
    col = im2col(x, pool_h, pool_w, stride, pad)
    col = col.reshape(-1, pool_h*pool_w)
    out = np.max(col, axis=1)
    out_argmax = np.argmax(col, axis=1)
    out = out.reshape(num, out_h, out_w, channel).transpose(0, 3, 1, 2)
    out_argmax = out_argmax.reshape(num, out_h, out_w, channel).transpose(0, 3, 1, 2)
    return out, out_argmax
예제 #4
0
def max_pooling(x, pool_h, pool_w, stride):
    """Max pooling."""
    validator.check_integer("stride", stride, 0, Rel.GT)
    num, channel, height, width = x.shape
    out_h = (height - pool_h) // stride + 1
    out_w = (width - pool_w) // stride + 1

    col = im2col(x, pool_h, pool_w, stride)
    col = col.reshape(-1, pool_h * pool_w)

    out = np.max(col, axis=1)
    out = out.reshape((num, out_h, out_w, channel)).transpose(0, 3, 1, 2)

    return out
예제 #5
0
def conv2d(x, weight, bias=None, stride=1, pad=0,
           dilation=1, groups=1, padding_mode='zeros'):
    """Convolution 2D."""
    # pylint: disable=unused-argument
    validator.check_integer("stride", stride, 0, Rel.GT)
    batch_num, _, x_h, x_w = x.shape
    filter_num, _, filter_h, filter_w = weight.shape
    out_h = 1 + int((x_h + 2 * pad - filter_h - (filter_h - 1) * (dilation - 1)) / stride)
    out_w = 1 + int((x_w + 2 * pad - filter_w - (filter_w - 1) * (dilation - 1)) / stride)
    col = im2col(x, filter_h, filter_w, stride, pad, dilation)
    col_w = np.reshape(weight, (filter_num, -1)).T
    out = np.dot(col, col_w)
    out = out.reshape(batch_num, out_h, out_w, -1).transpose(0, 3, 1, 2)
    if bias is not None:
        out += bias
    return out
예제 #6
0
 def __init__(self, kernel_size, stride, pad_mode, padding=0, pool=None):
     super(_PoolNd, self).__init__()
     self.kernel_size = kernel_size
     self.stride = stride
     self.pad_mode = pad_mode
     self.padding = validator.check_integer('padding', padding, 0, Rel.GE)
     self.pool = pool
     if self.pool is None:
         raise NotImplementedError
예제 #7
0
 def _init_depthwise_conv2d(self):
     """Init depthwise conv2d op"""
     if context.get_context("device_target") == "Ascend" and self.group > 1:
         self.dilation = self._dilation
         validator.check_integer('group', self.group, self.in_channels,
                                 Rel.EQ)
         validator.check_integer('group', self.group, self.out_channels,
                                 Rel.EQ)
         self.conv2d = P.DepthwiseConv2dNative(channel_multiplier=1,
                                               kernel_size=self.kernel_size,
                                               pad_mode=self.pad_mode,
                                               pad=self.padding,
                                               stride=self.stride,
                                               dilation=self.dilation)
         weight_shape = [1, self.in_channels, *self.kernel_size]
         self.weight = Parameter(initializer(self.weight_init,
                                             weight_shape),
                                 name='weight')
예제 #8
0
def im2col(img, filter_h, filter_w, stride=1, pad=0, dilation=1):
    """Rearranges an image to row vector."""
    validator.check_integer("stride", stride, 0, Rel.GT)
    batch_num, channel, height, width = img.shape
    out_h = (height + 2*pad - filter_h- (filter_h - 1) * (dilation - 1))//stride + 1
    out_w = (width + 2*pad - filter_w- (filter_w - 1) * (dilation - 1))//stride + 1

    img = np.pad(img, [(0, 0), (0, 0), (pad, pad), (pad, pad)], 'constant')
    col = np.zeros((batch_num, channel, filter_h, filter_w, out_h, out_w)).astype(img.dtype)

    for y in range(filter_h):
        y_max = y + stride*out_h
        for x in range(filter_w):
            x_max = x + stride*out_w
            col[:, :, y, x, :, :] = img[:, :, y:y_max:stride, x:x_max:stride]

    col = col.transpose(0, 4, 5, 1, 2, 3).reshape(batch_num*out_h*out_w, -1)
    return col
예제 #9
0
def col2im(col, input_shape, filter_h, filter_w, stride=1, pad=0):
    """Rearranges a row vector to an image."""
    validator.check_integer("stride", stride, 0, Rel.GT)
    batch_num, channel, height, width = input_shape
    out_h = (height + 2*pad - filter_h)//stride + 1
    out_w = (width + 2*pad - filter_w)//stride + 1
    col = col.reshape(batch_num, out_h, out_w, channel, filter_h, filter_w) \
             .transpose(0, 3, 4, 5, 1, 2)

    img = np.zeros((batch_num,
                    channel,
                    height + 2*pad + stride - 1,
                    width + 2*pad + stride - 1)) \
            .astype(col.dtype)
    for y in range(filter_h):
        y_max = y + stride*out_h
        for x in range(filter_w):
            x_max = x + stride*out_w
            img[:, :, y:y_max:stride, x:x_max:stride] += col[:, :, y, x, :, :]

    return img[:, :, pad:height + pad, pad:width + pad]
예제 #10
0
 def __init__(self,
              in_channels,
              out_channels,
              kernel_size,
              stride=1,
              pad_mode='same',
              padding=0,
              dilation=1,
              group=1,
              has_bias=False,
              weight_init='normal',
              bias_init='zeros'):
     super(DepthwiseConv2d, self).__init__()
     self.kernel_size = twice(kernel_size)
     self.stride = twice(stride)
     self.dilation = twice(dilation)
     self.in_channels = check_int_positive(in_channels)
     self.out_channels = check_int_positive(out_channels)
     validator.check_integer('group', group, in_channels, Rel.EQ)
     validator.check_integer('group', group, out_channels, Rel.EQ)
     validator.check_integer('group', group, 1, Rel.GE)
     self.pad_mode = pad_mode
     self.dilation = dilation
     self.group = group
     self.has_bias = has_bias
     self.weight_init = weight_init
     self.bias_init = bias_init
     Validator.check_value_type('padding', padding, (int, tuple),
                                self.cls_name)
     if isinstance(padding, tuple):
         Validator.check_integer('padding size', len(padding), 4, Rel.EQ,
                                 self.cls_name)
     self.padding = padding
     self.conv = P.DepthwiseConv2dNative(channel_multiplier=1,
                                         kernel_size=self.kernel_size,
                                         pad_mode=self.pad_mode,
                                         pad=self.padding,
                                         stride=self.stride,
                                         dilation=self.dilation)
     self.bias_add = P.BiasAdd()
     weight_shape = [1, in_channels, *self.kernel_size]
     self.weight = Parameter(initializer(weight_init, weight_shape),
                             name='weight')
     if check_bool(has_bias):
         self.bias = Parameter(initializer(bias_init, [out_channels]),
                               name='bias')
     else:
         if bias_init != 'zeros':
             logger.warning(
                 "value of `has_bias` is False, value of `bias_init` will be ignore."
             )
         self.bias = None
예제 #11
0
 def __init__(self,
              max_val=1.0,
              filter_size=11,
              filter_sigma=1.5,
              k1=0.01,
              k2=0.03):
     super(SSIM, self).__init__()
     validator.check_type('max_val', max_val, [int, float])
     validator.check('max_val', max_val, '', 0.0, Rel.GT)
     self.max_val = max_val
     self.filter_size = validator.check_integer('filter_size', filter_size,
                                                1, Rel.GE)
     self.filter_sigma = validator.check_float_positive(
         'filter_sigma', filter_sigma)
     validator.check_type('k1', k1, [float])
     self.k1 = validator.check_number_range('k1', k1, 0.0, 1.0,
                                            Rel.INC_NEITHER)
     validator.check_type('k2', k2, [float])
     self.k2 = validator.check_number_range('k2', k2, 0.0, 1.0,
                                            Rel.INC_NEITHER)
     self.mean = P.DepthwiseConv2dNative(channel_multiplier=1,
                                         kernel_size=filter_size)