Ë
    •\;jO  ã                   ób   — d dl Zd dlmZ ddlmZmZmZ ddlm	Z	m
Z
 ddlmZ g Z G d„ d	e«      Zy)
é    N)Ú_C_opsé   )ÚcoreÚ	frameworkÚunique_name)Ú_current_expected_placeÚin_dygraph_modeé   )ÚInitializerc                   ó*   ‡ — e Zd ZdZˆ fd„Zdd„Zˆ xZS )ÚBilinearae  
    This initializer can be used in transposed convolution operator to
    act as upsampling. Users can upsample a feature map with shape of
    (B, C, H, W) by any integer factor.

    Returns:
        Bilinear initializer instance objects.

    Examples:

        .. code-block:: python

            >>> import math

            >>> import paddle
            >>> import paddle.nn as nn
            >>> from paddle.regularizer import L2Decay

            >>> factor = 2
            >>> C = 2
            >>> B = 8
            >>> H = W = 32
            >>> w_attr = paddle.ParamAttr(learning_rate=0.,
            ...                           regularizer=L2Decay(0.),
            ...                           initializer=nn.initializer.Bilinear())
            >>> data = paddle.rand([B, 3, H, W], dtype='float32')
            >>> conv_up = nn.Conv2DTranspose(3,
            ...                              out_channels=C,
            ...                              kernel_size=2 * factor - factor % 2,
            ...                              padding=int(
            ...                                  math.ceil((factor - 1) / 2.)),
            ...                              stride=factor,
            ...                              weight_attr=w_attr,
            ...                              bias_attr=False)
            >>> x = conv_up(data)

    Where, `out_channels=C` and `groups=C` means this is channel-wise transposed
    convolution. The filter shape will be (C, 1, K, K) where K is `kernel_size`,
    This initializer will set a (K, K) interpolation kernel for every channel
    of the filter identically. The resulting shape of the output feature map
    will be (B, C, factor * H, factor * W). Note that the learning rate and the
    weight decay are set to 0 in order to keep coefficient values of bilinear
    interpolation unchanged during training.

    c                 ó"   •— t         ‰| �  «        y)z$Constructor for BilinearInitializer.N)ÚsuperÚ__init__)ÚselfÚ	__class__s    €úgG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/nn/initializer/Bilinear.pyr   zBilinear.__init__I   s   ø€ ä‰ÑÕó    c           
      ó°	  — | j                  |«      }t        |t        j                  «      st	        d«      ‚t        |t        j
                  «      st	        d«      ‚|j                  }t        |«      dk7  rt	        d«      ‚|d   |d   k7  rt	        d«      ‚t        j                  t        j                  |j                  «      d¬	«      }|d   }t        j                  |d
z  «      }d|z  dz
  |dz  z
  d
|z  z  }t        t        j                  |«      «      D ];  }||z  }	||z  |z  }
dt        |	|z  |z
  «      z
  dt        |
|z  |z
  «      z
  z  ||<   Œ= t        j                  ||«      }|j                  t         j"                  j$                  j&                  t         j"                  j$                  j(                  t         j"                  j$                  j*                  fv r–t         j"                  j$                  j,                  }|j/                  t1        j2                  dj5                  d|j6                  dg«      «      |j                  |t         j"                  j$                  j8                  d¬«      }n|j                  }|}|t         j"                  j$                  j,                  k(  r%d}|j:                  D �cg c]  }t=        |«      ‘Œ }}nt?        d|j                  «      ‚t        j                  |«      dkD  rt	        d«      ‚tA        «       råtC        jD                  |tG        |«      ||tI        «       «       |j                  t         j"                  j$                  j&                  t         j"                  j$                  j(                  t         j"                  j$                  j*                  fv r2tC        jJ                  ||j                  «      }|jM                  |«       y|jM                  |«       y|jO                  dd|gid|dtG        |«      ||i¬«      }|j                  t         j"                  j$                  j&                  t         j"                  j$                  j(                  t         j"                  j$                  j*                  fv r0|jO                  dd|id|i|j                  |j                  dœ¬«       ||_(        |S c c}w )aZ  Initialize the input tensor with Bilinear initialization.

        Args:
            var(Tensor): Tensor that needs to be initialized.
            block(Block, optional): The block in which initialization ops
                   should be added. Used in static graph only, default None.

        Returns:
            The initialization op
        zvar must be framework.Variable.zblock must be framework.Block.é   zthe length of shape must be 4.é   r   z#shape[2] must be equal to shape[3].Úfloat32)Údtypeg       @r
   Ú.Úbilinear_initÚtmpF)ÚnameÚshaper   ÚtypeÚpersistableÚfp32_valueszUnsupported dtype %si   zThe size of input is too big. NÚassign_valueÚOutr   r   )r   ÚoutputsÚattrsÚcastÚX)Úin_dtypeÚ	out_dtype)r   Úinputsr$   r%   ))Ú_check_blockÚ
isinstancer   ÚVariableÚ
ValueErrorÚBlockr   ÚlenÚnpÚzerosÚprodÚceilÚrangeÚabsÚreshaper   r   ÚVarDescÚVarTypeÚFP16ÚBF16ÚFP64ÚFP32Ú
create_varr   ÚgenerateÚjoinr   Ú
LOD_TENSORÚflatÚfloatÚ	TypeErrorr	   r   Úassign_value_Úlistr   r&   Ú_share_underline_tensor_toÚ	append_opÚop)r   ÚvarÚblockr   ÚweightÚsizeÚfÚcÚiÚxÚyr)   Úout_varÚ
value_nameÚvÚvaluesÚvar_tmprI   s                     r   ÚforwardzBilinear.forwardM   s  € ð ×!Ñ! %Ó(ˆä˜#œy×1Ñ1Ô2ÜÐ>Ó?Ð?ä˜%¤§¡Ô1ÜÐ=Ó>Ð>à—	‘	ˆÜˆu‹:˜Š?ÜÐ=Ó>Ð>Ø�‰8�u˜Q‘xÒÜÐBÓCÐCä—‘œ"Ÿ'™' #§)¡)Ó,°IÔ>ˆØ�Q‰xˆä�G‰G�D˜3‘JÓˆà�‰U�Q‰Y˜˜Q™Ñ 3¨¡7Ñ+ˆÜ”r—w‘w˜u“~Ö&ˆAØ�D‘ˆAØ�T‘˜TÑ!ˆAØœS  Q¡¨¡›^Ñ+°´C¸¸A¹À¹	³NÑ0BÑCˆF�1ŠIð 'ô —‘˜F EÓ*ˆð �9‰9Ü�L‰L× Ñ ×%Ñ%Ü�L‰L× Ñ ×%Ñ%Ü�L‰L× Ñ ×%Ñ%ð
ñ 
ô
 Ÿ™×,Ñ,×1Ñ1ˆIØ×&Ñ&Ü ×)Ñ)Ø—H‘H˜o¨s¯x©x¸Ð?Ó@óð —i‘iØÜ—\‘\×)Ñ)×4Ñ4Ø!ð 'ó ‰Gð Ÿ	™	ˆIØˆGàœŸ™×,Ñ,×1Ñ1Ò1Ø&ˆJØ(.¯ªÓ4© 1”e˜A•h¨ˆFÑ4äÐ2°C·I±IÓ>Ð>ä�7‰7�5‹>˜KÒ'ÜÐ=Ó>Ð>äÔÜ× Ñ ØÜ�U“ØØÜ'Ó)ôð �y‰yÜ—‘×$Ñ$×)Ñ)Ü—‘×$Ñ$×)Ñ)Ü—‘×$Ñ$×)Ñ)ðñ ô
 !Ÿ+™+ g¨s¯y©yÓ9�Ø×2Ñ2°3Ô7ð ð ×2Ñ2°3Ô7Øà—‘Ø#Ø  	Ð*à˜YØœT %›[Ø ðð !ó ˆBð �y‰yÜ—‘×$Ñ$×)Ñ)Ü—‘×$Ñ$×)Ñ)Ü—‘×$Ñ$×)Ñ)ðñ ð
 —‘ØØ ˜>Ø" C˜LØ'.§}¡}À3Ç9Á9ÑMð	  ô ð ˆCŒFØˆIùòc 5s   Ë S)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rX   Ú__classcell__)r   s   @r   r   r      s   ø„ ñ,ô\÷lr   r   )Únumpyr1   Úpaddler   Úbaser   r   r   Úbase.frameworkr   r	   Úinitializerr   Ú__all__r   © r   r   Ú<module>re      s,   ðó å ç 0Ñ 0ß FÝ $à
€ô_ˆ{õ _r   