§
    l¬¸j%' ã                  ó   — d Z ddlmZ ddlZddlZddlmc mZ ddlmZ ddl	m
Z
 ddlmZmZmZmZmZmZ ddlmZ d	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 G d„ de¦  «        Z  G d „ d!ej        ¦  «        Z! G d"„ d#e¦  «        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 d0„ d1ej        ¦  «        Z) G d2„ d3ej        ¦  «        Z* G d4„ d5ej        ¦  «        Z+ G d6„ d7ej        ¦  «        Z, G d8„ d9ej        ¦  «        Z- G d:„ d;e%¦  «        Z. G d<„ d=e¦  «        Z/ G d>„ d?ej        ¦  «        Z0 G d@„ dAe0¦  «        Z1 G dB„ dCej        ¦  «        Z2 G dD„ dEej        ¦  «        Z3 G dF„ dGej        ¦  «        Z4 G dH„ dIej        ¦  «        Z5 G dJ„ dKej        ¦  «        Z6 G dL„ dMej        ¦  «        Z7 G dN„ dOe¦  «        Z8 G dP„ dQe¦  «        Z9 G dR„ dSej        j        ¦  «        Z: G dT„ dUej        ¦  «        Z; G dV„ dWe¦  «        Z< G dX„ dYej        ¦  «        Z= G dZ„ d[ej        ¦  «        Z> G d\„ d]ej        ¦  «        Z? G d^„ d_ej        ¦  «        Z@ G d`„ dae¦  «        ZA G db„ dcej        ¦  «        ZB G dd„ deej        ¦  «        ZC G df„ dgej        ¦  «        ZD G dh„ diej        ¦  «        ZE G dj„ dkej        ¦  «        ZF G dl„ dmej        ¦  «        ZG G dn„ doej        ¦  «        ZH G dp„ dqej        ¦  «        ZI G dr„ dse¦  «        ZJ G dt„ duej        ¦  «        ZKdS )vzBlock modules.é    )ÚannotationsN)Únn)Úfuse_conv_and_bné   )ÚConvÚDWConvÚ	GhostConvÚ	LightConvÚRepConvÚautopad)ÚTransformerBlock)'ÚC1ÚC2ÚC2PSAÚC3ÚC3TRÚCIBÚDFLÚELAN1ÚPSAÚSPPÚSPPELANÚSPPFÚAConvÚADownÚ	AttentionÚBNContrastiveHeadÚ
BottleneckÚBottleneckCSPÚC2fÚC2fAttnÚC2fCIBÚC2fPSAÚC3GhostÚC3k2ÚC3xÚCBFuseÚCBLinearÚContrastiveHeadÚGhostBottleneckÚHGBlockÚHGStemÚImagePoolingAttnÚProtoÚRepC3ÚRepNCSPELAN4ÚRepVGGDWÚResNetLayerÚSCDownÚTorchVisionc                  ó.   ‡ — e Zd ZdZd
dˆ fd„Zdd	„Zˆ xZS )r   zIntegral module of Distribution Focal Loss (DFL).

    Proposed in Generalized Focal Loss https://arxiv.org/abs/2006.04388
    é   Úc1Úintc                ór  •— t          ¦   «                              ¦   «          t          j        |ddd¬¦  «                             d¦  «        | _        t          j        |t          j        ¬¦  «        }t          j	        | 
                    d|dd¦  «        ¦  «        | j        j        j        dd…<   || _        dS )z�Initialize a convolutional layer with a given number of input channels.

        Args:
            c1 (int): Number of input channels.
        r   F©Úbias©ÚdtypeN)ÚsuperÚ__init__r   ÚConv2dÚrequires_grad_ÚconvÚtorchÚarangeÚfloatÚ	ParameterÚviewÚweightÚdatar7   )Úselfr7   ÚxÚ	__class__s      €úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/nn/modules/block.pyr?   zDFL.__init__B   s–   ø€ õ 	‰Œ×ÒÑÔÐÝ”I˜b ! Q¨UÐ3Ñ3Ô3×BÒBÀ5ÑIÔIˆŒ	ÝŒL˜¥5¤;Ð/Ñ/Ô/ˆÝ#%¤<°·²°q¸"¸aÀÑ0CÔ0CÑ#DÔ#DˆŒ	ÔÔ˜a˜a˜aÑ ØˆŒˆˆó    rK   útorch.TensorÚreturnc                óð   — |j         \  }}}|                      |                     |d| j        |¦  «                             dd¦  «                             d¦  «        ¦  «                             |d|¦  «        S )zCApply the DFL module to input tensor and return transformed output.é   é   r   )ÚshaperB   rG   r7   Ú	transposeÚsoftmax)rJ   rK   ÚbÚ_Úas        rM   ÚforwardzDFL.forwardN   sh   € à”'‰ˆˆ1ˆaØ�yŠy˜Ÿš  1 d¤g¨qÑ1Ô1×;Ò;¸A¸qÑAÔA×IÒIÈ!ÑLÔLÑMÔM×RÒRÐSTÐVWÐYZÑ[Ô[Ð[rN   )r6   )r7   r8   ©rK   rO   rP   rO   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   rZ   Ú__classcell__©rL   s   @rM   r   r   <   sh   ø€ € € € € ðð ð

ð 
ð 
ð 
ð 
ð 
ð 
ð\ð \ð \ð \ð \ð \ð \ð \rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r.   zBUltralytics YOLO models mask Proto module for segmentation models.é   é    r7   r8   Úc_Úc2c                ó  •— t          ¦   «                              ¦   «          t          ||d¬¦  «        | _        t	          j        ||dddd¬¦  «        | _        t          ||d¬¦  «        | _        t          ||¦  «        | _        dS )a  Initialize the Ultralytics YOLO models mask Proto module with specified number of protos and masks.

        Args:
            c1 (int): Input channels.
            c_ (int): Intermediate channels.
            c2 (int): Output channels (number of protos).
        é   ©ÚkrS   r   Tr:   N)	r>   r?   r   Úcv1r   ÚConvTranspose2dÚupsampleÚcv2Úcv3)rJ   r7   rf   rg   rL   s       €rM   r?   zProto.__init__X   sy   ø€ õ 	‰Œ×ÒÑÔÐÝ˜˜B !Ð$Ñ$Ô$ˆŒÝÔ*¨2¨r°1°a¸ÀÐFÑFÔFˆŒÝ˜˜B !Ð$Ñ$Ô$ˆŒÝ˜˜B‘<”<ˆŒˆˆrN   rK   rO   rP   c           	     óž   — |                       |                      |                      |                      |¦  «        ¦  «        ¦  «        ¦  «        S )zEPerform a forward pass through layers using an upsampled input image.)rp   ro   rn   rl   ©rJ   rK   s     rM   rZ   zProto.forwardf   s6   € à�xŠx˜Ÿš §¢¨t¯xªx¸©{¬{Ñ!;Ô!;Ñ<Ô<Ñ=Ô=Ð=rN   )rd   re   )r7   r8   rf   r8   rg   r8   r[   r\   rb   s   @rM   r.   r.   U   s\   ø€ € € € € ØLÐLð ð  ð  ð  ð  ð  ð  ð>ð >ð >ð >ð >ð >ð >ð >rN   r.   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd
„Zˆ xZS )r,   z©StemBlock of PPHGNetV2 with 5 convolutions and one maxpool2d.

    https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
    r7   r8   Úcmrg   c           	     ó<  •— t          ¦   «                              ¦   «          t          ||ddt          j        ¦   «         ¬¦  «        | _        t          ||dz  dddt          j        ¦   «         ¬¦  «        | _        t          |dz  |dddt          j        ¦   «         ¬¦  «        | _        t          |dz  |ddt          j        ¦   «         ¬¦  «        | _        t          ||ddt          j        ¦   «         ¬¦  «        | _	        t          j
        dddd¬¦  «        | _        dS )	z²Initialize the StemBlock of PPHGNetV2.

        Args:
            c1 (int): Input channels.
            cm (int): Middle channels.
            c2 (int): Output channels.
        ri   rS   ©Úactr   r   T)Úkernel_sizeÚstrideÚpaddingÚ	ceil_modeN)r>   r?   r   r   ÚReLUÚstem1Ústem2aÚstem2bÚstem3Ústem4Ú	MaxPool2dÚpool)rJ   r7   rt   rg   rL   s       €rM   r?   zHGStem.__init__q   sé   ø€ õ 	‰Œ×ÒÑÔÐÝ˜"˜b ! Q­B¬G©I¬IÐ6Ñ6Ô6ˆŒ
Ý˜2˜r Q™w¨¨1¨aµR´W±Y´YÐ?Ñ?Ô?ˆŒÝ˜2 ™7 B¨¨1¨aµR´W±Y´YÐ?Ñ?Ô?ˆŒÝ˜"˜q™& " a¨µ´±	´	Ð:Ñ:Ô:ˆŒ
Ý˜"˜b ! Q­B¬G©I¬IÐ6Ñ6Ô6ˆŒ
Ý”L¨Q°qÀ!ÈtÐTÑTÔTˆŒ	ˆ	ˆ	rN   rK   rO   rP   c                óŽ  — |                       |¦  «        }t          j        |g d¢¦  «        }|                      |¦  «        }t          j        |g d¢¦  «        }|                      |¦  «        }|                      |¦  «        }t          j        ||gd¬¦  «        }|                      |¦  «        }|  	                    |¦  «        }|S )ú+Forward pass of a PPHGNetV2 backbone layer.)r   r   r   r   r   ©Údim)
r}   ÚFÚpadr~   r   rƒ   rC   Úcatr€   r�   )rJ   rK   Úx2Úx1s       rM   rZ   zHGStem.forward�   s£   € à�JŠJ�q‰MŒMˆÝŒE�!�\�\�\Ñ"Ô"ˆØ�[Š[˜‰^Œ^ˆÝŒU�2�|�|�|Ñ$Ô$ˆØ�[Š[˜‰_Œ_ˆØ�YŠY�q‰\Œ\ˆÝŒI�r˜2�h AÐ&Ñ&Ô&ˆØ�JŠJ�q‰MŒMˆØ�JŠJ�q‰MŒMˆØˆrN   )r7   r8   rt   r8   rg   r8   r[   r\   rb   s   @rM   r,   r,   k   sa   ø€ € € € € ðð ð
Uð Uð Uð Uð Uð Uð ð ð ð ð ð ð ð rN   r,   c                  ó8   ‡ — e Zd ZdZ	 	 	 	 	 ddˆ fd„Zdd„Zˆ xZS )r+   z¤HG_Block of PPHGNetV2 with 2 convolutions and LightConv.

    https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
    ri   é   FNr7   r8   rt   rg   rk   ÚnÚ	lightconvÚboolÚshortcutrw   únn.Module | Nonec	                ó¢  •‡‡‡‡‡	— t          ¦   «                              ¦   «          ‰€t          j        ¦   «         n‰Š|rt          nt
          Š	t          j        ˆˆ	ˆˆˆfd„t          |¦  «        D ¦   «         ¦  «        | _        t          ‰|‰z  z   |dz  dd‰¬¦  «        | _	        t          |dz  |dd‰¬¦  «        | _
        |o‰|k    | _        dS )a¿  Initialize HGBlock with specified parameters.

        Args:
            c1 (int): Input channels.
            cm (int): Middle channels.
            c2 (int): Output channels.
            k (int): Kernel size.
            n (int): Number of LightConv or Conv blocks.
            lightconv (bool): Whether to use LightConv.
            shortcut (bool): Whether to use shortcut connection.
            act (nn.Module): Activation function.
        Nc              3  óF   •K  — | ]} ‰|d k    r‰n‰‰‰‰¬¦  «        V — ŒdS )r   ©rk   rw   N© )Ú.0Úirw   Úblockr7   rt   rk   s     €€€€€rM   ú	<genexpr>z#HGBlock.__init__.<locals>.<genexpr>¯   sC   øè è € Ð_Ð_ÐQR˜u˜u¨1°ª6¨6 R R°r¸2ÀÈÐLÑLÔLÐ_Ð_Ð_Ð_Ð_Ð_rN   rS   r   rv   )r>   r?   r   r|   r
   r   Ú
ModuleListÚrangeÚmÚscÚecÚadd)rJ   r7   rt   rg   rk   r�   r�   r’   rw   rš   rL   s    `` `   `@€rM   r?   zHGBlock.__init__•   sÕ   øøøøøø€ õ. 	‰Œ×ÒÑÔÐØ˜;�bŒg‰iŒiˆi¨CˆØ&Ð0•	�	­DˆÝ”Ð_Ð_Ð_Ð_Ð_Ð_Ð_Ð_ÕV[Ð\]ÑV^ÔV^Ð_Ñ_Ô_Ñ_Ô_ˆŒÝ�r˜A ™F‘{ B¨!¡G¨Q°°sÐ;Ñ;Ô;ˆŒÝ�r˜Q‘w  A q¨cÐ2Ñ2Ô2ˆŒØÐ(  b¢ˆŒˆˆrN   rK   rO   rP   c                óè   ‡— |gŠ‰                      ˆfd„| j        D ¦   «         ¦  «         |                      |                      t	          j        ‰d¦  «        ¦  «        ¦  «        Š| j        r‰|z   n‰S )r…   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS ©éÿÿÿÿNr—   ©r˜   rž   Úys     €rM   r›   z"HGBlock.forward.<locals>.<genexpr>·   ó/   øè è € Ð*Ð*˜a���1�R”5‘”Ð*Ð*Ð*Ð*Ð*Ð*rN   r   )Úextendrž   r    rŸ   rC   rŠ   r¡   ©rJ   rK   r§   s     @rM   rZ   zHGBlock.forward´   so   ø€ àˆCˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø�GŠG�D—G’G�EœI a¨™OœOÑ,Ô,Ñ-Ô-ˆØœÐ'ˆq�1‰uˆu aÐ'rN   )ri   rŽ   FFN)r7   r8   rt   r8   rg   r8   rk   r8   r�   r8   r�   r‘   r’   r‘   rw   r“   r[   r\   rb   s   @rM   r+   r+   �   sq   ø€ € € € € ðð ð ØØØØ $ð)ð )ð )ð )ð )ð )ð )ð>(ð (ð (ð (ð (ð (ð (ð (rN   r+   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   zDSpatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729.©é   é	   é   r7   r8   rg   rk   útuple[int, ...]c                ó  •— t          ¦   «                              ¦   «          |dz  }t          ||dd¦  «        | _        t          |t	          |¦  «        dz   z  |dd¦  «        | _        t          j        d„ |D ¦   «         ¦  «        | _        dS )zçInitialize the SPP layer with input/output channels and pooling kernel sizes.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            k (tuple): Kernel sizes for max pooling.
        rS   r   c                óB   — g | ]}t          j        |d |dz  ¬¦  «        ‘ŒS )r   rS   ©rx   ry   rz   )r   r‚   )r˜   rK   s     rM   ú
<listcomp>z SPP.__init__.<locals>.<listcomp>Ë   s/   € ÐaÐaÐaÐZ[¥¤¸À1ÈaÐSTÉfÐ UÑ UÔ UÐaÐaÐarN   N)	r>   r?   r   rl   Úlenro   r   rœ   rž   )rJ   r7   rg   rk   rf   rL   s        €rM   r?   zSPP.__init__¿   s‚   ø€ õ 	‰Œ×ÒÑÔÐØ�1‰WˆÝ˜˜B  1Ñ%Ô%ˆŒÝ˜�c !™fœf q™jÑ)¨2¨q°!Ñ4Ô4ˆŒÝ”ÐaÐaÐ_`ÐaÑaÔaÑbÔbˆŒˆˆrN   rK   rO   rP   c                ó¨   ‡— |                       ‰¦  «        Š|                      t          j        ‰gˆfd„| j        D ¦   «         z   d¦  «        ¦  «        S )zBForward pass of the SPP layer, performing spatial pyramid pooling.c                ó&   •— g | ]} |‰¦  «        ‘ŒS r—   r—   )r˜   rž   rK   s     €rM   r´   zSPP.forward.<locals>.<listcomp>Ð   s!   ø€ Ð(>Ð(>Ð(>°!¨¨¨1©¬Ð(>Ð(>Ð(>rN   r   )rl   ro   rC   rŠ   rž   rr   s    `rM   rZ   zSPP.forwardÍ   sN   ø€ à�HŠH�Q‰KŒKˆØ�xŠx�œ	 1 #Ð(>Ð(>Ð(>Ð(>°t´vÐ(>Ñ(>Ô(>Ñ">ÀÑBÔBÑCÔCÐCrN   )r¬   )r7   r8   rg   r8   rk   r°   r[   r\   rb   s   @rM   r   r   ¼   sk   ø€ € € € € ØNÐNðcð cð cð cð cð cð cðDð Dð Dð Dð Dð Dð Dð DrN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   zGSpatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher.r­   ri   Fr7   r8   rg   rk   r�   r’   r‘   c                ó$  •— t          ¦   «                              ¦   «          |dz  }t          ||ddd¬¦  «        | _        t          ||dz   z  |dd¦  «        | _        t          j        |d|dz  ¬¦  «        | _        || _        |o||k    | _	        dS )a’  Initialize the SPPF layer with given input/output channels and kernel size.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            k (int): Kernel size.
            n (int): Number of pooling iterations.
            shortcut (bool): Whether to use shortcut connection.

        Notes:
            This module is equivalent to SPP(k=(5, 9, 13)).
        rS   r   Frv   r³   N)
r>   r?   r   rl   ro   r   r‚   rž   r�   r¡   )rJ   r7   rg   rk   r�   r’   rf   rL   s          €rM   r?   zSPPF.__init__Ö   s’   ø€ õ 	‰Œ×ÒÑÔÐØ�1‰WˆÝ˜˜B  1¨%Ð0Ñ0Ô0ˆŒÝ˜˜a !™e™ b¨!¨QÑ/Ô/ˆŒÝ”¨!°A¸qÀA¹vÐFÑFÔFˆŒØˆŒØÐ(  b¢ˆŒˆˆrN   rK   rO   rP   c           
     ó.  ‡ ‡— ‰                       |¦  «        gŠ‰                     ˆ ˆfd„t          t          ‰ dd¦  «        ¦  «        D ¦   «         ¦  «         ‰                      t          j        ‰d¦  «        ¦  «        Št          ‰ dd¦  «        r‰|z   n‰S )zRApply sequential pooling operations to input and return concatenated feature maps.c              3  óN   •K  — | ]}‰                      ‰d          ¦  «        V — Œ dS r¤   ©rž   )r˜   rX   rJ   r§   s     €€rM   r›   zSPPF.forward.<locals>.<genexpr>î   s1   øè è € ÐEÐE 1�—’˜˜"œ‘”ÐEÐEÐEÐEÐEÐErN   r�   ri   r   r¡   F)rl   r©   r�   Úgetattrro   rC   rŠ   rª   s   ` @rM   rZ   zSPPF.forwardë   s�   øø€ à�XŠX�a‰[Œ[ˆMˆØ	�ŠÐEÐEÐEÐEÐE­­g°d¸CÀÑ.CÔ.CÑ(DÔ(DÐEÑEÔEÑEÔEÐEØ�HŠH•U”Y˜q !‘_”_Ñ%Ô%ˆÝ  e¨UÑ3Ô3Ð:ˆq�1‰uˆu¸Ð:rN   )r­   ri   F)
r7   r8   rg   r8   rk   r8   r�   r8   r’   r‘   r[   r\   rb   s   @rM   r   r   Ó   s\   ø€ € € € € ØQÐQð)ð )ð )ð )ð )ð )ð )ð*;ð ;ð ;ð ;ð ;ð ;ð ;ð ;rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   z"CSP Bottleneck with 1 convolution.r   r7   r8   rg   r�   c                óÐ   •‡— t          ¦   «                              ¦   «          t          |‰dd¦  «        | _        t	          j        ˆfd„t          |¦  «        D ¦   «         Ž | _        dS )zÃInitialize the CSP Bottleneck with 1 convolution.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of convolutions.
        r   c              3  ó:   •K  — | ]}t          ‰‰d ¦  «        V — ŒdS )ri   N©r   )r˜   rX   rg   s     €rM   r›   zC1.__init__.<locals>.<genexpr>   s-   øè è € Ð CÐ C°Q¥ b¨"¨a¡¤Ð CÐ CÐ CÐ CÐ CÐ CrN   N)r>   r?   r   rl   r   Ú
Sequentialr�   rž   )rJ   r7   rg   r�   rL   s     ` €rM   r?   zC1.__init__ö   s^   øø€ õ 	‰Œ×ÒÑÔÐÝ˜˜B  1Ñ%Ô%ˆŒÝ”Ð CÐ CÐ CÐ C½%À¹(¼(Ð CÑ CÔ CÐDˆŒˆˆrN   rK   rO   rP   c                ó\   — |                       |¦  «        }|                      |¦  «        |z   S )z:Apply convolution and residual connection to input tensor.)rl   rž   rª   s      rM   rZ   z
C1.forward  s%   € à�HŠH�Q‰KŒKˆØ�vŠv�a‰yŒy˜1‰}ÐrN   ©r   )r7   r8   rg   r8   r�   r8   r[   r\   rb   s   @rM   r   r   ó   sc   ø€ € € € € Ø,Ð,ð
Eð 
Eð 
Eð 
Eð 
Eð 
Eð 
Eðð ð ð ð ð ð ð rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   z#CSP Bottleneck with 2 convolutions.r   Tç      à?r7   r8   rg   r�   r’   r‘   ÚgÚerE   c                óR  •‡ ‡‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        ‰ _        t	          |d‰ j        z  dd¦  «        ‰ _        t	          d‰ j        z  |d¦  «        ‰ _        t          j        ˆˆ ˆfd„t          |¦  «        D ¦   «         Ž ‰ _
        dS )a_  Initialize a CSP Bottleneck with 2 convolutions.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        rS   r   c           	   3  óV   •K  — | ]#}t          ‰j        ‰j        ‰‰d d¬¦  «        V — Œ$dS ©)©ri   ri   rÌ   ç      ð?©rk   rÈ   N©r   Úc©r˜   rX   rÇ   rJ   r’   s     €€€rM   r›   zC2.__init__.<locals>.<genexpr>  s?   øè è € Ð vÐ vÐhi¥¨D¬F°D´F¸HÀaÐK[Ð_bÐ!cÑ!cÔ!cÐ vÐ vÐ vÐ vÐ vÐ vrN   N©r>   r?   r8   rÐ   r   rl   ro   r   rÂ   r�   rž   ©rJ   r7   rg   r�   r’   rÇ   rÈ   rL   s   `   `` €rM   r?   zC2.__init__  sš   øøøø€ õ 	‰Œ×ÒÑÔÐÝ�R˜!‘V‘”ˆŒÝ˜˜A ¤™J¨¨1Ñ-Ô-ˆŒÝ˜˜DœF™
 B¨Ñ*Ô*ˆŒå”Ð vÐ vÐ vÐ vÐ vÐ vÕmrÐstÑmuÔmuÐ vÑ vÔ vÐwˆŒˆˆrN   rK   rO   rP   c                óÔ   — |                       |¦  «                             dd¦  «        \  }}|                      t          j        |                      |¦  «        |fd¦  «        ¦  «        S )z<Forward pass through the CSP bottleneck with 2 convolutions.rS   r   )rl   Úchunkro   rC   rŠ   rž   ©rJ   rK   rY   rW   s       rM   rZ   z
C2.forward  sQ   € à�xŠx˜‰{Œ{× Ò   AÑ&Ô&‰ˆˆ1Ø�xŠx�œ	 4§6¢6¨!¡9¤9¨a .°!Ñ4Ô4Ñ5Ô5Ð5rN   ©r   Tr   rÆ   ©r7   r8   rg   r8   r�   r8   r’   r‘   rÇ   r8   rÈ   rE   r[   r\   rb   s   @rM   r   r     sc   ø€ € € € € Ø-Ð-ðxð xð xð xð xð xð xð$6ð 6ð 6ð 6ð 6ð 6ð 6ð 6rN   r   c                  ó6   ‡ — e Zd ZdZddˆ fd„Zdd„Zdd„Zˆ xZS )r    ú<Faster Implementation of CSP Bottleneck with 2 convolutions.r   FrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                ód  •‡ ‡‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        ‰ _        t	          |d‰ j        z  dd¦  «        ‰ _        t	          d|z   ‰ j        z  |d¦  «        ‰ _        t          j        ˆˆ ˆfd„t          |¦  «        D ¦   «         ¦  «        ‰ _
        dS )a_  Initialize a CSP bottleneck with 2 convolutions.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        rS   r   c           	   3  óV   •K  — | ]#}t          ‰j        ‰j        ‰‰d d¬¦  «        V — Œ$dS rË   rÏ   rÑ   s     €€€rM   r›   zC2f.__init__.<locals>.<genexpr>5  ó?   øè è € ÐtÐtÐfg�z¨$¬&°$´&¸(ÀAÐIYÐ]`ÐaÑaÔaÐtÐtÐtÐtÐtÐtrN   N)r>   r?   r8   rÐ   r   rl   ro   r   rœ   r�   rž   rÓ   s   `   `` €rM   r?   zC2f.__init__&  s¢   øøøø€ õ 	‰Œ×ÒÑÔÐÝ�R˜!‘V‘”ˆŒÝ˜˜A ¤™J¨¨1Ñ-Ô-ˆŒÝ˜˜Q™ $¤&Ñ(¨"¨aÑ0Ô0ˆŒÝ”ÐtÐtÐtÐtÐtÐtÕkpÐqrÑksÔksÐtÑtÔtÑtÔtˆŒˆˆrN   rK   rO   rP   c                ó  ‡— t          |                      |¦  «                             dd¦  «        ¦  «        Š‰                     ˆfd„| j        D ¦   «         ¦  «         |                      t          j        ‰d¦  «        ¦  «        S )zForward pass through C2f layer.rS   r   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   zC2f.forward.<locals>.<genexpr>:  r¨   rN   )Úlistrl   rÕ   r©   rž   ro   rC   rŠ   rª   s     @rM   rZ   zC2f.forward7  so   ø€ å�—’˜!‘”×"Ò" 1 aÑ(Ô(Ñ)Ô)ˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   c                ó*  ‡— |                       |¦  «                             | j        | j        fd¦  «        Š‰d         ‰d         gŠ‰                     ˆfd„| j        D ¦   «         ¦  «         |                      t          j        ‰d¦  «        ¦  «        S )ú.Forward pass using split() instead of chunk().r   r   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z$C2f.forward_split.<locals>.<genexpr>A  r¨   rN   )rl   ÚsplitrÐ   r©   rž   ro   rC   rŠ   rª   s     @rM   Úforward_splitzC2f.forward_split=  s�   ø€ à�HŠH�Q‰KŒK×Ò˜tœv t¤vÐ.°Ñ2Ô2ˆØˆqŒT�1�Q”4ˆLˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   ©r   Fr   rÆ   rØ   r[   ©r]   r^   r_   r`   r?   rZ   rå   ra   rb   s   @rM   r    r    #  sw   ø€ € € € € ØFÐFðuð uð uð uð uð uð uð")ð )ð )ð )ð)ð )ð )ð )ð )ð )ð )ð )rN   r    c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   z#CSP Bottleneck with 3 convolutions.r   TrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                ó\  •‡‡‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        Št          |‰dd¦  «        | _        t          |‰dd¦  «        | _        t          d‰z  |d¦  «        | _        t          j        ˆˆˆfd„t          |¦  «        D ¦   «         Ž | _
        dS )aa  Initialize the CSP Bottleneck with 3 convolutions.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        r   rS   c           	   3  óB   •K  — | ]}t          ‰‰‰‰d d¬¦  «        V — ŒdS )))r   r   rÌ   rÍ   rÎ   N©r   ©r˜   rX   rf   rÇ   r’   s     €€€rM   r›   zC3.__init__.<locals>.<genexpr>X  s;   øè è € Ð nÐ nÐ`a¥¨B°°H¸aÐCSÐWZÐ![Ñ![Ô![Ð nÐ nÐ nÐ nÐ nÐ nrN   N)r>   r?   r8   r   rl   ro   rp   r   rÂ   r�   rž   ©	rJ   r7   rg   r�   r’   rÇ   rÈ   rf   rL   s	       `` @€rM   r?   zC3.__init__H  s¥   øøøø€ õ 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜B™  AÑ&Ô&ˆŒÝ”Ð nÐ nÐ nÐ nÐ nÐ nÕejÐklÑemÔemÐ nÑ nÔ nÐoˆŒˆˆrN   rK   rO   rP   c           	     óÈ   — |                       t          j        |                      |                      |¦  «        ¦  «        |                      |¦  «        fd¦  «        ¦  «        S )z<Forward pass through the CSP bottleneck with 3 convolutions.r   )rp   rC   rŠ   rž   rl   ro   rr   s     rM   rZ   z
C3.forwardZ  sE   € à�xŠx�œ	 4§6¢6¨$¯(ª(°1©+¬+Ñ#6Ô#6¸¿ºÀ¹¼Ð"DÀaÑHÔHÑIÔIÐIrN   r×   rØ   r[   r\   rb   s   @rM   r   r   E  sk   ø€ € € € € Ø-Ð-ðpð pð pð pð pð pð pð$Jð Jð Jð Jð Jð Jð Jð JrN   r   c                  ó&   ‡ — e Zd ZdZddˆ fd„Zˆ xZS )r&   z"C3 module with cross-convolutions.r   TrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                óä   •‡ ‡‡— t          ¦   «                              |||‰‰|¦  «         t          ||z  ¦  «        ‰ _        t	          j        ˆˆ ˆfd„t          |¦  «        D ¦   «         Ž ‰ _        dS )a\  Initialize C3 module with cross-convolutions.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        c           	   3  óV   •K  — | ]#}t          ‰j        ‰j        ‰‰d d¬¦  «        V — Œ$dS )))r   ri   ©ri   r   r   rÎ   N)r   rf   rÑ   s     €€€rM   r›   zC3x.__init__.<locals>.<genexpr>o  s?   øè è € Ð vÐ vÐhi¥¨D¬G°T´W¸hÈÐM]ÐabÐ!cÑ!cÔ!cÐ vÐ vÐ vÐ vÐ vÐ vrN   N)r>   r?   r8   rf   r   rÂ   r�   rž   rÓ   s   `   `` €rM   r?   zC3x.__init__b  sr   øøøø€ õ 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý�b˜1‘f‘+”+ˆŒÝ”Ð vÐ vÐ vÐ vÐ vÐ vÕmrÐstÑmuÔmuÐ vÑ vÔ vÐwˆŒˆˆrN   r×   rØ   ©r]   r^   r_   r`   r?   ra   rb   s   @rM   r&   r&   _  sS   ø€ € € € € Ø,Ð,ðxð xð xð xð xð xð xð xð xð xð xrN   r&   c                  ó.   ‡ — e Zd ZdZddˆ fd
„Zdd„Zˆ xZS )r/   zRep C3.ri   rÍ   r7   r8   rg   r�   rÈ   rE   c                ó‚  •‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        Št          |‰dd¦  «        | _        t          |‰dd¦  «        | _        t          j        ˆfd„t          |¦  «        D ¦   «         Ž | _	        ‰|k    rt          ‰|dd¦  «        nt          j
        ¦   «         | _        dS )zèInitialize RepC3 module with RepConv blocks.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of RepConv blocks.
            e (float): Expansion ratio.
        r   c                ó0   •— g | ]}t          ‰‰¦  «        ‘ŒS r—   )r   ©r˜   rX   rf   s     €rM   r´   z"RepC3.__init__.<locals>.<listcomp>‚  s!   ø€ Ð CÐ CÐ C°Q¥¨¨R¡¤Ð CÐ CÐ CrN   N)r>   r?   r8   r   rl   ro   r   rÂ   r�   rž   ÚIdentityrp   )rJ   r7   rg   r�   rÈ   rf   rL   s        @€rM   r?   zRepC3.__init__u  sª   øø€ õ 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ”Ð CÐ CÐ CÐ C½%À¹(¼(Ð CÑ CÔ CÐDˆŒØ)+¨rª¨•4˜˜B  1Ñ%Ô%Ð%µr´{±}´}ˆŒˆˆrN   rK   rO   rP   c                ó¤   — |                       |                      |                      |¦  «        ¦  «        |                      |¦  «        z   ¦  «        S )zForward pass of RepC3 module.)rp   rž   rl   ro   rr   s     rM   rZ   zRepC3.forward…  s9   € à�xŠx˜Ÿš˜tŸxšx¨™{œ{Ñ+Ô+¨d¯hªh°q©k¬kÑ9Ñ:Ô:Ð:rN   )ri   rÍ   ©r7   r8   rg   r8   r�   r8   rÈ   rE   r[   r\   rb   s   @rM   r/   r/   r  sb   ø€ € € € € Ø€MðEð Eð Eð Eð Eð Eð Eð ;ð ;ð ;ð ;ð ;ð ;ð ;ð ;rN   r/   c                  ó&   ‡ — e Zd ZdZddˆ fd„Zˆ xZS )r   z"C3 module with TransformerBlock().r   TrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                ó¦   •— t          ¦   «                              ||||||¦  «         t          ||z  ¦  «        }t          ||d|¦  «        | _        dS )a[  Initialize C3 module with TransformerBlock.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Transformer blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        rR   N)r>   r?   r8   r   rž   rí   s	           €rM   r?   zC3TR.__init__�  sO   ø€ õ 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý��a‘‰[Œ[ˆÝ! " b¨!¨QÑ/Ô/ˆŒˆˆrN   r×   rØ   ró   rb   s   @rM   r   r   Š  sH   ø€ € € € € Ø,Ð,ð0ð 0ð 0ð 0ð 0ð 0ð 0ð 0ð 0ð 0ð 0rN   r   c                  ó&   ‡ — e Zd ZdZddˆ fd„Zˆ xZS )r$   z!C3 module with GhostBottleneck().r   TrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                óÒ   •‡— t          ¦   «                              ||||||¦  «         t          ||z  ¦  «        Št          j        ˆfd„t          |¦  «        D ¦   «         Ž | _        dS )a_  Initialize C3 module with GhostBottleneck.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Ghost bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        c              3  ó8   •K  — | ]}t          ‰‰¦  «        V — Œd S )N)r*   r÷   s     €rM   r›   z#C3Ghost.__init__.<locals>.<genexpr>­  s-   øè è € Ð KÐ K¸Q¥°°RÑ!8Ô!8Ð KÐ KÐ KÐ KÐ KÐ KrN   N©r>   r?   r8   r   rÂ   r�   rž   rí   s	          @€rM   r?   zC3Ghost.__init__   sd   øø€ õ 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý��a‘‰[Œ[ˆÝ”Ð KÐ KÐ KÐ KÅ%ÈÁ(Ä(Ð KÑ KÔ KÐLˆŒˆˆrN   r×   rØ   ró   rb   s   @rM   r$   r$   �  sS   ø€ € € € € Ø+Ð+ðMð Mð Mð Mð Mð Mð Mð Mð Mð Mð MrN   r$   c                  ó.   ‡ — e Zd ZdZddˆ fd	„Zdd„Zˆ xZS )r*   zGGhost Bottleneck https://github.com/huawei-noah/Efficient-AI-Backbones.ri   r   r7   r8   rg   rk   Úsc                óÐ  •— t          ¦   «                              ¦   «          |dz  }t          j        t	          ||dd¦  «        |dk    rt          ||||d¬¦  «        nt          j        ¦   «         t	          ||ddd¬¦  «        ¦  «        | _        |dk    r9t          j        t          ||||d¬¦  «        t          ||ddd¬¦  «        ¦  «        nt          j        ¦   «         | _	        dS )zÇInitialize Ghost Bottleneck module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            k (int): Kernel size.
            s (int): Stride.
        rS   r   Frv   N)
r>   r?   r   rÂ   r	   r   rø   rB   r   r’   )rJ   r7   rg   rk   r  rf   rL   s         €rM   r?   zGhostBottleneck.__init__³  sæ   ø€ õ 	‰Œ×ÒÑÔÐØ�1‰WˆÝ”MÝ�b˜"˜a Ñ#Ô#Ø/0°Aªv¨v�F�2�r˜1˜a UÐ+Ñ+Ô+Ð+½2¼;¹=¼=Ý�b˜"˜a ¨Ð.Ñ.Ô.ñ
ô 
ˆŒ	ð ^_ÐbcÒ]cÐ]c�BŒM�&  R¨¨A°5Ð9Ñ9Ô9½4ÀÀBÈÈ1ÐRWÐ;XÑ;XÔ;XÑYÔYÐYÕikÔitÑivÔivð 	ŒˆˆrN   rK   rO   rP   c                óX   — |                       |¦  «        |                      |¦  «        z   S )z3Apply skip connection and addition to input tensor.)rB   r’   rr   s     rM   rZ   zGhostBottleneck.forwardÇ  s#   € à�yŠy˜‰|Œ|˜dŸmšm¨AÑ.Ô.Ñ.Ð.rN   rò   ©r7   r8   rg   r8   rk   r8   r  r8   r[   r\   rb   s   @rM   r*   r*   °  s\   ø€ € € € € ØQÐQð
ð 
ð 
ð 
ð 
ð 
ð 
ð(/ð /ð /ð /ð /ð /ð /ð /rN   r*   c                  ó0   ‡ — e Zd ZdZ	 ddˆ fd„Zdd„Zˆ xZS )r   zStandard bottleneck.Tr   rÌ   rÆ   r7   r8   rg   r’   r‘   rÇ   rk   útuple[int, int]rÈ   rE   c                óþ   •— t          ¦   «                              ¦   «          t          ||z  ¦  «        }t          |||d         d¦  «        | _        t          |||d         d|¬¦  «        | _        |o||k    | _        dS )aZ  Initialize a standard bottleneck module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            shortcut (bool): Whether to use shortcut connection.
            g (int): Groups for convolutions.
            k (tuple): Kernel sizes for convolutions.
            e (float): Expansion ratio.
        r   r   ©rÇ   N)r>   r?   r8   r   rl   ro   r¡   ©	rJ   r7   rg   r’   rÇ   rk   rÈ   rf   rL   s	           €rM   r?   zBottleneck.__init__Ï  su   ø€ õ 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆÝ˜˜B  !¤ aÑ(Ô(ˆŒÝ˜˜B  !¤ a¨1Ð-Ñ-Ô-ˆŒØÐ(  b¢ˆŒˆˆrN   rK   rO   rP   c                ó¶   — | j         r+||                      |                      |¦  «        ¦  «        z   n'|                      |                      |¦  «        ¦  «        S )z3Apply bottleneck with optional shortcut connection.)r¡   ro   rl   rr   s     rM   rZ   zBottleneck.forwardâ  sE   € à,0¬HÐOˆq�4—8’8˜DŸHšH Q™KœKÑ(Ô(Ñ(Ð(¸$¿(º(À4Ç8Â8ÈAÁ;Ä;Ñ:OÔ:OÐOrN   ©Tr   rÌ   rÆ   ©r7   r8   rg   r8   r’   r‘   rÇ   r8   rk   r  rÈ   rE   r[   r\   rb   s   @rM   r   r   Ì  sk   ø€ € € € € ØÐð loð)ð )ð )ð )ð )ð )ð )ð&Pð Pð Pð Pð Pð Pð Pð PrN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   zGCSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks.r   TrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                ó  •‡‡‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        Št          |‰dd¦  «        | _        t          j        |‰ddd¬¦  «        | _        t          j        ‰‰ddd¬¦  «        | _        t          d‰z  |dd¦  «        | _	        t          j
        d‰z  ¦  «        | _        t          j        ¦   «         | _        t          j        ˆˆˆfd„t          |¦  «        D ¦   «         Ž | _        dS )aI  Initialize CSP Bottleneck.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        r   Fr:   rS   c              3  ó@   •K  — | ]}t          ‰‰‰‰d ¬¦  «        V — ŒdS ©rÍ   ©rÈ   Nrë   rì   s     €€€rM   r›   z)BottleneckCSP.__init__.<locals>.<genexpr>ý  s6   øè è € Ð ZÐ ZÈA¥¨B°°H¸aÀ3Ð!GÑ!GÔ!GÐ ZÐ ZÐ ZÐ ZÐ ZÐ ZrN   N)r>   r?   r8   r   rl   r   r@   ro   rp   Úcv4ÚBatchNorm2dÚbnÚSiLUrw   rÂ   r�   rž   rí   s	       `` @€rM   r?   zBottleneckCSP.__init__ê  sì   øøøø€ õ 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆÝ˜˜B  1Ñ%Ô%ˆŒÝ”9˜R  Q¨°Ð6Ñ6Ô6ˆŒÝ”9˜R  Q¨°Ð6Ñ6Ô6ˆŒÝ˜˜B™  A qÑ)Ô)ˆŒÝ”.  R¡Ñ(Ô(ˆŒÝ”7‘9”9ˆŒÝ”Ð ZÐ ZÐ ZÐ ZÐ ZÐ ZÕQVÐWXÑQYÔQYÐ ZÑ ZÔ ZÐ[ˆŒˆˆrN   rK   rO   rP   c           
     óB  — |                       |                      |                      |¦  «        ¦  «        ¦  «        }|                      |¦  «        }|                      |                      |                      t          j        ||fd¦  «        ¦  «        ¦  «        ¦  «        S )z)Apply CSP bottleneck with 4 convolutions.r   )	rp   rž   rl   ro   r  rw   r  rC   rŠ   )rJ   rK   Úy1Úy2s       rM   rZ   zBottleneckCSP.forwardÿ  sr   € à�XŠX�d—f’f˜TŸXšX a™[œ[Ñ)Ô)Ñ*Ô*ˆØ�XŠX�a‰[Œ[ˆØ�xŠx˜Ÿš §¢­¬°B¸°8¸QÑ)?Ô)?Ñ!@Ô!@ÑAÔAÑBÔBÐBrN   r×   rØ   r[   r\   rb   s   @rM   r   r   ç  sk   ø€ € € € € ØQÐQð\ð \ð \ð \ð \ð \ð \ð*Cð Cð Cð Cð Cð Cð Cð CrN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd	„Zdd„Zˆ xZS )ÚResNetBlockz.ResNet block with standard convolution layers.r   rR   r7   r8   rg   r  rÈ   c           	     ó|  •— t          ¦   «                              ¦   «          ||z  }t          ||ddd¬¦  «        | _        t          ||d|dd¬¦  «        | _        t          ||dd¬¦  «        | _        |dk    s||k    r&t          j        t          ||d|d¬¦  «        ¦  «        nt          j        ¦   «         | _	        dS )	zÀInitialize ResNet block.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            s (int): Stride.
            e (int): Expansion ratio.
        r   T©rk   r  rw   ri   ©rk   r  Úprw   Fr–   N)
r>   r?   r   rl   ro   rp   r   rÂ   rø   r’   )rJ   r7   rg   r  rÈ   Úc3rL   s         €rM   r?   zResNetBlock.__init__	  s¾   ø€ õ 	‰Œ×ÒÑÔÐØ�‰VˆÝ˜˜B ! q¨dÐ3Ñ3Ô3ˆŒÝ˜˜B ! q¨A°4Ð8Ñ8Ô8ˆŒÝ˜˜B !¨Ð/Ñ/Ô/ˆŒØLMÐQRÊFÈFÐVXÐ\^ÒV^ÐV^�œ¥d¨2¨r°Q¸!ÀÐ&GÑ&GÔ&GÑHÔHÐHÕdfÔdoÑdqÔdqˆŒˆˆrN   rK   rO   rP   c           	     óÈ   — t          j        |                      |                      |                      |¦  «        ¦  «        ¦  «        |                      |¦  «        z   ¦  «        S )z&Forward pass through the ResNet block.)rˆ   Úrelurp   ro   rl   r’   rr   s     rM   rZ   zResNetBlock.forward  sE   € åŒv�d—h’h˜tŸxšx¨¯ª°©¬Ñ4Ô4Ñ5Ô5¸¿ºÀaÑ8HÔ8HÑHÑIÔIÐIrN   )r   rR   )r7   r8   rg   r8   r  r8   rÈ   r8   r[   r\   rb   s   @rM   r  r    sk   ø€ € € € € Ø8Ð8ðrð rð rð rð rð rð rð Jð Jð Jð Jð Jð Jð Jð JrN   r  c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r2   z)ResNet layer with multiple ResNet blocks.r   FrR   r7   r8   rg   r  Úis_firstr‘   r�   rÈ   c           
     ó¢  •‡‡— t          ¦   «                              ¦   «          || _        | j        rDt          j        t          |‰dddd¬¦  «        t          j        ddd¬¦  «        ¦  «        | _        d
S t          |‰|‰¬¦  «        g}| 	                    ˆˆfd	„t          |dz
  ¦  «        D ¦   «         ¦  «         t          j        |Ž | _        d
S )a,  Initialize ResNet layer.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            s (int): Stride.
            is_first (bool): Whether this is the first layer.
            n (int): Number of ResNet blocks.
            e (int): Expansion ratio.
        é   rS   ri   Tr  r   r³   r  c                ó<   •— g | ]}t          ‰‰z  ‰d ‰¬¦  «        ‘ŒS )r   r  )r  )r˜   rX   rg   rÈ   s     €€rM   r´   z(ResNetLayer.__init__.<locals>.<listcomp>5  s.   ø€ ÐQÐQÐQ¸q�; q¨2¡v¨r°1¸Ð:Ñ:Ô:ÐQÐQÐQrN   N)r>   r?   r$  r   rÂ   r   r‚   Úlayerr  r©   r�   )	rJ   r7   rg   r  r$  r�   rÈ   ÚblocksrL   s	     `   ` €rM   r?   zResNetLayer.__init__!  sÔ   øøø€ õ 	‰Œ×ÒÑÔÐØ ˆŒàŒ=ð 	0ÝœÝ�R˜˜q A¨°Ð5Ñ5Ô5µr´|ÐPQÐZ[ÐefÐ7gÑ7gÔ7gñô ˆDŒJˆJˆJõ " " b¨!¨qÐ1Ñ1Ô1Ð2ˆFØ�MŠMÐQÐQÐQÐQÐQÅEÈ!ÈaÉ%ÁLÄLÐQÑQÔQÑRÔRÐRÝœ¨Ð/ˆDŒJˆJˆJrN   rK   rO   rP   c                ó,   — |                       |¦  «        S )z&Forward pass through the ResNet layer.)r(  rr   s     rM   rZ   zResNetLayer.forward8  s   € à�zŠz˜!‰}Œ}ÐrN   )r   Fr   rR   )r7   r8   rg   r8   r  r8   r$  r‘   r�   r8   rÈ   r8   r[   r\   rb   s   @rM   r2   r2     s\   ø€ € € € € Ø3Ð3ð0ð 0ð 0ð 0ð 0ð 0ð 0ð.ð ð ð ð ð ð ð rN   r2   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )ÚMaxSigmoidAttnBlockzMax Sigmoid attention block.r   é€   é   Fr7   r8   rg   Únhr    ÚgcÚscaler‘   c                óÊ  •— t          ¦   «                              ¦   «          || _        ||z  | _        ||k    rt	          ||dd¬¦  «        nd| _        t          j        ||¦  «        | _        t          j	        t          j        |¦  «        ¦  «        | _        t	          ||ddd¬¦  «        | _        |r)t          j	        t          j        d|dd¦  «        ¦  «        nd| _        dS )a?  Initialize MaxSigmoidAttnBlock.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            nh (int): Number of heads.
            ec (int): Embedding channels.
            gc (int): Guide channels.
            scale (bool): Whether to use learnable scale parameter.
        r   Fr–   Nri   r  rÍ   )r>   r?   r/  Úhcr   r    r   ÚLinearÚglrF   rC   Úzerosr;   Ú	proj_convÚonesr1  )rJ   r7   rg   r/  r    r0  r1  rL   s          €rM   r?   zMaxSigmoidAttnBlock.__init__@  sÇ   ø€ õ 	‰Œ×ÒÑÔÐØˆŒØ˜‘(ˆŒØ24¸²(°(•$�r˜2 ¨Ð.Ñ.Ô.Ð.ÀˆŒÝ”)˜B Ñ#Ô#ˆŒÝ”L¥¤¨R¡¤Ñ1Ô1ˆŒ	Ý˜b "¨¨Q°EÐ:Ñ:Ô:ˆŒØ>CÐL•R”\¥%¤*¨Q°°A°qÑ"9Ô"9Ñ:Ô:Ð:ÈˆŒ
ˆ
ˆ
rN   rK   rO   ÚguiderP   c                óÈ  — |j         \  }}}}|                      |¦  «        }|                     ||j         d         | j        | j        ¦  «        }| j        �|                      |¦  «        n|}|                     || j        | j        ||¦  «        }t          j        d||¦  «        }|                     d¬¦  «        d         }|| j        dz  z  }|| j	        ddd…ddf         z   }| 
                    ¦   «         | j        z  }|                      |¦  «        }|                     || j        d||¦  «        }||                     d¦  «        z  }|                     |d||¦  «        S )	zåForward pass of MaxSigmoidAttnBlock.

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

        Returns:
            (torch.Tensor): Output tensor after attention.
        r   Nzbmchw,bnmc->bmhwnr¥   r†   r   rÆ   rS   )rT   r5  rG   r/  r3  r    rC   ÚeinsumÚmaxr;   Úsigmoidr1  r7  Ú	unsqueeze)	rJ   rK   r9  ÚbsrX   ÚhÚwÚembedÚaws	            rM   rZ   zMaxSigmoidAttnBlock.forwardT  s>  € ð ”g‰ˆˆAˆq�!à—’˜‘”ˆØ—
’
˜2˜uœ{¨1œ~¨t¬w¸¼Ñ@Ô@ˆØ"œgÐ1�—’˜‘
”
�
°qˆØ—
’
˜2˜tœw¨¬°°AÑ6Ô6ˆåŒ\Ð-¨u°eÑ<Ô<ˆØ�VŠV˜ˆV‰^Œ^˜AÔˆØ�4”7˜C‘<Ñ ˆØ�$”)˜D ! ! ! T¨4Ð/Ô0Ñ0ˆØ�ZŠZ‰\Œ\˜DœJÑ&ˆà�NŠN˜1ÑÔˆØ�FŠF�2�t”w  A qÑ)Ô)ˆØ�—’˜Q‘”ÑˆØ�vŠv�b˜"˜a Ñ#Ô#Ð#rN   )r   r-  r.  F)r7   r8   rg   r8   r/  r8   r    r8   r0  r8   r1  r‘   ©rK   rO   r9  rO   rP   rO   r\   rb   s   @rM   r,  r,  =  sc   ø€ € € € € Ø&Ð&ðMð Mð Mð Mð Mð Mð Mð($ð $ð $ð $ð $ð $ð $ð $rN   r,  c                  óD   ‡ — e Zd ZdZ	 	 	 	 	 	 	 ddˆ fd„Zdd„Zdd„Zˆ xZS )r!   z*C2f module with an additional attn module.r   r-  r.  FrÆ   r7   r8   rg   r�   r    r/  r0  r’   r‘   rÇ   rÈ   rE   c
                óª  •‡ ‡‡— t          ¦   «                              ¦   «          t          ||	z  ¦  «        ‰ _        t	          |d‰ j        z  dd¦  «        ‰ _        t	          d|z   ‰ j        z  |d¦  «        ‰ _        t          j        ˆˆ ˆfd„t          |¦  «        D ¦   «         ¦  «        ‰ _
        t          ‰ j        ‰ j        |||¬¦  «        ‰ _        dS )aÿ  Initialize C2f module with attention mechanism.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            ec (int): Embedding channels for attention.
            nh (int): Number of heads for attention.
            gc (int): Guide channels for attention.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        rS   r   ri   c           	   3  óV   •K  — | ]#}t          ‰j        ‰j        ‰‰d d¬¦  «        V — Œ$dS rË   rÏ   rÑ   s     €€€rM   r›   z#C2fAttn.__init__.<locals>.<genexpr>‘  rÝ   rN   )r0  r    r/  N)r>   r?   r8   rÐ   r   rl   ro   r   rœ   r�   rž   r,  Úattn)rJ   r7   rg   r�   r    r/  r0  r’   rÇ   rÈ   rL   s   `      `` €rM   r?   zC2fAttn.__init__t  sÀ   øøøø€ õ2 	‰Œ×ÒÑÔÐÝ�R˜!‘V‘”ˆŒÝ˜˜A ¤™J¨¨1Ñ-Ô-ˆŒÝ˜˜Q™ $¤&Ñ(¨"¨aÑ0Ô0ˆŒÝ”ÐtÐtÐtÐtÐtÐtÕkpÐqrÑksÔksÐtÑtÔtÑtÔtˆŒÝ'¨¬°´¸2À"ÈÐLÑLÔLˆŒ	ˆ	ˆ	rN   rK   rO   r9  rP   c                ój  ‡— t          |                      |¦  «                             dd¦  «        ¦  «        Š‰                     ˆfd„| j        D ¦   «         ¦  «         ‰                     |                      ‰d         |¦  «        ¦  «         |                      t          j	        ‰d¦  «        ¦  «        S )zþForward pass through C2f layer with attention.

        Args:
            x (torch.Tensor): Input tensor.
            guide (torch.Tensor): Guide tensor for attention.

        Returns:
            (torch.Tensor): Output tensor after processing.
        rS   r   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z"C2fAttn.forward.<locals>.<genexpr>Ÿ  r¨   rN   r¥   )
rà   rl   rÕ   r©   rž   ÚappendrH  ro   rC   rŠ   ©rJ   rK   r9  r§   s      @rM   rZ   zC2fAttn.forward”  s•   ø€ õ �—’˜!‘”×"Ò" 1 aÑ(Ô(Ñ)Ô)ˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø	�Š�—’˜1˜Rœ5 %Ñ(Ô(Ñ)Ô)Ð)Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   c                ó‚  ‡— t          |                      |¦  «                             | j        | j        fd¦  «        ¦  «        Š‰                     ˆfd„| j        D ¦   «         ¦  «         ‰                     |                      ‰d         |¦  «        ¦  «         |                      t          j
        ‰d¦  «        ¦  «        S )zþForward pass using split() instead of chunk().

        Args:
            x (torch.Tensor): Input tensor.
            guide (torch.Tensor): Guide tensor for attention.

        Returns:
            (torch.Tensor): Output tensor after processing.
        r   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z(C2fAttn.forward_split.<locals>.<genexpr>®  r¨   rN   r¥   )rà   rl   rä   rÐ   r©   rž   rK  rH  ro   rC   rŠ   rL  s      @rM   rå   zC2fAttn.forward_split£  sž   ø€ õ �—’˜!‘”×"Ò" D¤F¨D¬FÐ#3°QÑ7Ô7Ñ8Ô8ˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø	�Š�—’˜1˜Rœ5 %Ñ(Ô(Ñ)Ô)Ð)Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   )r   r-  r   r.  Fr   rÆ   )r7   r8   rg   r8   r�   r8   r    r8   r/  r8   r0  r8   r’   r‘   rÇ   r8   rÈ   rE   rD  rç   rb   s   @rM   r!   r!   q  s�   ø€ € € € € Ø4Ð4ð ØØØØØØðMð Mð Mð Mð Mð Mð Mð@)ð )ð )ð )ð)ð )ð )ð )ð )ð )ð )ð )rN   r!   c                  ó0   ‡ — e Zd ZdZ	 ddˆ fd„Zdd„Zˆ xZS )r-   zKImagePoolingAttn: Enhance the text embeddings with image-aware information.rd   r—   r.  é   ri   Fr    r8   Úchr°   Úctr/  rk   r1  r‘   c                óv  •‡‡— t          ¦   «                              ¦   «          t          |¦  «        }t          j        t          j        |¦  «        t          j        |‰¦  «        ¦  «        | _        t          j        t          j        ‰¦  «        t          j        ‰‰¦  «        ¦  «        | _        t          j        t          j        ‰¦  «        t          j        ‰‰¦  «        ¦  «        | _	        t          j        ‰|¦  «        | _
        |r)t          j        t          j        dg¦  «        d¬¦  «        nd| _        t          j        ˆfd„|D ¦   «         ¦  «        | _        t          j        ˆfd„t#          |¦  «        D ¦   «         ¦  «        | _        ‰| _        || _        || _        ‰|z  | _        ‰| _        dS )a‚  Initialize ImagePoolingAttn module.

        Args:
            ec (int): Embedding channels.
            ch (tuple): Channel dimensions for feature maps.
            ct (int): Channel dimension for text embeddings.
            nh (int): Number of attention heads.
            k (int): Kernel size for pooling.
            scale (bool): Whether to use learnable scale parameter.
        g        T©Úrequires_gradrÍ   c                ó>   •— g | ]}t          j        |‰d ¬¦  «        ‘ŒS )r   )rx   )r   r@   )r˜   Úin_channelsr    s     €rM   r´   z-ImagePoolingAttn.__init__.<locals>.<listcomp>Ë  s,   ø€ Ð)jÐ)jÐ)jÐXc­"¬)°KÀÐQRÐ*SÑ*SÔ*SÐ)jÐ)jÐ)jrN   c                ó<   •— g | ]}t          j        ‰‰f¦  «        ‘ŒS r—   )r   ÚAdaptiveMaxPool2d)r˜   rX   rk   s     €rM   r´   z-ImagePoolingAttn.__init__.<locals>.<listcomp>Ì  s(   ø€ Ð&WÐ&WÐ&WÈ¥rÔ';¸QÀ¸FÑ'CÔ'CÐ&WÐ&WÐ&WrN   N)r>   r?   rµ   r   rÂ   Ú	LayerNormr4  ÚqueryÚkeyÚvalueÚprojrF   rC   Útensorr1  rœ   Úprojectionsr�   Úim_poolsr    r/  Únfr3  rk   )	rJ   r    rQ  rR  r/  rk   r1  rb  rL   s	    `   `  €rM   r?   zImagePoolingAttn.__init__¶  sc  øøø€ õ 	‰Œ×ÒÑÔÐå�‰WŒWˆÝ”]¥2¤<°Ñ#3Ô#3µR´Y¸rÀ2Ñ5FÔ5FÑGÔGˆŒ
Ý”=¥¤¨bÑ!1Ô!1µ2´9¸RÀÑ3DÔ3DÑEÔEˆŒÝ”]¥2¤<°Ñ#3Ô#3µR´Y¸rÀ2Ñ5FÔ5FÑGÔGˆŒ
Ý”I˜b "Ñ%Ô%ˆŒ	ØNSÐ\•R”\¥%¤,°¨uÑ"5Ô"5ÀTÐJÑJÔJÐJÐY\ˆŒ
Ýœ=Ð)jÐ)jÐ)jÐ)jÐgiÐ)jÑ)jÔ)jÑkÔkˆÔÝœÐ&WÐ&WÐ&WÐ&WÍUÐSUÉYÌYÐ&WÑ&WÔ&WÑXÔXˆŒØˆŒØˆŒØˆŒØ˜‘(ˆŒØˆŒˆˆrN   rK   úlist[torch.Tensor]ÚtextrO   rP   c                óx  ‡‡— |d         j         d         Št          |¦  «        | j        k    sJ ‚| j        dz  Šˆˆfd„t	          || j        | j        ¦  «        D ¦   «         }t          j        |d¬¦  «         	                    dd¦  «        }|  
                    |¦  «        }|                      |¦  «        }|                      |¦  «        }|                     ‰d| j        | j        ¦  «        }|                     ‰d| j        | j        ¦  «        }|                     ‰d| j        | j        ¦  «        }t          j        d||¦  «        }|| j        dz  z  }t#          j        |d¬¦  «        }t          j        d	||¦  «        }|                      |                     ‰d| j        ¦  «        ¦  «        }|| j        z  |z   S )
zóForward pass of ImagePoolingAttn.

        Args:
            x (list[torch.Tensor]): List of input feature maps.
            text (torch.Tensor): Text embeddings.

        Returns:
            (torch.Tensor): Enhanced text embeddings.
        r   rS   c                ój   •— g | ]/\  }}} | ||¦  «        ¦  «                              ‰d ‰¦  «        ‘Œ0S )r¥   )rG   )r˜   rK   r^  rƒ   r?  Únum_patchess       €€rM   r´   z,ImagePoolingAttn.forward.<locals>.<listcomp>à  sA   ø€ ÐtÐtÐt¹¸!¸TÀ4ˆTˆT�$�$�q‘'”'‰]Œ]×Ò  B¨Ñ4Ô4ÐtÐtÐtrN   r¥   r†   r   zbnmc,bkmc->bmnkrÆ   zbmnk,bkmc->bnmc)rT   rµ   rb  rk   Úzipr`  ra  rC   rŠ   rU   r[  r\  r]  Úreshaper/  r3  r;  rˆ   rV   r^  r    r1  )	rJ   rK   rd  Úqrk   ÚvrC  r?  rg  s	          @@rM   rZ   zImagePoolingAttn.forwardÓ  s�  øø€ ð ˆqŒTŒZ˜Œ]ˆÝ�1‰vŒv˜œÒ Ð Ð Ð Ø”f˜a‘iˆØtÐtÐtÐtÐtÍCÐPQÐSWÔScÐeiÔerÑLsÔLsÐtÑtÔtˆÝŒI�a˜RÐ Ñ Ô ×*Ò*¨1¨aÑ0Ô0ˆØ�JŠJ�tÑÔˆØ�HŠH�Q‰KŒKˆØ�JŠJ�q‰MŒMˆð �IŠI�b˜"˜dœg t¤wÑ/Ô/ˆØ�IŠI�b˜"˜dœg t¤wÑ/Ô/ˆØ�IŠI�b˜"˜dœg t¤wÑ/Ô/ˆåŒ\Ð+¨Q°Ñ2Ô2ˆØ�4”7˜C‘<Ñ ˆÝŒY�r˜rÐ"Ñ"Ô"ˆåŒLÐ*¨B°Ñ2Ô2ˆØ�IŠI�a—i’i  B¨¬Ñ0Ô0Ñ1Ô1ˆØ�4”:‰~ Ñ$Ð$rN   )rd   r—   r.  rP  ri   F)r    r8   rQ  r°   rR  r8   r/  r8   rk   r8   r1  r‘   )rK   rc  rd  rO   rP   rO   r\   rb   s   @rM   r-   r-   ³  sc   ø€ € € € € ØUÐUð nsðð ð ð ð ð ð ð:%ð %ð %ð %ð %ð %ð %ð %rN   r-   c                  ó*   ‡ — e Zd ZdZˆ fd„Zdd„Zˆ xZS )	r)   zZImplements contrastive learning head for region-text similarity in vision-language models.c                óD  •— t          ¦   «                              ¦   «          t          j        t	          j        dg¦  «        ¦  «        | _        t          j        t	          j        g ¦  «        t	          j        d¦  «                             ¦   «         z  ¦  «        | _	        dS )zBInitialize ContrastiveHead with region-text similarity parameters.ç      $Àg$I’$I’,@N)
r>   r?   r   rF   rC   r_  r;   r8  ÚlogÚlogit_scale©rJ   rL   s    €rM   r?   zContrastiveHead.__init__÷  sq   ø€ å‰Œ×ÒÑÔÐå”L¥¤¨u¨gÑ!6Ô!6Ñ7Ô7ˆŒ	Ýœ<­¬
°2©¬½¼ÀhÑ9OÔ9O×9SÒ9SÑ9UÔ9UÑ(UÑVÔVˆÔÐÐrN   rK   rO   rA  rP   c                óÒ   — t          j        |dd¬¦  «        }t          j        |dd¬¦  «        }t          j        d||¦  «        }|| j                             ¦   «         z  | j        z   S )zÝForward function of contrastive learning.

        Args:
            x (torch.Tensor): Image features.
            w (torch.Tensor): Text features.

        Returns:
            (torch.Tensor): Similarity scores.
        r   rS   ©r‡   r  r¥   úbchw,bkc->bkhw)rˆ   Ú	normalizerC   r;  rp  Úexpr;   ©rJ   rK   rA  s      rM   rZ   zContrastiveHead.forwardþ  se   € õ ŒK˜˜q AÐ&Ñ&Ô&ˆÝŒK˜˜r QÐ'Ñ'Ô'ˆÝŒLÐ)¨1¨aÑ0Ô0ˆØ�4Ô#×'Ò'Ñ)Ô)Ñ)¨D¬IÑ5Ð5rN   ©rK   rO   rA  rO   rP   rO   r\   rb   s   @rM   r)   r)   ô  sW   ø€ € € € € ØdÐdðWð Wð Wð Wð Wð6ð 6ð 6ð 6ð 6ð 6ð 6ð 6rN   r)   c                  óJ   ‡ — e Zd ZdZdˆ fd„Zd„ Zedd
„¦   «         Zdd„Zˆ xZ	S )r   z Batch Norm Contrastive Head using batch norm instead of l2-normalization.

    Args:
        embed_dims (int): Embed dimensions of text and image features.
    Ú
embed_dimsr8   c                ó.  •— t          ¦   «                              ¦   «          t          j        |¦  «        | _        t          j        t          j        dg¦  «        ¦  «        | _        t          j        dt          j	        g ¦  «        z  ¦  «        | _
        dS )zvInitialize BNContrastiveHead.

        Args:
            embed_dims (int): Embedding dimensions for features.
        rn  g      ð¿N)r>   r?   r   r  ÚnormrF   rC   r_  r;   r8  rp  )rJ   rz  rL   s     €rM   r?   zBNContrastiveHead.__init__  sn   ø€ õ 	‰Œ×ÒÑÔÐÝ”N :Ñ.Ô.ˆŒ	å”L¥¤¨u¨gÑ!6Ô!6Ñ7Ô7ˆŒ	åœ<¨­u¬z¸"©~¬~Ñ(=Ñ>Ô>ˆÔÐÐrN   c                ó*   — | ` | `| `| j        | _        dS )zCFuse the batch normalization layer in the BNContrastiveHead module.N)r|  r;   rp  Úforward_fuserZ   ©rJ   s    rM   ÚfusezBNContrastiveHead.fuse"  s    € àˆIØˆIØÐØÔ(ˆŒˆˆrN   rK   rO   rA  rP   c                ó   — | S )z5Passes image features through unchanged after fusing.r—   )rK   rA  s     rM   r~  zBNContrastiveHead.forward_fuse)  s	   € ð ˆrN   c                óÎ   — |                       |¦  «        }t          j        |dd¬¦  «        }t          j        d||¦  «        }|| j                             ¦   «         z  | j        z   S )zöForward function of contrastive learning with batch normalization.

        Args:
            x (torch.Tensor): Image features.
            w (torch.Tensor): Text features.

        Returns:
            (torch.Tensor): Similarity scores.
        r¥   rS   rs  rt  )r|  rˆ   ru  rC   r;  rp  rv  r;   rw  s      rM   rZ   zBNContrastiveHead.forward.  s^   € ð �IŠI�a‰LŒLˆÝŒK˜˜r QÐ'Ñ'Ô'ˆåŒLÐ)¨1¨aÑ0Ô0ˆØ�4Ô#×'Ò'Ñ)Ô)Ñ)¨D¬IÑ5Ð5rN   )rz  r8   rx  )
r]   r^   r_   r`   r?   r€  Ústaticmethodr~  rZ   ra   rb   s   @rM   r   r     sŠ   ø€ € € € € ðð ð?ð ?ð ?ð ?ð ?ð ?ð)ð )ð )ð ðð ð ñ „\ðð6ð 6ð 6ð 6ð 6ð 6ð 6ð 6rN   r   c                  ó(   ‡ — e Zd ZdZ	 ddˆ fd„Zˆ xZS )ÚRepBottleneckzRep bottleneck.Tr   rÌ   rÆ   r7   r8   rg   r’   r‘   rÇ   rk   r  rÈ   rE   c                ó²   •— t          ¦   «                              ||||||¦  «         t          ||z  ¦  «        }t          |||d         d¦  «        | _        dS )aK  Initialize RepBottleneck.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            shortcut (bool): Whether to use shortcut connection.
            g (int): Groups for convolutions.
            k (tuple): Kernel sizes for convolutions.
            e (float): Expansion ratio.
        r   r   N)r>   r?   r8   r   rl   r
  s	           €rM   r?   zRepBottleneck.__init__B  sS   ø€ õ 	‰Œ×Ò˜˜R ¨1¨a°Ñ3Ô3Ð3Ý��a‘‰[Œ[ˆÝ˜2˜r 1 Q¤4¨Ñ+Ô+ˆŒˆˆrN   r  r  ró   rb   s   @rM   r…  r…  ?  sO   ø€ € € € € ØÐð loð,ð ,ð ,ð ,ð ,ð ,ð ,ð ,ð ,ð ,ð ,rN   r…  c                  ó&   ‡ — e Zd ZdZddˆ fd„Zˆ xZS )ÚRepCSPzXRepeatable Cross Stage Partial Network (RepCSP) module for efficient feature extraction.r   TrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                óÚ   •‡‡‡— t          ¦   «                              |||‰‰|¦  «         t          ||z  ¦  «        Št          j        ˆˆˆfd„t          |¦  «        D ¦   «         Ž | _        dS )aJ  Initialize RepCSP layer.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of RepBottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        c              3  ó@   •K  — | ]}t          ‰‰‰‰d ¬¦  «        V — ŒdS r  )r…  rì   s     €€€rM   r›   z"RepCSP.__init__.<locals>.<genexpr>d  s6   øè è € Ð ]Ð ]Èq¥¨r°2°xÀÀcÐ!JÑ!JÔ!JÐ ]Ð ]Ð ]Ð ]Ð ]Ð ]rN   Nr   rí   s	       `` @€rM   r?   zRepCSP.__init__W  sp   øøøø€ õ 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý��a‘‰[Œ[ˆÝ”Ð ]Ð ]Ð ]Ð ]Ð ]Ð ]ÕTYÐZ[ÑT\ÔT\Ð ]Ñ ]Ô ]Ð^ˆŒˆˆrN   r×   rØ   ró   rb   s   @rM   rˆ  rˆ  T  sS   ø€ € € € € ØbÐbð_ð _ð _ð _ð _ð _ð _ð _ð _ð _ð _rN   rˆ  c                  ó6   ‡ — e Zd ZdZddˆ fd	„Zdd„Zdd„Zˆ xZS )r0   z	CSP-ELAN.r   r7   r8   rg   r   Úc4r�   c           	     ó®  •— t          ¦   «                              ¦   «          |dz  | _        t          ||dd¦  «        | _        t          j        t          |dz  ||¦  «        t          ||dd¦  «        ¦  «        | _        t          j        t          |||¦  «        t          ||dd¦  «        ¦  «        | _	        t          |d|z  z   |dd¦  «        | _
        dS )a  Initialize CSP-ELAN layer.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            c3 (int): Intermediate channels.
            c4 (int): Intermediate channels for RepCSP.
            n (int): Number of RepCSP blocks.
        rS   r   ri   N)r>   r?   rÐ   r   rl   r   rÂ   rˆ  ro   rp   r  )rJ   r7   rg   r   rŒ  r�   rL   s         €rM   r?   zRepNCSPELAN4.__init__j  sº   ø€ õ 	‰Œ×ÒÑÔÐØ�q‘ˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ”=¥¨¨a©°°QÑ!7Ô!7½¸bÀ"ÀaÈÑ9KÔ9KÑLÔLˆŒÝ”=¥¨¨B°Ñ!2Ô!2µD¸¸RÀÀAÑ4FÔ4FÑGÔGˆŒÝ˜˜a "™f™ r¨1¨aÑ0Ô0ˆŒˆˆrN   rK   rO   rP   c                ó  ‡— t          |                      |¦  «                             dd¦  «        ¦  «        Š‰                     ˆfd„| j        | j        fD ¦   «         ¦  «         |                      t          j        ‰d¦  «        ¦  «        S )z(Forward pass through RepNCSPELAN4 layer.rS   r   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z'RepNCSPELAN4.forward.<locals>.<genexpr>~  s/   øè è € Ð:Ð: �!�!�A�b”E‘(”(Ð:Ð:Ð:Ð:Ð:Ð:rN   )	rà   rl   rÕ   r©   ro   rp   r  rC   rŠ   rª   s     @rM   rZ   zRepNCSPELAN4.forward{  sv   ø€ å�—’˜!‘”×"Ò" 1 aÑ(Ô(Ñ)Ô)ˆØ	�ŠÐ:Ð:Ð:Ð: d¤h°´Ð%9Ð:Ñ:Ô:Ñ:Ô:Ð:Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   c                ó2  ‡— t          |                      |¦  «                             | j        | j        fd¦  «        ¦  «        Š‰                     ˆfd„| j        | j        fD ¦   «         ¦  «         |                      t          j	        ‰d¦  «        ¦  «        S )râ   r   c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z-RepNCSPELAN4.forward_split.<locals>.<genexpr>„  s/   øè è € Ð8Ð8˜a���1�R”5‘”Ð8Ð8Ð8Ð8Ð8Ð8rN   )
rà   rl   rä   rÐ   r©   ro   rp   r  rC   rŠ   rª   s     @rM   rå   zRepNCSPELAN4.forward_split�  s   ø€ å�—’˜!‘”×"Ò" D¤F¨D¬FÐ#3°QÑ7Ô7Ñ8Ô8ˆØ	�ŠÐ8Ð8Ð8Ð8 D¤H¨d¬hÐ#7Ð8Ñ8Ô8Ñ8Ô8Ð8Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   rÄ   )
r7   r8   rg   r8   r   r8   rŒ  r8   r�   r8   r[   rç   rb   s   @rM   r0   r0   g  so   ø€ € € € € Ø€Oð1ð 1ð 1ð 1ð 1ð 1ð 1ð")ð )ð )ð )ð)ð )ð )ð )ð )ð )ð )ð )rN   r0   c                  ó$   ‡ — e Zd ZdZdˆ fd„Zˆ xZS )	r   z!ELAN1 module with 4 convolutions.r7   r8   rg   r   rŒ  c                ó.  •— t          ¦   «                              ||||¦  «         |dz  | _        t          ||dd¦  «        | _        t          |dz  |dd¦  «        | _        t          ||dd¦  «        | _        t          |d|z  z   |dd¦  «        | _        dS )zçInitialize ELAN1 layer.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            c3 (int): Intermediate channels.
            c4 (int): Intermediate channels for convolutions.
        rS   r   ri   N)r>   r?   rÐ   r   rl   ro   rp   r  )rJ   r7   rg   r   rŒ  rL   s        €rM   r?   zELAN1.__init__‹  s’   ø€ õ 	‰Œ×Ò˜˜R  RÑ(Ô(Ð(Ø�q‘ˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜a™  Q¨Ñ*Ô*ˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜a "™f™ r¨1¨aÑ0Ô0ˆŒˆˆrN   )r7   r8   rg   r8   r   r8   rŒ  r8   ró   rb   s   @rM   r   r   ˆ  sC   ø€ € € € € Ø+Ð+ð1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1ð 1rN   r   c                  ó,   ‡ — e Zd ZdZd
ˆ fd„Zdd	„Zˆ xZS )r   zAConv.r7   r8   rg   c                óx   •— t          ¦   «                              ¦   «          t          ||ddd¦  «        | _        dS )z}Initialize AConv module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
        ri   rS   r   N)r>   r?   r   rl   ©rJ   r7   rg   rL   s      €rM   r?   zAConv.__init__Ÿ  s6   ø€ õ 	‰Œ×ÒÑÔÐÝ˜˜B  1 aÑ(Ô(ˆŒˆˆrN   rK   rO   rP   c                ó~   — t           j        j                             |ddddd¦  «        }|                      |¦  «        S )z!Forward pass through AConv layer.rS   r   r   FT)rC   r   Ú
functionalÚ
avg_pool2drl   rr   s     rM   rZ   zAConv.forward©  s4   € åŒHÔ×*Ò*¨1¨a°°A°u¸dÑCÔCˆØ�xŠx˜‰{Œ{ÐrN   ©r7   r8   rg   r8   r[   r\   rb   s   @rM   r   r   œ  sV   ø€ € € € € Ø€Lð)ð )ð )ð )ð )ð )ðð ð ð ð ð ð ð rN   r   c                  ó,   ‡ — e Zd ZdZd
ˆ fd„Zdd	„Zˆ xZS )r   zADown.r7   r8   rg   c                óÜ   •— t          ¦   «                              ¦   «          |dz  | _        t          |dz  | j        ddd¦  «        | _        t          |dz  | j        ddd¦  «        | _        dS )z}Initialize ADown module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
        rS   ri   r   r   N)r>   r?   rÐ   r   rl   ro   r–  s      €rM   r?   zADown.__init__²  sd   ø€ õ 	‰Œ×ÒÑÔÐØ�q‘ˆŒÝ˜˜a™ ¤¨¨A¨qÑ1Ô1ˆŒÝ˜˜a™ ¤¨¨A¨qÑ1Ô1ˆŒˆˆrN   rK   rO   rP   c                óV  — t           j        j                             |ddddd¦  «        }|                     dd¦  «        \  }}|                      |¦  «        }t           j        j                             |ddd¦  «        }|                      |¦  «        }t          j        ||fd¦  «        S )z!Forward pass through ADown layer.rS   r   r   FTri   )	rC   r   r˜  r™  rÕ   rl   Ú
max_pool2dro   rŠ   )rJ   rK   rŒ   r‹   s       rM   rZ   zADown.forward¾  s‹   € åŒHÔ×*Ò*¨1¨a°°A°u¸dÑCÔCˆØ—’˜˜A‘”‰ˆˆBØ�XŠX�b‰\Œ\ˆÝŒXÔ ×+Ò+¨B°°1°aÑ8Ô8ˆØ�XŠX�b‰\Œ\ˆÝŒy˜"˜b˜ 1Ñ%Ô%Ð%rN   rš  r[   r\   rb   s   @rM   r   r   ¯  sV   ø€ € € € € Ø€Lð
2ð 
2ð 
2ð 
2ð 
2ð 
2ð&ð &ð &ð &ð &ð &ð &ð &rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   z	SPP-ELAN.r­   r7   r8   rg   r   rk   c                ór  •— t          ¦   «                              ¦   «          || _        t          ||dd¦  «        | _        t          j        |d|dz  ¬¦  «        | _        t          j        |d|dz  ¬¦  «        | _        t          j        |d|dz  ¬¦  «        | _	        t          d|z  |dd¦  «        | _
        dS )zÞInitialize SPP-ELAN block.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            c3 (int): Intermediate channels.
            k (int): Kernel size for max pooling.
        r   rS   r³   rR   N)r>   r?   rÐ   r   rl   r   r‚   ro   rp   r  Úcv5)rJ   r7   rg   r   rk   rL   s        €rM   r?   zSPPELAN.__init__Ë  s¨   ø€ õ 	‰Œ×ÒÑÔÐØˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ”<¨A°aÀÀaÁÐHÑHÔHˆŒÝ”<¨A°aÀÀaÁÐHÑHÔHˆŒÝ”<¨A°aÀÀaÁÐHÑHÔHˆŒÝ˜˜B™  A qÑ)Ô)ˆŒˆˆrN   rK   rO   rP   c                óæ   ‡— |                       |¦  «        gŠ‰                     ˆfd„| j        | j        | j        fD ¦   «         ¦  «         |                      t          j        ‰d¦  «        ¦  «        S )z#Forward pass through SPPELAN layer.c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z"SPPELAN.forward.<locals>.<genexpr>ß  s/   øè è € ÐBÐB˜a���1�R”5‘”ÐBÐBÐBÐBÐBÐBrN   r   )rl   r©   ro   rp   r  r¡  rC   rŠ   rª   s     @rM   rZ   zSPPELAN.forwardÜ  sd   ø€ à�XŠX�a‰[Œ[ˆMˆØ	�ŠÐBÐBÐBÐB D¤H¨d¬h¸¼Ð#AÐBÑBÔBÑBÔBÐBØ�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   )r­   )r7   r8   rg   r8   r   r8   rk   r8   r[   r\   rb   s   @rM   r   r   È  s[   ø€ € € € € Ø€Oð*ð *ð *ð *ð *ð *ð *ð")ð )ð )ð )ð )ð )ð )ð )rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r(   z	CBLinear.r   Nr7   r8   Úc2sú	list[int]rk   r  r  ú
int | NonerÇ   c           
     óÌ   •— t          ¦   «                              ¦   «          || _        t          j        |t          |¦  «        ||t          ||¦  «        |d¬¦  «        | _        dS )a  Initialize CBLinear module.

        Args:
            c1 (int): Input channels.
            c2s (list[int]): List of output channel sizes.
            k (int): Kernel size.
            s (int): Stride.
            p (int | None): Padding.
            g (int): Groups.
        T)Úgroupsr;   N)r>   r?   r¥  r   r@   Úsumr   rB   )rJ   r7   r¥  rk   r  r  rÇ   rL   s          €rM   r?   zCBLinear.__init__æ  sU   ø€ õ 	‰Œ×ÒÑÔÐØˆŒÝ”I˜b¥# c¡(¤(¨A¨qµ'¸!¸Q±-´-ÈÐPTÐUÑUÔUˆŒ	ˆ	ˆ	rN   rK   rO   rP   rc  c                ó`   — |                       |¦  «                             | j        d¬¦  «        S )z$Forward pass through CBLinear layer.r   r†   )rB   rä   r¥  rr   s     rM   rZ   zCBLinear.forwardõ  s'   € à�yŠy˜‰|Œ|×!Ò! $¤(°Ð!Ñ2Ô2Ð2rN   )r   r   Nr   )r7   r8   r¥  r¦  rk   r8   r  r8   r  r§  rÇ   r8   )rK   rO   rP   rc  r\   rb   s   @rM   r(   r(   ã  sb   ø€ € € € € Ø€OðVð Vð Vð Vð Vð Vð Vð3ð 3ð 3ð 3ð 3ð 3ð 3ð 3rN   r(   c                  ó,   ‡ — e Zd ZdZd
ˆ fd„Zdd	„Zˆ xZS )r'   zCBFuse.Úidxr¦  c                óV   •— t          ¦   «                              ¦   «          || _        dS )zmInitialize CBFuse module.

        Args:
            idx (list[int]): Indices for feature selection.
        N)r>   r?   r­  )rJ   r­  rL   s     €rM   r?   zCBFuse.__init__ý  s&   ø€ õ 	‰Œ×ÒÑÔÐØˆŒˆˆrN   Úxsrc  rP   rO   c                óÞ   ‡ ‡— |d         j         dd…         Šˆ ˆfd„t          |dd…         ¦  «        D ¦   «         }t          j        t          j        ||dd…         z   ¦  «        d¬¦  «        S )z¹Forward pass through CBFuse layer.

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

        Returns:
            (torch.Tensor): Fused output tensor.
        r¥   rS   Nc                óf   •— g | ]-\  }}t          j        |‰j        |                  ‰d ¬¦  «        ‘Œ.S )Únearest)ÚsizeÚmode)rˆ   Úinterpolater­  )r˜   r™   rK   rJ   Útarget_sizes      €€rM   r´   z"CBFuse.forward.<locals>.<listcomp>  s;   ø€ ÐnÐnÐnÑSWÐSTÐVW�qŒ}˜Q˜tœx¨œ{œ^°+ÀIÐNÑNÔNÐnÐnÐnrN   r   r†   )rT   Ú	enumeraterC   rª  Ústack)rJ   r¯  Úresr¶  s   `  @rM   rZ   zCBFuse.forward  sw   øø€ ð ˜”f”l 1 2 2Ô&ˆØnÐnÐnÐnÐnÕ[dÐegÐhkÐikÐhkÔelÑ[mÔ[mÐnÑnÔnˆÝŒy�œ S¨2¨b¨c¨c¬7¡]Ñ3Ô3¸Ð;Ñ;Ô;Ð;rN   )r­  r¦  )r¯  rc  rP   rO   r\   rb   s   @rM   r'   r'   ú  sV   ø€ € € € € Ø€Mðð ð ð ð ð ð<ð <ð <ð <ð <ð <ð <ð <rN   r'   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )ÚC3fz<Faster Implementation of CSP Bottleneck with 3 convolutions.r   FrÆ   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   c                ón  •‡‡‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        Št          |‰dd¦  «        | _        t          |‰dd¦  «        | _        t          d|z   ‰z  |d¦  «        | _        t          j        ˆˆˆfd„t          |¦  «        D ¦   «         ¦  «        | _
        dS )ag  Initialize CSP bottleneck layer with three convolutions.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        r   rS   c           	   3  óB   •K  — | ]}t          ‰‰‰‰d d¬¦  «        V — ŒdS rË   rë   rì   s     €€€rM   r›   zC3f.__init__.<locals>.<genexpr>'  s;   øè è € ÐlÐlÐ^_�z¨"¨b°(¸AÐAQÐUXÐYÑYÔYÐlÐlÐlÐlÐlÐlrN   N)r>   r?   r8   r   rl   ro   rp   r   rœ   r�   rž   rí   s	       `` @€rM   r?   zC3f.__init__  s¬   øøøø€ õ 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜Q™ "™ b¨!Ñ,Ô,ˆŒÝ”ÐlÐlÐlÐlÐlÐlÕchÐijÑckÔckÐlÑlÔlÑlÔlˆŒˆˆrN   rK   rO   rP   c                óô   ‡— |                       |¦  «        |                      |¦  «        gŠ‰                     ˆfd„| j        D ¦   «         ¦  «         |                      t          j        ‰d¦  «        ¦  «        S )zForward pass through C3f layer.c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   zC3f.forward.<locals>.<genexpr>,  r¨   rN   r   )ro   rl   r©   rž   rp   rC   rŠ   rª   s     @rM   rZ   zC3f.forward)  sf   ø€ à�XŠX�a‰[Œ[˜$Ÿ(š( 1™+œ+Ð&ˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø�xŠx�œ	 ! Q™œÑ(Ô(Ð(rN   ræ   rØ   r[   r\   rb   s   @rM   r»  r»    sc   ø€ € € € € ØFÐFðmð mð mð mð mð mð mð$)ð )ð )ð )ð )ð )ð )ð )rN   r»  c                  ó2   ‡ — e Zd ZdZ	 	 	 	 	 	 ddˆ fd„Zˆ xZS )r%   rÚ   r   FrÆ   Tr7   r8   rg   r�   Úc3kr‘   rÈ   rE   rH  rÇ   r’   c	                óÊ   •‡ ‡‡‡‡— t          ¦   «                              |||‰‰|¦  «         t          j        ˆˆˆˆ ˆfd„t	          |¦  «        D ¦   «         ¦  «        ‰ _        dS )a¨  Initialize C3k2 module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of blocks.
            c3k (bool): Whether to use C3k blocks.
            e (float): Expansion ratio.
            attn (bool): Whether to use attention blocks.
            g (int): Groups for convolutions.
            shortcut (bool): Whether to use shortcut connections.
        c              3  óF  •K  — | ]›}‰rZt          j        t          ‰j        ‰j        ‰‰¦  «        t	          ‰j        d t          ‰j        dz  d¦  «        ¬¦  «        ¦  «        n:‰rt          ‰j        ‰j        d‰‰¦  «        nt          ‰j        ‰j        ‰‰¦  «        V — ŒœdS )rÆ   é@   r   ©Ú
attn_ratioÚ	num_headsrS   N)r   rÂ   r   rÐ   ÚPSABlockr<  ÚC3k)r˜   rX   rH  rÁ  rÇ   rJ   r’   s     €€€€€rM   r›   z C3k2.__init__.<locals>.<genexpr>K  sÂ   øè è € ð 

ð 

ð ð	 ð	9�BŒMÝ˜4œ6 4¤6¨8°QÑ7Ô7Ý˜œ¨C½3¸t¼vÈ¹|ÈQÑ;OÔ;OÐPÑPÔPñô ð ð ð9•�T”V˜TœV Q¨°!Ñ4Ô4Ð4å˜DœF D¤F¨H°aÑ8Ô8ð

ð 

ð 

ð 

ð 

ð 

rN   N©r>   r?   r   rœ   r�   rž   )
rJ   r7   rg   r�   rÁ  rÈ   rH  rÇ   r’   rL   s
   `   ` ```€rM   r?   zC3k2.__init__3  s…   øøøøøø€ õ. 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý”ð 

ð 

ð 

ð 

ð 

ð 

ð 

ð 

õ ˜1‘X”Xð

ñ 

ô 

ñ 

ô 

ˆŒˆˆrN   )r   FrÆ   Fr   T)r7   r8   rg   r8   r�   r8   rÁ  r‘   rÈ   rE   rH  r‘   rÇ   r8   r’   r‘   ró   rb   s   @rM   r%   r%   0  s\   ø€ € € € € ØFÐFð ØØØØØð"
ð "
ð "
ð "
ð "
ð "
ð "
ð "
ð "
ð "
ð "
rN   r%   c                  ó&   ‡ — e Zd ZdZddˆ fd„Zˆ xZS )rÉ  zhC3k is a CSP bottleneck module with customizable kernel sizes for feature extraction in neural networks.r   TrÆ   ri   r7   r8   rg   r�   r’   r‘   rÇ   rÈ   rE   rk   c                óÞ   •‡‡‡‡— t          ¦   «                              |||‰‰|¦  «         t          ||z  ¦  «        Št          j        ˆˆˆˆfd„t          |¦  «        D ¦   «         Ž | _        dS )ag  Initialize C3k module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of Bottleneck blocks.
            shortcut (bool): Whether to use shortcut connections.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
            k (int): Kernel size.
        c           	   3  óF   •K  — | ]}t          ‰‰‰‰‰‰fd ¬¦  «        V — ŒdS )rÍ   rÎ   Nrë   )r˜   rX   rf   rÇ   rk   r’   s     €€€€rM   r›   zC3k.__init__.<locals>.<genexpr>j  s=   øè è € Ð dÐ dÐVW¥¨B°°H¸aÀAÀqÀ6ÈSÐ!QÑ!QÔ!QÐ dÐ dÐ dÐ dÐ dÐ drN   Nr   )
rJ   r7   rg   r�   r’   rÇ   rÈ   rk   rf   rL   s
       `` `@€rM   r?   zC3k.__init__[  st   øøøøø€ õ 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý��a‘‰[Œ[ˆå”Ð dÐ dÐ dÐ dÐ dÐ dÐ dÕ[`ÐabÑ[cÔ[cÐ dÑ dÔ dÐeˆŒˆˆrN   )r   Tr   rÆ   ri   )r7   r8   rg   r8   r�   r8   r’   r‘   rÇ   r8   rÈ   rE   rk   r8   ró   rb   s   @rM   rÉ  rÉ  X  sS   ø€ € € € € ØrÐrðfð fð fð fð fð fð fð fð fð fð frN   rÉ  c                  ód   ‡ — e Zd ZdZdˆ fd„Zdd	„Zdd
„Z ej        ¦   «         d„ ¦   «         Z	ˆ xZ
S )r1   z\RepVGGDW is a class that represents a depth-wise convolutional block in RepVGG architecture.Úedr8   rP   ÚNonec           	     óò   •— t          ¦   «                              ¦   «          t          ||ddd|d¬¦  «        | _        t          ||ddd|d¬¦  «        | _        || _        t          j        ¦   «         | _        dS )zdInitialize RepVGGDW module.

        Args:
            ed (int): Input and output channels.
        r&  r   ri   F©rÇ   rw   N)	r>   r?   r   rB   Úconv1r‡   r   r  rw   )rJ   rÏ  rL   s     €rM   r?   zRepVGGDW.__init__p  so   ø€ õ 	‰Œ×ÒÑÔÐÝ˜˜R  A q¨B°EÐ:Ñ:Ô:ˆŒ	Ý˜"˜b ! Q¨¨R°UÐ;Ñ;Ô;ˆŒ
ØˆŒÝ”7‘9”9ˆŒˆˆrN   rK   rO   c                ó~   — |                       |                      |¦  «        |                      |¦  «        z   ¦  «        S )zØPerform a forward pass of the RepVGGDW block.

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

        Returns:
            (torch.Tensor): Output tensor after applying the depth-wise convolution.
        )rw   rB   rÓ  rr   s     rM   rZ   zRepVGGDW.forward|  s/   € ð �xŠx˜Ÿ	š	 !™œ t§z¢z°!¡}¤}Ñ4Ñ5Ô5Ð5rN   c                óR   — |                       |                      |¦  «        ¦  «        S )zÞPerform a forward pass of the fused RepVGGDW block.

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

        Returns:
            (torch.Tensor): Output tensor after applying the depth-wise convolution.
        )rw   rB   rr   s     rM   r~  zRepVGGDW.forward_fuse‡  s    € ð �xŠx˜Ÿ	š	 !™œÑ%Ô%Ð%rN   c                óâ  — t          | d¦  «        sdS t          | j        j        | j        j        ¦  «        }t          | j        j        | j        j        ¦  «        }|j        }|j        }|j        }|j        }t          j        j	         
                    |g d¢¦  «        }||z   }||z   }|j        j                             |¦  «         |j        j                             |¦  «         || _        | `dS )z¡Fuse the convolutional layers in the RepVGGDW block.

        This method fuses the convolutional layers and updates the weights and biases accordingly.
        rÓ  N)rS   rS   rS   rS   )Úhasattrr   rB   r  rÓ  rH   r;   rC   r   r˜  r‰   rI   Úcopy_)	rJ   rB   rÓ  Úconv_wÚconv_bÚconv1_wÚconv1_bÚfinal_conv_wÚfinal_conv_bs	            rM   r€  zRepVGGDW.fuse’  sØ   € õ �t˜WÑ%Ô%ð 	ØˆFÝ ¤	¤°´	´Ñ=Ô=ˆÝ  ¤¤°$´*´-Ñ@Ô@ˆà”ˆØ”ˆØ”,ˆØ”*ˆå”(Ô%×)Ò)¨'°<°<°<Ñ@Ô@ˆà Ñ'ˆØ Ñ'ˆàŒÔ×Ò˜|Ñ,Ô,Ð,ØŒ	Œ×Ò˜\Ñ*Ô*Ð*àˆŒ	ØˆJˆJˆJrN   )rÏ  r8   rP   rÐ  r[   )r]   r^   r_   r`   r?   rZ   r~  rC   Úno_gradr€  ra   rb   s   @rM   r1   r1   m  sŽ   ø€ € € € € ØfÐfð
ð 
ð 
ð 
ð 
ð 
ð	6ð 	6ð 	6ð 	6ð	&ð 	&ð 	&ð 	&ð €U„]�_„_ðð ñ „_ðð ð ð ð rN   r1   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   a©  Compact Inverted Block (CIB) module.

    Args:
        c1 (int): Number of input channels.
        c2 (int): Number of output channels.
        shortcut (bool, optional): Whether to add a shortcut connection. Defaults to True.
        e (float, optional): Scaling factor for the hidden channels. Defaults to 0.5.
        lk (bool, optional): Whether to use RepVGGDW for the third convolutional layer. Defaults to False.
    TrÆ   Fr7   r8   rg   r’   r‘   rÈ   rE   Úlkc                ó¨  •— t          ¦   «                              ¦   «          t          ||z  ¦  «        }t          j        t          ||d|¬¦  «        t          |d|z  d¦  «        |rt          d|z  ¦  «        nt          d|z  d|z  dd|z  ¬¦  «        t          d|z  |d¦  «        t          ||d|¬¦  «        ¦  «        | _        |o||k    | _        dS )a  Initialize the CIB module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            shortcut (bool): Whether to use shortcut connection.
            e (float): Expansion ratio.
            lk (bool): Whether to use RepVGGDW.
        ri   r	  rS   r   N)	r>   r?   r8   r   rÂ   r   r1   rl   r¡   )rJ   r7   rg   r’   rÈ   rá  rf   rL   s          €rM   r?   zCIB.__init__¹  s×   ø€ õ 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆÝ”=Ý��R˜˜bÐ!Ñ!Ô!Ý��Q˜‘V˜QÑÔØ "ÐI�H�Q˜‘VÑÔÐ­¨Q°©V°Q¸±V¸QÀ!ÀbÁ&Ð(IÑ(IÔ(IÝ��R‘˜˜QÑÔÝ��R˜˜bÐ!Ñ!Ô!ñ
ô 
ˆŒð Ð(  b¢ˆŒˆˆrN   rK   rO   rP   c                ój   — | j         r||                      |¦  «        z   n|                      |¦  «        S )z Forward pass of the CIB module.

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

        Returns:
            (torch.Tensor): Output tensor.
        )r¡   rl   rr   s     rM   rZ   zCIB.forwardÏ  s-   € ð #'¤(Ð;ˆq�4—8’8˜A‘;”;‰ˆ°·²¸±´Ð;rN   )TrÆ   F)
r7   r8   rg   r8   r’   r‘   rÈ   rE   rá  r‘   r[   r\   rb   s   @rM   r   r   ®  s`   ø€ € € € € ðð ð)ð )ð )ð )ð )ð )ð )ð,	<ð 	<ð 	<ð 	<ð 	<ð 	<ð 	<ð 	<rN   r   c                  ó(   ‡ — e Zd ZdZ	 ddˆ fd„Zˆ xZS )r"   aD  C2fCIB class represents a convolutional block with C2f and CIB modules.

    Args:
        c1 (int): Number of input channels.
        c2 (int): Number of output channels.
        n (int, optional): Number of CIB modules to stack. Defaults to 1.
        shortcut (bool, optional): Whether to use shortcut connection. Defaults to False.
        lk (bool, optional): Whether to use large kernel. Defaults to False.
        g (int, optional): Number of groups for grouped convolution. Defaults to 1.
        e (float, optional): Expansion ratio for CIB modules. Defaults to 0.5.
    r   FrÆ   r7   r8   rg   r�   r’   r‘   rá  rÇ   rÈ   rE   c                óÂ   •‡ ‡‡— t          ¦   «                              |||‰||¦  «         t          j        ˆˆ ˆfd„t	          |¦  «        D ¦   «         ¦  «        ‰ _        dS )au  Initialize C2fCIB module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of CIB modules.
            shortcut (bool): Whether to use shortcut connection.
            lk (bool): Whether to use large kernel.
            g (int): Groups for convolutions.
            e (float): Expansion ratio.
        c              3  óT   •K  — | ]"}t          ‰j        ‰j        ‰d ‰¬¦  «        V — Œ#dS )rÍ   )rÈ   rá  N)r   rÐ   )r˜   rX   rá  rJ   r’   s     €€€rM   r›   z"C2fCIB.__init__.<locals>.<genexpr>÷  s:   øè è € Ð]Ð]Èq�s 4¤6¨4¬6°8¸sÀrÐJÑJÔJÐ]Ð]Ð]Ð]Ð]Ð]rN   NrÊ  )	rJ   r7   rg   r�   r’   rá  rÇ   rÈ   rL   s	   `   ``  €rM   r?   zC2fCIB.__init__è  sd   øøøø€ õ 	‰Œ×Ò˜˜R  H¨a°Ñ3Ô3Ð3Ý”Ð]Ð]Ð]Ð]Ð]Ð]ÕTYÐZ[ÑT\ÔT\Ð]Ñ]Ô]Ñ]Ô]ˆŒˆˆrN   )r   FFr   rÆ   )r7   r8   rg   r8   r�   r8   r’   r‘   rá  r‘   rÇ   r8   rÈ   rE   ró   rb   s   @rM   r"   r"   Û  s^   ø€ € € € € ð
ð 
ð nqð^ð ^ð ^ð ^ð ^ð ^ð ^ð ^ð ^ð ^ð ^rN   r"   c                  ó2   ‡ — e Zd ZdZdZddˆ fd
„Zdd„Zˆ xZS )r   aù  Attention module that performs self-attention on the input tensor.

    Args:
        dim (int): The input tensor dimension.
        num_heads (int): The number of attention heads.
        attn_ratio (float): The ratio of the attention key dimension to the head dimension.

    Attributes:
        num_heads (int): The number of attention heads.
        head_dim (int): The dimension of each attention head.
        key_dim (int): The dimension of the attention key.
        scale (float): The scaling factor for the attention scores.
        qkv (Conv): Convolutional layer for computing the query, key, and value.
        proj (Conv): Convolutional layer for projecting the attended values.
        pe (Conv): Convolutional layer for positional encoding.
    NrP  rÆ   r‡   r8   rÇ  rÆ  rE   c                óx  •— t          ¦   «                              ¦   «          || _        ||z  | _        t	          | j        |z  ¦  «        | _        | j        dz  | _        | j        |z  }||dz  z   }t          ||dd¬¦  «        | _        t          ||dd¬¦  «        | _	        t          ||dd|d¬¦  «        | _
        dS )	zâInitialize multi-head attention module.

        Args:
            dim (int): Input dimension.
            num_heads (int): Number of attention heads.
            attn_ratio (float): Attention ratio for key dimension.
        ç      à¿rS   r   Frv   ri   rÒ  N)r>   r?   rÇ  Úhead_dimr8   Úkey_dimr1  r   Úqkvr^  Úpe)rJ   r‡   rÇ  rÆ  Únh_kdr@  rL   s         €rM   r?   zAttention.__init__  s»   ø€ õ 	‰Œ×ÒÑÔÐØ"ˆŒØ˜yÑ(ˆŒÝ˜4œ=¨:Ñ5Ñ6Ô6ˆŒØ”\ 4Ñ'ˆŒ
Ø”˜yÑ(ˆØ�%˜!‘)‰OˆÝ˜˜Q  uÐ-Ñ-Ô-ˆŒÝ˜˜c 1¨%Ð0Ñ0Ô0ˆŒ	Ý�s˜C  A¨°%Ð8Ñ8Ô8ˆŒˆˆrN   rK   rO   rP   c           	     óâ  — |j         \  }}}}||z  }|                      |¦  «        }|                     || j        | j        dz  | j        z   |¦  «                             | j        | j        | j        gd¬¦  «        \  }}	}
| j        dk    rÀt          t          d¦  «        r«t          j
        |                     dd¦  «        |	                     dd¦  «        |
                     dd¦  «        ¦  «        }|                     dd¦  «                             ||||¦  «        |                      |
                     ||||¦  «        ¦  «        z   }n�|| j        z                       dd¦  «        |	z                       d¬¦  «        }|
|                     dd¦  «        z                       ||||¦  «        |                      |
                     ||||¦  «        ¦  «        z   }|                      |¦  «        }|S )zÃForward pass of the Attention module.

        Args:
            x (torch.Tensor): The input tensor.

        Returns:
            (torch.Tensor): The output tensor after self-attention.
        rS   r†   ÚcoremlÚscaled_dot_product_attentionéþÿÿÿr¥   )rT   rì  rG   rÇ  rë  rê  rä   Úformatr×  rˆ   rñ  rU   ri  rí  r1  rV   r^  )rJ   rK   ÚBÚCÚHÚWÚNrì  rj  rk   rk  rH  s               rM   rZ   zAttention.forward!  sË  € ð ”W‰
ˆˆ1ˆa�Ø�‰EˆØ�hŠh�q‰kŒkˆØ—(’(˜1˜dœn¨d¬l¸QÑ.>ÀÄÑ.NÐPQÑRÔR×XÒXØŒ\˜4œ<¨¬Ð7¸Qð Yñ 
ô 
‰ˆˆ1ˆað Œ;˜(Ò"Ð"¥w­qÐ2PÑ'QÔ'QÐ"ÝÔ.¨q¯{ª{¸2¸rÑ/BÔ/BÀAÇKÂKÐPRÐTVÑDWÔDWÐYZ×YdÒYdÐegÐikÑYlÔYlÑmÔmˆAØ—’˜B Ñ#Ô#×+Ò+¨A¨q°!°QÑ7Ô7¸$¿'º'À!Ç)Â)ÈAÈqÐRSÐUVÑBWÔBWÑ:XÔ:XÑXˆAˆAà˜œ‘^×.Ò.¨r°2Ñ6Ô6¸Ñ:×CÒCÈÐCÑKÔKˆDØ�T—^’^ B¨Ñ+Ô+Ñ+×1Ò1°!°Q¸¸1Ñ=Ô=ÀÇÂÈÏ	Ê	ÐRSÐUVÐXYÐ[\ÑH]ÔH]Ñ@^Ô@^Ñ^ˆAØ�IŠI�a‰LŒLˆØˆrN   )rP  rÆ   )r‡   r8   rÇ  r8   rÆ  rE   r[   )r]   r^   r_   r`   ró  r?   rZ   ra   rb   s   @rM   r   r   ú  sg   ø€ € € € € ðð ð" €Fð9ð 9ð 9ð 9ð 9ð 9ð 9ð&ð ð ð ð ð ð ð rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )rÈ  aF  PSABlock class implementing a Position-Sensitive Attention block for neural networks.

    This class encapsulates the functionality for applying multi-head attention and feed-forward neural network layers
    with optional shortcut connections.

    Attributes:
        attn (Attention): Multi-head attention module.
        ffn (nn.Sequential): Feed-forward neural network module.
        add (bool): Flag indicating whether to add shortcut connections.

    Methods:
        forward: Performs a forward pass through the PSABlock, applying attention and feed-forward layers.

    Examples:
        Create a PSABlock and perform a forward pass
        >>> psablock = PSABlock(c=128, attn_ratio=0.5, num_heads=4, shortcut=True)
        >>> input_tensor = torch.randn(1, 128, 32, 32)
        >>> output_tensor = psablock(input_tensor)
    rÆ   rR   TrÐ   r8   rÆ  rE   rÇ  r’   r‘   rP   rÐ  c           	     ó  •— t          ¦   «                              ¦   «          t          |||¬¦  «        | _        t	          j        t          ||dz  d¦  «        t          |dz  |dd¬¦  «        ¦  «        | _        || _        dS )a  Initialize the PSABlock.

        Args:
            c (int): Input and output channels.
            attn_ratio (float): Attention ratio for key dimension.
            num_heads (int): Number of attention heads.
            shortcut (bool): Whether to use shortcut connections.
        rÅ  rS   r   Frv   N)	r>   r?   r   rH  r   rÂ   r   Úffnr¡   )rJ   rÐ   rÆ  rÇ  r’   rL   s        €rM   r?   zPSABlock.__init__P  st   ø€ õ 	‰Œ×ÒÑÔÐå˜a¨JÀ)ÐLÑLÔLˆŒ	Ý”=¥ a¨¨Q©°Ñ!2Ô!2µD¸¸Q¹ÀÀ1È%Ð4PÑ4PÔ4PÑQÔQˆŒØˆŒˆˆrN   rK   rO   c                óÖ   — | j         r||                      |¦  «        z   n|                      |¦  «        }| j         r||                      |¦  «        z   n|                      |¦  «        }|S )zÕExecute a forward pass through PSABlock.

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

        Returns:
            (torch.Tensor): Output tensor after attention and feed-forward processing.
        )r¡   rH  rû  rr   s     rM   rZ   zPSABlock.forward_  s[   € ð !%¤Ð:ˆA�—	’	˜!‘”ÑÐ¨d¯iªi¸©l¬lˆØ#œxÐ8ˆA�—’˜‘”‰OˆO¨T¯XªX°a©[¬[ˆØˆrN   )rÆ   rR   T)
rÐ   r8   rÆ  rE   rÇ  r8   r’   r‘   rP   rÐ  r[   r\   rb   s   @rM   rÈ  rÈ  ;  s`   ø€ € € € € ðð ð(ð ð ð ð ð ð ðð ð ð ð ð ð ð rN   rÈ  c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   a  PSA class for implementing Position-Sensitive Attention in neural networks.

    This class encapsulates the functionality for applying position-sensitive attention and feed-forward networks to
    input tensors, enhancing feature extraction and processing capabilities.

    Attributes:
        c (int): Number of hidden channels after applying the initial convolution.
        cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
        cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c1.
        attn (Attention): Attention module for position-sensitive attention.
        ffn (nn.Sequential): Feed-forward network for further processing.

    Methods:
        forward: Applies position-sensitive attention and feed-forward network to the input tensor.

    Examples:
        Create a PSA module and apply it to an input tensor
        >>> psa = PSA(c1=128, c2=128, e=0.5)
        >>> input_tensor = torch.randn(1, 128, 64, 64)
        >>> output_tensor = psa.forward(input_tensor)
    rÆ   r7   r8   rg   rÈ   rE   c           	     ó  •— t          ¦   «                              ¦   «          ||k    sJ ‚t          ||z  ¦  «        | _        t	          |d| j        z  dd¦  «        | _        t	          d| j        z  |d¦  «        | _        t          | j        dt          | j        dz  d¦  «        ¬¦  «        | _	        t          j        t	          | j        | j        dz  d¦  «        t	          | j        dz  | j        dd¬¦  «        ¦  «        | _        dS )	z£Initialize PSA module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            e (float): Expansion ratio.
        rS   r   rÆ   rÄ  rÅ  Frv   N)r>   r?   r8   rÐ   r   rl   ro   r   r<  rH  r   rÂ   rû  )rJ   r7   rg   rÈ   rL   s       €rM   r?   zPSA.__init__„  sÜ   ø€ õ 	‰Œ×ÒÑÔÐØ�RŠxˆxˆxˆxÝ�R˜!‘V‘”ˆŒÝ˜˜A ¤™J¨¨1Ñ-Ô-ˆŒÝ˜˜DœF™
 B¨Ñ*Ô*ˆŒå˜dœf°ÅÀDÄFÈbÁLÐRSÑ@TÔ@TÐUÑUÔUˆŒ	Ý”=¥ d¤f¨d¬f°q©j¸!Ñ!<Ô!<½dÀ4Ä6ÈAÁ:ÈtÌvÐWXÐ^cÐ>dÑ>dÔ>dÑeÔeˆŒˆˆrN   rK   rO   rP   c                ó(  — |                       |¦  «                             | j        | j        fd¬¦  «        \  }}||                      |¦  «        z   }||                      |¦  «        z   }|                      t          j        ||fd¦  «        ¦  «        S )zÐExecute forward pass in PSA module.

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

        Returns:
            (torch.Tensor): Output tensor after attention and feed-forward processing.
        r   r†   )rl   rä   rÐ   rH  rû  ro   rC   rŠ   rÖ   s       rM   rZ   zPSA.forward•  s|   € ð �xŠx˜‰{Œ{× Ò  $¤&¨$¬&Ð!1°qÐ Ñ9Ô9‰ˆˆ1Ø�—	’	˜!‘”ÑˆØ�—’˜‘”‰OˆØ�xŠx�œ	 1 a &¨!Ñ,Ô,Ñ-Ô-Ð-rN   )rÆ   )r7   r8   rg   r8   rÈ   rE   r[   r\   rb   s   @rM   r   r   m  sg   ø€ € € € € ðð ð,fð fð fð fð fð fð fð".ð .ð .ð .ð .ð .ð .ð .rN   r   c                  ó.   ‡ — e Zd ZdZddˆ fd
„Zdd„Zˆ xZS )r   aH  C2PSA module with attention mechanism for enhanced feature extraction and processing.

    This module implements a convolutional block with attention mechanisms to enhance feature extraction and processing
    capabilities. It includes a series of PSABlock modules for self-attention and feed-forward operations.

    Attributes:
        c (int): Number of hidden channels.
        cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
        cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c1.
        m (nn.Sequential): Sequential container of PSABlock modules for attention and feed-forward operations.

    Methods:
        forward: Performs a forward pass through the C2PSA module, applying attention and feed-forward operations.

    Examples:
        >>> c2psa = C2PSA(c1=256, c2=256, n=3, e=0.5)
        >>> input_tensor = torch.randn(1, 256, 64, 64)
        >>> output_tensor = c2psa(input_tensor)

    Notes:
        This module essentially is the same as PSA module, but refactored to allow stacking more PSABlock modules.
    r   rÆ   r7   r8   rg   r�   rÈ   rE   c                óZ  •‡ — t          ¦   «                              ¦   «          ||k    sJ ‚t          ||z  ¦  «        ‰ _        t	          |d‰ j        z  dd¦  «        ‰ _        t	          d‰ j        z  |d¦  «        ‰ _        t          j        ˆ fd„t          |¦  «        D ¦   «         Ž ‰ _
        dS )zÖInitialize C2PSA module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of PSABlock modules.
            e (float): Expansion ratio.
        rS   r   c           	   3  ór   •K  — | ]1}t          ‰j        d t          ‰j        dz  d¦  «        ¬¦  «        V — Œ2dS ©rÆ   rÄ  r   rÅ  N©rÈ  rÐ   r<  ©r˜   rX   rJ   s     €rM   r›   z!C2PSA.__init__.<locals>.<genexpr>Ë  sJ   øè è € Ð tÐ tÐfg¥¨$¬&¸SÍCÐPTÔPVÐZ\ÑP\Ð^_ÑL`ÔL`Ð!aÑ!aÔ!aÐ tÐ tÐ tÐ tÐ tÐ trN   NrÒ   ©rJ   r7   rg   r�   rÈ   rL   s   `    €rM   r?   zC2PSA.__init__¼  sŸ   øø€ õ 	‰Œ×ÒÑÔÐØ�RŠxˆxˆxˆxÝ�R˜!‘V‘”ˆŒÝ˜˜A ¤™J¨¨1Ñ-Ô-ˆŒÝ˜˜DœF™
 B¨Ñ*Ô*ˆŒå”Ð tÐ tÐ tÐ tÕkpÐqrÑksÔksÐ tÑ tÔ tÐuˆŒˆˆrN   rK   rO   rP   c                óò   — |                       |¦  «                             | j        | j        fd¬¦  «        \  }}|                      |¦  «        }|                      t          j        ||fd¦  «        ¦  «        S )zÊProcess the input tensor through a series of PSA blocks.

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

        Returns:
            (torch.Tensor): Output tensor after processing.
        r   r†   )rl   rä   rÐ   rž   ro   rC   rŠ   rÖ   s       rM   rZ   zC2PSA.forwardÍ  sd   € ð �xŠx˜‰{Œ{× Ò  $¤&¨$¬&Ð!1°qÐ Ñ9Ô9‰ˆˆ1Ø�FŠF�1‰IŒIˆØ�xŠx�œ	 1 a &¨!Ñ,Ô,Ñ-Ô-Ð-rN   ©r   rÆ   rú   r[   r\   rb   s   @rM   r   r   ¤  sg   ø€ € € € € ðð ð.vð vð vð vð vð vð vð".ð .ð .ð .ð .ð .ð .ð .rN   r   c                  ó&   ‡ — e Zd ZdZddˆ fd
„Zˆ xZS )r#   a¨  C2fPSA module with enhanced feature extraction using PSA blocks.

    This class extends the C2f module by incorporating PSA blocks for improved attention mechanisms and feature
    extraction.

    Attributes:
        c (int): Number of hidden channels.
        cv1 (Conv): 1x1 convolution layer to reduce the number of input channels to 2*c.
        cv2 (Conv): 1x1 convolution layer to reduce the number of output channels to c2.
        m (nn.ModuleList): List of PSABlock modules for feature extraction.

    Methods:
        forward: Performs a forward pass through the C2fPSA module.
        forward_split: Performs a forward pass using split() instead of chunk().

    Examples:
        >>> import torch
        >>> from ultralytics.nn.modules.block import C2fPSA
        >>> model = C2fPSA(c1=64, c2=64, n=3, e=0.5)
        >>> x = torch.randn(1, 64, 128, 128)
        >>> output = model(x)
        >>> print(output.shape)
    r   rÆ   r7   r8   rg   r�   rÈ   rE   c                óÈ   •‡ — ||k    sJ ‚t          ¦   «                              ||||¬¦  «         t          j        ˆ fd„t	          |¦  «        D ¦   «         ¦  «        ‰ _        dS )z×Initialize C2fPSA module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            n (int): Number of PSABlock modules.
            e (float): Expansion ratio.
        )r�   rÈ   c           	   3  ór   •K  — | ]1}t          ‰j        d t          ‰j        dz  d¦  «        ¬¦  «        V — Œ2dS r  r  r  s     €rM   r›   z"C2fPSA.__init__.<locals>.<genexpr>ÿ  sG   øè è € ÐrÐrÐde�x¨¬¸3Í#ÈdÌfÐXZÉlÐ\]ÑJ^ÔJ^Ð_Ñ_Ô_ÐrÐrÐrÐrÐrÐrrN   NrÊ  r  s   `    €rM   r?   zC2fPSA.__init__ô  sh   øø€ ð �RŠxˆxˆxˆxÝ‰Œ×Ò˜˜R 1¨ÐÑ*Ô*Ð*Ý”ÐrÐrÐrÐrÕinÐopÑiqÔiqÐrÑrÔrÑrÔrˆŒˆˆrN   r  rú   ró   rb   s   @rM   r#   r#   Û  sW   ø€ € € € € ðð ð0sð sð sð sð sð sð sð sð sð sð srN   r#   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )r3   aH  SCDown module for downsampling with separable convolutions.

    This module performs downsampling using a combination of pointwise and depthwise convolutions, which helps in
    efficiently reducing the spatial dimensions of the input tensor while maintaining the channel information.

    Attributes:
        cv1 (Conv): Pointwise convolution layer that reduces the number of channels.
        cv2 (Conv): Depthwise convolution layer that performs spatial downsampling.

    Methods:
        forward: Applies the SCDown module to the input tensor.

    Examples:
        >>> import torch
        >>> from ultralytics.nn.modules.block import SCDown
        >>> model = SCDown(c1=64, c2=128, k=3, s=2)
        >>> x = torch.randn(1, 64, 128, 128)
        >>> y = model(x)
        >>> print(y.shape)
        torch.Size([1, 128, 64, 64])
    r7   r8   rg   rk   r  c                óª   •— t          ¦   «                              ¦   «          t          ||dd¦  «        | _        t          |||||d¬¦  «        | _        dS )z½Initialize SCDown module.

        Args:
            c1 (int): Input channels.
            c2 (int): Output channels.
            k (int): Kernel size.
            s (int): Stride.
        r   F)rk   r  rÇ   rw   N)r>   r?   r   rl   ro   )rJ   r7   rg   rk   r  rL   s        €rM   r?   zSCDown.__init__  sP   ø€ õ 	‰Œ×ÒÑÔÐÝ˜˜B  1Ñ%Ô%ˆŒÝ˜˜B ! q¨B°EÐ:Ñ:Ô:ˆŒˆˆrN   rK   rO   rP   c                óR   — |                       |                      |¦  «        ¦  «        S )zÄApply convolution and downsampling to the input tensor.

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

        Returns:
            (torch.Tensor): Downsampled output tensor.
        )ro   rl   rr   s     rM   rZ   zSCDown.forward&  s    € ð �xŠx˜Ÿš ™œÑ$Ô$Ð$rN   r  r[   r\   rb   s   @rM   r3   r3     s[   ø€ € € € € ðð ð,;ð ;ð ;ð ;ð ;ð ;ð	%ð 	%ð 	%ð 	%ð 	%ð 	%ð 	%ð 	%rN   r3   c                  ó0   ‡ — e Zd ZdZ	 ddˆ fd„Zdd„Zˆ xZS )r4   a?  TorchVision module to allow loading any torchvision model.

    This class provides a way to load a model from the torchvision library, optionally load pre-trained weights, and
    customize the model by truncating or unwrapping layers.

    Args:
        model (str): Name of the torchvision model to load.
        weights (str, optional): Pre-trained weights to load. Default is "DEFAULT".
        unwrap (bool, optional): Unwraps the model to a sequential containing all but the last `truncate` layers.
        truncate (int, optional): Number of layers to truncate from the end if `unwrap` is True. Default is 2.
        split (bool, optional): Returns output from intermediate child modules as list. Default is False.

    Attributes:
        m (nn.Module): The loaded torchvision model, possibly truncated and unwrapped.
    ÚDEFAULTTrS   FÚmodelÚstrÚweightsÚunwrapr‘   Útruncater8   rä   c                óÂ  •— ddl }t          ¦   «                              ¦   «          t          |j        d¦  «        r"|j                             ||¬¦  «        | _        n. |j        j        |         t          |¦  «        ¬¦  «        | _        |r¢t          | j         
                    ¦   «         ¦  «        }t          |d         t          j        ¦  «        r3g t          |d          
                    ¦   «         ¦  «        ¢|dd…         ¢}t          j        |r|d| …         n|Ž | _        || _        dS d| _        t          j        ¦   «         x| j        _        | j        _        dS )ae  Load the model and weights from torchvision.

        Args:
            model (str): Name of the torchvision model to load.
            weights (str): Pre-trained weights to load.
            unwrap (bool): Whether to unwrap the model.
            truncate (int): Number of layers to truncate.
            split (bool): Whether to split the output.
        r   NÚ	get_model)r  )Ú
pretrainedr   F)Útorchvisionr>   r?   r×  Úmodelsr  rž   Ú__dict__r‘   rà   ÚchildrenÚ
isinstancer   rÂ   rä   rø   ÚheadÚheads)	rJ   r  r  r  r  rä   r  ÚlayersrL   s	           €rM   r?   zTorchVision.__init__C  s;  ø€ ð 	ÐÐÐå‰Œ×ÒÑÔÐÝ�;Ô% {Ñ3Ô3ð 	RØ Ô'×1Ò1°%ÀÐ1ÑIÔIˆDŒFˆFà7�[Ô'Ô0°Ô7Å4ÈÁ=Ä=ÐQÑQÔQˆDŒFØð 	7Ý˜$œ&Ÿ/š/Ñ+Ô+Ñ,Ô,ˆFÝ˜& œ)¥R¤]Ñ3Ô3ð DØC�4  q¤	× 2Ò 2Ñ 4Ô 4Ñ5Ô5ÐC¸¸q¸r¸r¼
ÐC�Ý”]¸8Ð%O V¨J¨h¨Y¨JÔ%7Ð%7ÈÐQˆDŒFØˆDŒJˆJˆJàˆDŒJÝ)+¬©¬Ð6ˆDŒFŒK˜$œ&œ,˜,˜,rN   rK   rO   rP   c                ó”   ‡— | j         r*|gŠ‰                     ˆfd„| j        D ¦   «         ¦  «         n|                      |¦  «        Š‰S )zÈForward pass through the model.

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

        Returns:
            (torch.Tensor | list[torch.Tensor]): Output tensor or list of tensors.
        c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z&TorchVision.forward.<locals>.<genexpr>k  s/   øè è € Ð.Ð. !�Q�Q�q˜”u‘X”XÐ.Ð.Ð.Ð.Ð.Ð.rN   )rä   r©   rž   rª   s     @rM   rZ   zTorchVision.forward`  sV   ø€ ð Œ:ð 	Ø�ˆAØ�HŠHÐ.Ð.Ð.Ð. t¤vÐ.Ñ.Ô.Ñ.Ô.Ð.Ð.à—’�q‘	”	ˆAØˆrN   )r  TrS   F)
r  r  r  r  r  r‘   r  r8   rä   r‘   r[   r\   rb   s   @rM   r4   r4   2  sg   ø€ € € € € ðð ð" kpð7ð 7ð 7ð 7ð 7ð 7ð 7ð:ð ð ð ð ð ð ð rN   r4   c                  ó8   ‡ — e Zd ZdZddˆ fd„Zˆ fd„Zdd„Zˆ xZS )ÚAAttnaÏ  Area-attention module for YOLO models, providing efficient attention mechanisms.

    This module implements an area-based attention mechanism that processes input features in a spatially-aware manner,
    making it particularly effective for object detection tasks.

    Attributes:
        area (int): Number of areas the feature map is divided into.
        num_heads (int): Number of heads into which the attention mechanism is divided.
        head_dim (int): Dimension of each attention head.
        qkv (Conv): Convolution layer for computing query, key and value tensors.
        proj (Conv): Projection convolution layer.
        pe (Conv): Position encoding convolution layer.

    Methods:
        forward: Applies area-attention to input tensor.

    Examples:
        >>> attn = AAttn(dim=256, num_heads=8, area=4)
        >>> x = torch.randn(1, 256, 32, 32)
        >>> output = attn(x)
        >>> print(output.shape)
        torch.Size([1, 256, 32, 32])
    r   r‡   r8   rÇ  Úareac           	     ó:  •— t          ¦   «                              ¦   «          || _        || _        ||z  x| _        }|| j        z  x| _        }t          ||dz  dd¬¦  «        | _        t          ||dd¬¦  «        | _        t          ||ddd|d¬¦  «        | _	        dS )a#  Initialize an Area-attention module for YOLO models.

        Args:
            dim (int): Number of hidden channels.
            num_heads (int): Number of heads into which the attention mechanism is divided.
            area (int): Number of areas the feature map is divided into.
        ri   r   Frv   r&  rÒ  N)
r>   r?   r%  rÇ  rê  Úall_head_dimr   rì  r^  rí  )rJ   r‡   rÇ  r%  rê  r'  rL   s         €rM   r?   zAAttn.__init__Š  s¦   ø€ õ 	‰Œ×ÒÑÔÐØˆŒ	à"ˆŒØ#&¨)Ñ#3Ð3ˆŒ˜Ø+3°d´nÑ+DÐDˆÔ˜Lå˜˜\¨AÑ-¨q°eÐ<Ñ<Ô<ˆŒÝ˜ s¨A°5Ð9Ñ9Ô9ˆŒ	Ý�| \°1°a¸¸lÐPUÐVÑVÔVˆŒˆˆrN   c                ó–   •— t          ¦   «                              |¦  «         t          | d¦  «        s| j        | j        z  | _        dS dS )z6Add missing all_head_dim attribute to old checkpoints.r'  N)r>   Ú__setstate__r×  rê  rÇ  r'  )rJ   ÚstaterL   s     €rM   r)  zAAttn.__setstate__�  sN   ø€ å‰Œ×Ò˜UÑ#Ô#Ð#Ý�t˜^Ñ,Ô,ð 	?Ø $¤°´Ñ >ˆDÔÐÐð	?ð 	?rN   rK   rO   rP   c                óB  — |j         \  }}}}||z  }|                      |¦  «                             d¦  «                             dd¦  «        }| j        dk    r:|                     || j        z  || j        z  | j        dz  ¦  «        }|j         \  }}}|                     ||| j        | j	        dz  ¦  «         
                    dddd¦  «                             | j	        | j	        | j	        gd¬¦  «        \  }}	}
|| j	        dz  z                       dd¦  «        |	z  }|                     d¬¦  «        }|
|                     dd¦  «        z  }| 
                    dddd¦  «        }|
 
                    dddd¦  «        }
| j        dk    rc|                     || j        z  || j        z  | j        ¦  «        }|
                     || j        z  || j        z  | j        ¦  «        }
|j         \  }}}|                     |||| j        ¦  «         
                    dddd¦  «                             ¦   «         }|
                     |||| j        ¦  «         
                    dddd¦  «                             ¦   «         }
||                      |
¦  «        z   }|                      |¦  «        S )	zÊProcess the input tensor through the area-attention.

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

        Returns:
            (torch.Tensor): Output tensor after area-attention.
        rS   r   ri   r   r†   ré  rò  r¥   )rT   rì  ÚflattenrU   r%  ri  r'  rG   rÇ  rê  Úpermuterä   rV   Ú
contiguousrí  r^  )rJ   rK   rô  rX   rö  r÷  rø  rì  rj  rk   rk  rH  s               rM   rZ   zAAttn.forward£  s]  € ð ”W‰
ˆˆ1ˆa�Ø�‰Eˆà�hŠh�q‰kŒk×!Ò! !Ñ$Ô$×.Ò.¨q°!Ñ4Ô4ˆØŒ9�qŠ=ˆ=Ø—+’+˜a $¤)™m¨Q°$´)©^¸TÔ=NÐQRÑ=RÑSÔSˆCØ”i‰GˆAˆq�!à�HŠH�Q˜˜4œ>¨4¬=¸1Ñ+<Ñ=Ô=ßŠW�Q˜˜1˜aÑ Ô ßŠU�D”M 4¤=°$´-Ð@ÀaˆUÑHÔHñ 	ˆˆ1ˆað
 �T”] DÑ(Ñ)×4Ò4°R¸Ñ<Ô<¸qÑ@ˆØ�|Š| ˆ|Ñ#Ô#ˆØ�—’˜r 2Ñ&Ô&Ñ&ˆØ�IŠI�a˜˜A˜qÑ!Ô!ˆØ�IŠI�a˜˜A˜qÑ!Ô!ˆàŒ9�qŠ=ˆ=Ø—	’	˜!˜tœy™.¨!¨d¬i©-¸Ô9JÑKÔKˆAØ—	’	˜!˜tœy™.¨!¨d¬i©-¸Ô9JÑKÔKˆAØ”g‰GˆAˆq�!à�IŠI�a˜˜A˜tÔ0Ñ1Ô1×9Ò9¸!¸QÀÀ1ÑEÔE×PÒPÑRÔRˆØ�IŠI�a˜˜A˜tÔ0Ñ1Ô1×9Ò9¸!¸QÀÀ1ÑEÔE×PÒPÑRÔRˆà�—’˜‘
”
‰NˆØ�yŠy˜‰|Œ|ÐrN   rÄ   )r‡   r8   rÇ  r8   r%  r8   r[   )r]   r^   r_   r`   r?   r)  rZ   ra   rb   s   @rM   r$  r$  q  s€   ø€ € € € € ðð ð0Wð Wð Wð Wð Wð Wð Wð&?ð ?ð ?ð ?ð ?ð$ð $ð $ð $ð $ð $ð $ð $rN   r$  c                  óF   ‡ — e Zd ZdZddˆ fd
„Zedd„¦   «         Zdd„Zˆ xZS )ÚABlocka§  Area-attention block module for efficient feature extraction in YOLO models.

    This module implements an area-attention mechanism combined with a feed-forward network for processing feature maps.
    It uses a novel area-based attention approach that is more efficient than traditional self-attention while
    maintaining effectiveness.

    Attributes:
        attn (AAttn): Area-attention module for processing spatial features.
        mlp (nn.Sequential): Multi-layer perceptron for feature transformation.

    Methods:
        _init_weights: Initializes module weights using truncated normal distribution.
        forward: Applies area-attention and feed-forward processing to input tensor.

    Examples:
        >>> block = ABlock(dim=256, num_heads=8, mlp_ratio=1.2, area=1)
        >>> x = torch.randn(1, 256, 32, 32)
        >>> output = block(x)
        >>> print(output.shape)
        torch.Size([1, 256, 32, 32])
    ç333333ó?r   r‡   r8   rÇ  Ú	mlp_ratiorE   r%  c           	     óB  •— t          ¦   «                              ¦   «          t          |||¬¦  «        | _        t	          ||z  ¦  «        }t          j        t          ||d¦  «        t          ||dd¬¦  «        ¦  «        | _        |  	                    | j
        ¦  «         dS )aa  Initialize an Area-attention block module.

        Args:
            dim (int): Number of input channels.
            num_heads (int): Number of heads into which the attention mechanism is divided.
            mlp_ratio (float): Expansion ratio for MLP hidden dimension.
            area (int): Number of areas the feature map is divided into.
        )rÇ  r%  r   Frv   N)r>   r?   r$  rH  r8   r   rÂ   r   ÚmlpÚapplyÚ_init_weights)rJ   r‡   rÇ  r2  r%  Úmlp_hidden_dimrL   s         €rM   r?   zABlock.__init__á  s�   ø€ õ 	‰Œ×ÒÑÔÐå˜#¨¸Ð>Ñ>Ô>ˆŒ	Ý˜S 9™_Ñ-Ô-ˆÝ”=¥ c¨>¸1Ñ!=Ô!=½tÀNÐTWÐYZÐ`eÐ?fÑ?fÔ?fÑgÔgˆŒà�
Š
�4Ô%Ñ&Ô&Ð&Ð&Ð&rN   rž   ú	nn.Modulec                óæ   — t          | t          j        ¦  «        rTt          j                             | j        d¬¦  «         | j        �)t          j                             | j        d¦  «         dS dS dS )z‚Initialize weights using a truncated normal distribution.

        Args:
            m (nn.Module): Module to initialize.
        g{®Gáz”?)ÚstdNr   )r  r   r@   ÚinitÚtrunc_normal_rH   r;   Ú	constant_r¼   s    rM   r6  zABlock._init_weightsò  sm   € õ �a�œÑ#Ô#ð 	-ÝŒG×!Ò! !¤(°Ð!Ñ5Ô5Ð5ØŒvÐ!Ý”×!Ò! !¤&¨!Ñ,Ô,Ð,Ð,Ð,ð	-ð 	-à!Ð!rN   rK   rO   rP   c                ób   — ||                       |¦  «        z   }||                      |¦  «        z   S )zÎForward pass through ABlock.

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

        Returns:
            (torch.Tensor): Output tensor after area-attention and feed-forward processing.
        )rH  r4  rr   s     rM   rZ   zABlock.forwardþ  s,   € ð �—	’	˜!‘”ÑˆØ�4—8’8˜A‘;”;‰ÐrN   )r1  r   )r‡   r8   rÇ  r8   r2  rE   r%  r8   )rž   r8  r[   )	r]   r^   r_   r`   r?   rƒ  r6  rZ   ra   rb   s   @rM   r0  r0  Ê  s€   ø€ € € € € ðð ð,'ð 'ð 'ð 'ð 'ð 'ð 'ð" ð	-ð 	-ð 	-ñ „\ð	-ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
rN   r0  c                  ó>   ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 ddˆ fd„Zdd„Zˆ xZS )ÚA2C2faê  Area-Attention C2f module for enhanced feature extraction with area-based attention mechanisms.

    This module extends the C2f architecture by incorporating area-attention and ABlock layers for improved feature
    processing. It supports both area-attention and standard convolution modes.

    Attributes:
        cv1 (Conv): Initial 1x1 convolution layer that reduces input channels to hidden channels.
        cv2 (Conv): Final 1x1 convolution layer that processes concatenated features.
        gamma (nn.Parameter | None): Learnable parameter for residual scaling when using area attention.
        m (nn.ModuleList): List of either ABlock or C3k modules for feature processing.

    Methods:
        forward: Processes input through area-attention or standard convolution pathway.

    Examples:
        >>> m = A2C2f(512, 512, n=1, a2=True, area=1)
        >>> x = torch.randn(1, 512, 32, 32)
        >>> output = m(x)
        >>> print(output.shape)
        torch.Size([1, 512, 32, 32])
    r   TFç       @rÆ   r7   r8   rg   r�   Úa2r‘   r%  Úresidualr2  rE   rÈ   rÇ   r’   c                óÞ  •‡‡‡‡	‡
‡— t          ¦   «                              ¦   «          t          ||z  ¦  «        Š‰dz  dk    s
J d¦   «         ‚t          |‰dd¦  «        | _        t          d|z   ‰z  |d¦  «        | _        ‰r-|r+t          j        dt          j	        |¦  «        z  d¬¦  «        nd| _
        t          j        ˆˆˆˆ	ˆˆ
fd	„t          |¦  «        D ¦   «         ¦  «        | _        dS )
a  Initialize Area-Attention C2f module.

        Args:
            c1 (int): Number of input channels.
            c2 (int): Number of output channels.
            n (int): Number of ABlock or C3k modules to stack.
            a2 (bool): Whether to use area attention blocks. If False, uses C3k blocks instead.
            area (int): Number of areas the feature map is divided into.
            residual (bool): Whether to use residual connections with learnable gamma parameter.
            mlp_ratio (float): Expansion ratio for MLP hidden dimension.
            e (float): Channel expansion ratio for hidden channels.
            g (int): Number of groups for grouped convolutions.
            shortcut (bool): Whether to use shortcut connections in C3k blocks.
        re   r   z-Dimension of ABlock must be a multiple of 32.r   ç{®Gáz„?TrT  Nc              3  ó”   •K  — | ]B}‰r)t          j        ˆˆˆfd „t          d¦  «        D ¦   «         Ž nt          ‰‰d‰‰¦  «        V — ŒCdS )c              3  óB   •K  — | ]}t          ‰‰d z  ‰‰¦  «        V — ŒdS )re   N)r0  )r˜   rX   r%  rf   r2  s     €€€rM   r›   z+A2C2f.__init__.<locals>.<genexpr>.<genexpr>F  s5   øè è € ÐTÐTÀa�F 2 r¨R¡x°¸DÑAÔAÐTÐTÐTÐTÐTÐTrN   rS   N)r   rÂ   r�   rÉ  )r˜   rX   rB  r%  rf   rÇ   r2  r’   s     €€€€€€rM   r›   z!A2C2f.__init__.<locals>.<genexpr>E  s   øè è € ð 
ð 
ð ð ð-�BŒMÐTÐTÐTÐTÐTÐTÍ5ÐQRÉ8Ì8ÐTÑTÔTÐUÐUå�R˜˜Q ¨!Ñ,Ô,ð
ð 
ð 
ð 
ð 
ð 
rN   )r>   r?   r8   r   rl   ro   r   rF   rC   r8  Úgammarœ   r�   rž   )rJ   r7   rg   r�   rB  r%  rC  r2  rÈ   rÇ   r’   rf   rL   s       `` ` ``@€rM   r?   zA2C2f.__init__"  s
  øøøøøøø€ õ6 	‰Œ×ÒÑÔÐÝ��a‘‰[Œ[ˆØ�B‰w˜!Š|ˆ|ˆ|ÐL‰|Œ|ˆ|å˜˜B  1Ñ%Ô%ˆŒÝ˜˜Q™ "™ b¨!Ñ,Ô,ˆŒàPRÐiÐW_Ði•R”\ $­¬°B©¬Ñ"7ÀtÐLÑLÔLÐLÐeiˆŒ
Ý”ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
ð 
õ ˜1‘X”Xð	
ñ 
ô 
ñ 
ô 
ˆŒˆˆrN   rK   rO   rP   c                óD  ‡— |                       |¦  «        gŠ‰                     ˆfd„| j        D ¦   «         ¦  «         |                      t	          j        ‰d¦  «        ¦  «        Š| j        �3|| j                             d| j        j        d         dd¦  «        ‰z  z   S ‰S )z³Forward pass through A2C2f layer.

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

        Returns:
            (torch.Tensor): Output tensor after processing.
        c              3  ó:   •K  — | ]} |‰d          ¦  «        V — ŒdS r¤   r—   r¦   s     €rM   r›   z A2C2f.forward.<locals>.<genexpr>V  r¨   rN   r   Nr¥   r   )	rl   r©   rž   ro   rC   rŠ   rH  rG   rT   rª   s     @rM   rZ   zA2C2f.forwardL  s•   ø€ ð �XŠX�a‰[Œ[ˆMˆØ	�ŠÐ*Ð*Ð*Ð* 4¤6Ð*Ñ*Ô*Ñ*Ô*Ð*Ø�HŠH•U”Y˜q !‘_”_Ñ%Ô%ˆØŒ:Ð!Ø�t”z—’ r¨4¬:Ô+;¸AÔ+>ÀÀ1ÑEÔEÈÑIÑIÐIØˆrN   )r   Tr   FrA  rÆ   r   T)r7   r8   rg   r8   r�   r8   rB  r‘   r%  r8   rC  r‘   r2  rE   rÈ   rE   rÇ   r8   r’   r‘   r[   r\   rb   s   @rM   r@  r@    s{   ø€ € € € € ðð ð4 ØØØØØØØð(
ð (
ð (
ð (
ð (
ð (
ð (
ðTð ð ð ð ð ð ð rN   r@  c                  ó.   ‡ — e Zd ZdZddˆ fd	„Zdd„Zˆ xZS )Ú	SwiGLUFFNz@SwiGLU Feed-Forward Network for transformer-based architectures.rR   r0  r8   r    rÈ   rP   rÐ  c                óÂ   •— t          ¦   «                              ¦   «          t          j        |||z  ¦  «        | _        t          j        ||z  dz  |¦  «        | _        dS )zâInitialize SwiGLU FFN with input dimension, output dimension, and expansion factor.

        Args:
            gc (int): Guide channels.
            ec (int): Embedding channels.
            e (int): Expansion factor.
        rS   N)r>   r?   r   r4  Úw12Úw3)rJ   r0  r    rÈ   rL   s       €rM   r?   zSwiGLUFFN.__init__`  sQ   ø€ õ 	‰Œ×ÒÑÔÐÝ”9˜R  R¡Ñ(Ô(ˆŒÝ”)˜A ™F a™K¨Ñ,Ô,ˆŒˆˆrN   rK   rO   c                ó¸   — |                       |¦  «        }|                     dd¬¦  «        \  }}t          j        |¦  «        |z  }|                      |¦  «        S )z.Apply SwiGLU transformation to input features.rS   r¥   r†   )rN  rÕ   rˆ   ÚsilurO  )rJ   rK   Úx12rŒ   r‹   Úhiddens         rM   rZ   zSwiGLUFFN.forwardl  sK   € à�hŠh�q‰kŒkˆØ—’˜1 "�Ñ%Ô%‰ˆˆBÝ”˜‘”˜b‘ˆØ�wŠw�v‰ŒÐrN   )rR   )r0  r8   r    r8   rÈ   r8   rP   rÐ  r[   r\   rb   s   @rM   rL  rL  ]  s\   ø€ € € € € ØJÐJð
-ð 
-ð 
-ð 
-ð 
-ð 
-ð 
-ðð ð ð ð ð ð ð rN   rL  c                  ó,   ‡ — e Zd ZdZd
ˆ fd„Zdd	„Zˆ xZS )ÚResidualz7Residual connection wrapper for neural network modules.rž   r8  rP   rÐ  c                ó  •— t          ¦   «                              ¦   «          || _        t          j                             | j        j        j        ¦  «         t          j                             | j        j        j        ¦  «         dS )z�Initialize residual module with the wrapped module.

        Args:
            m (nn.Module): Module to wrap with residual connection.
        N)	r>   r?   rž   r   r;  Úzeros_rO  r;   rH   )rJ   rž   rL   s     €rM   r?   zResidual.__init__w  s_   ø€ õ 	‰Œ×ÒÑÔÐØˆŒÝ
Œ�Š�t”v”y”~Ñ&Ô&Ð&õ 	Œ�Š�t”v”yÔ'Ñ(Ô(Ð(Ð(Ð(rN   rK   rO   c                ó2   — ||                       |¦  «        z   S )z,Apply residual connection to input features.r¼   rr   s     rM   rZ   zResidual.forward„  s   € à�4—6’6˜!‘9”9‰}ÐrN   )rž   r8  rP   rÐ  r[   r\   rb   s   @rM   rU  rU  t  sW   ø€ € € € € ØAÐAð)ð )ð )ð )ð )ð )ðð ð ð ð ð ð ð rN   rU  c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )ÚSAVPEzESpatial-Aware Visual Prompt Embedding module for feature enhancement.rQ  r¦  r   r8   rB  c           	     ó–  •‡— t          ¦   «                              ¦   «          t          j        ˆfd„t	          |¦  «        D ¦   «         ¦  «        | _        t          j        ˆfd„t	          |¦  «        D ¦   «         ¦  «        | _        d| _        t          j        d‰z  |d¦  «        | _	        t          j        d‰z  | j        dd¬¦  «        | _
        t          j        d| j        dd¬¦  «        | _        t          j        t          d| j        z  | j        d¦  «        t          j        | j        | j        dd¬¦  «        ¦  «        | _        dS )	a  Initialize SAVPE module with channels, intermediate channels, and embedding dimension.

        Args:
            ch (list[int]): List of input channel dimensions.
            c3 (int): Intermediate channels.
            embed (int): Embedding dimension.
        c           	   3  óà   •K  — | ]h\  }}t          j        t          |‰d ¦  «        t          ‰‰d ¦  «        |dv rt          j        |dz  ¬¦  «        nt          j        ¦   «         ¦  «        V — ŒidS )ri   ¾   r   rS   rS   ©Úscale_factorN©r   rÂ   r   ÚUpsamplerø   ©r˜   r™   rK   r   s      €rM   r›   z!SAVPE.__init__.<locals>.<genexpr>•  s�   øè è € ð !
ð !
ñ ��1õ ŒMÝ�Q˜˜A‘”¥ R¨¨Q¡¤ÐTUÐY_ÐT_ÐT_µ´È!ÈaÉ%Ð1PÑ1PÔ1PÐ1PÕegÔepÑerÔerñô ð!
ð !
ð !
ð !
ð !
ð !
rN   c              3  óÀ   •K  — | ]X\  }}t          j        t          |‰d ¦  «        |dv rt          j        |dz  ¬¦  «        nt          j        ¦   «         ¦  «        V — ŒYdS )r   r]  rS   r^  Nr`  rb  s      €rM   r›   z!SAVPE.__init__.<locals>.<genexpr>œ  sz   øè è € ð !
ð !
á��1õ ŒM�$˜q " a™.œ.ÈQÐRXÈ[È[­"¬+À1ÀqÁ5Ð*IÑ*IÔ*IÐ*IÕ^`Ô^iÑ^kÔ^kÑlÔlð!
ð !
ð !
ð !
ð !
ð !
rN   r6   ri   r   )rz   rS   N)r>   r?   r   rœ   r·  rl   ro   rÐ   r@   rp   r  r¡  rÂ   r   Úcv6)rJ   rQ  r   rB  rL   s     ` €rM   r?   zSAVPE.__init__Œ  sB  øø€ õ 	‰Œ×ÒÑÔÐÝ”=ð !
ð !
ð !
ð !
õ " "™œð	!
ñ !
ô !
ñ 
ô 
ˆŒõ ”=ð !
ð !
ð !
ð !
å! "™œð!
ñ !
ô !
ñ 
ô 
ˆŒð
 ˆŒÝ”9˜Q ™V U¨AÑ.Ô.ˆŒÝ”9˜Q ™V T¤V¨Q¸Ð:Ñ:Ô:ˆŒÝ”9˜Q ¤¨°1Ð5Ñ5Ô5ˆŒÝ”=¥ a¨$¬&¡j°$´&¸!Ñ!<Ô!<½b¼iÈÌÐPTÔPVÐXYÐcdÐ>eÑ>eÔ>eÑfÔfˆŒˆˆrN   rK   rc  ÚvprO   rP   c                óR  ‡ — ˆ fd„t          |¦  «        D ¦   «         }‰                      t          j        |d¬¦  «        ¦  «        }ˆ fd„t          |¦  «        D ¦   «         }‰                      t          j        |d¬¦  «        ¦  «        }|j        \  }}}}|j        d         }|                     ||d¦  «        }|                     |d‰ j        ||¦  «         	                    d|ddd¦  «                             ||z  ‰ j        ||¦  «        }|                     ||d||¦  «                             ||z  d||¦  «        }‰  
                    t          j        |‰                      |¦  «        fd¬¦  «        ¦  «        }|                     ||‰ j        d¦  «        }|                     ||dd¦  «        }||z  t          j        |¦  «        t          j        |j        ¦  «        j        z  z   }	t!          j        |	d¬¦  «                             |j        ¦  «        }	|	                     dd¦  «        |                     |‰ j        |‰ j        z  d¦  «                             dd¦  «        z  }
t!          j        |
                     dd¦  «                             ||d¦  «        dd¬	¦  «        S )
zJProcess input features and visual prompts to generate enhanced embeddings.c                óB   •— g | ]\  }} ‰j         |         |¦  «        ‘ŒS r—   )ro   ©r˜   r™   ÚxirJ   s      €rM   r´   z!SAVPE.forward.<locals>.<listcomp>©  ó+   ø€ Ð7Ð7Ð7¡  Bˆ[ˆTŒX�aŒ[˜‰_Œ_Ð7Ð7Ð7rN   r   r†   c                óB   •— g | ]\  }} ‰j         |         |¦  «        ‘ŒS r—   )rl   rh  s      €rM   r´   z!SAVPE.forward.<locals>.<listcomp>¬  rj  rN   r¥   rò  éýÿÿÿrS   rs  )r·  r  rC   rŠ   rp   rT   rG   ri  rÐ   Úexpandrd  r¡  Úlogical_notÚfinfor=   Úminrˆ   rV   ÚtorU   ru  )rJ   rK   re  r§   rô  rõ  rö  r÷  ÚQÚscoreÚ
aggregateds   `          rM   rZ   zSAVPE.forward§  s`  ø€ à7Ð7Ð7Ð7­)°A©,¬,Ð7Ñ7Ô7ˆØ�HŠH•U”Y˜q aÐ(Ñ(Ô(Ñ)Ô)ˆà7Ð7Ð7Ð7­)°A©,¬,Ð7Ñ7Ô7ˆØ�HŠH•U”Y˜q aÐ(Ñ(Ô(Ñ)Ô)ˆà”W‰
ˆˆ1ˆa�àŒH�QŒKˆà�FŠF�1�a˜ÑÔˆà�IŠI�a˜˜DœF A qÑ)Ô)×0Ò0°°Q¸¸BÀÑCÔC×KÒKÈAÐPQÉEÐSWÔSYÐ[\Ð^_Ñ`Ô`ˆØ�ZŠZ˜˜1˜a  AÑ&Ô&×.Ò.¨q°1©u°a¸¸AÑ>Ô>ˆà�HŠH•U”Y  4§8¢8¨B¡<¤<Ð0°aÐ8Ñ8Ô8Ñ9Ô9ˆà�IŠI�a˜˜DœF BÑ'Ô'ˆØ�ZŠZ˜˜1˜a Ñ$Ô$ˆà�B‘�Ô*¨2Ñ.Ô.µ´¸Q¼WÑ1EÔ1EÔ1IÑIÑIˆÝ”	˜% RÐ(Ñ(Ô(×+Ò+¨A¬GÑ4Ô4ˆØ—_’_ R¨Ñ,Ô,¨q¯yªy¸¸D¼FÀAÈÌÁKÐQSÑ/TÔ/T×/^Ò/^Ð_aÐceÑ/fÔ/fÑfˆ
åŒ{˜:×/Ò/°°BÑ7Ô7×?Ò?ÀÀ1ÀbÑIÔIÈrÐUVÐWÑWÔWÐWrN   )rQ  r¦  r   r8   rB  r8   )rK   rc  re  rO   rP   rO   r\   rb   s   @rM   rZ  rZ  ‰  se   ø€ € € € € ØOÐOðgð gð gð gð gð gð6Xð Xð Xð Xð Xð Xð Xð XrN   rZ  c                  ó:   ‡ — e Zd ZdZddˆ fd„Zddˆ fd„Zd„ Zˆ xZS )ÚProto26zDUltralytics YOLO26 models mask Proto module for segmentation models.r—   rd   re   éP   rQ  Útuplerf   r8   rg   Úncc           	     óŽ  •‡— t          ¦   «                              |||¦  «         t          j        ˆfd„‰dd…         D ¦   «         ¦  «        | _        t          ‰d         |d¬¦  «        | _        t          j        t          ‰d         |d¬¦  «        t          ||d¬¦  «        t          j        ||d¦  «        ¦  «        | _	        dS )ap  Initialize the Ultralytics YOLO models mask Proto module with specified number of protos and masks.

        Args:
            ch (tuple): Tuple of channel sizes from backbone feature maps.
            c_ (int): Intermediate channels.
            c2 (int): Output channels (number of protos).
            nc (int): Number of classes for semantic segmentation.
        c              3  óH   •K  — | ]}t          |‰d          d¬¦  «        V — ŒdS )r   r   rj   NrÁ   )r˜   rK   rQ  s     €rM   r›   z#Proto26.__init__.<locals>.<genexpr>Ñ  s6   øè è € Ð(MÐ(MÀ­¨a°°A´¸!Ð)<Ñ)<Ô)<Ð(MÐ(MÐ(MÐ(MÐ(MÐ(MrN   r   Nr   ri   rj   )
r>   r?   r   rœ   Úfeat_refiner   Ú	feat_fuserÂ   r@   Úsemseg)rJ   rQ  rf   rg   ry  rL   s    `   €rM   r?   zProto26.__init__Ç  s·   øø€ õ 	‰Œ×Ò˜˜R Ñ$Ô$Ð$Ýœ=Ð(MÐ(MÐ(MÐ(MÀbÈÈÈÄfÐ(MÑ(MÔ(MÑMÔMˆÔÝ˜b œe R¨1Ð-Ñ-Ô-ˆŒÝ”m¥D¨¨A¬°°aÐ$8Ñ$8Ô$8½$¸rÀ2ÈÐ:KÑ:KÔ:KÍRÌYÐWYÐ[]Ð_`ÑMaÔMaÑbÔbˆŒˆˆrN   TrK   rO   Úreturn_semanticr‘   rP   c                ód  •— |d         }t          | j        ¦  «        D ];\  }} |||dz            ¦  «        }t          j        |d|dz   z  d¬¦  «        }||z   }Œ<t	          ¦   «                              |                      |¦  «        ¦  «        }| j        r|r|                      |¦  «        }||fS |S )zUPerform a forward pass by fusing multi-scale feature maps and generating proto masks.r   r   rS   r²  )r_  r´  )	r·  r|  rˆ   rµ  r>   rZ   r}  Útrainingr~  )
rJ   rK   r  Úfeatr™   ÚfÚup_featr  ÚsemanticrL   s
            €rM   rZ   zProto26.forwardÕ  sº   ø€ à�ŒtˆÝ˜dÔ.Ñ/Ô/ð 	"ð 	"‰DˆAˆqØ�a˜˜!˜a™%œ‘k”kˆGå”m G¸!ÀÀAÁ¹,ÈYÐWÑWÔWˆGØ˜'‘>ˆDˆDÝ‰GŒG�OŠO˜DŸNšN¨4Ñ0Ô0Ñ1Ô1ˆØŒ=ð 	!˜_ð 	!Ø—{’{ 4Ñ(Ô(ˆHØ�x�=Ð ØˆrN   c                ó   — d| _         dS )zHFuse the model for inference by removing the semantic segmentation head.N)r~  r  s    rM   r€  zProto26.fuseã  s   € àˆŒˆˆrN   )r—   rd   re   rw  )rQ  rx  rf   r8   rg   r8   ry  r8   )T)rK   rO   r  r‘   rP   rO   )r]   r^   r_   r`   r?   rZ   r€  ra   rb   s   @rM   rv  rv  Ä  s�   ø€ € € € € ØNÐNðcð cð cð cð cð cð cðð ð ð ð ð ð ðð ð ð ð ð ð rN   rv  c                  ó`   ‡ — e Zd ZdZed„ ¦   «         Zed„ ¦   «         Zˆ fd„Zd„ Zd„ Z	d„ Z
ˆ xZS )ÚRealNVPz¼RealNVP: a flow-based generative model.

    References:
        https://arxiv.org/abs/1605.08803
        https://github.com/open-mmlab/mmpose/blob/main/mmpose/models/utils/realnvp.py
    c            
     ó  — t          j        t          j        dd¦  «        t          j        ¦   «         t          j        dd¦  «        t          j        ¦   «         t          j        dd¦  «        t          j        ¦   «         ¦  «        S )z3Get the scale model in a single invertible mapping.rS   rÄ  )r   rÂ   r4  r  ÚTanhr—   rN   rM   ÚnetszRealNVP.netsð  s\   € õ Œ}�RœY q¨"Ñ-Ô-­r¬w©y¬y½"¼)ÀBÈÑ:KÔ:KÍRÌWÉYÌYÕXZÔXaÐbdÐfgÑXhÔXhÕjlÔjqÑjsÔjsÑtÔtÐtrN   c            
     óè   — t          j        t          j        dd¦  «        t          j        ¦   «         t          j        dd¦  «        t          j        ¦   «         t          j        dd¦  «        ¦  «        S )z9Get the translation model in a single invertible mapping.rS   rÄ  )r   rÂ   r4  r  r—   rN   rM   ÚnettzRealNVP.nettõ  sP   € õ Œ}�RœY q¨"Ñ-Ô-­r¬w©y¬y½"¼)ÀBÈÑ:KÔ:KÍRÌWÉYÌYÕXZÔXaÐbdÐfgÑXhÔXhÑiÔiÐirN   c                óÈ  •‡ — t          ¦   «                              ¦   «          ‰                      dt          j        d¦  «        ¦  «         ‰                      dt          j        d¦  «        ¦  «         ‰                      dt          j        ddgddggdz  t          j        ¬¦  «        ¦  «         t          j         	                    ˆ fd	„t          t          ‰ j        ¦  «        ¦  «        D ¦   «         ¦  «        ‰ _        t          j         	                    ˆ fd
„t          t          ‰ j        ¦  «        ¦  «        D ¦   «         ¦  «        ‰ _        ‰                      ¦   «          d S )NÚlocrS   ÚcovÚmaskr   r   ri   r<   c                ó8   •— g | ]}‰                      ¦   «         ‘ŒS r—   )r‹  r  s     €rM   r´   z$RealNVP.__init__.<locals>.<listcomp>  ó!   ø€ Ð%QÐ%QÐ%Q°a d§i¢i¡k¤kÐ%QÐ%QÐ%QrN   c                ó8   •— g | ]}‰                      ¦   «         ‘ŒS r—   )r�  r  s     €rM   r´   z$RealNVP.__init__.<locals>.<listcomp>  r“  rN   )r>   r?   Úregister_bufferrC   r6  Úeyer_  Úfloat32r   rœ   r�   rµ   r‘  r  ÚtÚinit_weightsrq  s   `€rM   r?   zRealNVP.__init__ú  s!  øø€ Ý‰Œ×ÒÑÔÐð 	×Ò˜U¥E¤K°¡N¤NÑ3Ô3Ð3Ø×Ò˜U¥E¤I¨a¡L¤LÑ1Ô1Ð1Ø×Ò˜V¥U¤\°A°q°6¸A¸q¸6Ð2BÀQÑ2FÍeÌmÐ%\Ñ%\Ô%\Ñ]Ô]Ð]å”×$Ò$Ð%QÐ%QÐ%QÐ%Q½5ÅÀTÄYÁÄÑ;PÔ;PÐ%QÑ%QÔ%QÑRÔRˆŒÝ”×$Ò$Ð%QÐ%QÐ%QÐ%Q½5ÅÀTÄYÁÄÑ;PÔ;PÐ%QÑ%QÔ%QÑRÔRˆŒØ×ÒÑÔÐÐÐrN   c                ó´   — |                       ¦   «         D ]B}t          |t          j        ¦  «        r&t          j                             |j        d¬¦  «         ŒCdS )zInitialize model weights.rE  )ÚgainN)Úmodulesr  r   r4  r;  Úxavier_uniform_rH   )rJ   rž   s     rM   r™  zRealNVP.init_weights  sU   € à—’‘”ð 	=ð 	=ˆAÝ˜!�RœYÑ'Ô'ð =Ý”×'Ò'¨¬°tÐ'Ñ<Ô<Ð<øð	=ð 	=rN   c                óü  — |                      |j        d         ¦  «        |}}t          t          t	          | j        ¦  «        ¦  «        ¦  «        D ]¨}| j        |         |z  } | j        |         |¦  «        d| j        |         z
  z  } | j        |         |¦  «        d| j        |         z
  z  }d| j        |         z
  ||z
  z  t          j	        | ¦  «        z  |z   }|| 
                    d¬¦  «        z  }Œ©||fS )z€Apply mapping from the data space to the latent space and calculate the log determinant of the Jacobian
        matrix.
        r   r   r†   )Ú	new_zerosrT   Úreversedr�   rµ   r˜  r‘  r  rC   rv  rª  )rJ   rK   Úlog_det_jacobÚzr™   Úz_r  r˜  s           rM   Ú
backward_pzRealNVP.backward_p  sì   € ð Ÿ;š; q¤w¨q¤zÑ2Ô2°A�qˆÝ�%¥ D¤F¡¤Ñ,Ô,Ñ-Ô-ð 	*ð 	*ˆAØ”˜1” Ñ!ˆBØ�”�q”	˜"‘”  T¤Y¨q¤\Ñ!1Ñ2ˆAØ�”�q”	˜"‘”  T¤Y¨q¤\Ñ!1Ñ2ˆAØ�T”Y˜q”\Ñ! a¨!¡eÑ,­u¬y¸!¸©}¬}Ñ<¸rÑAˆAØ˜QŸUšU q˜U™\œ\Ñ)ˆMˆMØ�-ÐÐrN   c                ó~  — |j         t          j        k    r?| j        d         d         j        j         t          j        k    r|                      ¦   «          |                      |¦  «        \  }}d|                     ¦   «         dz                       d¦  «        z  t          j	        dt          j
        z  ¦  «        z
  |z   S )z<Calculate the log probability of given sample in data space.r   ré  rS   r¥   )r=   rC   r—  r  rH   rE   r¤  rª  Úmathro  Úpi)rJ   rK   r¢  Úlog_dets       rM   Úlog_probzRealNVP.log_prob  sŽ   € àŒ7•e”mÒ#Ð#¨¬¨q¬	°!¬Ô(;Ô(AÅUÄ]Ò(RÐ(RØ�JŠJ‰LŒLˆLØ—_’_ QÑ'Ô'‰
ˆˆ7à�q—w’w‘y”y A‘~×*Ò*¨2Ñ.Ô.Ñ.µ´¸!½d¼g¹+Ñ1FÔ1FÑFÈÑPÐPrN   )r]   r^   r_   r`   rƒ  r‹  r�  r?   r™  r¤  r©  ra   rb   s   @rM   rˆ  rˆ  è  s²   ø€ € € € € ðð ð ðuð uñ „\ðuð ðjð jñ „\ðjðð ð ð ð ð=ð =ð =ð ð  ð  ðQð Qð Qð Qð Qð Qð QrN   rˆ  )Lr`   Ú
__future__r   r¦  rC   Útorch.nn.functionalr   r˜  rˆ   Úultralytics.utils.torch_utilsr   rB   r   r   r	   r
   r   r   Útransformerr   Ú__all__ÚModuler   r.   r,   r+   r   r   r   r   r    r   r&   r/   r   r$   r*   r   r   r  r2   r,  r!   r-   r)   r   r…  rˆ  r0   r   r   r   r   r(   r'   r»  r%   rÉ  r1   r   r"   r   rÈ  r   r   r#   r3   r4   r$  r0  r@  rL  rU  rZ  rv  rˆ  r—   rN   rM   ú<module>r°     s�
  ðà Ð à "Ð "Ð "Ð "Ð "Ð "à €€€à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à :Ð :Ð :Ð :Ð :Ð :à FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ )Ð )Ð )Ð )Ð )Ð )ð(€ðV\ð \ð \ð \ð \ˆ"Œ)ñ \ô \ð \ð2>ð >ð >ð >ð >ˆBŒIñ >ô >ð >ð,!ð !ð !ð !ð !ˆRŒYñ !ô !ð !ðH*(ð *(ð *(ð *(ð *(ˆbŒiñ *(ô *(ð *(ðZDð Dð Dð Dð Dˆ"Œ)ñ Dô Dð Dð.;ð ;ð ;ð ;ð ;ˆ2Œ9ñ ;ô ;ð ;ð@ð ð ð ð ˆŒñ ô ð ð*6ð 6ð 6ð 6ð 6ˆŒñ 6ô 6ð 6ð6)ð )ð )ð )ð )ˆ"Œ)ñ )ô )ð )ðDJð Jð Jð Jð JˆŒñ Jô Jð Jð4xð xð xð xð xˆ"ñ xô xð xð&;ð ;ð ;ð ;ð ;ˆBŒIñ ;ô ;ð ;ð00ð 0ð 0ð 0ð 0ˆ2ñ 0ô 0ð 0ð&Mð Mð Mð Mð Mˆbñ Mô Mð Mð&/ð /ð /ð /ð /�b”iñ /ô /ð /ð8Pð Pð Pð Pð P�”ñ Pô Pð Pð6Cð Cð Cð Cð C�B”Iñ Cô Cð Cð>Jð Jð Jð Jð J�"”)ñ Jô Jð Jð0ð ð ð ð �"”)ñ ô ð ð>1$ð 1$ð 1$ð 1$ð 1$˜"œ)ñ 1$ô 1$ð 1$ðh?)ð ?)ð ?)ð ?)ð ?)ˆbŒiñ ?)ô ?)ð ?)ðD>%ð >%ð >%ð >%ð >%�r”yñ >%ô >%ð >%ðB6ð 6ð 6ð 6ð 6�b”iñ 6ô 6ð 6ð4.6ð .6ð .6ð .6ð .6˜œ	ñ .6ô .6ð .6ðb,ð ,ð ,ð ,ð ,�Jñ ,ô ,ð ,ð*_ð _ð _ð _ð _ˆRñ _ô _ð _ð&)ð )ð )ð )ð )�2”9ñ )ô )ð )ðB1ð 1ð 1ð 1ð 1ˆLñ 1ô 1ð 1ð(ð ð ð ð ˆBŒIñ ô ð ð&&ð &ð &ð &ð &ˆBŒIñ &ô &ð &ð2)ð )ð )ð )ð )ˆbŒiñ )ô )ð )ð63ð 3ð 3ð 3ð 3ˆrŒyñ 3ô 3ð 3ð.<ð <ð <ð <ð <ˆRŒYñ <ô <ð <ð4)ð )ð )ð )ð )ˆ"Œ)ñ )ô )ð )ð8%
ð %
ð %
ð %
ð %
ˆ3ñ %
ô %
ð %
ðPfð fð fð fð fˆ"ñ fô fð fð*>ð >ð >ð >ð >ˆuŒxŒñ >ô >ð >ðB*<ð *<ð *<ð *<ð *<ˆ"Œ)ñ *<ô *<ð *<ðZ^ð ^ð ^ð ^ð ^ˆSñ ^ô ^ð ^ð>>ð >ð >ð >ð >�”	ñ >ô >ð >ðB/ð /ð /ð /ð /ˆrŒyñ /ô /ð /ðd4.ð 4.ð 4.ð 4.ð 4.ˆ"Œ)ñ 4.ô 4.ð 4.ðn4.ð 4.ð 4.ð 4.ð 4.ˆBŒIñ 4.ô 4.ð 4.ðn$sð $sð $sð $sð $sˆSñ $sô $sð $sðN-%ð -%ð -%ð -%ð -%ˆRŒYñ -%ô -%ð -%ð`<ð <ð <ð <ð <�"”)ñ <ô <ð <ð~Vð Vð Vð Vð VˆBŒIñ Vô Vð Vðr>ð >ð >ð >ð >ˆRŒYñ >ô >ð >ðBOð Oð Oð Oð OˆBŒIñ Oô Oð Oðdð ð ð ð �”	ñ ô ð ð.ð ð ð ð ˆrŒyñ ô ð ð*8Xð 8Xð 8Xð 8Xð 8XˆBŒIñ 8Xô 8Xð 8Xðv!ð !ð !ð !ð !ˆeñ !ô !ð !ðH8Qð 8Qð 8Qð 8Qð 8QˆbŒiñ 8Qô 8Qð 8Qð 8Qð 8QrN   