def __init__( self, in_channels: int, out_channels: int, kernel_size: _size_2_t, stride: _size_2_t = 1, padding: _size_2_t = 0, output_padding: _size_2_t = 0, groups: int = 1, bias: bool = True, dilation: int = 1, padding_mode: str = "zeros", ) -> None: super().__init__() assert padding_mode == "zeros" self.kernel_size = _pair(kernel_size) self.stride = _pair(stride) self.padding = _pair(padding) self.output_padding = _pair(output_padding) self.dilation = _pair(dilation) self.groups = groups assert in_channels % groups == 0 assert out_channels % groups == 0 self.weight = flow.nn.Parameter( flow.Tensor(in_channels, out_channels // groups, *self.kernel_size) ) self.in_channel_groups = in_channels // groups self.filters = out_channels self.bias = None self._bias_add_op = None if bias: self.bias = flow.nn.Parameter(flow.Tensor(out_channels)) self.reset_parameters()
def __init__( self, kernel_size: _size_2_t, dilation: _size_2_t = 1, padding: _size_2_t = 0, stride: _size_2_t = 1, ) -> None: r"""This op extracts elements in a local window from input tensor, it also called `img2col`. Consider a batched :attr:`input` tensor of shape :math:`(N, C, *)`, where :math:`N` is the batch dimension, :math:`C` is the channel dimension, and :math:`*` represent arbitrary spatial dimensions. This operation flattens each sliding :attr:`kernel_size`-sized block within the spatial dimensions of :attr:`input` into a column (i.e., last dimension) of a 3-D :attr:`output` tensor of shape :math:`(N, C \times \prod(\text{kernel\_size}), L)`, where :math:`C \times \prod(\text{kernel\_size})` is the total number of values within each block (a block has :math:`\prod(\text{kernel\_size})` spatial locations each containing a :math:`C`-channeled vector), and :math:`L` is the total number of such blocks: .. math:: L = \prod_d \left\lfloor\frac{\text{spatial\_size}[d] + 2 \times \text{padding}[d] % - \text{dilation}[d] \times (\text{kernel\_size}[d] - 1) - 1}{\text{stride}[d]} + 1\right\rfloor, where :math:`\text{spatial\_size}` is formed by the spatial dimensions of :attr:`input` (:math:`*` above), and :math:`d` is over all spatial dimensions. Therefore, indexing :attr:`output` at the last dimension (column dimension) gives all values within a certain block. Args: kernel_size (_size_2_t): The size of kernel. dilation (_size_2_t, optional): The dilation rate. Defaults to 1. padding (_size_2_t, optional): The padding value. Defaults to 0. stride (_size_2_t, optional): The stride of sliding window. Defaults to 1. For example: .. code-block:: python >>> import oneflow as flow >>> import numpy as np >>> x_tensor = flow.Tensor(np.random.randn(1, 1, 4, 4)) >>> unfold = flow.nn.Unfold(kernel_size=3, padding=1) >>> out = unfold(x_tensor) >>> out.shape oneflow.Size([1, 9, 16]) """ super(Unfold, self).__init__() self.kernel_size = _pair(kernel_size) self.dilation = _pair(dilation) self.padding = _pair(padding) self.stride = _pair(stride)
def __init__( self, output_size: _size_2_t, kernel_size: _size_2_t, dilation: _size_2_t = 1, padding: _size_2_t = 0, stride: _size_2_t = 1, ) -> None: r"""Combines an array of sliding local blocks into a large containing tensor, it also called `col2img`. Consider a batched :attr:`input` tensor containing sliding local blocks, e.g., patches of images, of shape :math:`(N, C \times \prod(\text{kernel\_size}), L)`, where :math:`N` is batch dimension, :math:`C \times \prod(\text{kernel\_size})` is the number of values within a block (a block has :math:`\prod(\text{kernel\_size})` spatial locations each containing a :math:`C`-channeled vector), and :math:`L` is the total number of blocks. (This is exactly the same specification as the output shape of :class:`~torch.nn.Unfold`.) This operation combines these local blocks into the large :attr:`output` tensor of shape :math:`(N, C, \text{output\_size}[0], \text{output\_size}[1], \dots)` by summing the overlapping values. Similar to :class:`~torch.nn.Unfold`, the arguments must satisfy .. math:: L = \prod_d \left\lfloor\frac{\text{output\_size}[d] + 2 \times \text{padding}[d] % - \text{dilation}[d] \times (\text{kernel\_size}[d] - 1) - 1}{\text{stride}[d]} + 1\right\rfloor, Args: output_size (_size_2_t): The spatial dimension of output tensor. kernel_size (_size_2_t): The size of kernel. dilation (_size_2_t, optional): The dilation rate. Defaults to 1. padding (_size_2_t, optional): The padding value. Defaults to 0. stride (_size_2_t, optional): The stride of sliding window. Defaults to 1. For example: .. code-block:: python >>> import oneflow as flow >>> import numpy as np >>> x_tensor = flow.Tensor(np.random.randn(1, 9, 16)) >>> fold = flow.nn.Fold(output_size=(4, 4), kernel_size=3, padding=1) >>> out = fold(x_tensor) >>> out.shape oneflow.Size([1, 1, 4, 4]) """ super(Fold, self).__init__() self.output_size = output_size self.kernel_size = _pair(kernel_size) self.dilation = _pair(dilation) self.padding = _pair(padding) self.stride = _pair(stride)
def __init__( self, kernel_size: _size_2_t, stride: Optional[_size_2_t] = None, padding: _size_2_t = 0, ceil_mode: bool = False, count_include_pad: bool = True, divisor_override: int = 0, ): super().__init__() self.kernel_size = _pair(kernel_size) self.stride = _pair(stride) if (stride is not None) else _pair(kernel_size) self.ceil_mode = ceil_mode if os.getenv("ONEFLOW_ENABLE_NHWC") == "1": self.data_format = "NHWC" self.channel_pos = "channels_last" assert isinstance(padding, int) or isinstance( padding, tuple), "padding can only int int or tuple of 2 ints." padding = _pair(padding) if len(padding) == 2: if self.data_format == "NCHW": padding = (0, 0, padding[0], padding[1]) elif self.data_format == "NHWC": padding = (0, padding[0], padding[1], 0) else: raise ValueError("error padding param!") self.padding = padding if not count_include_pad: raise ValueError( "AvgPool2d with NHWC data format don't support count_include_pad for now." ) if divisor_override != 0: raise ValueError( "AvgPool2d with NHWC data format don't support divisor_override for now." ) # TODO(yaochi): align with pytorch when padding is asymmetric self._padding_type, _pads_list = calc_pool_padding( padding, get_dhw_offset(self.channel_pos), 2) self._padding_before = [pad[0] for pad in _pads_list] self._padding_after = [pad[1] for pad in _pads_list] else: self.data_format = "NCHW" self.channel_pos = "channels_first" self.padding = _pair(padding) self.count_include_pad = count_include_pad self.divisor_override = int(divisor_override)
def __init__( self, in_channels: int, out_channels: int, kernel_size: _size_2_t, stride: _size_2_t = 1, padding: Union[str, _size_2_t] = 0, dilation: _size_2_t = 1, groups: int = 1, bias: bool = True, padding_mode: str = "zeros", ): super().__init__() assert padding_mode == "zeros" self.padding_mode = padding_mode self.kernel_size = _pair(kernel_size) self.stride = _pair(stride) self.dilation = _pair(dilation) self.padding = (get_padding(padding, self.kernel_size, self.dilation, self.stride) if isinstance( padding, str) else _pair(padding)) self.groups = groups if os.getenv("ONEFLOW_ENABLE_NHWC") == "1": self.channel_pos = "channels_last" else: self.channel_pos = "channels_first" assert in_channels % groups == 0 assert out_channels % groups == 0 self.in_channels = in_channels self.out_channels = out_channels if self.channel_pos == "channels_first": self.weight = flow.nn.Parameter( flow.Tensor(out_channels, in_channels // groups, *self.kernel_size)) else: self.weight = flow.nn.Parameter( flow.Tensor(out_channels, *self.kernel_size, in_channels // groups)) self.out_channel_groups = out_channels // groups self.bias = None if bias: self.bias = flow.nn.Parameter(flow.Tensor(out_channels)) self.reset_parameters()
def __init__( self, kernel_size: _size_2_t, stride: Optional[_size_2_t] = None, padding: _size_2_t = 0, dilation: _size_2_t = 1, return_indices: bool = False, ceil_mode: bool = False, ): super().__init__() self.kernel_size = _pair(kernel_size) self.stride = _pair(stride) if (stride is not None) else _pair(kernel_size) self.padding = _pair(padding) self.dilation = _pair(dilation) self.return_indices = return_indices self.ceil_mode = ceil_mode if os.getenv("ONEFLOW_ENABLE_NHWC") == "1": self.channel_pos = "channels_last" else: self.channel_pos = "channels_first"
def __init__(self, output_size) -> None: super().__init__() assert output_size is not None, "'output_size' cannot be NoneType" self.output_size = _pair(output_size)