o
    Qe%                     @   sP   d dl mZ d dl mZ g ZG dd deZG dd deZG dd deZd	S )
   )Layer)
functionalc                       2   e Zd ZdZd
 fdd	Zdd Zdd	 Z  ZS )PixelShufflea  
    
    PixelShuffle Layer    

    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 	imes upscale_factor]`,
    or from shape :math:`[N, H, W, C]` to :math:`[N, H 	imes upscale_factor, W 	imes upscale_factor, C/upscale_factor^2]`.
    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.
        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].
        name (str, optional): Name for the operation (optional, default is None). 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, H, W, C)`.
        - out: 4-D tensor with shape of :math:`(N, C/upscale_factor^2, H 	imes upscale_factor, W 	imes upscale_factor)` or :math:`(N, H 	imes upscale_factor, W 	imes upscale_factor, C/upscale_factor^2)`.


    Examples:
        .. code-block:: python
            
            import paddle
            import paddle.nn as nn

            x = paddle.randn(shape=[2,9,4,4])
            pixel_shuffle = nn.PixelShuffle(3)
            out_var = pixel_shuffle(x)
            out = out_var.numpy()
            print(out.shape)
            # (2, 1, 12, 12)

    NCHWNc                    sL   t t|   t|tstd|dvrtd||| _|| _	|| _
d S )Nzupscale factor must be int typer   ZNHWCBData format should be 'NCHW' or 'NHWC'.But recevie data format: {})superr   __init__
isinstanceint	TypeError
ValueErrorformat_upscale_factor_data_format_name)selfZupscale_factordata_formatname	__class__ FD:\Projects\ConvertPro\env\Lib\site-packages\paddle/nn/layer/vision.pyr
   ?   s   

zPixelShuffle.__init__c                 C      t || j| j| jS N)r   Zpixel_shuffler   r   r   r   xr   r   r   forwardM      
zPixelShuffle.forwardc                 C   D   d | j}| jdkr|d | j7 }| jd ur |d | j7 }|S )Nzupscale_factor={}r   , data_format={}	, name={})r   r   r   r   r   Zmain_strr   r   r   
extra_reprQ      

zPixelShuffle.extra_reprr   N__name__
__module____qualname____doc__r
   r   r$   __classcell__r   r   r   r   r      s
    'r   c                       r   )PixelUnshuffleaO  
    Rearranges elements in a tensor of shape :math:`[N, C, H, W]`
    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 
    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.
        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].
        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)
            print(out.shape)
            # [2, 9, 4, 4]

    r   Nc                    \   t t|   t|tstd|dkrtd|dvr#td||| _|| _	|| _
d S )Nz!Downscale factor must be int type    z!Downscale factor must be positiver   r   )r	   r-   r
   r   r   r   r   r   _downscale_factorr   r   )r   Zdownscale_factorr   r   r   r   r   r
   {      

zPixelUnshuffle.__init__c                 C   r   r   )r   Zpixel_unshuffler0   r   r   r   r   r   r   r      r   zPixelUnshuffle.forwardc                 C   r    )Nzdownscale_factor={}r   r!   r"   )r   r0   r   r   r#   r   r   r   r$      r%   zPixelUnshuffle.extra_reprr&   r'   r   r   r   r   r-   Z   s
     r-   c                       r   )ChannelShufflea  
    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
    operation can improve the interaction between channels, using features efficiently. Please 
    refer to the paper: `ShuffleNet: An Extremely Efficient 
    Convolutional Neural Network for Mobile Devices <https://arxiv.org/abs/1707.01083>`_ .
    by Zhang et. al (2017) for more details. 

    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]]]]
    r   Nc                    r.   )Nzgroups must be int typer/   zgroups must be positiver   r   )r	   r2   r
   r   r   r   r   r   _groupsr   r   )r   groupsr   r   r   r   r   r
      r1   zChannelShuffle.__init__c                 C   r   r   )r   Zchannel_shuffler3   r   r   r   r   r   r   r      s   zChannelShuffle.forwardc                 C   r    )Nz	groups={}r   r!   r"   )r   r3   r   r   r#   r   r   r   r$      r%   zChannelShuffle.extra_reprr&   r'   r   r   r   r   r2      s
    )r2   N) r   r   __all__r   r-   r2   r   r   r   r   <module>   s   C?