
    \hM.                         S SK r S SKJr  S SKrS SKJrJr  SSKJr   " S S\R                  5      r	 " S S	\R                  5      r
g)
    N)Optional)nnTensor   )	ImageListc                     ^  \ rS rSrSrS\\R                     0r  SU 4S jjr	\R                  \R                  " S5      4S\\   S\\   S\R                  S	\R                  S
\4
S jjrS\R                  S	\R                  4S jrS
\\   4S jrS\\\      S\\\      S
\\   4S jrS\S\\   S
\\   4S jrSrU =r$ )AnchorGenerator
   ao  
Module that generates anchors for a set of feature maps and
image sizes.

The module support computing anchors at multiple sizes and aspect ratios
per feature map. This module assumes aspect ratio = height / width for
each anchor.

sizes and aspect_ratios should have the same number of elements, and it should
correspond to the number of feature maps.

sizes[i] and aspect_ratios[i] can have an arbitrary number of elements,
and AnchorGenerator will output a set of sizes[i] * aspect_ratios[i] anchors
per spatial location for feature map i.

Args:
    sizes (Tuple[Tuple[int]]):
    aspect_ratios (Tuple[Tuple[float]]):
cell_anchorsc                 h  > [         TU ]  5         [        US   [        [        45      (       d  [	        S U 5       5      n[        US   [        [        45      (       d  U4[        U5      -  nXl        X l        [        X5       VVs/ s H  u  p4U R                  X45      PM     snnU l
        g s  snnf )Nr   c              3   &   #    U  H  o4v   M	     g 7fN ).0ss     c/var/www/fran/franai/venv/lib/python3.13/site-packages/torchvision/models/detection/anchor_utils.py	<genexpr>+AnchorGenerator.__init__.<locals>.<genexpr>,   s     .1$s   )super__init__
isinstancelisttuplelensizesaspect_ratioszipgenerate_anchorsr   )selfr   r   sizeaspect_ratio	__class__s        r   r   AnchorGenerator.__init__#   s    
 	%(T5M22...E-*T5M::*,s5z9M
*PSTYPi
Pi:L$D!!$5Pi
 
s   B.cpuscalesr   dtypedevicereturnc                 n   [         R                  " XUS9n[         R                  " X#US9n[         R                  " U5      nSU-  nUS S 2S 4   US S S 24   -  R                  S5      nUS S 2S 4   US S S 24   -  R                  S5      n[         R                  " U* U* Xx/SS9S-  n	U	R                  5       $ )Nr&   r'   r   dim   )torch	as_tensorsqrtviewstackround)
r   r%   r   r&   r'   h_ratiosw_ratioswshsbase_anchorss
             r   r    AnchorGenerator.generate_anchors:   s     VD6R::m,x<q$w&q/177;q$w&q/177;{{RC"b#51=A!!##    c                 j    U R                    Vs/ s H  o3R                  XS9PM     snU l         g s  snf )Nr*   )r   to)r   r&   r'   cell_anchors       r   set_cell_anchors AnchorGenerator.set_cell_anchorsL   s-    [_[l[lm[lK^^%^G[lmms   0c                     [        U R                  U R                  5       VVs/ s H  u  p[        U5      [        U5      -  PM     snn$ s  snnf r   )r   r   r   r   )r   r   as      r   num_anchors_per_location(AnchorGenerator.num_anchors_per_locationO   s;    ,/

D<N<N,OP,ODAAQ,OPPPs   #A
grid_sizesstridesc           	         / nU R                   n[        R                  " US LS5        [        R                  " [        U5      [        U5      s=:H  =(       a    [        U5      :H  Os  S5        [	        XU5       GH  u  pVnUu  pUu  pUR
                  n[        R                  " SU	[        R                  US9U-  n[        R                  " SU[        R                  US9U
-  n[        R                  " XSS9u  nnUR                  S5      nUR                  S5      n[        R                  " UUUU4SS	9nUR                  UR                  SSS
5      UR                  SSS
5      -   R                  SS
5      5        GM     U$ )Nzcell_anchors should not be NonezAnchors should be Tuple[Tuple[int]] because each feature map could potentially have different sizes and aspect ratios. There needs to be a match between the number of feature maps passed and the number of sizes / aspect ratios specified.r   r*   ijindexingr+   r   r,      )r   r/   _assertr   r   r'   arangeint32meshgridreshaper3   appendr2   )r   rE   rF   anchorsr   r    strider9   grid_height
grid_widthstride_heightstride_widthr'   shifts_xshifts_yshift_yshift_xshiftss                     r   grid_anchorsAnchorGenerator.grid_anchorsT   sV   ((l$.0QR
Os7|@@s</@@U	
 +.j<*P&D,&*#K*0'M!((F ||AzVTWccH||A{%++fUXeeH$~~h4PGWoob)Goob)G[['7GW!E1MF NNFKKAq1L4E4EaQ4OOXXY[]^_` +Q" r;   
image_listfeature_mapsc           
         U Vs/ s H  o3R                   SS  PM     nnUR                  R                   SS  nUS   R                  US   R                  pvU Vs/ s H{  n[        R
                  " S[        R                  US9R                  US   US   -  5      [        R
                  " S[        R                  US9R                  US   US   -  5      /PM}     n	nU R                  Xg5        U R                  XI5      n
/ n[        [        UR                  5      5       H%  nU
 Vs/ s H  oPM     nnUR                  U5        M'     U Vs/ s H  n[        R                  " U5      PM     nnU$ s  snf s  snf s  snf s  snf )Nr   r   r*   r   )shapetensorsr&   r'   r/   emptyint64fill_r?   r]   ranger   image_sizesrQ   cat)r   r_   r`   feature_maprE   
image_sizer&   r'   grF   anchors_over_all_feature_mapsrR   _anchors_per_feature_mapanchors_in_imageanchors_per_images                   r   forwardAnchorGenerator.forwards   s~   @LM'',
M''--bc2
$Q--|A/E/Ev  

   Bekk&AGG
STYZ[\Y]H]^Bekk&AGG
STYZ[\Y]H]^   	 
 	e,(,(9(9*(N%,.s:1123AWtuWt<S 7WtuNN+, 4 JQQ4E599./Q# N
  vQs   E0BE5&E: E?)r   r   r   )))      i   ))      ?      ?g       @)__name__
__module____qualname____firstlineno____doc__r   r/   r   __annotations__r   float32r'   intfloatr&   r   r?   rC   r]   r   rs   __static_attributes____classcell__r"   s   @r   r	   r	   
   s   * 	U\\*O !(
6 #]]$||E2$S	$ E{$ {{	$
 $ 
$$nekk n5<< nQ$s) Q
tDI d6lAS X\]cXd >) 4< DQWL  r;   r	   c                     ^  \ rS rSrSr     SS\\\      S\S\S\\\      S\\\      S\	4U 4S	 jjjr
\R                  \R                  " S
5      4S\S\R                  S\R                  S\\   4S jjrS\\   4S jr\R                  4S\\\      S\\   S\R                  S\4S jjrS\4S jrS\S\\   S\\   4S jrSrU =r$ )DefaultBoxGenerator   a  
This module generates the default boxes of SSD for a set of feature maps and image sizes.

Args:
    aspect_ratios (List[List[int]]): A list with all the aspect ratios used in each feature map.
    min_ratio (float): The minimum scale :math:`    ext{s}_{        ext{min}}` of the default boxes used in the estimation
        of the scales of each feature map. It is used only if the ``scales`` parameter is not provided.
    max_ratio (float): The maximum scale :math:`    ext{s}_{        ext{max}}`  of the default boxes used in the estimation
        of the scales of each feature map. It is used only if the ``scales`` parameter is not provided.
    scales (List[float]], optional): The scales of the default boxes. If not provided it will be estimated using
        the ``min_ratio`` and ``max_ratio`` parameters.
    steps (List[int]], optional): It's a hyper-parameter that affects the tiling of default boxes. If not provided
        it will be estimated from the data.
    clip (bool): Whether the standardized values of default boxes should be clipped between 0 and 1. The clipping
        is applied while the boxes are encoded in format ``(cx, cy, w, h)``.
r   	min_ratio	max_ratior%   stepsclipc                   > [         T
U ]  5         Ub#  [        U5      [        U5      :w  a  [        S5      eXl        XPl        X`l        [        U5      nUcZ  US:  aK  X2-
  n[        U5       V	s/ s H  oX-  US-
  -  -   PM     sn	U l        U R                  R                  S5        OX#/U l        OX@l        U R                  U5      U l        g s  sn	f )Nz3aspect_ratios and steps should have the same lengthr   rx   )r   r   r   
ValueErrorr   r   r   rh   r%   rQ   _generate_wh_pairs	_wh_pairs)r   r   r   r   r%   r   r   num_outputsrange_ratiokr"   s             r   r   DefaultBoxGenerator.__init__   s     	]!3s5z!ARSS*
	-( >Q'3Z_`kZlmZlUV;?kC>O+PPZlm""3'(4 K00= ns   -Cr$   r   r&   r'   r(   c           	         / n[        U5       H  nU R                  U   n[        R                  " U R                  U   U R                  US-      -  5      nXf/Xw//nU R                  U    HR  n	[        R                  " U	5      n
U R                  U   U
-  nU R                  U   U
-  nUR                  X/X//5        MT     UR                  [        R                  " XUS95        M     U$ )Nr   r*   )	rh   r%   mathr1   r   extendrQ   r/   r0   )r   r   r&   r'   r   r   s_k	s_prime_kwh_pairsarsq_arwhs                r   r   &DefaultBoxGenerator._generate_wh_pairs   s     #%	{#A++a.C		$++a.4;;q1u3E"EFI
Y$:;H ((+		"KKNU*KKNU*!! 01	 , U__X6RS $ r;   c                 d    U R                    Vs/ s H  nSS[        U5      -  -   PM     sn$ s  snf )Nr.   )r   r   )r   rs     r   rC   ,DefaultBoxGenerator.num_anchors_per_location   s.    (,(:(:;(:1AAJ(:;;;s   -rE   rl   c                 v   / n[        U5       GH  u  pVU R                  b+  US   U R                  U   -  nUS   U R                  U   -  nOUu  p[        R                  " SUS   5      S-   U-  R	                  US9n	[        R                  " SUS   5      S-   U-  R	                  US9n
[        R
                  " XSS9u  pUR                  S5      nUR                  S5      n[        R                  " X4[        U R                  U   5      -  SS9R                  SS	5      nU R                  (       a  U R                  U   R                  SSS
9OU R                  U   nUR                  US   US   -  S5      n[        R                  " X4SS9nUR                  U5        GM     [        R                  " USS9$ )Nr   r   rw   r&   rH   rI   r+   r,   r.   )minmax)	enumerater   r/   rM   r=   rO   rP   r3   r   r   r   clamprepeatrj   rQ   )r   rE   rl   r&   default_boxesr   f_kx_f_ky_f_krX   rY   rZ   r[   r\   _wh_pairr   default_boxs                    r   _grid_default_boxes'DefaultBoxGenerator._grid_default_boxes   s    
+FAzz%"1

15"1

15"aQ036%?CC%CPHaQ036%?CC%CPH$~~h4PGoob)Goob)G[['!3c$..:K6L!LRTU]]^`bcdF@D		t~~a(..1!.<t~~^_O`HAQ!<H))V$6A>K  -+ ,. yyA..r;   c           
          U R                   R                   SU R                   SU R                   SU R                   SU R
                   S3
nU$ )Nz(aspect_ratios=z, clip=z	, scales=z, steps=))r"   ry   r   r   r%   r   )r   r   s     r   __repr__DefaultBoxGenerator.__repr__   sX    ~~&&' (!//0dii[}tzzl 	
 r;   r_   r`   c           
      6   U Vs/ s H  o3R                   SS  PM     nnUR                  R                   SS  nUS   R                  US   R                  pvU R	                  XEUS9nUR                  U5      n/ n	[        R                  " US   US   /UR                  S9n
UR                   He  nUn[        R                  " US S 2S S24   SUS S 2SS 24   -  -
  U
-  US S 2S S24   SUS S 2SS 24   -  -   U
-  /S5      nU	R                  U5        Mg     U	$ s  snf )	Nrb   r   r   r   )r'   r.   rw   r+   )rc   rd   r&   r'   r   r=   r/   tensorri   rj   rQ   )r   r_   r`   rk   rE   rl   r&   r'   r   dboxesx_y_sizero   dboxes_in_images                r   rs   DefaultBoxGenerator.forward   s=   @LM'',
M''--bc2
$Q--|A/E/Ev00u0U%((0<<A
1 >}G[G[\''A+O#ii$QU+cOAqrE4J.JJhV$QU+cOAqrE4J.JJhV O MM/* ( % Ns   D)r   r   r   r%   r   )g333333?g?NNT)ry   rz   r{   r|   r}   r   r   r   r   boolr   r/   r   r'   r&   r   r   rC   r   strr   r   rs   r   r   r   s   @r   r   r      sF   (  (,%)>DI> > 	>
 e%> S	"> > >> 6;]][`[g[ghm[n',{{LQLL	f(<$s) < X]WdWd/tCy//7;Cy/IN/	/:	# 	) 4< DQWL  r;   r   )r   typingr   r/   r   r   r_   r   Moduler	   r   r   r;   r   <module>r      s9        !{bii {|D")) Dr;   