§
    l¬¸jŒR  ã                  óF  — 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dS )%zConvolution modules.é    )ÚannotationsN)Únn)ÚCBAMÚChannelAttentionÚConcatÚConvÚConv2ÚConvTransposeÚDWConvÚDWConvTranspose2dÚFocusÚ	GhostConvÚIndexÚ	LightConvÚRepConvÚSpatialAttentioné   c                óÀ   ‡— ‰dk    r.t          | t          ¦  «        r‰| dz
  z  dz   nˆfd„| D ¦   «         } |€&t          | t          ¦  «        r| dz  nd„ | D ¦   «         }|S )zPad to 'same' shape outputs.r   c                ó&   •— g | ]}‰|d z
  z  d z   ‘ŒS ©r   © )Ú.0ÚxÚds     €úk/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/nn/modules/conv.pyú
<listcomp>zautopad.<locals>.<listcomp>!   s%   ø€ Ð7SÐ7SÐ7SÈA¸¸QÀ¹U¹Àa¹Ð7SÐ7SÐ7Só    Né   c                ó   — g | ]}|d z  ‘ŒS ©r   r   ©r   r   s     r   r   zautopad.<locals>.<listcomp>#   s   € Ð.AÐ.AÐ.A¸!¨q°A©vÐ.AÐ.AÐ.Ar   )Ú
isinstanceÚint)ÚkÚpr   s     `r   Úautopadr&      s{   ø€ àˆ1‚u€uÝ)¨!­SÑ1Ô1ÐSˆA��Q‘‰K˜!‰OˆOÐ7SÐ7SÐ7SÐ7SÐQRÐ7SÑ7SÔ7SˆØ€yÝ  ¥CÑ(Ô(ÐAˆA�‰FˆFÐ.AÐ.A¸qÐ.AÑ.AÔ.AˆØ€Hr   c                  óN   ‡ — 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).
    r   NTc	                óX  •— t          ¦   «                              ¦   «          t          j        ||||t	          |||¦  «        ||d¬¦  «        | _        t          j        |¦  «        | _        |du r| j        n.t          |t          j
        ¦  «        r|nt          j        ¦   «         | _        dS )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__r   ÚConv2dr&   ÚconvÚBatchNorm2dÚbnÚdefault_actr"   ÚModuleÚIdentityÚact©
ÚselfÚc1Úc2r$   Úsr%   Úgr   r6   Ú	__class__s
            €r   r.   zConv.__init__3   s–   ø€ õ 	‰Œ×ÒÑÔÐÝ”I˜b " a¨­G°A°q¸!Ñ,<Ô,<ÀQÐQRÐY^Ð_Ñ_Ô_ˆŒ	Ý”. Ñ$Ô$ˆŒØ'*¨d { {�4Ô#Ð#½zÈ#ÍrÌyÑ?YÔ?YÐ8l¸¸Õ_aÔ_jÑ_lÔ_lˆŒˆˆr   c                óx   — |                       |                      |                      |¦  «        ¦  «        ¦  «        S ©zÇApply convolution, batch normalization and activation to input tensor.

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

        Returns:
            (torch.Tensor): Output tensor.
        ©r6   r2   r0   ©r8   r   s     r   ÚforwardzConv.forwardE   ó,   € ð �xŠx˜Ÿš §	¢	¨!¡¤Ñ-Ô-Ñ.Ô.Ð.r   c                óR   — |                       |                      |¦  «        ¦  «        S )z¾Apply convolution and activation without batch normalization.

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

        Returns:
            (torch.Tensor): Output tensor.
        ©r6   r0   rA   s     r   Úforward_fusezConv.forward_fuseP   ó    € ð �xŠx˜Ÿ	š	 !™œÑ%Ô%Ð%r   )r   r   Nr   r   T©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚSiLUr3   r.   rB   rF   Ú__classcell__©r=   s   @r   r   r   '   sz   ø€ € € € € ðð ð �"”'‘)”)€Kðmð mð mð mð mð mð$	/ð 	/ð 	/ð	&ð 	&ð 	&ð 	&ð 	&ð 	&ð 	&r   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.
    é   r   NTc	                óº   •— t          ¦   «                              ||||||||¬¦  «         t          j        ||d|t	          d||¦  «        ||d¬¦  «        | _        dS )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.
        ©r<   r   r6   r   Fr)   N)r-   r.   r   r/   r&   Úcv2r7   s
            €r   r.   zConv2.__init__f   sb   ø€ õ 	‰Œ×Ò˜˜R  A q¨A°¸ÐÑ<Ô<Ð<Ý”9˜R  Q¨­7°1°a¸Ñ+;Ô+;ÀAÐPQÐX]Ð^Ñ^Ô^ˆŒˆˆr   c                ó¤   — |                       |                      |                      |¦  «        |                      |¦  «        z   ¦  «        ¦  «        S r?   )r6   r2   r0   rT   rA   s     r   rB   zConv2.forwardv   s;   € ð �xŠx˜Ÿš §	¢	¨!¡¤¨t¯xªx¸©{¬{Ñ :Ñ;Ô;Ñ<Ô<Ð<r   c                óx   — |                       |                      |                      |¦  «        ¦  «        ¦  «        S )zÍApply fused convolution, batch normalization and activation to input tensor.

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

        Returns:
            (torch.Tensor): Output tensor.
        r@   rA   s     r   rF   zConv2.forward_fuse�   rC   r   c                ó”  — t          j        | j        j        j        ¦  «        }d„ |j        dd…         D ¦   «         }| j        j        j                             ¦   «         |dd…dd…|d         |d         dz   …|d         |d         dz   …f<   | j        j        xj        |z  c_        |                      d¦  «         | j	        | _
        dS )zFuse parallel convolutions.c                ó   — g | ]}|d z  ‘ŒS r    r   r!   s     r   r   z$Conv2.fuse_convs.<locals>.<listcomp>�   s   € Ð)Ð)Ð)˜ˆQ�!‰VÐ)Ð)Ð)r   r   Nr   r   rT   )ÚtorchÚ
zeros_liker0   ÚweightÚdataÚshaperT   ÚcloneÚ__delattr__rF   rB   )r8   ÚwÚis      r   Ú
fuse_convszConv2.fuse_convsŒ   sÀ   € åÔ˜TœYÔ-Ô2Ñ3Ô3ˆØ)Ð)˜QœW Q R Rœ[Ð)Ñ)Ô)ˆØ48´H´OÔ4H×4NÒ4NÑ4PÔ4Pˆˆ!ˆ!ˆ!ˆQˆQˆQ��!”�q˜”t˜a‘x�  1¤¨¨!¬¨q© Ð
0Ñ1ØŒ	ÔÐÔ Ñ"ÐÔØ×Ò˜ÑÔÐØÔ(ˆŒˆˆr   )rQ   r   Nr   r   T)	rI   rJ   rK   rL   r.   rB   rF   rb   rN   rO   s   @r   r	   r	   \   sz   ø€ € € € € ðð ð_ð _ð _ð _ð _ð _ð 	=ð 	=ð 	=ð	/ð 	/ð 	/ð)ð )ð )ð )ð )ð )ð )r   r	   c                  ó*   ‡ — e Zd ZdZdˆ 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   Nc                óÖ   •— t          ¦   «                              ¦   «          |€t          j        ¦   «         n|}t	          ||dd¬¦  «        | _        t          ||||¬¦  «        | _        dS )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.
        Nr   F©r6   )r-   r.   r   ÚReLUr   Úconv1r   Úconv2)r8   r9   r:   r$   r6   r=   s        €r   r.   zLightConv.__init__    sb   ø€ õ 	‰Œ×ÒÑÔÐØ˜;�bŒg‰iŒiˆi¨CˆÝ˜"˜b !¨Ð/Ñ/Ô/ˆŒ
Ý˜B  A¨3Ð/Ñ/Ô/ˆŒ
ˆ
ˆ
r   c                óR   — |                       |                      |¦  «        ¦  «        S )z¦Apply 2 convolutions to input tensor.

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

        Returns:
            (torch.Tensor): Output tensor.
        )rh   rg   rA   s     r   rB   zLightConv.forward®   s    € ð �zŠz˜$Ÿ*š* Q™-œ-Ñ(Ô(Ð(r   )r   N©rI   rJ   rK   rL   r.   rB   rN   rO   s   @r   r   r   –   sV   ø€ € € € € ðð ð0ð 0ð 0ð 0ð 0ð 0ð	)ð 	)ð 	)ð 	)ð 	)ð 	)ð 	)r   r   c                  ó$   ‡ — e Zd ZdZdˆ fd„	Zˆ xZS )r   zDepth-wise convolution module.r   Tc           
     ó~   •— t          ¦   «                              ||||t          j        ||¦  «        ||¬¦  «         dS )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.
        rS   N©r-   r.   ÚmathÚgcd)r8   r9   r:   r$   r;   r   r6   r=   s          €r   r.   zDWConv.__init__½   s>   ø€ õ 	‰Œ×Ò˜˜R  A­¬°"°bÑ)9Ô)9¸QÀCÐÑHÔHÐHÐHÐHr   ©r   r   r   T©rI   rJ   rK   rL   r.   rN   rO   s   @r   r   r   º   sM   ø€ € € € € Ø(Ð(ðIð Ið Ið Ið Ið Ið Ið Ið Ið Ir   r   c                  ó$   ‡ — e Zd ZdZdˆ fd„	Zˆ xZS )r   z(Depth-wise transpose convolution module.r   r   c                ó~   •— t          ¦   «                              ||||||t          j        ||¦  «        ¬¦  «         dS )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*   Nrm   )r8   r9   r:   r$   r;   Úp1Úp2r=   s          €r   r.   zDWConvTranspose2d.__init__Î   s>   ø€ õ 	‰Œ×Ò˜˜R  A r¨2µd´h¸rÀ2Ñ6FÔ6FÐÑGÔGÐGÐGÐGr   )r   r   r   r   rq   rO   s   @r   r   r   Ë   sM   ø€ € € € € Ø2Ð2ðHð Hð Hð Hð Hð Hð Hð Hð Hð Hr   r   c                  óN   ‡ — 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).
    r   r   Tc                ób  •— t          ¦   «                              ¦   «          t          j        |||||| ¬¦  «        | _        |rt          j        |¦  «        nt          j        ¦   «         | _        |du r| j        n.t          |t          j
        ¦  «        r|nt          j        ¦   «         | _        dS )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_transposer1   r5   r2   r3   r"   r4   r6   )	r8   r9   r:   r$   r;   r%   r2   r6   r=   s	           €r   r.   zConvTranspose.__init__è   s˜   ø€ õ 	‰Œ×ÒÑÔÐÝ Ô0°°R¸¸A¸qÈ2ÀvÐNÑNÔNˆÔØ(*Ð=•"”. Ñ$Ô$Ð$µ´±´ˆŒØ'*¨d { {�4Ô#Ð#½zÈ#ÍrÌyÑ?YÔ?YÐ8l¸¸Õ_aÔ_jÑ_lÔ_lˆŒˆˆr   c                óx   — |                       |                      |                      |¦  «        ¦  «        ¦  «        S )zËApply transposed convolution, batch normalization and activation to input.

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

        Returns:
            (torch.Tensor): Output tensor.
        )r6   r2   rz   rA   s     r   rB   zConvTranspose.forwardù   s0   € ð �xŠx˜Ÿš × 3Ò 3°AÑ 6Ô 6Ñ7Ô7Ñ8Ô8Ð8r   c                óR   — |                       |                      |¦  «        ¦  «        S )zµApply convolution transpose and activation to input.

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

        Returns:
            (torch.Tensor): Output tensor.
        )r6   rz   rA   s     r   rF   zConvTranspose.forward_fuse  s$   € ð �xŠx˜×+Ò+¨AÑ.Ô.Ñ/Ô/Ð/r   )r   r   r   TTrH   rO   s   @r   r
   r
   Ü   sz   ø€ € € € € ðð ð �"”'‘)”)€Kðmð mð mð mð mð mð"	9ð 	9ð 	9ð	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0r   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.
    r   NTc           	     ó„   •— t          ¦   «                              ¦   «          t          |dz  ||||||¬¦  «        | _        dS )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.
        é   re   N)r-   r.   r   r0   )	r8   r9   r:   r$   r;   r%   r<   r6   r=   s	           €r   r.   zFocus.__init__  sA   ø€ õ 	‰Œ×ÒÑÔÐÝ˜˜a™  Q¨¨1¨a°SÐ9Ñ9Ô9ˆŒ	ˆ	ˆ	r   c                óÒ   — |                       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   )r0   rY   ÚcatrA   s     r   rB   zFocus.forward)  s›   € ð �yŠy�œ A c¨3¨3¨Q¨3°°°!° mÔ$4°a¸¸Q¸TÀ¸TÀ3À3ÀQÀ3¸Ô6GÈÈ3ÐPSÐPSÐRSÐPSÐUVÐUYÐXYÐUYÈ>ÔIZÐ\]Ð^aÐcdÐcgÐfgÐcgÐijÐimÐlmÐimÐ^mÔ\nÐ#oÐqrÑsÔsÑtÔtÐtr   )r   r   Nr   Trj   rO   s   @r   r   r     s]   ø€ € € € € ðð ð:ð :ð :ð :ð :ð :ð uð uð uð uð uð uð ur   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
    r   Tc           	     ó¾   •— t          ¦   «                              ¦   «          |dz  }t          ||||d||¬¦  «        | _        t          ||ddd||¬¦  «        | _        dS )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   Nre   é   r   )r-   r.   r   Úcv1rT   )	r8   r9   r:   r$   r;   r<   r6   Úc_r=   s	           €r   r.   zGhostConv.__init__E  sd   ø€ õ 	‰Œ×ÒÑÔÐØ�1‰WˆÝ˜˜B  1 d¨A°3Ð7Ñ7Ô7ˆŒÝ˜˜B  1 d¨B°CÐ8Ñ8Ô8ˆŒˆˆr   c                ó€   — |                       |¦  «        }t          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…   rY   r�   rT   )r8   r   Úys      r   rB   zGhostConv.forwardU  s4   € ð �HŠH�Q‰KŒKˆÝŒy˜!˜TŸXšX a™[œ[Ð)¨1Ñ-Ô-Ð-r   rp   rj   rO   s   @r   r   r   8  sV   ø€ € € € € ð
ð 
ð9ð 9ð 9ð 9ð 9ð 9ð 
.ð 
.ð 
.ð 
.ð 
.ð 
.ð 
.r   r   c                  óv   ‡ — 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
    rQ   r   TFc           	     óØ  •— 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¬¦  «        | _        dS )	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.
        rQ   r   T)Únum_featuresNF)r%   r<   r6   r   )r-   r.   r<   r9   r:   r3   r"   r   r4   r5   r6   r1   r2   r   rg   rh   )r8   r9   r:   r$   r;   r%   r<   r   r6   r2   Údeployr=   s              €r   r.   zRepConv.__init__t  só   ø€ õ 	‰Œ×ÒÑÔÐØ�AŠvˆv˜!˜qš&˜&˜&Ð ØˆŒØˆŒØˆŒØ'*¨d { {�4Ô#Ð#½zÈ#ÍrÌyÑ?YÔ?YÐ8l¸¸Õ_aÔ_jÑ_lÔ_lˆŒà57ÐY¸BÀ"ºH¸HÈÈaÊÈ•"”.¨bÐ1Ñ1Ô1Ð1ÐUYˆŒÝ˜"˜b ! Q¨!¨q°eÐ<Ñ<Ô<ˆŒ
Ý˜"˜b ! Q¨1¨q°A©v©:¸!ÀÐGÑGÔGˆŒ
ˆ
ˆ
r   c                óR   — |                       |                      |¦  «        ¦  «        S )zžForward pass for deploy mode.

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

        Returns:
            (torch.Tensor): Output tensor.
        rE   rA   s     r   rF   zRepConv.forward_fuseŽ  rG   r   c                óÀ   — | j         €dn|                       |¦  «        }|                      |                      |¦  «        |                      |¦  «        z   |z   ¦  «        S )z Forward pass for training mode.

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

        Returns:
            (torch.Tensor): Output tensor.
        Nr   )r2   r6   rg   rh   )r8   r   Úid_outs      r   rB   zRepConv.forward™  sM   € ð ”g�o��¨4¯7ª7°1©:¬:ˆØ�xŠx˜Ÿ
š
 1™œ¨¯
ª
°1©¬Ñ5¸Ñ>Ñ?Ô?Ð?r   c                óö   — |                       | 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_tensorrg   rh   r2   Ú_pad_1x1_to_3x3_tensor)r8   Ú	kernel3x3Úbias3x3Ú	kernel1x1Úbias1x1ÚkernelidÚbiasids          r   Úget_equivalent_kernel_biasz"RepConv.get_equivalent_kernel_bias¥  s�   € ð "×1Ò1°$´*Ñ=Ô=Ñˆ	�7Ø!×1Ò1°$´*Ñ=Ô=Ñˆ	�7Ø×/Ò/°´Ñ8Ô8Ñˆ�&Ø˜4×6Ò6°yÑAÔAÑAÀHÑLÈgÐX_ÑN_ÐbhÑNhÐhÐhr   c                óX   — | €dS t           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.
        Nr   )r   r   r   r   )rY   r   Ú
functionalÚpad)r•   s    r   r’   zRepConv._pad_1x1_to_3x3_tensor±  s.   € ð ÐØ�1å”8Ô&×*Ò*¨9°l°l°lÑCÔCÐCr   c                ó  — |€dS t          |t          ¦  «        rI|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        }||z                        ¦   «         }||z                       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.
        N)r   r   Ú	id_tensorrQ   )Údtyper   éÿÿÿÿ)r"   r   r0   r[   r2   Úrunning_meanÚrunning_varr,   Úepsr   r1   Úhasattrr9   r<   ÚnpÚzerosÚfloat32ÚrangerY   Ú
from_numpyÚtoÚdevicerž   ÚsqrtÚreshape)r8   ÚbranchÚkernelr¡   r¢   ÚgammaÚbetar£   Ú	input_dimÚkernel_valuera   ÚstdÚts                r   r‘   zRepConv._fuse_bn_tensorÀ  s…  € ð ˆ>Ø�4Ý�f�dÑ#Ô#ð 	Ø”[Ô'ˆFØ!œ9Ô1ˆLØ œ)Ô/ˆKØ”IÔ$ˆEØ”9”>ˆDØ”)”-ˆCˆCÝ˜¥¤Ñ/Ô/ð 	Ý˜4 Ñ-Ô-ð YØ œG t¤vÑ-�	Ý!œx¨¬°)¸QÀÐ(BÍ"Ì*ÐUÑUÔU�Ý˜tœw™œð =ð =�AØ;<�L  A¨	¡M°1°aÐ!7Ñ8Ð8Ý!&Ô!1°,Ñ!?Ô!?×!BÒ!BÀ6Ä=ÔCWÑ!XÔ!X�”Ø”^ˆFØ!Ô.ˆLØ Ô,ˆKØ”MˆEØ”;ˆDØ”*ˆCØ˜SÑ ×&Ò&Ñ(Ô(ˆØ�S‰[×!Ò! " a¨¨AÑ.Ô.ˆØ˜‰z˜4 ,°Ñ"6¸Ñ"<Ñ<Ð<Ð<r   c           
     ób  — t          | d¦  «        rdS |                      ¦   «         \  }}t          j        | j        j        j        | j        j        j        | j        j        j        | j        j        j	        | j        j        j
        | j        j        j        | j        j        j        d¬¦  «                             d¦  «        | _        || j        j        _        || j        j        _        |                      ¦   «         D ]}|                     ¦   «          Œ|                      d¦  «         |                      d¦  «         t          | d¦  «        r|                      d¦  «         t          | d	¦  «        r|                      d	¦  «         t          | d
¦  «        r|                      d
¦  «         dS dS )zLFuse convolutions for inference by creating a single equivalent convolution.r0   NT)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingr+   r*   r,   Frg   rh   Únmr2   rž   )r¤   r™   r   r/   rg   r0   r·   r¸   r¹   rº   r»   r+   r*   Úrequires_grad_r[   r\   r,   Ú
parametersÚdetach_r_   )r8   r¯   r,   Úparas       r   rb   zRepConv.fuse_convsä  sŽ  € å�4˜Ñ Ô ð 	ØˆFØ×6Ò6Ñ8Ô8‰ˆ�Ý”IØœ
œÔ3ØœœÔ5Øœ
œÔ3Ø”:”?Ô)Ø”J”OÔ+Ø”Z”_Ô-Ø”:”?Ô)Øð	
ñ 	
ô 	
÷ Š.˜Ñ
Ô
ð 	Œ	ð !'ˆŒ	ÔÔØ"ˆŒ	ŒÔØ—O’OÑ%Ô%ð 	ð 	ˆDØ�LŠL‰NŒNˆNˆNØ×Ò˜Ñ!Ô!Ð!Ø×Ò˜Ñ!Ô!Ð!Ý�4˜ÑÔð 	#Ø×Ò˜TÑ"Ô"Ð"Ý�4˜ÑÔð 	#Ø×Ò˜TÑ"Ô"Ð"Ý�4˜Ñ%Ô%ð 	*Ø×Ò˜[Ñ)Ô)Ð)Ð)Ð)ð	*ð 	*r   )rQ   r   r   r   r   TFF)rI   rJ   rK   rL   r   rM   r3   r.   rF   rB   r™   Ústaticmethodr’   r‘   rb   rN   rO   s   @r   r   r   b  sÌ   ø€ € € € € ðð ð �"”'‘)”)€KðHð Hð Hð Hð Hð Hð4	&ð 	&ð 	&ð
@ð 
@ð 
@ð
ið 
ið 
ið ðDð Dñ „\ðDð"=ð "=ð "=ðH*ð *ð *ð *ð *ð *ð *r   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
    Úchannelsr#   ÚreturnÚNonec                óè   •— t          ¦   «                              ¦   «          t          j        d¦  «        | _        t          j        ||dddd¬¦  «        | _        t          j        ¦   «         | _        dS )zrInitialize Channel-attention module.

        Args:
            channels (int): Number of input channels.
        r   r   Trx   N)	r-   r.   r   ÚAdaptiveAvgPool2dÚpoolr/   ÚfcÚSigmoidr6   )r8   rÃ   r=   s     €r   r.   zChannelAttention.__init__  s\   ø€ õ 	‰Œ×ÒÑÔÐÝÔ(¨Ñ+Ô+ˆŒ	Ý”)˜H h°°1°a¸dÐCÑCÔCˆŒÝ”:‘<”<ˆŒˆˆr   r   útorch.Tensorc                ó~   — ||                       |                      |                      |¦  «        ¦  «        ¦  «        z  S )zºApply channel attention to input tensor.

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

        Returns:
            (torch.Tensor): Channel-attended output tensor.
        )r6   rÉ   rÈ   rA   s     r   rB   zChannelAttention.forward  s1   € ð �4—8’8˜DŸGšG D§I¢I¨a¡L¤LÑ1Ô1Ñ2Ô2Ñ2Ð2r   )rÃ   r#   rÄ   rÅ   )r   rË   rÄ   rË   rj   rO   s   @r   r   r     s[   ø€ € € € € ðð ð	 ð 	 ð 	 ð 	 ð 	 ð 	 ð	3ð 	3ð 	3ð 	3ð 	3ð 	3ð 	3ð 	3r   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        ¦   «         | _        d	S )
z†Initialize Spatial-attention module.

        Args:
            kernel_size (int): Size of the convolutional kernel (3 or 7).
        >   rQ   rÎ   zkernel size must be 3 or 7rÎ   rQ   r   r   F)r»   r,   N)r-   r.   r   r/   r…   rÊ   r6   )r8   r¹   r»   r=   s      €r   r.   zSpatialAttention.__init__0  su   ø€ õ 	‰Œ×ÒÑÔÐØ˜fÐ$Ð$Ð$Ð&BÑ$Ô$Ð$Ø" aÒ'Ð'�!�!¨QˆÝ”9˜Q  ;¸ÀeÐLÑLÔLˆŒÝ”:‘<”<ˆŒˆˆr   c                óâ   — ||                       |                      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   )r6   r…   rY   r�   ÚmeanÚmaxrA   s     r   rB   zSpatialAttention.forward<  sf   € ð �4—8’8˜DŸHšH¥U¤Yµ´
¸1¸aÈÐ0NÑ0NÔ0NÕPUÔPYÐZ[Ð]^ÐhlÐPmÑPmÔPmÐnoÔPpÐ/qÐstÑ%uÔ%uÑvÔvÑwÔwÑwÐwr   ©rÎ   rj   rO   s   @r   r   r   &  s]   ø€ € € € € ðð ð
 ð 
 ð 
 ð 
 ð 
 ð 
 ð	xð 	xð 	xð 	xð 	xð 	xð 	xr   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.
    rÎ   c                ó˜   •— t          ¦   «                              ¦   «          t          |¦  «        | _        t	          |¦  «        | _        dS )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)r8   r9   r¹   r=   s      €r   r.   zCBAM.__init__R  sA   ø€ õ 	‰Œ×ÒÑÔÐÝ!1°"Ñ!5Ô!5ˆÔÝ!1°+Ñ!>Ô!>ˆÔÐÐr   c                óR   — |                       |                      |¦  «        ¦  «        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×   rA   s     r   rB   zCBAM.forward]  s&   € ð ×%Ò% d×&<Ò&<¸QÑ&?Ô&?Ñ@Ô@Ð@r   rÔ   rj   rO   s   @r   r   r   H  s]   ø€ € € € € ðð ð	?ð 	?ð 	?ð 	?ð 	?ð 	?ð	Að 	Að 	Að 	Að 	Að 	Að 	Ar   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.
    r   c                óV   •— t          ¦   «                              ¦   «          || _        dS )z|Initialize Concat module.

        Args:
            dimension (int): Dimension along which to concatenate tensors.
        N)r-   r.   r   )r8   Ú	dimensionr=   s     €r   r.   zConcat.__init__p  s&   ø€ õ 	‰Œ×ÒÑÔÐØˆŒˆˆr   r   úlist[torch.Tensor]c                ó6   — 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.
        )rY   r�   r   rA   s     r   rB   zConcat.forwardy  s   € õ Œy˜˜DœFÑ#Ô#Ð#r   r   ©r   rÝ   rj   rO   s   @r   r   r   i  s[   ø€ € € € € ðð ðð ð ð ð ð ð	$ð 	$ð 	$ð 	$ð 	$ð 	$ð 	$ð 	$r   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.
    r   c                óV   •— t          ¦   «                              ¦   «          || _        dS )zeInitialize Index module.

        Args:
            index (int): Index to select from input.
        N)r-   r.   Úindex)r8   râ   r=   s     €r   r.   zIndex.__init__Œ  s&   ø€ õ 	‰Œ×ÒÑÔÐØˆŒ
ˆ
ˆ
r   r   rÝ   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â   rA   s     r   rB   zIndex.forward•  s   € ð �”Œ}Ðr   )r   rß   rj   rO   s   @r   r   r   …  s[   ø€ € € € € ðð ðð ð ð ð ð ð	ð 	ð 	ð 	ð 	ð 	ð 	ð 	r   r   )Nr   )rL   Ú
__future__r   rn   Únumpyr¥   rY   r   Ú__all__r&   r4   r   r	   r   r   ry   r   r
   r   r   r   r   r   r   r   r   r   r   r   ú<module>rç      s  ðà Ð à "Ð "Ð "Ð "Ð "Ð "à €€€à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð ð€ð$ð ð ð ð2&ð 2&ð 2&ð 2&ð 2&ˆ2Œ9ñ 2&ô 2&ð 2&ðj7)ð 7)ð 7)ð 7)ð 7)ˆDñ 7)ô 7)ð 7)ðt!)ð !)ð !)ð !)ð !)�”	ñ !)ô !)ð !)ðHIð Ið Ið Ið IˆTñ Iô Ið Ið"Hð Hð Hð Hð H˜Ô*ñ Hô Hð Hð"10ð 10ð 10ð 10ð 10�B”Iñ 10ô 10ð 10ðh$uð $uð $uð $uð $uˆBŒIñ $uô $uð $uðP'.ð '.ð '.ð '.ð '.�”	ñ '.ô '.ð '.ðT\*ð \*ð \*ð \*ð \*ˆbŒiñ \*ô \*ð \*ð~"3ð "3ð "3ð "3ð "3�r”yñ "3ô "3ð "3ðJxð xð xð xð x�r”yñ xô xð xðDAð Að Að Að Aˆ2Œ9ñ Aô Að AðB$ð $ð $ð $ð $ˆRŒYñ $ô $ð $ð8ð ð ð ð ˆBŒIñ ô ð ð ð r   