
    FPhl              
           d Z ddlZddlZddlZddlZddlmZ ddlmc m	Z
 ddlmZ dZd ZddZd ZddZd	ej$                  d
ej$                  dej$                  dej$                  dej$                  f
dZy)zModule utils.    N)uniform_)#multi_scale_deformable_attn_pytorchinverse_sigmoidc                     t        j                  t        |      D cg c]  }t        j                  |        c}      S c c}w )z6Create a list of cloned modules from the given module.)nn
ModuleListrangecopydeepcopy)modulen_s      gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/nn/modules/utils.py_get_clonesr      s0    ==qBA$--/BCCBs   A c                 L    t        t        j                  d| z
  | z               S )zEInitialize conv/fc bias value according to a given probability value.   )floatnplog)
prior_probs    r   bias_init_with_probr      s"    "&&!j.J67788    c                     dt        j                  | j                  j                  d         z  }t	        | j                  | |       t        | d      r&| j                  t	        | j                  | |       yyy)z5Initialize the weights and biases of a linear module.r   r   biasN)mathsqrtweightshaper   hasattrr   )r   bounds     r   linear_init_r!      sc    		&----a011EV]]UFE*vv6;;#:ufe, $;r   c                     | j                  dd      } | j                  |      }d| z
  j                  |      }t        j                  ||z        S )z4Calculate the inverse sigmoid function for a tensor.r   r   )minmax)r#   )clamptorchr   )xepsx1x2s       r   r   r   "   sK    	A1A	
S	B
a%3	B99R"Wr   valuevalue_spatial_shapessampling_locationsattention_weightsreturnc                 :   | j                   \  }}}}|j                   \  }}}}	}
}| j                  |D cg c]
  \  }}||z   c}}d      }d|z  dz
  }g }t        |      D ]  \  }\  }}||   j                  d      j	                  dd      j                  ||z  |||      }|dddddd|f   j	                  dd      j                  dd      }t        j                  ||ddd	      }|j                  |        |j	                  dd      j                  ||z  d||	|
z        }t        j                  |d
      j                  d
      |z  j                  d      j                  |||z  |      }|j	                  dd      j                         S c c}}w )z
    Multi-scale deformable attention.

    https://github.com/IDEA-Research/detrex/blob/main/detrex/layers/multi_scale_deform_attn.py
    r   )dim   Nr   bilinearzerosF)modepadding_modealign_corners)r   split	enumerateflatten	transposereshapeFgrid_sampleappendr&   stacksumview
contiguous)r+   r,   r-   r.   bsr   	num_heads
embed_dimsnum_queries
num_levels
num_pointsH_W_
value_listsampling_gridssampling_value_listlevelvalue_l_sampling_grid_l_sampling_value_l_outputs                        r   r   r   *   s    $);; B9j;M;S;S8A{Iz:q4HI4H&"bb2g4HIqQJ++a/N$%9:xB
 u%--a0::1a@HHiYcegikl *!Q5.9CCAqIQQRSUVWMM(*:/97>8=	?
 	""#45! ;( *33Aq9AA"y.RSU`BLzBY[{{.B7??CFWW\\]_`ee
I
"K1FAq!,,..7 Js   F
)g{Gz?)gh㈵>)__doc__r
   r   numpyr   r&   torch.nnr   torch.nn.functional
functionalr?   torch.nn.initr   __all__r   r   r!   r   Tensorr    r   r   <module>r_      s|            "
BD
9
-&/u|| &/SXS_S_ &/<ALL&/;@<<&/LQLL&/r   