U
    (d                  	   @   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   >/tmp/pip-unpacked-wheel-vx7f76es/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