Ë
    FêñibR  ã                  óh  — d Z ddlmZ ddlZddlZddlZddlmZ dZ	d"d„Z
 G d„ dej                  «      Z G d„ d	e«      Z G d
„ dej                  «      Z G d„ de«      Z G d„ dej                   «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d„ dej                  «      Z G d „ d!ej                  «      Zy)#zConvolution modules.é    )ÚannotationsN)ÚCBAMÚChannelAttentionÚConcatÚConvÚConv2ÚConvTransposeÚDWConvÚDWConvTranspose2dÚFocusÚ	GhostConvÚIndexÚ	LightConvÚRepConvÚSpatialAttentionc                óà   — |dkD  r4t        | t        «      r|| dz
  z  dz   n| D �cg c]  }||dz
  z  dz   ‘Œ c}} |€(t        | t        «      r| dz  n| D �cg c]  }|dz  ‘Œ	 c}}|S c c}w c c}w )zPad to 'same' shape outputs.é   é   )Ú
isinstanceÚint)ÚkÚpÚdÚxs       ú]/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/nn/modules/conv.pyÚautopadr      ss   € àˆ1‚uÜ)¨!¬SÔ1ˆA��Q‘‰K˜!ŠOÐQRÖ7SÈA¸¸QÀ¹U¹Àa»Ò7SˆØ€yÜ  ¤CÔ(ˆA�ŠF¸qÖ.A¸!¨q°A«vÒ.AˆØ€Hùò 8Tùâ.As   ¥A&ÁA+c                  óR   ‡ — e Zd ZdZ ej
                  «       Zdˆ fd„	Zd„ Zd„ Z	ˆ xZ
S )r   a:  Standard convolution module with batch normalization and activation.

    Attributes:
        conv (nn.Conv2d): Convolutional layer.
        bn (nn.BatchNorm2d): Batch normalization layer.
        act (nn.Module): Activation function layer.
        default_act (nn.Module): Default activation function (SiLU).
    c	                óR  •— t         ‰	| �  «        t        j                  ||||t	        |||«      ||d¬«      | _        t        j                  |«      | _        |du r| j                  | _        yt        |t        j                  «      r|| _        yt        j                  «       | _        y)a�  Initialize Conv layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int, optional): Padding.
            g (int): Groups.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
        F©ÚgroupsÚdilationÚbiasTN)ÚsuperÚ__init__ÚnnÚConv2dr   ÚconvÚBatchNorm2dÚbnÚdefault_actr   ÚModuleÚIdentityÚact©
ÚselfÚc1Úc2r   Úsr   Úgr   r-   Ú	__class__s
            €r   r$   zConv.__init__3   s   ø€ ô 	‰ÑÔÜ—I‘I˜b " a¨¬G°A°q¸!Ó,<ÀQÐQRÐY^Ô_ˆŒ	Ü—.‘. Ó$ˆŒØ'*¨d¡{�4×#Ñ#ˆ�¼zÈ#ÌrÏyÉyÔ?Y¸ˆ�Ô_a×_jÑ_jÓ_lˆ�ó    c                ó`   — | j                  | j                  | j                  |«      «      «      S ©zÇApply convolution, batch normalization and activation to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        ©r-   r)   r'   ©r/   r   s     r   ÚforwardzConv.forwardE   ó$   € ð �x‰x˜Ÿ™ §	¡	¨!£Ó-Ó.Ð.r5   c                óB   — | j                  | j                  |«      «      S )z¾Apply convolution and activation without batch normalization.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        ©r-   r'   r9   s     r   Úforward_fusezConv.forward_fuseP   ó   € ð �x‰x˜Ÿ	™	 !›Ó%Ð%r5   )r   r   Nr   r   T©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   ÚSiLUr*   r$   r:   r>   Ú__classcell__©r4   s   @r   r   r   '   s'   ø„ ñð �"—'‘'“)€Kõmò$	/ö	&r5   r   c                  ó6   ‡ — e Zd ZdZdˆ fd„	Zd„ Zd„ Zd„ Zˆ xZS )r   a"  Simplified RepConv module with Conv fusing.

    Attributes:
        conv (nn.Conv2d): Main 3x3 convolutional layer.
        cv2 (nn.Conv2d): Additional 1x1 convolutional layer.
        bn (nn.BatchNorm2d): Batch normalization layer.
        act (nn.Module): Activation function layer.
    c	                óŽ   •— t         ‰	| �  ||||||||¬«       t        j                  ||d|t	        d||«      ||d¬«      | _        y)a‚  Initialize Conv2 layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int, optional): Padding.
            g (int): Groups.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
        ©r3   r   r-   r   Fr   N)r#   r$   r%   r&   r   Úcv2r.   s
            €r   r$   zConv2.__init__f   sL   ø€ ô 	‰Ñ˜˜R  A q¨A°¸ÐÔ<Ü—9‘9˜R  Q¨¬7°1°a¸Ó+;ÀAÐPQÐX]Ô^ˆ�r5   c                ó„   — | j                  | j                  | j                  |«      | j                  |«      z   «      «      S r7   )r-   r)   r'   rK   r9   s     r   r:   zConv2.forwardv   s1   € ð �x‰x˜Ÿ™ §	¡	¨!£¨t¯x©x¸«{Ñ :Ó;Ó<Ð<r5   c                ó`   — | j                  | j                  | j                  |«      «      «      S )zÍApply fused convolution, batch normalization and activation to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        r8   r9   s     r   r>   zConv2.forward_fuse�   r;   r5   c                óæ  — t        j                  | j                  j                  j                  «      }|j
                  dd D �cg c]  }|dz  ‘Œ	 }}| j                  j                  j                  j                  «       |dd…dd…|d   |d   dz   …|d   |d   dz   …f<   | j                  j                  xj                  |z  c_        | j                  d«       | j                  | _
        yc c}w )zFuse parallel convolutions.r   Nr   r   rK   )ÚtorchÚ
zeros_liker'   ÚweightÚdataÚshaperK   ÚcloneÚ__delattr__r>   r:   )r/   Úwr   Úis       r   Ú
fuse_convszConv2.fuse_convsŒ   sÈ   € ä×Ñ˜TŸY™Y×-Ñ-×2Ñ2Ó3ˆØŸW™W Q R˜[Ö)˜ˆQ�!‹VÐ)ˆÐ)Ø48·H±H·O±O×4HÑ4H×4NÑ4NÓ4PˆŠ!ŠQ��!‘�q˜‘t˜a‘x�  1¡¨¨!©¨q© Ð
0Ñ1Ø�	‰	×Ñ×Ò Ñ"ÕØ×Ñ˜ÔØ×(Ñ(ˆ�ùò	 *s   ÁC.)é   r   Nr   r   T)	rA   rB   rC   rD   r$   r:   r>   rX   rF   rG   s   @r   r   r   \   s   ø„ ñõ_ò 	=ò	/ö)r5   r   c                  óL   ‡ — e Zd ZdZd ej
                  «       fˆ fd„	Zd„ Zˆ xZS )r   a   Light convolution module with 1x1 and depthwise convolutions.

    This implementation is based on the PaddleDetection HGNetV2 backbone.

    Attributes:
        conv1 (Conv): 1x1 convolution layer.
        conv2 (DWConv): Depthwise convolution layer.
    r   c                ór   •— t         ‰| �  «        t        ||dd¬«      | _        t	        ||||¬«      | _        y)a  Initialize LightConv layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size for depthwise convolution.
            act (nn.Module): Activation function.
        r   F©r-   N)r#   r$   r   Úconv1r
   Úconv2)r/   r0   r1   r   r-   r4   s        €r   r$   zLightConv.__init__    s4   ø€ ô 	‰ÑÔÜ˜"˜b !¨Ô/ˆŒ
Ü˜B  A¨3Ô/ˆ�
r5   c                óB   — | j                  | j                  |«      «      S )z¦Apply 2 convolutions to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        )r^   r]   r9   s     r   r:   zLightConv.forward­   s   € ð �z‰z˜$Ÿ*™* Q›-Ó(Ð(r5   )	rA   rB   rC   rD   r%   ÚReLUr$   r:   rF   rG   s   @r   r   r   –   s!   ø„ ñð "#¨¨¯©«	õ 0ö	)r5   r   c                  ó$   ‡ — e Zd ZdZdˆ fd„	Zˆ xZS )r
   zDepth-wise convolution module.c           
     óZ   •— t         ‰| �  ||||t        j                  ||«      ||¬«       y)aH  Initialize depth-wise convolution with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
        rJ   N©r#   r$   ÚmathÚgcd)r/   r0   r1   r   r2   r   r-   r4   s          €r   r$   zDWConv.__init__¼   s,   ø€ ô 	‰Ñ˜˜R  A¬¯©°"°bÓ)9¸QÀCÐÕHr5   ©r   r   r   T©rA   rB   rC   rD   r$   rF   rG   s   @r   r
   r
   ¹   s   ø„ Ù(÷Iñ Ir5   r
   c                  ó$   ‡ — e Zd ZdZdˆ fd„	Zˆ xZS )r   z(Depth-wise transpose convolution module.c                óZ   •— t         ‰| �  ||||||t        j                  ||«      ¬«       y)a?  Initialize depth-wise transpose convolution with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p1 (int): Padding.
            p2 (int): Output padding.
        )r    Nrc   )r/   r0   r1   r   r2   Úp1Úp2r4   s          €r   r$   zDWConvTranspose2d.__init__Í   s,   ø€ ô 	‰Ñ˜˜R  A r¨2´d·h±h¸rÀ2Ó6FÐÕGr5   )r   r   r   r   rg   rG   s   @r   r   r   Ê   s   ø„ Ù2÷Hñ Hr5   r   c                  óR   ‡ — e Zd ZdZ ej
                  «       Zdˆ fd„	Zd„ Zd„ Z	ˆ xZ
S )r	   an  Convolution transpose module with optional batch normalization and activation.

    Attributes:
        conv_transpose (nn.ConvTranspose2d): Transposed convolution layer.
        bn (nn.BatchNorm2d | nn.Identity): Batch normalization layer.
        act (nn.Module): Activation function layer.
        default_act (nn.Module): Default activation function (SiLU).
    c                óf  •— t         ‰| �  «        t        j                  |||||| ¬«      | _        |rt        j
                  |«      nt        j                  «       | _        |du r| j                  | _        yt        |t        j                  «      r|| _        yt        j                  «       | _        y)at  Initialize ConvTranspose layer with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int): Padding.
            bn (bool): Use batch normalization.
            act (bool | nn.Module): Activation function.
        ©r"   TN)r#   r$   r%   ÚConvTranspose2dÚconv_transposer(   r,   r)   r*   r   r+   r-   )	r/   r0   r1   r   r2   r   r)   r-   r4   s	           €r   r$   zConvTranspose.__init__ç   s   ø€ ô 	‰ÑÔÜ ×0Ñ0°°R¸¸A¸qÈ2ÀvÔNˆÔÙ(*”"—.‘. Ô$´·±³ˆŒØ'*¨d¡{�4×#Ñ#ˆ�¼zÈ#ÌrÏyÉyÔ?Y¸ˆ�Ô_a×_jÑ_jÓ_lˆ�r5   c                ó`   — | j                  | j                  | j                  |«      «      «      S )zËApply transposed convolution, batch normalization and activation to input.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        )r-   r)   rp   r9   s     r   r:   zConvTranspose.forwardø   s'   € ð �x‰x˜Ÿ™ × 3Ñ 3°AÓ 6Ó7Ó8Ð8r5   c                óB   — | j                  | j                  |«      «      S )zµApply convolution transpose and activation to input.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        )r-   rp   r9   s     r   r>   zConvTranspose.forward_fuse  s   € ð �x‰x˜×+Ñ+¨AÓ.Ó/Ð/r5   )r   r   r   TTr@   rG   s   @r   r	   r	   Û   s'   ø„ ñð �"—'‘'“)€Kõmò"	9ö	0r5   r	   c                  ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )r   zÇFocus module for concentrating feature information.

    Slices input tensor into 4 parts and concatenates them in the channel dimension.

    Attributes:
        conv (Conv): Convolution layer.
    c           	     óV   •— t         ‰| �  «        t        |dz  ||||||¬«      | _        y)ad  Initialize Focus module with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int, optional): Padding.
            g (int): Groups.
            act (bool | nn.Module): Activation function.
        é   r\   N)r#   r$   r   r'   )	r/   r0   r1   r   r2   r   r3   r-   r4   s	           €r   r$   zFocus.__init__  s,   ø€ ô 	‰ÑÔÜ˜˜a™  Q¨¨1¨a°SÔ9ˆ�	r5   c                ó´   — | j                  t        j                  |dddd…ddd…f   |dddd…ddd…f   |dddd…ddd…f   |dddd…ddd…f   fd«      «      S )a  Apply Focus operation and convolution to input tensor.

        Input shape is (B, C, H, W) and output shape is (B, c2, H/2, W/2).

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        .Nr   r   )r'   rO   Úcatr9   s     r   r:   zFocus.forward(  sŽ   € ð �y‰yœŸ™ A c©3¨Q¨3±°!° mÑ$4°a¸¸Q¸TÀ¸TÁ3ÀQÀ3¸Ñ6GÈÈ3ÑPSÐRSÐPSÐUVÐUYÐXYÐUYÈ>ÑIZÐ\]Ð^aÐcdÐcgÐfgÐcgÐijÐimÐlmÐimÐ^mÑ\nÐ#oÐqrÓsÓtÐtr5   )r   r   Nr   T©rA   rB   rC   rD   r$   r:   rF   rG   s   @r   r   r     s   ø„ ñõ:ö ur5   r   c                  ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )r   a&  Ghost Convolution module.

    Generates more features with fewer parameters by using cheap operations.

    Attributes:
        cv1 (Conv): Primary convolution.
        cv2 (Conv): Cheap operation convolution.

    References:
        https://github.com/huawei-noah/Efficient-AI-Backbones
    c           	     óˆ   •— t         ‰| �  «        |dz  }t        ||||d||¬«      | _        t        ||ddd||¬«      | _        y)aH  Initialize Ghost Convolution module with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            g (int): Groups.
            act (bool | nn.Module): Activation function.
        r   Nr\   é   r   )r#   r$   r   Úcv1rK   )	r/   r0   r1   r   r2   r3   r-   Úc_r4   s	           €r   r$   zGhostConv.__init__D  sI   ø€ ô 	‰ÑÔØ�1‰WˆÜ˜˜B  1 d¨A°3Ô7ˆŒÜ˜˜B  1 d¨B°CÔ8ˆ�r5   c                ór   — | j                  |«      }t        j                  || j                  |«      fd«      S )zÄApply Ghost Convolution to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor with concatenated features.
        r   )r|   rO   rw   rK   )r/   r   Úys      r   r:   zGhostConv.forwardT  s/   € ð �H‰H�Q‹KˆÜ�y‰y˜!˜TŸX™X a›[Ð)¨1Ó-Ð-r5   rf   rx   rG   s   @r   r   r   7  s   ø„ ñ
õ9ö 
.r5   r   c                  ót   ‡ — e Zd ZdZ ej
                  «       Zd	ˆ fd„	Zd„ Zd„ Z	d„ Z
ed„ «       Zd„ Zd„ Zˆ xZS )
r   a  RepConv module with training and deploy modes.

    This module is used in RT-DETR and can fuse convolutions during inference for efficiency.

    Attributes:
        conv1 (Conv): 3x3 convolution.
        conv2 (Conv): 1x1 convolution.
        bn (nn.BatchNorm2d, optional): Batch normalization for identity branch.
        act (nn.Module): Activation function.
        default_act (nn.Module): Default activation function (SiLU).

    References:
        https://github.com/DingXiaoH/RepVGG/blob/main/repvgg.py
    c           	     ó¨  •— t         ‰| �  «        |dk(  r|dk(  sJ ‚|| _        || _        || _        |du r| j
                  n/t        |t        j                  «      r|nt        j                  «       | _
        |	r ||k(  r|dk(  rt        j                  |¬«      nd| _        t        ||||||d¬«      | _        t        ||d|||dz  z
  |d¬«      | _        y)	aõ  Initialize RepConv module with given parameters.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            k (int): Kernel size.
            s (int): Stride.
            p (int): Padding.
            g (int): Groups.
            d (int): Dilation.
            act (bool | nn.Module): Activation function.
            bn (bool): Use batch normalization for identity branch.
            deploy (bool): Deploy mode for inference.
        rY   r   T)Únum_featuresNF)r   r3   r-   r   )r#   r$   r3   r0   r1   r*   r   r%   r+   r,   r-   r(   r)   r   r]   r^   )r/   r0   r1   r   r2   r   r3   r   r-   r)   Údeployr4   s              €r   r$   zRepConv.__init__s  s¿   ø€ ô 	‰ÑÔØ�AŠv˜!˜qš&Ð Ð ØˆŒØˆŒØˆŒØ'*¨d¡{�4×#Ò#¼zÈ#ÌrÏyÉyÔ?Y¹Ô_a×_jÑ_jÓ_lˆŒá57¸BÀ"ºHÈÈaÊ”"—.‘.¨bÕ1ÐUYˆŒÜ˜"˜b ! Q¨!¨q°eÔ<ˆŒ
Ü˜"˜b ! Q¨1¨q°A©v©:¸!ÀÔGˆ�
r5   c                óB   — | j                  | j                  |«      «      S )zžForward pass for deploy mode.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        r=   r9   s     r   r>   zRepConv.forward_fuse�  r?   r5   c                óª   — | j                   €dn| j                  |«      }| j                  | j                  |«      | j                  |«      z   |z   «      S )z Forward pass for training mode.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor.
        r   )r)   r-   r]   r^   )r/   r   Úid_outs      r   r:   zRepConv.forward˜  sD   € ð —g‘g�o‘¨4¯7©7°1«:ˆØ�x‰x˜Ÿ
™
 1›¨¯
©
°1«Ñ5¸Ñ>Ó?Ð?r5   c                óô   — | j                  | j                  «      \  }}| j                  | j                  «      \  }}| j                  | j                  «      \  }}|| j	                  |«      z   |z   ||z   |z   fS )z±Calculate equivalent kernel and bias by fusing convolutions.

        Returns:
            (torch.Tensor): Equivalent kernel
            (torch.Tensor): Equivalent bias
        )Ú_fuse_bn_tensorr]   r^   r)   Ú_pad_1x1_to_3x3_tensor)r/   Ú	kernel3x3Úbias3x3Ú	kernel1x1Úbias1x1ÚkernelidÚbiasids          r   Úget_equivalent_kernel_biasz"RepConv.get_equivalent_kernel_bias¤  s{   € ð "×1Ñ1°$·*±*Ó=Ñˆ	�7Ø!×1Ñ1°$·*±*Ó=Ñˆ	�7Ø×/Ñ/°·±Ó8Ñˆ�&Ø˜4×6Ñ6°yÓAÑAÀHÑLÈgÐX_ÑN_ÐbhÑNhÐhÐhr5   c                ó`   — | €yt         j                  j                  j                  | g d¢«      S )z´Pad a 1x1 kernel to 3x3 size.

        Args:
            kernel1x1 (torch.Tensor): 1x1 convolution kernel.

        Returns:
            (torch.Tensor): Padded 3x3 kernel.
        r   )r   r   r   r   )rO   r%   Ú
functionalÚpad)rŒ   s    r   r‰   zRepConv._pad_1x1_to_3x3_tensor°  s*   € ð ÐØä—8‘8×&Ñ&×*Ñ*¨9²lÓCÐCr5   c                óä  — |€yt        |t        «      r†|j                  j                  }|j                  j
                  }|j                  j                  }|j                  j                  }|j                  j                  }|j                  j                  }�nt        |t        j                  «      �rt        | d«      s¯| j                  | j                  z  }t        j                  | j                  |ddft        j                   ¬«      }	t#        | j                  «      D ]  }
d|	|
|
|z  ddf<   Œ t%        j&                  |	«      j)                  |j                  j*                  «      | _        | j,                  }|j
                  }|j                  }|j                  }|j                  }|j                  }z   j/                  «       }|z  j1                  dddd«      }|z  |z  |z  z
  fS )zýFuse batch normalization with convolution weights.

        Args:
            branch (Conv | nn.BatchNorm2d | None): Branch to fuse.

        Returns:
            kernel (torch.Tensor): Fused kernel.
            bias (torch.Tensor): Fused bias.
        )r   r   Ú	id_tensorrY   )Údtyper   éÿÿÿÿ)r   r   r'   rQ   r)   Úrunning_meanÚrunning_varr"   Úepsr%   r(   Úhasattrr0   r3   ÚnpÚzerosÚfloat32ÚrangerO   Ú
from_numpyÚtoÚdevicer•   ÚsqrtÚreshape)r/   ÚbranchÚkernelr˜   r™   ÚgammaÚbetarš   Ú	input_dimÚkernel_valuerW   ÚstdÚts                r   rˆ   zRepConv._fuse_bn_tensor¿  s‘  € ð ˆ>ØÜ�fœdÔ#Ø—[‘[×'Ñ'ˆFØ!Ÿ9™9×1Ñ1ˆLØ Ÿ)™)×/Ñ/ˆKØ—I‘I×$Ñ$ˆEØ—9‘9—>‘>ˆDØ—)‘)—-‘-ŠCÜ˜¤§¡Õ/Ü˜4 Ô-Ø ŸG™G t§v¡vÑ-�	Ü!Ÿx™x¨¯©°)¸QÀÐ(BÌ"Ï*É*ÔU�Ü˜tŸw™w›ò =�AØ;<�L  A¨	¡M°1°aÐ!7Ò8ð=ä!&×!1Ñ!1°,Ó!?×!BÑ!BÀ6Ç=Á=×CWÑCWÓ!X�”Ø—^‘^ˆFØ!×.Ñ.ˆLØ ×,Ñ,ˆKØ—M‘MˆEØ—;‘;ˆDØ—*‘*ˆCØ˜SÑ ×&Ñ&Ó(ˆØ�S‰[×!Ñ! " a¨¨AÓ.ˆØ˜‰z˜4 ,°Ñ"6¸Ñ"<Ñ<Ð<Ð<r5   c           
     óô  — t        | d«      ry| j                  «       \  }}t        j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  d¬«      j                  d«      | _        || j
                  j                  _        || j
                  j                   _        | j#                  «       D ]  }|j%                  «        Œ | j'                  d«       | j'                  d«       t        | d«      r| j'                  d«       t        | d	«      r| j'                  d	«       t        | d
«      r| j'                  d
«       yy)zLFuse convolutions for inference by creating a single equivalent convolution.r'   NT)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingr!   r    r"   Fr]   r^   Únmr)   r•   )r›   r�   r%   r&   r]   r'   r®   r¯   r°   r±   r²   r!   r    Úrequires_grad_rQ   rR   r"   Ú
parametersÚdetach_rU   )r/   r¦   r"   Úparas       r   rX   zRepConv.fuse_convsã  sf  € ä�4˜Ô ØØ×6Ñ6Ó8‰ˆ�Ü—I‘IØŸ
™
Ÿ™×3Ñ3ØŸ™Ÿ™×5Ñ5ØŸ
™
Ÿ™×3Ñ3Ø—:‘:—?‘?×)Ñ)Ø—J‘J—O‘O×+Ñ+Ø—Z‘Z—_‘_×-Ñ-Ø—:‘:—?‘?×)Ñ)Øô	
÷ ‰.˜Ó
ð 	Œ	ð !'ˆ�	‰	×ÑÔØ"ˆ�	‰	�‰ÔØ—O‘OÓ%ò 	ˆDØ�L‰L�Nð	à×Ñ˜Ô!Ø×Ñ˜Ô!Ü�4˜ÔØ×Ñ˜TÔ"Ü�4˜ÔØ×Ñ˜TÔ"Ü�4˜Ô%Ø×Ñ˜[Õ)ð &r5   )rY   r   r   r   r   TFF)rA   rB   rC   rD   r%   rE   r*   r$   r>   r:   r�   Ústaticmethodr‰   rˆ   rX   rF   rG   s   @r   r   r   a  sO   ø„ ñð �"—'‘'“)€KõHò4	&ò
@ò
ið ñDó ðDò"=öH*r5   r   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )r   aÅ  Channel-attention module for feature recalibration.

    Applies attention weights to channels based on global average pooling.

    Attributes:
        pool (nn.AdaptiveAvgPool2d): Global average pooling.
        fc (nn.Conv2d): Fully connected layer implemented as 1x1 convolution.
        act (nn.Sigmoid): Sigmoid activation for attention weights.

    References:
        https://github.com/open-mmlab/mmdetection/tree/v3.0.0rc1/configs/rtmdet
    c                óÈ   •— t         ‰| �  «        t        j                  d«      | _        t        j
                  ||dddd¬«      | _        t        j                  «       | _        y)zrInitialize Channel-attention module.

        Args:
            channels (int): Number of input channels.
        r   r   Trn   N)	r#   r$   r%   ÚAdaptiveAvgPool2dÚpoolr&   ÚfcÚSigmoidr-   )r/   Úchannelsr4   s     €r   r$   zChannelAttention.__init__  sI   ø€ ô 	‰ÑÔÜ×(Ñ(¨Ó+ˆŒ	Ü—)‘)˜H h°°1°a¸dÔCˆŒÜ—:‘:“<ˆ�r5   c                óf   — || j                  | j                  | j                  |«      «      «      z  S )zºApply channel attention to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Channel-attended output tensor.
        )r-   r½   r¼   r9   s     r   r:   zChannelAttention.forward  s)   € ð �4—8‘8˜DŸG™G D§I¡I¨a£LÓ1Ó2Ñ2Ð2r5   )r¿   r   ÚreturnÚNone)r   útorch.TensorrÁ   rÃ   rx   rG   s   @r   r   r      s   ø„ ñõ	 ÷	3r5   r   c                  ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )r   a!  Spatial-attention module for feature recalibration.

    Applies attention weights to spatial dimensions based on channel statistics.

    Attributes:
        cv1 (nn.Conv2d): Convolution layer for spatial attention.
        act (nn.Sigmoid): Sigmoid activation for attention weights.
    c                óº   •— t         ‰| �  «        |dv sJ d«       ‚|dk(  rdnd}t        j                  dd||d¬«      | _        t        j
                  «       | _        y	)
z†Initialize Spatial-attention module.

        Args:
            kernel_size (int): Size of the convolutional kernel (3 or 7).
        >   rY   é   zkernel size must be 3 or 7rÆ   rY   r   r   F)r²   r"   N)r#   r$   r%   r&   r|   r¾   r-   )r/   r°   r²   r4   s      €r   r$   zSpatialAttention.__init__/  sW   ø€ ô 	‰ÑÔØ˜fÑ$ÐBÐ&BÓBÐ$Ø" aÒ'‘!¨QˆÜ—9‘9˜Q  ;¸ÀeÔLˆŒÜ—:‘:“<ˆ�r5   c                óÒ   — || j                  | j                  t        j                  t        j                  |dd¬«      t        j
                  |dd¬«      d   gd«      «      «      z  S )zºApply spatial attention to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Spatial-attended output tensor.
        r   T)Úkeepdimr   )r-   r|   rO   rw   ÚmeanÚmaxr9   s     r   r:   zSpatialAttention.forward;  sX   € ð �4—8‘8˜DŸH™H¤U§Y¡Y´·
±
¸1¸aÈÔ0NÔPU×PYÑPYÐZ[Ð]^ÐhlÔPmÐnoÑPpÐ/qÐstÓ%uÓvÓwÑwÐwr5   ©rÆ   rx   rG   s   @r   r   r   %  s   ø„ ñõ
 ö	xr5   r   c                  ó*   ‡ — e Zd ZdZdˆ fd„	Zd„ Zˆ xZS )r   a(  Convolutional Block Attention Module.

    Combines channel and spatial attention mechanisms for comprehensive feature refinement.

    Attributes:
        channel_attention (ChannelAttention): Channel attention module.
        spatial_attention (SpatialAttention): Spatial attention module.
    c                ób   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        y)zÅInitialize CBAM with given parameters.

        Args:
            c1 (int): Number of input channels.
            kernel_size (int): Size of the convolutional kernel for spatial attention.
        N)r#   r$   r   Úchannel_attentionr   Úspatial_attention)r/   r0   r°   r4   s      €r   r$   zCBAM.__init__Q  s*   ø€ ô 	‰ÑÔÜ!1°"Ó!5ˆÔÜ!1°+Ó!>ˆÕr5   c                óB   — | j                  | j                  |«      «      S )zËApply channel and spatial attention sequentially to input tensor.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Attended output tensor.
        )rÏ   rÎ   r9   s     r   r:   zCBAM.forward\  s    € ð ×%Ñ% d×&<Ñ&<¸QÓ&?Ó@Ð@r5   rË   rx   rG   s   @r   r   r   G  s   ø„ ñõ	?ö	Ar5   r   c                  ó,   ‡ — e Zd ZdZdˆ fd„	Zdd„Zˆ xZS )r   z�Concatenate a list of tensors along specified dimension.

    Attributes:
        d (int): Dimension along which to concatenate tensors.
    c                ó0   •— t         ‰| �  «        || _        y)z|Initialize Concat module.

        Args:
            dimension (int): Dimension along which to concatenate tensors.
        N)r#   r$   r   )r/   Ú	dimensionr4   s     €r   r$   zConcat.__init__o  s   ø€ ô 	‰ÑÔØˆ�r5   c                óB   — t        j                  || j                  «      S )zÊConcatenate input tensors along specified dimension.

        Args:
            x (list[torch.Tensor]): List of input tensors.

        Returns:
            (torch.Tensor): Concatenated tensor.
        )rO   rw   r   r9   s     r   r:   zConcat.forwardx  s   € ô �y‰y˜˜DŸF™FÓ#Ð#r5   )r   ©r   zlist[torch.Tensor]rx   rG   s   @r   r   r   h  s   ø„ ñõ÷	$r5   r   c                  ó,   ‡ — e Zd ZdZdˆ fd„	Zdd„Zˆ xZS )r   zoReturns a particular index of the input.

    Attributes:
        index (int): Index to select from input.
    c                ó0   •— t         ‰| �  «        || _        y)zeInitialize Index module.

        Args:
            index (int): Index to select from input.
        N)r#   r$   Úindex)r/   rØ   r4   s     €r   r$   zIndex.__init__‹  s   ø€ ô 	‰ÑÔØˆ�
r5   c                ó    — || j                      S )zÂSelect and return a particular index from input.

        Args:
            x (list[torch.Tensor]): List of input tensors.

        Returns:
            (torch.Tensor): Selected tensor.
        )rØ   r9   s     r   r:   zIndex.forward”  s   € ð �—‘‰}Ðr5   )r   rÕ   rx   rG   s   @r   r   r   „  s   ø„ ñõ÷	r5   r   )Nr   )rD   Ú
__future__r   rd   Únumpyrœ   rO   Útorch.nnr%   Ú__all__r   r+   r   r   r   r
   ro   r   r	   r   r   r   r   r   r   r   r   © r5   r   ú<module>rß      s  ðá å "ã ã Û Ý ð€ó$ô2&ˆ2�9‰9ô 2&ôj7)ˆDô 7)ôt )�—	‘	ô  )ôFIˆTô Iô"H˜×*Ñ*ô Hô"10�B—I‘Iô 10ôh$uˆB�I‰Iô $uôP'.�—	‘	ô '.ôT\*ˆb�i‰iô \*ô~"3�r—y‘yô "3ôJx�r—y‘yô xôDAˆ2�9‰9ô AôB$ˆR�Y‰Yô $ô8ˆB�I‰Iõ r5   