
    FPh1                     2   d Z ddlZddlZddlZddlmZ dZddZ G d dej                        Z
 G d de
      Z G d	 d
ej                        Z G d de
      Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Z G d dej                        Zy) zConvolution modules.    N)ConvConv2	LightConvDWConvDWConvTranspose2dConvTransposeFocus	GhostConvChannelAttentionSpatialAttentionCBAMConcatRepConvc                     |dkD  r4t        | t              r|| dz
  z  dz   n| D cg c]  }||dz
  z  dz    c}} |(t        | t              r| dz  n| D cg c]  }|dz  	 c}}|S c c}w c c}w )zPad to 'same' shape outputs.      )
isinstanceint)kpdxs       fC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/nn/modules/conv.pyautopadr      s}    1u)!S1AQK!OQR7SQRAQUaQR7Sy C(AFq.Aq!qAvq.AH 8T.As   A&A+c                   R     e Zd ZdZ ej
                         Zd fd	Zd Zd Z	 xZ
S )r   zeStandard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation).c	                 R   t         	|           t        j                  ||||t	        |||      ||d      | _        t        j                  |      | _        |du r| j                  | _        yt        |t        j                        r|| _        yt        j                         | _        y)@Initialize Conv layer with given arguments including activation.FgroupsdilationbiasTN)super__init__nnConv2dr   convBatchNorm2dbndefault_actr   ModuleIdentityact
selfc1c2r   sr   gr   r,   	__class__s
            r   r#   zConv.__init__   s}    IIb"aGAq!,<QQRY^_	..$'*d{4##z#ryy?Y_a_j_j_l    c                 `    | j                  | j                  | j                  |                  S zFApply convolution, batch normalization and activation to input tensor.r,   r(   r&   r.   r   s     r   forwardzConv.forward"   "    xx		!-..r4   c                 B    | j                  | j                  |            S )z*Perform transposed convolution of 2D data.r,   r&   r8   s     r   forward_fusezConv.forward_fuse&       xx		!%%r4   )r   r   Nr   r   T__name__
__module____qualname____doc__r$   SiLUr)   r#   r9   r=   __classcell__r3   s   @r   r   r      s#    o"'')Km/&r4   r   c                   6     e Zd ZdZd fd	Zd Zd Zd Z xZS )r   z+Simplified RepConv module with Conv fusing.c	                     t         	|   ||||||||       t        j                  ||d|t	        d||      ||d      | _        y)r   r2   r   r,   r   Fr   N)r"   r#   r$   r%   r   cv2r-   s
            r   r#   zConv2.__init__.   sJ    RAqA<99RQ71a+;APQX]^r4   c                     | j                  | j                  | j                  |      | j                  |      z               S r6   )r,   r(   r&   rJ   r8   s     r   r9   zConv2.forward3   s/    xx		!txx{ :;<<r4   c                 `    | j                  | j                  | j                  |                  S )zLApply fused convolution, batch normalization and activation to input tensor.r7   r8   s     r   r=   zConv2.forward_fuse7   r:   r4   c                    t        j                  | j                  j                  j                        }|j
                  dd D cg c]  }|dz  	 }}| j                  j                  j                  j                         |dddd|d   |d   dz   |d   |d   dz   f<   | j                  j                  xj                  |z  c_        | j                  d       | j                  | _
        yc c}w )zFuse parallel convolutions.r   Nr   r   rJ   )torch
zeros_liker&   weightdatashaperJ   clone__delattr__r=   r9   )r.   wr   is       r   
fuse_convszConv2.fuse_convs;   s    TYY--223WWQR[)[Q!V[)040D0D0J0J0L!Q!QqTAXqtAaD1H}
,-		"((	 *s   C.)   r   Nr   r   T)	r@   rA   rB   rC   r#   r9   r=   rW   rE   rF   s   @r   r   r   +   s    5_
=/)r4   r   c                   L     e Zd ZdZd ej
                         f fd	Zd Z xZS )r   z
    Light convolution with args(ch_in, ch_out, kernel).

    https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
    r   c                 r    t         |           t        ||dd      | _        t	        ||||      | _        y)r   r   Fr,   N)r"   r#   r   conv1r   conv2)r.   r/   r0   r   r,   r3   s        r   r#   zLightConv.__init__L   s2    "b!/
BA3/
r4   c                 B    | j                  | j                  |            S )z%Apply 2 convolutions to input tensor.)r]   r\   r8   s     r   r9   zLightConv.forwardR   s    zz$**Q-((r4   )	r@   rA   rB   rC   r$   ReLUr#   r9   rE   rF   s   @r   r   r   E   s!     "#	 0)r4   r   c                   $     e Zd ZdZd fd	Z xZS )r   zDepth-wise convolution.c           
      Z    t         |   ||||t        j                  ||      ||       y)z8Initialize Depth-wise convolution with given parameters.rI   Nr"   r#   mathgcd)r.   r/   r0   r   r1   r   r,   r3   s          r   r#   zDWConv.__init__Z   s*    RA"b)9QCHr4   r   r   r   Tr@   rA   rB   rC   r#   rE   rF   s   @r   r   r   W   s    !I Ir4   r   c                   $     e Zd ZdZd fd	Z xZS )r   z!Depth-wise transpose convolution.c                 Z    t         |   ||||||t        j                  ||             y)z9Initialize DWConvTranspose2d class with given parameters.)r   Nrb   )r.   r/   r0   r   r1   p1p2r3   s          r   r#   zDWConvTranspose2d.__init__b   s*    RAr2dhhr26FGr4   )r   r   r   r   rf   rF   s   @r   r   r   _   s    +H Hr4   r   c                   R     e Zd ZdZ ej
                         Zd fd	Zd Zd Z	 xZ
S )r   zConvolution transpose 2d layer.c                 f   t         |           t        j                  ||||||       | _        |rt        j
                  |      nt        j                         | _        |du r| j                  | _        yt        |t        j                        r|| _        yt        j                         | _        y)zRInitialize ConvTranspose2d layer with batch normalization and activation function.r!   TN)r"   r#   r$   ConvTranspose2dconv_transposer'   r+   r(   r)   r   r*   r,   )	r.   r/   r0   r   r1   r   r(   r,   r3   s	           r   r#   zConvTranspose.__init__k   s}     00RAq2vN(*"..$'*d{4##z#ryy?Y_a_j_j_lr4   c                 `    | j                  | j                  | j                  |                  S )zMApplies transposed convolutions, batch normalization and activation to input.)r,   r(   ro   r8   s     r   r9   zConvTranspose.forwardr   s%    xx 3 3A 6788r4   c                 B    | j                  | j                  |            S )z@Applies activation and convolution transpose operation to input.)r,   ro   r8   s     r   r=   zConvTranspose.forward_fusev   s    xx++A.//r4   )r   r   r   TTr?   rF   s   @r   r   r   g   s#    )"'')Km90r4   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r	   z"Focus wh information into c-space.c           	      V    t         |           t        |dz  ||||||      | _        y)zfInitializes Focus object with user defined channel, convolution, padding, group and activation values.   r[   N)r"   r#   r   r&   )	r.   r/   r0   r   r1   r   r2   r,   r3   s	           r   r#   zFocus.__init__~   s*    aQ1aS9	r4   c                     | j                  t        j                  |dddddddf   |dddddddf   |dddddddf   |dddddddf   fd            S )z
        Applies convolution to concatenated tensor and returns the output.

        Input shape is (b,c,w,h) and output shape is (b,4c,w/2,h/2).
        .Nr   r   )r&   rN   catr8   s     r   r9   zFocus.forward   s     yyAc3Q3!m$4aQTT3Q36G3PSRSPSUVUYXYUY>IZ\]^acdcgfgcgijimlmim^m\n#oqrsttr4   )r   r   Nr   Tr@   rA   rB   rC   r#   r9   rE   rF   s   @r   r	   r	   {   s    ,:ur4   r	   c                   *     e Zd ZdZd fd	Zd Z xZS )r
   z:Ghost Convolution https://github.com/huawei-noah/ghostnet.c           	          t         |           |dz  }t        ||||d||      | _        t        ||ddd||      | _        y)zInitializes the GhostConv object with input channels, output channels, kernel size, stride, groups and
        activation.
        r   Nr[      r   )r"   r#   r   cv1rJ   )	r.   r/   r0   r   r1   r2   r,   c_r3   s	           r   r#   zGhostConv.__init__   sI     	1WB1dA37B1dBC8r4   c                 r    | j                  |      }t        j                  || j                  |      fd      S )zJForward propagation through a Ghost Bottleneck layer with skip connection.r   )r{   rN   rv   rJ   )r.   r   ys      r   r9   zGhostConv.forward   s-    HHQKyy!TXXa[)1--r4   re   rw   rF   s   @r   r
   r
      s    D9.r4   r
   c                   j     e Zd ZdZ ej
                         Zd	 fd	Zd Zd Z	d Z
d Zd Zd Z xZS )
r   z
    RepConv is a basic rep-style block, including training and deploy status.

    This module is used in RT-DETR.
    Based on https://github.com/DingXiaoH/RepVGG/blob/main/repvgg.py
    c           	         t         |           |dk(  r|dk(  sJ || _        || _        || _        |du r| j
                  n/t        |t        j                        r|nt        j                         | _
        |	r ||k(  r|dk(  rt        j                  |      nd| _        t        ||||||d      | _        t        ||d|||dz  z
  |d      | _        y)	zXInitializes Light Convolution layer with inputs, outputs & optional activation function.rX   r   T)num_featuresNF)r   r2   r,   r   )r"   r#   r2   r/   r0   r)   r   r$   r*   r+   r,   r'   r(   r   r\   r]   )r.   r/   r0   r   r1   r   r2   r   r,   r(   deployr3   s              r   r#   zRepConv.__init__   s    Av!q&  '*d{4##z#ryy?Y_a_j_j_l57B"Ha"..b1UY"b!Q!qe<
"b!Q1qAv:!G
r4   c                 B    | j                  | j                  |            S )Forward process.r<   r8   s     r   r=   zRepConv.forward_fuse   r>   r4   c                     | j                   dn| j                  |      }| j                  | j                  |      | j                  |      z   |z         S )r   r   )r(   r,   r\   r]   )r.   r   id_outs      r   r9   zRepConv.forward   sB    ggo4771:xx

1

15>??r4   c                     | j                  | j                        \  }}| j                  | j                        \  }}| j                  | j                        \  }}|| j	                  |      z   |z   ||z   |z   fS )zjReturns equivalent kernel and bias by adding 3x3 kernel, 1x1 kernel and identity kernel with their biases.)_fuse_bn_tensorr\   r]   r(   _pad_1x1_to_3x3_tensor)r.   	kernel3x3bias3x3	kernel1x1bias1x1kernelidbiasids          r   get_equivalent_kernel_biasz"RepConv.get_equivalent_kernel_bias   sy    !11$**=	7!11$**=	7//8&466yAAHLgX_N_bhNhhhr4   c                 `    |yt         j                  j                  j                  |g d      S )z"Pads a 1x1 tensor to a 3x3 tensor.r   )r   r   r   r   )rN   r$   
functionalpad)r.   r   s     r   r   zRepConv._pad_1x1_to_3x3_tensor   s(    88&&**9lCCr4   c                    |yt        |t              r|j                  j                  }|j                  j
                  }|j                  j                  }|j                  j                  }|j                  j                  }|j                  j                  }nt        |t        j                        rt        | d      s| j                  | j                  z  }t        j                  | j                  |ddft        j                         }	t#        | j                        D ]  }
d|	|
|
|z  ddf<    t%        j&                  |	      j)                  |j                  j*                        | _        | j,                  }|j
                  }|j                  }|j                  }|j                  }|j                  }z   j/                         }|z  j1                  dddd      }|z  |z  |z  z
  fS )zbGenerates appropriate kernels and biases for convolution by fusing branches of the neural network.)r   r   	id_tensorrX   )dtyper   )r   r   r&   rP   r(   running_meanrunning_varr!   epsr$   r'   hasattrr/   r2   npzerosfloat32rangerN   
from_numpytodevicer   sqrtreshape)r.   branchkernelr   r   gammabetar   	input_dimkernel_valuerV   stdts                r   r   zRepConv._fuse_bn_tensor   s   >fd#[[''F!9911L ))//KII$$E99>>D))--C/4- GGtvv-	!xx)Q(B"**UtwwA;<LA	M1a!78 (!&!1!1,!?!B!B6==CWCW!X^^F!..L ,,KMME;;D**CS &&(S[!!"aA.z4,"6"<<<<r4   c           
         t        | d      ry| j                         \  }}t        j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  | j                  j
                  j                  d      j                  d      | _        || j
                  j                  _        || j
                  j                   _        | j#                         D ]  }|j%                           | j'                  d       | j'                  d       t        | d      r| j'                  d       t        | d	      r| j'                  d	       t        | d
      r| j'                  d
       yy)zaCombines two convolution layers into a single layer and removes unused attributes from the class.r&   NT)in_channelsout_channelskernel_sizestridepaddingr    r   r!   Fr\   r]   nmr(   r   )r   r   r$   r%   r\   r&   r   r   r   r   r   r    r   requires_grad_rP   rQ   r!   
parametersdetach_rT   )r.   r   r!   paras       r   rW   zRepConv.fuse_convs   s`   4 668II$**//*E*E+/::??+G+G*.**//*E*E%)ZZ__%;%;&*jjoo&=&='+zz'?'?%)ZZ__%;%;#') *8)> 		 !'		"		OO%DLLN &!!4T"4T"4%[) &r4   )rX   r   r   r   r   TFF)r@   rA   rB   rC   r$   rD   r)   r#   r=   r9   r   r   r   rW   rE   rF   s   @r   r   r      s>     "'')KH&@
iD=8*r4   r   c                   d     e Zd ZdZdeddf fdZdej                  dej                  fdZ xZ	S )r   zaChannel-attention module https://github.com/open-mmlab/mmdetection/tree/v3.0.0rc1/configs/rtmdet.channelsreturnNc                     t         |           t        j                  d      | _        t        j
                  ||dddd      | _        t        j                         | _        y)zXInitializes the class and sets the basic configurations and instance variables required.r   r   Trm   N)	r"   r#   r$   AdaptiveAvgPool2dpoolr%   fcSigmoidr,   )r.   r   r3   s     r   r#   zChannelAttention.__init__  sG    ((+	))Hh1adC::<r4   r   c                 f    || j                  | j                  | j                  |                  z  S )ziApplies forward pass using activation on convolutions of the input, optionally using batch normalization.)r,   r   r   r8   s     r   r9   zChannelAttention.forward  s'    488DGGDIIaL1222r4   )
r@   rA   rB   rC   r   r#   rN   Tensorr9   rE   rF   s   @r   r   r     s4    k     3 3%,, 3r4   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   zSpatial-attention module.c                     t         |           |dv sJ d       |dk(  rdnd}t        j                  dd||d      | _        t        j
                         | _        y	)
z>Initialize Spatial-attention module with kernel size argument.)rX      zkernel size must be 3 or 7r   rX   r   r   F)r   r!   N)r"   r#   r$   r%   r{   r   r,   )r.   r   r   r3   s      r   r#   zSpatialAttention.__init__  sU    f$B&BB$"a'!Q99Q;eL::<r4   c                     || j                  | j                  t        j                  t        j                  |dd      t        j
                  |dd      d   gd                  z  S )zGApply channel and spatial attention on input for feature recalibration.r   T)keepdimr   )r,   r{   rN   rv   meanmaxr8   s     r   r9   zSpatialAttention.forward  sV    488DHHUYY

1a0NPUPYPYZ[]^hlPmnoPp/qst%uvwwwr4   r   rw   rF   s   @r   r   r     s    # xr4   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z%Convolutional Block Attention Module.c                 b    t         |           t        |      | _        t	        |      | _        y)z>Initialize CBAM with given input channel (c1) and kernel size.N)r"   r#   r   channel_attentionr   spatial_attention)r.   r/   r   r3   s      r   r#   zCBAM.__init__&  s(    !1"!5!1+!>r4   c                 B    | j                  | j                  |            S )z+Applies the forward pass through C1 module.)r   r   r8   s     r   r9   zCBAM.forward,  s    %%d&<&<Q&?@@r4   r   rw   rF   s   @r   r   r   #  s    /?Ar4   r   c                   *     e Zd ZdZd fd	Zd Z xZS )r   z.Concatenate a list of tensors along dimension.c                 0    t         |           || _        y)z;Concatenates a list of tensors along a specified dimension.N)r"   r#   r   )r.   	dimensionr3   s     r   r#   zConcat.__init__4  s    r4   c                 B    t        j                  || j                        S )z.Forward pass for the YOLOv8 mask Proto module.)rN   rv   r   r8   s     r   r9   zConcat.forward9  s    yyDFF##r4   )r   rw   rF   s   @r   r   r   1  s    8
$r4   r   )Nr   )rC   rc   numpyr   rN   torch.nnr$   __all__r   r*   r   r   r   r   rn   r   r   r	   r
   r   r   r   r   r    r4   r   <module>r      s        P&299 &()D )4)		 )$IT IH** H0BII 0(uBII u&.		 .$a*bii a*H3ryy 3xryy x A299 A
$RYY 
$r4   