o
    #j                     @   s\   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	 d	eZdS )
    N)_C_ops   )core	frameworkunique_name)_current_expected_placein_dygraph_mode   )Initializerc                       s*   e Zd ZdZ fddZdddZ  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                    s   t    dS )z$Constructor for BilinearInitializer.N)super__init__)self	__class__ _/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/initializer/Bilinear.pyr   I   s   zBilinear.__init__Nc              	   C   s  |  |}t|tjstdt|tjstd|j}t|dkr&td|d |d kr2tdtj	t
|jdd	}|d }t|d
 }d| d |d  d
|  }tt
|D ]"}|| }	|| | }
dt|	| |  dt|
| |   ||< q]t||}|jtjjjtjjjtjjjfv rtjjj}|jtdd|jdg|j|tjjjdd}n|j}|}|tjjjkrd}dd |jD }ntd|jt
|dkrtdt rt !|t"|||t#  |jtjjjtjjjtjjjfv rt $||j}|%| dS |%| dS |j&dd|gid|dt"|||id}|jtjjjtjjjtjjjfv rK|j&dd|id|i|j|jdd  ||_'|S )!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].Zfloat32)dtypeg       @r	   .Zbilinear_inittmpF)nameshaper   typeZpersistableZfp32_valuesc                 S   s   g | ]}t |qS r   )float).0vr   r   r   
<listcomp>   s    z$Bilinear.forward.<locals>.<listcomp>zUnsupported dtype %si   zThe size of input is too big. NZassign_valueZOutr   r   )r   outputsattrscastX)Zin_dtype	out_dtype)r   Zinputsr   r    )(Z_check_block
isinstancer   Variable
ValueErrorZBlockr   lennpZzerosprodceilrangeabsZreshaper   r   ZVarDescZVarTypeZFP16ZBF16ZFP64ZFP32Z
create_varr   generatejoinr   Z
LOD_TENSORZflat	TypeErrorr   r   Zassign_value_listr   r!   Z_share_underline_tensor_toZ	append_opop)r   varblockr   weightsizefcixyr#   Zout_varZ
value_namevaluesZvar_tmpr1   r   r   r   forwardM   s   
.




zBilinear.forward)N)__name__
__module____qualname____doc__r   r<   __classcell__r   r   r   r   r      s    .r   )numpyr(   Zpaddler   baser   r   r   Zbase.frameworkr   r   Zinitializerr
   __all__r   r   r   r   r   <module>   s   