o
    ôj5  ã                   @   sÞ   d dl Z d dlmZmZmZmZmZmZ d dlZd dlm	Z	 ddl
mZmZ ejjjZG dd„ dejjƒZG dd	„ d	ejjƒZG d
d„ deƒZG dd„ deƒZG dd„ dejjƒZG dd„ dejjƒZG dd„ dejjƒZdS )é    N)ÚCallableÚListÚOptionalÚSequenceÚTupleÚUnion)ÚTensoré   )Ú_log_api_usage_onceÚ_make_ntuplec                       s„   e Zd ZdZ	ddedef‡ fdd„Zdeded	ed
e	de
e de
e de
e f‡ fdd„Zdedefdd„Zdefdd„Z‡  ZS )ÚFrozenBatchNorm2da!  
    BatchNorm2d where the batch statistics and the affine parameters are fixed

    Args:
        num_features (int): Number of features ``C`` from an expected input of size ``(N, C, H, W)``
        eps (float): a value added to the denominator for numerical stability. Default: 1e-5
    çñhãˆµøä>Únum_featuresÚepsc                    sd   t ƒ  ¡  t| ƒ || _|  dt |¡¡ |  dt |¡¡ |  dt |¡¡ |  dt |¡¡ d S )NÚweightÚbiasÚrunning_meanÚrunning_var)ÚsuperÚ__init__r
   r   Úregister_bufferÚtorchÚonesÚzeros)Úselfr   r   ©Ú	__class__© úe/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/ops/misc.pyr      s   
zFrozenBatchNorm2d.__init__Ú
state_dictÚprefixÚlocal_metadataÚstrictÚmissing_keysÚunexpected_keysÚ
error_msgsc           	   	      s2   |d }||v r||= t ƒ  |||||||¡ d S )NÚnum_batches_tracked)r   Ú_load_from_state_dict)	r   r   r    r!   r"   r#   r$   r%   Únum_batches_tracked_keyr   r   r   r'   #   s   
ÿz'FrozenBatchNorm2d._load_from_state_dictÚxÚreturnc                 C   sr   | j  dddd¡}| j dddd¡}| j dddd¡}| j dddd¡}||| j  ¡  }|||  }|| | S )Né   éÿÿÿÿ)r   Úreshaper   r   r   r   Úrsqrt)r   r)   ÚwÚbÚrvÚrmÚscaler   r   r   r   Úforward5   s   zFrozenBatchNorm2d.forwardc                 C   s$   | j j› d| jjd › d| j› d�S )Nú(r   z, eps=ú))r   Ú__name__r   Úshaper   )r   r   r   r   Ú__repr__@   s   $zFrozenBatchNorm2d.__repr__)r   )r7   Ú
__module__Ú__qualname__Ú__doc__ÚintÚfloatr   ÚdictÚstrÚboolr   r'   r   r4   r9   Ú__classcell__r   r   r   r   r      s2    ýþýþýüûúùør   c                       sî   e Zd Zddddejjejjdddejjf
dedede	ee
edf f d	e	ee
edf f d
ee	ee
edf ef  dedeedejjf  deedejjf  de	ee
edf f dee dee dedejjf ddf‡ fdd„Z‡  ZS )ÚConvNormActivationé   r+   NTÚin_channelsÚout_channelsÚkernel_size.ÚstrideÚpaddingÚgroupsÚ
norm_layerÚactivation_layerÚdilationÚinplacer   Ú
conv_layerr*   c              
      s  |d u r<t ˆtƒrt ˆ tƒrˆd d ˆ  }n%t ˆtƒr tˆƒntˆ ƒ}tˆ|ƒ‰tˆ |ƒ‰ t‡ ‡fdd„t|ƒD ƒƒ}|d u rD|d u }|||ˆ||ˆ ||d�g}|d ur\| ||ƒ¡ |d urt|
d u rfi nd|
i}| |di |¤Ž¡ tƒ j	|Ž  t
| ƒ || _| jtkr�t d¡ d S d S )	Nr+   r	   c                 3   s(   � | ]}ˆ| d  d ˆ |  V  qdS )r+   r	   Nr   )Ú.0Úi©rM   rG   r   r   Ú	<genexpr>\   s   €& z.ConvNormActivation.__init__.<locals>.<genexpr>)rM   rJ   r   rN   zhDon't use ConvNormActivation directly, please use Conv2dNormActivation and Conv3dNormActivation instead.r   )Ú
isinstancer=   r   Úlenr   ÚtupleÚrangeÚappendr   r   r
   rF   r   rC   ÚwarningsÚwarn)r   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r   rO   Z	_conv_dimÚlayersÚparamsr   rR   r   r   E   sB   

øÿ
ÿÿzConvNormActivation.__init__)r7   r:   r;   r   ÚnnÚBatchNorm2dÚReLUÚConv2dr=   r   r   r   r@   r   ÚModulerA   r   rB   r   r   r   r   rC   D   sL    óþýüûúùø	÷
öõôóòrC   c                       sÜ   e Zd ZdZddddejjejjdddf	dedede	ee
eef f d	e	ee
eef f d
ee	ee
eef ef  dedeedejjf  deedejjf  de	ee
eef f dee dee ddf‡ fdd„Z‡  ZS )ÚConv2dNormActivationaë  
    Configurable block used for Convolution2d-Normalization-Activation blocks.

    Args:
        in_channels (int): Number of channels in the input image
        out_channels (int): Number of channels produced by the Convolution-Normalization-Activation block
        kernel_size: (int, optional): Size of the convolving kernel. Default: 3
        stride (int, optional): Stride of the convolution. Default: 1
        padding (int, tuple or str, optional): Padding added to all four sides of the input. Default: None, in which case it will be calculated as ``padding = (kernel_size - 1) // 2 * dilation``
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
        norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the convolution layer. If ``None`` this layer won't be used. Default: ``torch.nn.BatchNorm2d``
        activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the conv layer. If ``None`` this layer won't be used. Default: ``torch.nn.ReLU``
        dilation (int): Spacing between kernel elements. Default: 1
        inplace (bool): Parameter for the activation layer, which can optionally do the operation in-place. Default ``True``
        bias (bool, optional): Whether to use bias in the convolution layer. By default, biases are included if ``norm_layer is None``.

    rD   r+   NTrE   rF   rG   rH   rI   rJ   rK   .rL   rM   rN   r   r*   c                    ó*   t ƒ  |||||||||	|
|tjj¡ d S ©N)r   r   r   r]   r`   ©r   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rN   r   r   r   r   r   �   ó   ôzConv2dNormActivation.__init__)r7   r:   r;   r<   r   r]   r^   r_   r=   r   r   r   r@   r   ra   rA   r   rB   r   r   r   r   rb   }   sH    ôþýüûúùø	÷
öõôórb   c                       sä   e Zd ZdZddddejjejjdddf	dedede	ee
eeef f d	e	ee
eeef f d
ee	ee
eeef ef  dedeedejjf  deedejjf  de	ee
eeef f dee dee ddf‡ fdd„Z‡  ZS )ÚConv3dNormActivationaë  
    Configurable block used for Convolution3d-Normalization-Activation blocks.

    Args:
        in_channels (int): Number of channels in the input video.
        out_channels (int): Number of channels produced by the Convolution-Normalization-Activation block
        kernel_size: (int, optional): Size of the convolving kernel. Default: 3
        stride (int, optional): Stride of the convolution. Default: 1
        padding (int, tuple or str, optional): Padding added to all four sides of the input. Default: None, in which case it will be calculated as ``padding = (kernel_size - 1) // 2 * dilation``
        groups (int, optional): Number of blocked connections from input channels to output channels. Default: 1
        norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the convolution layer. If ``None`` this layer won't be used. Default: ``torch.nn.BatchNorm3d``
        activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the conv layer. If ``None`` this layer won't be used. Default: ``torch.nn.ReLU``
        dilation (int): Spacing between kernel elements. Default: 1
        inplace (bool): Parameter for the activation layer, which can optionally do the operation in-place. Default ``True``
        bias (bool, optional): Whether to use bias in the convolution layer. By default, biases are included if ``norm_layer is None``.
    rD   r+   NTrE   rF   rG   rH   rI   rJ   rK   .rL   rM   rN   r   r*   c                    rc   rd   )r   r   r   r]   ÚConv3dre   r   r   r   r   Á   rf   zConv3dNormActivation.__init__)r7   r:   r;   r<   r   r]   ÚBatchNorm3dr_   r=   r   r   r   r@   r   ra   rA   r   rB   r   r   r   r   rg   ¯   sH    ôþýüûúùø	÷
öõôórg   c                       s€   e Zd ZdZejjejjfdedede	dejj
f de	dejj
f ddf
‡ fd	d
„Zdedefdd„Zdedefdd„Z‡  ZS )ÚSqueezeExcitationaE  
    This block implements the Squeeze-and-Excitation block from https://arxiv.org/abs/1709.01507 (see Fig. 1).
    Parameters ``activation``, and ``scale_activation`` correspond to ``delta`` and ``sigma`` in eq. 3.

    Args:
        input_channels (int): Number of channels in the input image
        squeeze_channels (int): Number of squeeze channels
        activation (Callable[..., torch.nn.Module], optional): ``delta`` activation. Default: ``torch.nn.ReLU``
        scale_activation (Callable[..., torch.nn.Module]): ``sigma`` activation. Default: ``torch.nn.Sigmoid``
    Úinput_channelsÚsqueeze_channelsÚ
activation.Úscale_activationr*   Nc                    sX   t ƒ  ¡  t| ƒ tj d¡| _tj ||d¡| _tj ||d¡| _	|ƒ | _
|ƒ | _d S )Nr+   )r   r   r
   r   r]   ÚAdaptiveAvgPool2dÚavgpoolr`   Úfc1Úfc2rm   rn   )r   rk   rl   rm   rn   r   r   r   r   ì   s   
zSqueezeExcitation.__init__Úinputc                 C   s2   |   |¡}|  |¡}|  |¡}|  |¡}|  |¡S rd   )rp   rq   rm   rr   rn   ©r   rs   r3   r   r   r   Ú_scaleû   s
   




zSqueezeExcitation._scalec                 C   s   |   |¡}|| S rd   )ru   rt   r   r   r   r4     s   
zSqueezeExcitation.forward)r7   r:   r;   r<   r   r]   r_   ÚSigmoidr=   r   ra   r   r   ru   r4   rB   r   r   r   r   rj   à   s"    ûþýüûúrj   c                       sv   e Zd ZdZdejjdddfdedee de	e
dejjf  d	e	e
dejjf  d
e	e dedef‡ fdd„Z‡  ZS )ÚMLPa  This block implements the multi-layer perceptron (MLP) module.

    Args:
        in_channels (int): Number of channels of the input
        hidden_channels (List[int]): List of the hidden channel dimensions
        norm_layer (Callable[..., torch.nn.Module], optional): Norm layer that will be stacked on top of the linear layer. If ``None`` this layer won't be used. Default: ``None``
        activation_layer (Callable[..., torch.nn.Module], optional): Activation function which will be stacked on top of the normalization layer (if not None), otherwise on top of the linear layer. If ``None`` this layer won't be used. Default: ``torch.nn.ReLU``
        inplace (bool, optional): Parameter for the activation layer, which can optionally do the operation in-place.
            Default is ``None``, which uses the respective default values of the ``activation_layer`` and Dropout layer.
        bias (bool): Whether to use bias in the linear layer. Default ``True``
        dropout (float): The probability for the dropout layer. Default: 0.0
    NTg        rE   Úhidden_channelsrK   .rL   rN   r   Údropoutc                    sÜ   |d u ri nd|i}g }	|}
|d d… D ]2}|	  tjj|
||d�¡ |d ur-|	  ||ƒ¡ |	  |di |¤Ž¡ |	  tjj|fi |¤Ž¡ |}
q|	  tjj|
|d |d�¡ |	  tjj|fi |¤Ž¡ tƒ j|	Ž  t| ƒ d S )NrN   r,   )r   r   )rX   r   r]   ÚLinearÚDropoutr   r   r
   )r   rE   rx   rK   rL   rN   r   ry   r\   r[   Úin_dimÚ
hidden_dimr   r   r   r     s   zMLP.__init__)r7   r:   r;   r<   r   r]   r_   r=   r   r   r   ra   rA   r>   r   rB   r   r   r   r   rw     s,    øþýüûúùørw   c                       s<   e Zd ZdZdee f‡ fdd„Zdedefdd„Z‡  Z	S )	ÚPermutez™This module returns a view of the tensor input with its dimensions permuted.

    Args:
        dims (List[int]): The desired ordering of dimensions
    Údimsc                    s   t ƒ  ¡  || _d S rd   )r   r   r   )r   r   r   r   r   r   ;  s   

zPermute.__init__r)   r*   c                 C   s   t  || j¡S rd   )r   Úpermuter   )r   r)   r   r   r   r4   ?  s   zPermute.forward)
r7   r:   r;   r<   r   r=   r   r   r4   rB   r   r   r   r   r~   4  s    r~   )rY   Útypingr   r   r   r   r   r   r   r   Úutilsr
   r   r]   Ú
functionalÚinterpolatera   r   Ú
SequentialrC   rb   rg   rj   rw   r~   r   r   r   r   Ú<module>   s     
7921'-