vision.py 9.2 KB
Newer Older
R
ruri 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
# TODO: define specitial functions used in computer vision task
R
ruri 已提交
16

Z
zhiboniu 已提交
17
from .. import Layer
R
ruri 已提交
18 19
from .. import functional

20 21
__all__ = []

R
ruri 已提交
22

Z
zhiboniu 已提交
23
class PixelShuffle(Layer):
R
ruri 已提交
24
    """
25 26

    PixelShuffle Layer
R
ruri 已提交
27

28 29 30
    Rearranges elements in a tensor of shape :math:`[N, C, H, W]`
    to a tensor of shape :math:`[N, C/upscale_factor^2, H*upscale_factor, W \times upscale_factor]`,
    or from shape :math:`[N, H, W, C]` to :math:`[N, H \times upscale_factor, W \times upscale_factor, C/upscale_factor^2]`.
R
ruri 已提交
31 32 33 34 35 36 37 38 39
    This is useful for implementing efficient sub-pixel convolution
    with a stride of 1/upscale_factor.
    Please refer to the paper: `Real-Time Single Image and Video Super-Resolution
    Using an Efficient Sub-Pixel Convolutional Neural Network <https://arxiv.org/abs/1609.05158v2>`_ .
    by Shi et. al (2016) for more details.

    Parameters:

        upscale_factor(int): factor to increase spatial resolution.
40
        data_format (str, optional): The data format of the input and output data. An optional string from: "NCHW", "NHWC". The default is "NCHW". When it is "NCHW", the data is stored in the order of: [batch_size, input_channels, input_height, input_width].
R
ruri 已提交
41 42 43
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Shape:
44 45
        - x: 4-D tensor with shape of :math:`(N, C, H, W)` or :math:`(N, H, W, C)`.
        - out: 4-D tensor with shape of :math:`(N, C/upscale_factor^2, H \times upscale_factor, W \times upscale_factor)` or :math:`(N, H \times upscale_factor, W \times upscale_factor, C/upscale_factor^2)`.
R
ruri 已提交
46 47 48 49


    Examples:
        .. code-block:: python
50

R
ruri 已提交
51 52 53
            import paddle
            import paddle.nn as nn

54
            x = paddle.randn(shape=[2,9,4,4])
R
ruri 已提交
55
            pixel_shuffle = nn.PixelShuffle(3)
56
            out_var = pixel_shuffle(x)
R
ruri 已提交
57
            out = out_var.numpy()
58
            print(out.shape)
R
ruri 已提交
59 60 61 62 63 64 65 66 67 68 69
            # (2, 1, 12, 12)

    """

    def __init__(self, upscale_factor, data_format="NCHW", name=None):
        super(PixelShuffle, self).__init__()

        if not isinstance(upscale_factor, int):
            raise TypeError("upscale factor must be int type")

        if data_format not in ["NCHW", "NHWC"]:
70 71 72 73
            raise ValueError(
                "Data format should be 'NCHW' or 'NHWC'."
                "But recevie data format: {}".format(data_format)
            )
R
ruri 已提交
74 75 76 77 78 79

        self._upscale_factor = upscale_factor
        self._data_format = data_format
        self._name = name

    def forward(self, x):
80 81 82
        return functional.pixel_shuffle(
            x, self._upscale_factor, self._data_format, self._name
        )
83 84 85

    def extra_repr(self):
        main_str = 'upscale_factor={}'.format(self._upscale_factor)
86
        if self._data_format != 'NCHW':
87 88 89 90
            main_str += ', data_format={}'.format(self._data_format)
        if self._name is not None:
            main_str += ', name={}'.format(self._name)
        return main_str
91 92


93 94
class PixelUnshuffle(Layer):
    """
95
    Rearranges elements in a tensor of shape :math:`[N, C, H, W]`
96 97
    to a tensor of shape :math:`[N, r^2C, H/r, W/r]`, or from shape
    :math:`[N, H, W, C]` to :math:`[N, H/r, W/r, r^2C]`, where :math:`r` is the
98 99 100 101 102 103 104
    downscale factor. This operation is the reversion of PixelShuffle operation.
    Please refer to the paper: `Real-Time Single Image and Video Super-Resolution
    Using an Efficient Sub-Pixel Convolutional Neural Network <https://arxiv.org/abs/1609.05158v2>`_ .
    by Shi et. al (2016) for more details.

    Parameters:
        downscale_factor (int): Factor to decrease spatial resolution.
105
        data_format (str, optional): The data format of the input and output data. An optional string of NCHW or NHWC. The default is NCHW. When it is NCHW, the data is stored in the order of [batch_size, input_channels, input_height, input_width].
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        name (str, optional): Name for the operation (optional, default is None). Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - **x**: 4-D tensor with shape of :math:`[N, C, H, W]` or :math:`[N, C, H, W]`.
        - **out**: 4-D tensor with shape of :math:`[N, r^2C, H/r, W/r]` or :math:`[N, H/r, W/r, r^2C]`, where :math:`r` is :attr:`downscale_factor`.

    Examples:
        .. code-block:: python

            import paddle
            import paddle.nn as nn

            x = paddle.randn([2, 1, 12, 12])
            pixel_unshuffle = nn.PixelUnshuffle(3)
            out = pixel_unshuffle(x)
121 122
            print(out.shape)
            # [2, 9, 4, 4]
123 124 125 126 127 128 129 130 131 132 133 134 135

    """

    def __init__(self, downscale_factor, data_format="NCHW", name=None):
        super(PixelUnshuffle, self).__init__()

        if not isinstance(downscale_factor, int):
            raise TypeError("Downscale factor must be int type")

        if downscale_factor <= 0:
            raise ValueError("Downscale factor must be positive")

        if data_format not in ["NCHW", "NHWC"]:
136 137 138 139
            raise ValueError(
                "Data format should be 'NCHW' or 'NHWC'."
                "But recevie data format: {}".format(data_format)
            )
140 141 142 143 144 145

        self._downscale_factor = downscale_factor
        self._data_format = data_format
        self._name = name

    def forward(self, x):
146 147 148
        return functional.pixel_unshuffle(
            x, self._downscale_factor, self._data_format, self._name
        )
149 150 151 152 153 154 155 156 157 158

    def extra_repr(self):
        main_str = 'downscale_factor={}'.format(self._downscale_factor)
        if self._data_format != 'NCHW':
            main_str += ', data_format={}'.format(self._data_format)
        if self._name is not None:
            main_str += ', name={}'.format(self._name)
        return main_str


159 160 161 162 163
class ChannelShuffle(Layer):
    """
    This operator divides channels in a tensor of shape [N, C, H, W] or [N, H, W, C] into g groups,
    getting a tensor with the shape of [N, g, C/g, H, W] or [N, H, W, g, C/g], and transposes them
    as [N, C/g, g, H, W] or [N, H, W, g, C/g], then rearranges them to original tensor shape. This
164 165
    operation can improve the interaction between channels, using features efficiently. Please
    refer to the paper: `ShuffleNet: An Extremely Efficient
166
    Convolutional Neural Network for Mobile Devices <https://arxiv.org/abs/1707.01083>`_ .
167
    by Zhang et. al (2017) for more details.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

    Parameters:
        groups (int): Number of groups to divide channels in.
        data_format (str): The data format of the input and output data. An optional string of NCHW or NHWC. The default is NCHW. When it is NCHW, the data is stored in the order of [batch_size, input_channels, input_height, input_width].
        name (str, optional): Name for the operation (optional, default is None). Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.

    Shape:
        - **x**: 4-D tensor with shape of [N, C, H, W] or [N, H, W, C].
        - **out**: 4-D tensor with shape and dtype same as x.

    Examples:
        .. code-block:: python

            import paddle
            import paddle.nn as nn
            x = paddle.arange(0, 0.6, 0.1, 'float32')
            x = paddle.reshape(x, [1, 6, 1, 1])
            # [[[[0.        ]],
            #   [[0.10000000]],
            #   [[0.20000000]],
            #   [[0.30000001]],
            #   [[0.40000001]],
            #   [[0.50000000]]]]
            channel_shuffle = nn.ChannelShuffle(3)
            y = channel_shuffle(x)
            # [[[[0.        ]],
            #   [[0.20000000]],
            #   [[0.40000001]],
            #   [[0.10000000]],
            #   [[0.30000001]],
            #   [[0.50000000]]]]
    """

    def __init__(self, groups, data_format="NCHW", name=None):
        super(ChannelShuffle, self).__init__()

        if not isinstance(groups, int):
            raise TypeError("groups must be int type")

        if groups <= 0:
            raise ValueError("groups must be positive")

        if data_format not in ["NCHW", "NHWC"]:
211 212 213 214
            raise ValueError(
                "Data format should be 'NCHW' or 'NHWC'."
                "But recevie data format: {}".format(data_format)
            )
215 216 217 218 219 220

        self._groups = groups
        self._data_format = data_format
        self._name = name

    def forward(self, x):
221 222 223
        return functional.channel_shuffle(
            x, self._groups, self._data_format, self._name
        )
224 225 226 227 228 229 230 231

    def extra_repr(self):
        main_str = 'groups={}'.format(self._groups)
        if self._data_format != 'NCHW':
            main_str += ', data_format={}'.format(self._data_format)
        if self._name is not None:
            main_str += ', name={}'.format(self._name)
        return main_str