Ë
    èÓ^jN.  ã                   ó�   — d dl Z d dlmZ d dlZd dlmZmZ ddlmZ  G d„ dej                  «      Z	 G d„ d	ej                  «      Z
y)
é    N)ÚOptional)ÚnnÚTensoré   )Ú	ImageListc                   óp  ‡ — e Zd ZdZdeej                     iZ	 	 dˆ fd„	Zej                   ej                  d«      fdee   dee   dej                  dej                  d	ef
d
„Zdej                  dej                  fd„Zd	ee   fd„Zdeee      deee      d	ee   fd„Zdedee   d	ee   fd„Zˆ xZS )ÚAnchorGeneratora¨  
    Module that generates anchors for a set of feature maps and
    image sizes.

    The module supports 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                 óX  •— t         ‰| �  «        t        |d   t        t        f«      st	        d„ |D «       «      }t        |d   t        t        f«      s|ft        |«      z  }|| _        || _        t        ||«      D ��cg c]  \  }}| j                  ||«      ‘Œ c}}| _
        y c c}}w )Nr   c              3   ó"   K  — | ]  }|f–— Œ	 y ­w©N© )Ú.0Úss     út/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torchvision/models/detection/anchor_utils.pyú	<genexpr>z+AnchorGenerator.__init__.<locals>.<genexpr>,   s   è ø€ Ò. 1˜1œ$Ñ.ùs   ‚)ÚsuperÚ__init__Ú
isinstanceÚlistÚtupleÚlenÚsizesÚaspect_ratiosÚzipÚgenerate_anchorsr
   )Úselfr   r   ÚsizeÚaspect_ratioÚ	__class__s        €r   r   zAnchorGenerator.__init__#   sœ   ø€ ô
 	‰ÑÔä˜% ™(¤T¬5 MÔ2äÑ.¨Ô.Ó.ˆEÜ˜-¨Ñ*¬T´5¨MÔ:Ø*Ð,¬s°5«zÑ9ˆMàˆŒ
Ø*ˆÔäPSÐTYÐ[hÓPi÷
Ù:L¸$ÀˆD×!Ñ! $¨Õ5ó
ˆÕùó 
s   Â B&ÚcpuÚscalesr   ÚdtypeÚdeviceÚreturnc                 ó~  — t        j                  |||¬«      }t        j                  |||¬«      }t        j                  |«      }d|z  }|d d …d f   |d d d …f   z  j                  d«      }|d d …d f   |d d d …f   z  j                  d«      }t        j                  | | ||gd¬«      dz  }	|	j                  «       S )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   z AnchorGenerator.generate_anchors:   s¼   € ô —‘ ¨u¸VÔDˆÜŸ™¨¸UÈ6ÔRˆÜ—:‘:˜mÓ,ˆØ�x‘<ˆà’q˜$�wÑ &¨ªq¨¡/Ñ1×7Ñ7¸Ó;ˆØ’q˜$�wÑ &¨ªq¨¡/Ñ1×7Ñ7¸Ó;ˆä—{‘{ R C¨"¨¨b°"Ð#5¸1Ô=ÀÑAˆØ×!Ñ!Ó#Ð#ó    c                 ón   — | j                   D �cg c]  }|j                  ||¬«      ‘Œ c}| _         y c c}w )Nr'   )r
   Úto)r   r#   r$   Úcell_anchors       r   Úset_cell_anchorsz AnchorGenerator.set_cell_anchorsL   s+   € Ø[_×[lÑ[lÖmÈK˜[Ÿ^™^°%À˜^ÕGÒmˆÕùÒms   �2c                 óž   — t        | j                  | j                  «      D ��cg c]  \  }}t        |«      t        |«      z  ‘Œ c}}S c c}}w r   )r   r   r   r   )r   r   Úas      r   Únum_anchors_per_locationz(AnchorGenerator.num_anchors_per_locationO   s7   € Ü,/°·
±
¸D×<NÑ<NÓ,O×P¡D A q”�A“œ˜Q›“ÓPÐPùÓPs   ¤!A	Ú
grid_sizesÚstridesc           	      ó  — g }| j                   }t        j                  |d ud«       t        j                  t        |«      t        |«      cxk(  xr t        |«      k(  nc d«       t	        |||«      D �]  \  }}}|\  }}	|\  }
}|j
                  }t        j                  d|	t        j                  |¬«      |z  }t        j                  d|t        j                  |¬«      |
z  }t        j                  ||d¬«      \  }}|j                  d«      }|j                  d«      }t        j                  ||||fd¬	«      }|j                  |j                  ddd
«      |j                  ddd
«      z   j                  dd
«      «       �Œ |S )Nzcell_anchors should not be NonezíAnchors 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Úreshaper0   Úappendr/   )r   r?   r@   Úanchorsr
   r   Ústrider6   Úgrid_heightÚ
grid_widthÚstride_heightÚstride_widthr$   Úshifts_xÚshifts_yÚshift_yÚshift_xÚshiftss                     r   Úgrid_anchorszAnchorGenerator.grid_anchorsT   sc  € ØˆØ×(Ñ(ˆÜ�‰�l¨$Ð.Ð0QÔRÜ�‰Ü�
‹Oœs 7›|Ö@¬s°<Ó/@Ô@ðUô	
ô +.¨j¸'À<Ó*Pó 	aÑ&ˆD�&˜,Ø&*Ñ#ˆK˜Ø*0Ñ'ˆM˜<Ø!×(Ñ(ˆFô —|‘| A z¼¿¹ÈVÔTÐWcÑcˆHÜ—|‘| A {¼%¿+¹+ÈfÔUÐXeÑeˆHÜ$Ÿ~™~¨h¸È4ÔPÑˆG�WØ—o‘o bÓ)ˆGØ—o‘o bÓ)ˆGÜ—[‘[ '¨7°G¸WÐ!EÈ1ÔMˆFð �N‰N˜FŸK™K¨¨A¨qÓ1°L×4EÑ4EÀaÈÈQÓ4OÑO×XÑXÐY[Ð]^Ó_Ö`ð	að" ˆr7   Ú
image_listÚfeature_mapsc           	      óü  — |D �cg c]  }|j                   dd  ‘Œ }}|j                  j                   dd  }|d   j                  |d   j                  }}|D �cg c]~  }t	        j
                  dt        j                  |¬«      j                  |d   |d   z  «      t	        j
                  dt        j                  |¬«      j                  |d   |d   z  «      g‘Œ€ }	}| j                  ||«       | j                  ||	«      }
g }t        t        |j                  «      «      D ]"  }|
D �cg c]  }|‘Œ }}|j                  |«       Œ$ |D �cg c]  }t	        j                  |«      ‘Œ }}|S c c}w c c}w c c}w c c}w )Néþÿÿÿr   r   r'   r   )ÚshapeÚtensorsr#   r$   r,   ÚemptyÚint64Úfill_r;   rW   Úranger   Úimage_sizesrK   Úcat)r   rX   rY   Úfeature_mapr?   Ú
image_sizer#   r$   Úgr@   Úanchors_over_all_feature_mapsrL   Ú_Úanchors_per_feature_mapÚanchors_in_imageÚanchors_per_images                   r   ÚforwardzAnchorGenerator.forwards   s�  € Ø@LÖM°�k×'Ñ'¨¨Ò,ÐMˆ
ÐMØ×'Ñ'×-Ñ-¨b¨cÐ2ˆ
Ø$ Q™×-Ñ-¨|¸A©×/EÑ/Eˆvˆð  ö
ð
 ô —‘˜B¤e§k¡k¸&ÔA×GÑGÈ
ÐSTÉÐYZÐ[\ÑY]ÑH]Ó^Ü—‘˜B¤e§k¡k¸&ÔA×GÑGÈ
ÐSTÉÐYZÐ[\ÑY]ÑH]Ó^òð
ˆð 
ð 	×Ñ˜e VÔ,Ø(,×(9Ñ(9¸*ÀgÓ(NÐ%Ø,.ˆÜ”s˜:×1Ñ1Ó2Ó3ò 	-ˆAØWtÖuÐ<SÒ 7ÐuÐÐuØ�N‰NÐ+Õ,ð	-ð JQÖQÐ4E”5—9‘9Ð.Õ/ÐQˆÐQØˆùò# Nùò
ùò  vùâQs   …E*ÁBE/Ä(	E4Å
E9)))é€   é   i   ))ç      à?ç      ð?g       @)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   r   Ú__annotations__r   Úfloat32r$   ÚintÚfloatr#   r   r;   r>   rW   r   rl   Ú__classcell__©r    s   @r   r	   r	   
   s  ø„ ñð* 	˜˜UŸ\™\Ñ*ð€Oð !Ø(õ
ð6 #Ÿ]™]Ø+˜uŸ|™|¨EÓ2ñ$à�S‘	ð$ð ˜E‘{ð$ð �{‰{ð	$ð
 —‘ð$ð 
ó$ð$n e§k¡kð n¸5¿<¹<ó nðQ¨$¨s©)ó Qð
 t¨D°©I¡ð ÀÀdÈ6ÁlÑASð ÐX\Ð]cÑXdó ð> )ð ¸4À¹<ð ÈDÐQWÉL÷ r7   r	   c                   ó€  ‡ — e Zd ZdZ	 	 	 	 	 ddeee      dededeee      deee      defˆ fd„Z	e
j                   e
j                  d	«      fd
ede
j                  de
j                  dee   fd„Zdee   fd„Ze
j                  fdeee      dee   de
j                  defd„Zdefd„Zdedee   dee   fd„Zˆ xZS )ÚDefaultBoxGeneratoraµ  
    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         ‰
| �  «        |�"t        |«      t        |«      k7  rt        d«      ‚|| _        || _        || _        t        |«      }|€Z|dkD  rK||z
  }t        |«      D �	cg c]  }	|||	z  |dz
  z  z   ‘Œ c}	| _        | j                  j                  d«       n||g| _        n|| _        | j                  |«      | _        y c c}	w )Nz3aspect_ratios and steps should have the same lengthr   rp   )r   r   r   Ú
ValueErrorr   r   r€   ra   r"   rK   Ú_generate_wh_pairsÚ	_wh_pairs)r   r   r}   r~   r"   r   r€   Únum_outputsÚrange_ratioÚkr    s             €r   r   zDefaultBoxGenerator.__init__š   sÓ   ø€ ô 	‰ÑÔØÐ¤ ]Ó!3´s¸5³zÒ!AÜÐRÓSÐSØ*ˆÔØˆŒ
ØˆŒ	Ü˜-Ó(ˆð ˆ>Ø˜QŠØ'¨)Ñ3�ÜZ_Ð`kÓZlÖmÐUV˜y¨;¸©?¸kÈCÑ>OÑ+PÓPÒm�”Ø—‘×"Ñ" 3Õ'à(¨)Ð4�•à ˆDŒKà×0Ñ0°Ó=ˆ�ùò ns   Á-Cr!   r…   r#   r$   r%   c                 óØ  — g }t        |«      D ]Ù  }| j                  |   }t        j                  | j                  |   | j                  |dz      z  «      }||g||gg}| j                  |   D ]R  }	t        j                  |	«      }
| j                  |   |
z  }| j                  |   |
z  }|j                  ||g||gg«       ŒT |j                  t        j                  |||¬«      «       ŒÛ |S )Nr   r'   )	ra   r"   Úmathr.   r   ÚextendrK   r,   r-   )r   r…   r#   r$   r„   r‡   Ús_kÚ	s_prime_kÚwh_pairsÚarÚsq_arÚwÚhs                r   rƒ   z&DefaultBoxGenerator._generate_wh_pairs¸   sò   € ð #%ˆ	Ü�{Ó#ò 	TˆAà—+‘+˜a‘.ˆCÜŸ	™	 $§+¡+¨a¡.°4·;±;¸qÀ1¹uÑ3EÑ"EÓFˆIØ˜c˜
 Y°	Ð$:Ð;ˆHð ×(Ñ(¨Ñ+ò 2�ÜŸ	™	 "›�Ø—K‘K ‘N UÑ*�Ø—K‘K ‘N UÑ*�Ø—‘ ! Q ¨!¨Q¨Ð 0Õ1ð	2ð ×ÑœUŸ_™_¨X¸UÈ6ÔRÕSð	Tð Ðr7   c                 ó^   — | j                   D �cg c]  }ddt        |«      z  z   ‘Œ c}S c c}w )Nr+   )r   r   )r   Úrs     r   r>   z,DefaultBoxGenerator.num_anchors_per_locationÌ   s(   € à(,×(:Ñ(:Ö; 1��Aœ˜A›‘J“Ò;Ð;ùÒ;s   �*r?   re   c                 ó�  — g }t        |«      D �]Ÿ  \  }}| j                  �+|d   | j                  |   z  }|d   | j                  |   z  }n|\  }}t        j                  d|d   «      dz   |z  j	                  |¬«      }	t        j                  d|d   «      dz   |z  j	                  |¬«      }
t        j
                  |
|	d¬«      \  }}|j                  d«      }|j                  d«      }t        j                  ||ft        | j                  |   «      z  d¬«      j                  dd	«      }| j                  r | j                  |   j                  dd¬
«      n| j                  |   }|j                  |d   |d   z  d«      }t        j                  ||fd¬«      }|j                  |«       �Œ¢ t        j                  |d¬«      S )Nr   r   ro   ©r#   rB   rC   r(   r)   r+   )ÚminÚmax)Ú	enumerater   r,   rG   r9   rI   rJ   r0   r   r„   r€   ÚclampÚrepeatrc   rK   )r   r?   re   r#   Údefault_boxesr‡   Úf_kÚx_f_kÚy_f_krR   rS   rT   rU   rV   Ú_wh_pairr�   Údefault_boxs                    r   Ú_grid_default_boxesz'DefaultBoxGenerator._grid_default_boxesÑ   s¥  € ð ˆÜ 
Ó+ó 	.‰FˆAˆsà�z‰zÐ%Ø" 1™¨¯
©
°1©Ñ5�Ø" 1™¨¯
©
°1©Ñ5‘à"‘��uäŸ™ a¨¨Q©Ó0°3Ñ6¸%Ñ?×CÑCÈ%ÐCÓPˆHÜŸ™ a¨¨Q©Ó0°3Ñ6¸%Ñ?×CÑCÈ%ÐCÓPˆHÜ$Ÿ~™~¨h¸È4ÔPÑˆG�WØ—o‘o bÓ)ˆGØ—o‘o bÓ)ˆGä—[‘[ '¨7Ð!3´c¸$¿.¹.ÈÑ:KÓ6LÑ!LÐRTÔU×]Ñ]Ð^`ÐbcÓdˆFà@DÇ	Â	�t—~‘~ aÑ(×.Ñ.°1¸!Ð.Ô<ÈtÏ~É~Ð^_ÑO`ˆHØ—‘¨¨A©°°Q±©¸!Ó<ˆHäŸ)™) V¨XÐ$6¸AÔ>ˆKà× Ñ  Ö-ð+	.ô. �y‰y˜¨AÔ.Ð.r7   c           
      ó    — | j                   j                  › d| j                  › d| j                  › d| j                  › d| j
                  › d�
}|S )Nz(aspect_ratios=z, clip=z	, scales=z, steps=ú))r    rq   r   r€   r"   r   )r   r   s     r   Ú__repr__zDefaultBoxGenerator.__repr__î   sX   € à�~‰~×&Ñ&Ð'ð (Ø!×/Ñ/Ð0Ø�d—i‘i�[Ø˜Ÿ™�}Ø�t—z‘z�lØðð 	
ð ˆr7   rX   rY   c           
      ó:  — |D �cg c]  }|j                   dd  ‘Œ }}|j                  j                   dd  }|d   j                  |d   j                  }}| j	                  |||¬«      }|j                  |«      }g }	t        j                  |d   |d   g|j                  ¬«      }
|j                  D ]c  }|}t        j                  |d d …d d…f   d|d d …dd …f   z  z
  |
z  |d d …d d…f   d|d d …dd …f   z  z   |
z  gd«      }|	j                  |«       Œe |	S c c}w )	Nr[   r   r•   r   )r$   r+   ro   r(   )r\   r]   r#   r$   r¡   r9   r,   Útensorrb   rc   rK   )r   rX   rY   rd   r?   re   r#   r$   r›   ÚdboxesÚx_y_sizerh   Údboxes_in_images                r   rl   zDefaultBoxGenerator.forwardù   sG  € Ø@LÖM°�k×'Ñ'¨¨Ò,ÐMˆ
ÐMØ×'Ñ'×-Ñ-¨b¨cÐ2ˆ
Ø$ Q™×-Ñ-¨|¸A©×/EÑ/EˆvˆØ×0Ñ0°¸ZÈuÐ0ÓUˆØ%×(Ñ(¨Ó0ˆàˆÜ—<‘< ¨A¡°
¸1±Ð >À}×G[ÑG[Ô\ˆØ×'Ñ'ò 		+ˆAØ+ˆOÜ#Ÿi™ià$¢Q¨¨¨ UÑ+¨c°OÂAÀqÁrÀEÑ4JÑ.JÑJÈhÑVØ$¢Q¨¨¨ UÑ+¨c°OÂAÀqÁrÀEÑ4JÑ.JÑJÈhÑVðð óˆOð �M‰M˜/Õ*ð		+ð ˆùò% Ns   …D)g333333Ã?gÍÌÌÌÌÌì?NNT)rq   rr   rs   rt   r   rw   rx   r   Úboolr   r,   rv   r$   r#   r   rƒ   r>   r¡   Ústrr¤   r   rl   ry   rz   s   @r   r|   r|   ˆ   s?  ø„ ñð(  ØØ(,Ø%)Øñ>à˜D ™I‘ð>ð ð>ð ð	>ð
 ˜˜e™Ñ%ð>ð ˜˜S™	Ñ"ð>ð õ>ð> 6;·]±]Ð[gÐ[`×[gÑ[gÐhmÓ[nñØðØ',§{¡{ðØLQÏLÉLðà	ˆf‰óð(<¨$¨s©)ó <ð X]×WdÑWdñ/Ø˜t C™y™/ð/Ø7;¸C±yð/ØINÏÉð/à	ó/ð:	˜#ó 	ð )ð ¸4À¹<ð ÈDÐQWÉL÷ r7   r|   )r‰   Útypingr   r,   r   r   rX   r   ÚModuler	   r|   r   r7   r   ú<module>r®      s9   ðÛ Ý ã ß å !ô{�b—i‘iô {ô|D˜"Ÿ)™)õ Dr7   