U
    ­Ê¹jß  ã                	   @   s´   d dl Z d dlZ d dlm  mZ d dl mZmZ ddlmZ dee	e
ee	eedœd	d
„Zdee	e
ee	eedœdd„Ze j d
¡ G dd„ dejƒZe j d¡ G dd„ deƒZdS )é    N)ÚnnÚTensoré   )Ú_log_api_usage_onceFç�íµ ÷Æ°>T)ÚinputÚpÚ
block_sizeÚinplaceÚepsÚtrainingÚreturnc                 C   s^  t j ¡ st j ¡ sttƒ |dk s,|dkr<td|› d�ƒ‚| jdkrXtd| j› d�ƒ‚|rd|dkrh| S |  ¡ \}}}}	t	||	|ƒ}|| |	 |d || d	 |	| d	    }
t j
|||| d	 |	| d	 f| j| jd
�}| |
¡ tj||d gd dd�}tj|d||f|d d�}d	| }| ¡ || ¡   }|�rN|  |¡ |¡ n| | | } | S )a  
    Implements DropBlock2d from `"DropBlock: A regularization method for convolutional networks"
    <https://arxiv.org/abs/1810.12890>`.

    Args:
        input (Tensor[N, C, H, W]): The input tensor or 4-dimensions with the first one
                    being its batch i.e. a batch with ``N`` rows.
        p (float): Probability of an element to be dropped.
        block_size (int): Size of the block to drop.
        inplace (bool): If set to ``True``, will do this operation in-place. Default: ``False``.
        eps (float): A value added to the denominator for numerical stability. Default: 1e-6.
        training (bool): apply dropblock if is ``True``. Default: ``True``.

    Returns:
        Tensor[N, C, H, W]: The randomly zeroed tensor after dropblock.
    ç        ç      ð?ú4drop probability has to be between 0 and 1, but got Ú.é   z#input should be 4 dimensional. Got ú dimensions.r   é   ©ÚdtypeÚdevicer   ©Úvalue)r   r   ©ZstrideZkernel_sizeÚpadding)ÚtorchÚjitÚis_scriptingÚ
is_tracingr   Údrop_block2dÚ
ValueErrorÚndimÚsizeÚminÚemptyr   r   Ú
bernoulli_ÚFÚpadZ
max_pool2dÚnumelÚsumÚmul_)r   r   r	   r
   r   r   ÚNÚCÚHÚWÚgammaÚnoiseÚnormalize_scale© r3   úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/ops/drop_block.pyr    	   s*    
,,
r    c                 C   s~  t j ¡ st j ¡ sttƒ |dk s,|dkr<td|› d�ƒ‚| jdkrXtd| j› d�ƒ‚|rd|dkrh| S |  ¡ \}}}}	}
t	|||	|
ƒ}|| |	 |
 |d || d	 |	| d	  |
| d	    }t j
|||| d	 |	| d	 |
| d	 f| j| jd
�}| |¡ tj||d gd dd�}tj|d|||f|d d�}d	| }| ¡ || ¡   }|�rn|  |¡ |¡ n| | | } | S )a  
    Implements DropBlock3d from `"DropBlock: A regularization method for convolutional networks"
    <https://arxiv.org/abs/1810.12890>`.

    Args:
        input (Tensor[N, C, D, H, W]): The input tensor or 5-dimensions with the first one
                    being its batch i.e. a batch with ``N`` rows.
        p (float): Probability of an element to be dropped.
        block_size (int): Size of the block to drop.
        inplace (bool): If set to ``True``, will do this operation in-place. Default: ``False``.
        eps (float): A value added to the denominator for numerical stability. Default: 1e-6.
        training (bool): apply dropblock if is ``True``. Default: ``True``.

    Returns:
        Tensor[N, C, D, H, W]: The randomly zeroed tensor after dropblock.
    r   r   r   r   é   z#input should be 5 dimensional. Got r   é   r   r   r   é   r   r   )r   r   r   r   )r   r   r   r   r   Údrop_block3dr!   r"   r#   r$   r%   r   r   r&   r'   r(   Z
max_pool3dr)   r*   r+   )r   r   r	   r
   r   r   r,   r-   ÚDr.   r/   r0   r1   r2   r3   r3   r4   r8   7   s<    
<$  ÿ
   ÿr8   c                       sN   e Zd ZdZdeeeeddœ‡ fdd„Zeedœd	d
„Z	e
dœdd„Z‡  ZS )ÚDropBlock2dz#
    See :func:`drop_block2d`.
    Fr   N©r   r	   r
   r   r   c                    s&   t ƒ  ¡  || _|| _|| _|| _d S ©N)ÚsuperÚ__init__r   r	   r
   r   ©Úselfr   r	   r
   r   ©Ú	__class__r3   r4   r>   q   s
    
zDropBlock2d.__init__©r   r   c                 C   s   t || j| j| j| j| jƒS ©zÊ
        Args:
            input (Tensor): Input feature map on which some areas will be randomly
                dropped.
        Returns:
            Tensor: The tensor after DropBlock layer.
        )r    r   r	   r
   r   r   ©r@   r   r3   r3   r4   Úforwardy   s    zDropBlock2d.forward)r   c                 C   s*   | j j› d| j› d| j› d| j› d�}|S )Nz(p=z, block_size=z
, inplace=ú))rB   Ú__name__r   r	   r
   )r@   Úsr3   r3   r4   Ú__repr__ƒ   s    &zDropBlock2d.__repr__)Fr   )rH   Ú
__module__Ú__qualname__Ú__doc__ÚfloatÚintÚboolr>   r   rF   ÚstrrJ   Ú__classcell__r3   r3   rA   r4   r:   l   s   
r:   c                       s@   e Zd ZdZdeeeeddœ‡ fdd„Zeedœd	d
„Z	‡  Z
S )ÚDropBlock3dz#
    See :func:`drop_block3d`.
    Fr   Nr;   c                    s   t ƒ  ||||¡ d S r<   )r=   r>   r?   rA   r3   r4   r>   �   s    zDropBlock3d.__init__rC   c                 C   s   t || j| j| j| j| jƒS rD   )r8   r   r	   r
   r   r   rE   r3   r3   r4   rF   “   s    zDropBlock3d.forward)Fr   )rH   rK   rL   rM   rN   rO   rP   r>   r   rF   rR   r3   r3   rA   r4   rS   ‹   s   rS   )Fr   T)Fr   T)r   Ztorch.fxZtorch.nn.functionalr   Z
functionalr'   r   Úutilsr   rN   rO   rP   r    r8   ZfxÚwrapÚModuler:   rS   r3   r3   r3   r4   Ú<module>   s@        ÿ     þ/     ÿ     þ2