a
    PSic                  	   @   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   stridekernel_sizepadding)torchjitis_scripting
is_tracingr   drop_block2d
ValueErrorndimsizeminemptyr   r   
bernoulli_Fpad
max_pool2dnumelsummul_)r   r   r	   r
   r   r   NCHWgammanoisenormalize_scale r6   V/var/www/html/django/DPS/env/lib/python3.9/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*   
max_pool3dr,   r-   r.   )r   r   r	   r
   r   r   r/   r0   Dr1   r2   r3   r4   r5   r6   r6   r7   r;   7   s2    
<,
r;   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__r6   r7   rB   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   rD   r   r6   r6   r7   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=))rF   __name__r   r	   r
   )rD   sr6   r6   r7   __repr__   s    &zDropBlock2d.__repr__)Fr   )rL   
__module____qualname____doc__floatintboolrB   r   rJ   strrN   __classcell__r6   r6   rE   r7   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@   )rA   rB   rC   rE   r6   r7   rB      s    zDropBlock3d.__init__rG   c                 C   s   t || j| j| j| j| jS rH   )r;   r   r	   r
   r   r   rI   r6   r6   r7   rJ      s    zDropBlock3d.forward)Fr   )rL   rO   rP   rQ   rR   rS   rT   rB   r   rJ   rV   r6   r6   rE   r7   rW      s   rW   )Fr   T)Fr   T)r   Ztorch.fxtorch.nn.functionalr   
functionalr)   r   utilsr   rR   rS   rT   r"   r;   fxwrapModuler>   rW   r6   r6   r6   r7   <module>   s$    / 2