
    FPh2                        d Z ddlZddlmZ ddlmc mZ ddl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!y))zBlock modules.    N   )ConvDWConv	GhostConv	LightConvRepConv)TransformerBlock)DFLHGBlockHGStemSPPSPPFC1C2C3C2fC3xC3TRC3GhostGhostBottleneck
BottleneckBottleneckCSPProtoRepC3c                   *     e Zd ZdZd fd	Zd Z xZS )r
   z
    Integral module of Distribution Focal Loss (DFL).

    Proposed in Generalized Focal Loss https://ieeexplore.ieee.org/document/9792391
    c                 d   t         |           t        j                  |ddd      j	                  d      | _        t        j                  |t        j                        }t        j                  |j                  d|dd            | j
                  j                  j                  dd || _        y)zGInitialize a convolutional layer with a given number of input channels.r   Fbias)dtypeN)super__init__nnConv2drequires_grad_convtorcharangefloat	Parameterviewweightdatac1)selfr-   x	__class__s      gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/nn/modules/block.pyr!   zDFL.__init__   s|    IIb!QU3BB5I	LL5;;/#%<<q"a0C#D		a     c                     |j                   \  }}}| j                  |j                  |d| j                  |      j	                  dd      j                  d            j                  |d|      S )zEApplies a transformer layer on input tensor 'x' and returns a tensor.      r   )shaper%   r*   r-   	transposesoftmax)r.   r/   bcas        r1   forwardzDFL.forward   s]    ''1ayy1dggq1;;AqAII!LMRRSTVWYZ[[r2   )   __name__
__module____qualname____doc__r!   r<   __classcell__r0   s   @r1   r
   r
      s    \r2   r
   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z1YOLOv8 mask Proto module for segmentation models.c                     t         |           t        ||d      | _        t	        j
                  ||dddd      | _        t        ||d      | _        t        ||      | _        y)z
        Initializes the YOLOv8 mask Proto module with specified number of protos and masks.

        Input arguments are ch_in, number of protos, number of masks.
           )kr5   r   Tr   N)	r    r!   r   cv1r"   ConvTranspose2dupsamplecv2cv3)r.   r-   c_c2r0   s       r1   r!   zProto.__init__(   sY     	B!$**2r1aFB!$B<r2   c           	      ~    | j                  | j                  | j                  | j                  |                        S )zFPerforms a forward pass through layers using an upsampled input image.)rM   rL   rK   rI   r.   r/   s     r1   r<   zProto.forward4   s+    xxtxx{!;<==r2   )       r>   rD   s   @r1   r   r   %   s    ;
 >r2   r   c                   (     e Zd ZdZ fdZ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
    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      | _        y)	z_Initialize the SPP layer with input/output channels and specified kernel sizes for max pooling.rG   r5   actr   r   T)kernel_sizestridepadding	ceil_modeN)r    r!   r   r"   ReLUstem1stem2astem2bstem3stem4	MaxPool2dpool)r.   r-   cmrO   r0   s       r1   r!   zHGStem.__init__@   s    "b!QBGGI6
2rQw1aRWWY?27B1aRWWY?"q&"a	:
"b!QBGGI6
LLQq!tT	r2   c                 d   | j                  |      }t        j                  |g d      }| j                  |      }t        j                  |g d      }| j	                  |      }| j                  |      }t        j                  ||gd      }| j                  |      }| j                  |      }|S )+Forward pass of a PPHGNetV2 backbone layer.)r   r   r   r   r   )dim)
r]   Fpadr^   r_   rc   r&   catr`   ra   )r.   r/   x2x1s       r1   r<   zHGStem.forwardJ   s    JJqMEE!\"[[^UU2|$[[_YYq\IIr2hA&JJqMJJqMr2   r>   rD   s   @r1   r   r   9   s    Ur2   r   c                   R     e Zd ZdZdddd ej
                         f fd	Z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
    rG      Fc	                 0  	 t         
|           |rt        nt        	t	        j
                  	fdt        |      D              | _        t        |z  z   |dz  dd      | _        t        |dz  |dd      | _	        |xr |k(  | _
        y)zZInitializes a CSP Bottleneck with 1 convolution using specified input and output channels.c              3   D   K   | ]  } |d k(  rn        yw)r   )rH   rW   N ).0irW   blockr-   rd   rH   s     r1   	<genexpr>z#HGBlock.__init__.<locals>.<genexpr>c   s+     _V^QRu16Rr2LLV^s    r5   r   rV   N)r    r!   r   r   r"   
ModuleListrangemscecadd)r.   r-   rd   rO   rH   n	lightconvshortcutrW   rt   r0   s    `` `   `@r1   r!   zHGBlock.__init___   s|    &	D_V[\]V^__rAF{B!GQs;rQwAqc2(br2   c                     |gj                  fd| j                  D               | j                  | j                  t	        j
                  d                  | j                  r|z   S S )rf   c              3   4   K   | ]  } |d            ywNrq   rr   rx   ys     r1   ru   z"HGBlock.forward.<locals>.<genexpr>k        *6a1R56   r   )extendrx   rz   ry   r&   rj   r{   r.   r/   r   s     @r1   r<   zHGBlock.forwardh   sV    C	*466**GGDGGEIIaO,-q1u'a'r2   )	r?   r@   rA   rB   r"   r\   r!   r<   rC   rD   s   @r1   r   r   X   s,     &'!uuRYRTRYRYR[ )(r2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   zDSpatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729.c                 "   t         |           |dz  }t        ||dd      | _        t        |t	        |      dz   z  |dd      | _        t        j                  |D cg c]  }t        j                  |d|dz         c}      | _	        yc c}w )zMInitialize the SPP layer with input/output channels and pooling kernel sizes.r5   r   rX   rY   rZ   N)
r    r!   r   rI   lenrL   r"   rv   rb   rx   )r.   r-   rO   rH   rN   r/   r0   s         r1   r!   zSPP.__init__s   s    1WB1%c!fqj)2q!4_`a_`Z[1aSTf U_`abas   "Bc                     | j                  |      }| j                  t        j                  |g| j                  D cg c]
  } ||       c}z   d            S c c}w )zBForward pass of the SPP layer, performing spatial pyramid pooling.r   )rI   rL   r&   rj   rx   )r.   r/   rx   s      r1   r<   zSPP.forward{   sJ    HHQKxx		1#tvv(>v!1v(>">BCC(>s   A))   	      r>   rD   s   @r1   r   r   p   s    NcDr2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   zGSpatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher.c                     t         |           |dz  }t        ||dd      | _        t        |dz  |dd      | _        t        j                  |d|dz        | _        y)z
        Initializes the SPPF layer with given input/output channels and kernel size.

        This module is equivalent to SPP(k=(5, 9, 13)).
        r5   r   r4   r   N)r    r!   r   rI   rL   r"   rb   rx   )r.   r-   rO   rH   rN   r0   s        r1   r!   zSPPF.__init__   sY     	1WB1%QAq)!AqAvFr2   c           
          | j                  |      }| j                  |      }| j                  |      }| j                  t        j                  |||| j                  |      fd            S )z-Forward pass through Ghost Convolution block.r   )rI   rx   rL   r&   rj   r.   r/   y1y2s       r1   r<   zSPPF.forward   sT    HHQKVVAYVVBZxx		1b"dffRj"91=>>r2   )r   r>   rD   s   @r1   r   r      s    Q
G?r2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z"CSP Bottleneck with 1 convolution.c                     t         |           t        |dd      | _        t	        j
                  fdt        |      D         | _        y)zjInitializes the CSP Bottleneck with configurations for 1 convolution with arguments ch_in, ch_out, number.r   c              3   8   K   | ]  }t        d         yw)rG   N)r   )rr   _rO   s     r1   ru   zC1.__init__.<locals>.<genexpr>   s      C(Qb"a(s   N)r    r!   r   rI   r"   
Sequentialrw   rx   )r.   r-   rO   r|   r0   s     ` r1   r!   zC1.__init__   s:    B1% C%( CDr2   c                 L    | j                  |      }| j                  |      |z   S )z5Applies cross-convolutions to input in the C3 module.)rI   rx   r   s      r1   r<   z
C1.forward   s!    HHQKvvay1}r2   )r   r>   rD   s   @r1   r   r      s    ,Er2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z#CSP Bottleneck with 2 convolutions.c                 "    t                    t        ||z         _        t	        |d j                  z  dd       _        t	        d j                  z  |d       _        t        j                   fdt        |      D          _
        y)zInitializes the CSP Bottleneck with 2 convolutions module with arguments ch_in, ch_out, number, shortcut,
        groups, expansion.
        r5   r   c           	   3   h   K   | ])  }t        j                  j                  d d       + yw)rG   rG   r         ?rH   eNr   r:   rr   r   gr.   r~   s     r1   ru   zC2.__init__.<locals>.<genexpr>   s1      vmuhiDFFDFFHaK[_b!c!cmu   /2N)r    r!   intr:   r   rI   rL   r"   r   rw   rx   r.   r-   rO   r|   r~   r   r   r0   s   `   `` r1   r!   zC2.__init__   sn     	R!VAJ1-DFF
B* vmrstmu vwr2   c                     | j                  |      j                  dd      \  }}| j                  t        j                  | j                  |      |fd            S )<Forward pass through the CSP bottleneck with 2 convolutions.r5   r   )rI   chunkrL   r&   rj   rx   )r.   r/   r;   r9   s       r1   r<   z
C2.forward   sF    xx{  A&1xx		466!9a.!455r2   r   Tr         ?r>   rD   s   @r1   r   r      s    -	x6r2   r   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )r   z<Faster Implementation of CSP Bottleneck with 2 convolutions.c                 .    t                    t        ||z         _        t	        |d j                  z  dd       _        t	        d|z    j                  z  |d       _        t        j                   fdt        |      D               _
        y)zInitialize CSP bottleneck layer with two convolutions with arguments ch_in, ch_out, number, shortcut, groups,
        expansion.
        r5   r   c           	   3   h   K   | ])  }t        j                  j                  d d       + ywr   r   r   s     r1   ru   zC2f.__init__.<locals>.<genexpr>   s1     tksfgz$&&$&&(AIY]`aaksr   N)r    r!   r   r:   r   rI   rL   r"   rv   rw   rx   r   s   `   `` r1   r!   zC2f.__init__   ss     	R!VAJ1-Q$&&("a0tkpqrksttr2   c                     t        | j                  |      j                  dd            j                  fd| j                  D               | j                  t        j                  d            S )zForward pass through C2f layer.r5   r   c              3   4   K   | ]  } |d            ywr   rq   r   s     r1   ru   zC2f.forward.<locals>.<genexpr>   r   r   )listrI   r   r   rx   rL   r&   rj   r   s     @r1   r<   zC2f.forward   sQ    !""1a()	*466**xx		!Q((r2   c                    t        | j                  |      j                  | j                  | j                  fd            j	                  fd| j
                  D               | j                  t        j                  d            S )z.Forward pass using split() instead of chunk().r   c              3   4   K   | ]  } |d            ywr   rq   r   s     r1   ru   z$C2f.forward_split.<locals>.<genexpr>   r   r   )	r   rI   splitr:   r   rx   rL   r&   rj   r   s     @r1   forward_splitzC2f.forward_split   s^    !""DFFDFF#3Q78	*466**xx		!Q((r2   )r   Fr   r   )r?   r@   rA   rB   r!   r<   r   rC   rD   s   @r1   r   r      s    Fu))r2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z#CSP Bottleneck with 3 convolutions.c                    t         |           t        ||z        t        |dd      | _        t        |dd      | _        t        dz  |d      | _        t        j                  fdt        |      D         | _
        y)zbInitialize the CSP Bottleneck with given channels, number, shortcut, groups, and expansion values.r   r5   c           	   3   @   K   | ]  }t        d d        yw)))r   r   r   r   r   Nr   rr   r   rN   r   r~   s     r1   ru   zC3.__init__.<locals>.<genexpr>   s)      nem`aBHaCSWZ![![ems   N)r    r!   r   r   rI   rL   rM   r"   r   rw   rx   	r.   r-   rO   r|   r~   r   r   rN   r0   s	       `` @r1   r!   zC3.__init__   sp    a[B1%B1%BA& nejklem nor2   c           	          | j                  t        j                  | j                  | j	                  |            | j                  |      fd            S )r   r   )rM   r&   rj   rx   rI   rL   rQ   s     r1   r<   z
C3.forward   s:    xx		466$((1+#6"DaHIIr2   r   r>   rD   s   @r1   r   r      s    -pJr2   r   c                   $     e Zd ZdZd fd	Z xZS )r   z"C3 module with cross-convolutions.c                      t            ||||       t        ||z         _        t	        j
                   fdt        |      D          _        y)z4Initialize C3TR instance and set default parameters.c           	   3   h   K   | ])  }t        j                  j                  d d       + yw)))r   rG   rG   r   r   r   N)r   rN   r   s     r1   ru   zC3x.__init__.<locals>.<genexpr>   s1      vmuhiDGGTWWhM]ab!c!cmur   N)r    r!   r   rN   r"   r   rw   rx   r   s   `   `` r1   r!   zC3x.__init__   sF    RHa3b1f+ vmrstmu vwr2   r   r?   r@   rA   rB   r!   rC   rD   s   @r1   r   r      s    ,x xr2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   zRep C3.c           	      h   t         |           t        ||z        }t        ||dd      | _        t        ||dd      | _        t        j                  t        |      D cg c]  }t        ||       c} | _
        ||k7  rt        ||dd      | _        yt        j                         | _        yc c}w )zfInitialize CSP Bottleneck with a single convolution using input channels, output channels, and number.r   N)r    r!   r   r   rI   rL   r"   r   rw   r   rx   IdentityrM   )r.   r-   rO   r|   r   rN   r   r0   s          r1   r!   zRepC3.__init__   s    a[B1%B1%%( C(QR( CD)+r4B1%r{{} !Ds    B/c                     | j                  | j                  | j                  |            | j                  |      z         S )z#Forward pass of RT-DETR neck layer.)rM   rx   rI   rL   rQ   s     r1   r<   zRepC3.forward   s/    xxtxx{+dhhqk9::r2   )rG   r   r>   rD   s   @r1   r   r      s    E;r2   r   c                   $     e Zd ZdZd fd	Z xZS )r   z"C3 module with TransformerBlock().c                 p    t         |   ||||||       t        ||z        }t        ||d|      | _        y)z1Initialize C3Ghost module with GhostBottleneck().r4   N)r    r!   r   r	   rx   r   s	           r1   r!   zC3TR.__init__  s9    RHa3a[!"b!Q/r2   r   r   rD   s   @r1   r   r     s    ,0 0r2   r   c                   $     e Zd ZdZd fd	Z xZS )r   z!C3 module with GhostBottleneck().c                     t         |   ||||||       t        ||z        t        j                  fdt        |      D         | _        y)zOInitialize 'SPP' module with various pooling sizes for spatial pyramid pooling.c              3   6   K   | ]  }t                y w)N)r   )rr   r   rN   s     r1   ru   z#C3Ghost.__init__.<locals>.<genexpr>  s      K(QR!8(s   N)r    r!   r   r"   r   rw   rx   r   s	          @r1   r!   zC3Ghost.__init__  sA    RHa3a[ K%( KLr2   r   r   rD   s   @r1   r   r     s    +M Mr2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z9Ghost Bottleneck https://github.com/huawei-noah/ghostnet.c                    t         |           |dz  }t        j                  t	        ||dd      |dk(  rt        ||||d      nt        j                         t	        ||ddd            | _        |dk(  r8t        j                  t        ||||d      t        ||ddd            | _	        yt        j                         | _	        y)zPInitializes GhostBottleneck module with arguments ch_in, ch_out, kernel, stride.r5   r   FrV   N)
r    r!   r"   r   r   r   r   r%   r   r~   )r.   r-   rO   rH   srN   r0   s         r1   r!   zGhostBottleneck.__init__  s    1WMMb"a#/0AvF2r1aU+2;;=b"a.0	
 \]`a[a fRQu&EtBPRTUWXPUHW Xgigrgrgt 	r2   c                 H    | j                  |      | j                  |      z   S )z:Applies skip connection and concatenation to input tensor.)r%   r~   rQ   s     r1   r<   zGhostBottleneck.forward#  s    yy|dmmA...r2   r   r>   rD   s   @r1   r   r     s    C	u/r2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   zStandard bottleneck.c                     t         |           t        ||z        }t        |||d   d      | _        t        |||d   d|      | _        |xr ||k(  | _        y)zInitializes a bottleneck module with given input/output channels, shortcut option, group, kernels, and
        expansion.
        r   r   )r   N)r    r!   r   r   rI   rL   r{   )	r.   r-   rO   r~   r   rH   r   rN   r0   s	           r1   r!   zBottleneck.__init__+  s[     	a[B!a(B!a1-(br2   c                     | j                   r#|| j                  | j                  |            z   S | j                  | j                  |            S )z/'forward()' applies the YOLO FPN to input data.)r{   rL   rI   rQ   s     r1   r<   zBottleneck.forward5  s:    ,0HHq488DHHQK((O$((488A;:OOr2   )Tr   r   r   r>   rD   s   @r1   r   r   (  s    )Pr2   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   zGCSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks.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         | _        y)zfInitializes the CSP Bottleneck given arguments for ch_in, ch_out, number, shortcut, groups, expansion.r   Fr   r5   c              3   >   K   | ]  }t        d         yw)r   )r   Nr   r   s     r1   ru   z)BottleneckCSP.__init__.<locals>.<genexpr>G  s$      ZQYABHa3!G!GQYs   N)r    r!   r   r   rI   r"   r#   rL   rM   cv4BatchNorm2dbnSiLUrW   r   rw   rx   r   s	       `` @r1   r!   zBottleneckCSP.__init__=  s    a[B1%99RQ699RQ6BAq)..R(779 ZQVWXQY Z[r2   c           
         | j                  | j                  | j                  |                  }| j                  |      }| j	                  | j                  | j                  t        j                  ||fd                        S )z-Applies a CSP bottleneck with 3 convolutions.r   )	rM   rx   rI   rL   r   rW   r   r&   rj   r   s       r1   r<   zBottleneckCSP.forwardI  s^    XXdffTXXa[)*XXa[xxB8Q)?!@ABBr2   r   r>   rD   s   @r1   r   r   :  s    Q
\Cr2   r   )"rB   r&   torch.nnr"   torch.nn.functional
functionalrh   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   rq   r2   r1   <module>r      s7        = = )O\")) \,>BII >(RYY >(bii (0D")) D"?299 ?. 6 6()")) )4J J"x" x;BII ;"02 0Mb M/bii /&P P$CBII Cr2   