ó
    Eñi}.  ã                   ó˜  • S SK JrJr  S SKrS SKrS SKrS SKJrJr  S SKJ	r	  SSK
Jr  SSKJr  \R                  R                  S	\S
\\   S\4S j5       r   S(S\S\S\S\S\4
S jjr " S S5      rS\\   S\4S jrS\S\\   S\4S jr\R.                  R0                  S\\   S\\\\4      S\S\S\\\   \4   4
S j5       r\R.                  R0                  S\\\4   S\\   S\\   4S j5       r\R.                  R0                  S \\   S\\   S!\\   S"\S#\\\      S$\\   S\4S% j5       r " S& S'\R>                  5      r g))é    )ÚOptionalÚUnionN)ÚnnÚTensor)Úbox_areaé   )Ú_log_api_usage_onceé   )Ú	roi_alignÚlevelsÚunmerged_resultsÚreturnc           	      óN  • US   nUR                   UR                  pC[        R                  " U R	                  S5      UR	                  S5      UR	                  S5      UR	                  S5      4X4S9n[        [        U5      5       H›  n[        R                  " X:H  5      S   R                  SSSS5      nUR                  UR	                  S5      X   R	                  S5      X   R	                  S5      X   R	                  S5      5      nUR                  SXqU   5      nM�     U$ )Nr   r
   r   é   ©ÚdtypeÚdeviceéÿÿÿÿ)r   r   ÚtorchÚzerosÚsizeÚrangeÚlenÚwhereÚviewÚexpandÚscatter)r   r   Úfirst_resultr   r   ÚresÚlevelÚindexs           ÚT/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torchvision/ops/poolers.pyÚ_onnx_merge_levelsr#      s  € à# AÑ&€LØ ×&Ñ&¨×(;Ñ(;ˆ6Ü
�+Š+Ø	�‰�Q‹˜×*Ñ*¨1Ó-¨|×/@Ñ/@ÀÓ/CÀ\×EVÑEVÐWXÓEYÐZÐbgñ€Cô ”sÐ+Ó,Ö-ˆÜ—’˜F™OÓ,¨QÑ/×4Ñ4°R¸¸A¸qÓAˆØ—‘Ø�J‰J�q‹MØÑ#×(Ñ(¨Ó+ØÑ#×(Ñ(¨Ó+ØÑ#×(Ñ(¨Ó+ó	
ˆð �k‰k˜!˜U°UÑ$;Ó<Šñ .ð €Jó    Úk_minÚk_maxÚcanonical_scaleÚcanonical_levelÚepsc                 ó   • [        XX#U5      $ ©N)ÚLevelMapper)r%   r&   r'   r(   r)   s        r"   ÚinitLevelMapperr-   %   s   € ô �u _ÀsÓKÐKr$   c                   óX   • \ rS rSrSr   SS\S\S\S\S\4
S jjrS	\\	   S
\	4S jr
Srg)r,   é/   zÖDetermine which FPN level each RoI in a set of RoIs should map to based
on the heuristic in the FPN paper.

Args:
    k_min (int)
    k_max (int)
    canonical_scale (int)
    canonical_level (int)
    eps (float)
r%   r&   r'   r(   r)   c                 ó@   • Xl         X l        X0l        X@l        XPl        g r+   )r%   r&   Ús0Úlvl0r)   )Úselfr%   r&   r'   r(   r)   s         r"   Ú__init__ÚLevelMapper.__init__;   s   € ð Œ
ØŒ
Ø!ŒØ#Œ	Ø�r$   Úboxlistsr   c           
      óR  • [         R                  " [         R                  " U Vs/ s H  n[        U5      PM     sn5      5      n[         R                  " U R
                  [         R                  " X0R                  -  5      -   [         R                  " U R                  UR                  S9-   5      n[         R                  " X@R                  U R                  S9nUR                  [         R                  5      U R                  -
  R                  [         R                  5      $ s  snf )z$
Args:
    boxlists (list[BoxList])
©r   )ÚminÚmax)r   ÚsqrtÚcatr   Úfloorr2   Úlog2r1   Útensorr)   r   Úclampr%   r&   ÚtoÚint64)r3   r6   ÚboxlistÚsÚtarget_lvlss        r"   Ú__call__ÚLevelMapper.__call__I   s¾   € ô �JŠJ”u—y’yÁ8Ó!LÂ8¸¤(¨7Ö"3Á8Ñ!LÓMÓNˆô —k’k $§)¡)¬e¯jªj¸¿W¹W¹Ó.EÑ"EÌÏÊÐUY×U]ÑU]Ðef×elÑelÑHmÑ"mÓnˆÜ—k’k +·:±:À4Ç:Á:ÑNˆØ—‘œuŸ{™{Ó+¨d¯j©jÑ8×<Ñ<¼U¿[¹[ÓIÐIùò "Ms   ¥D$)r)   r&   r%   r2   r1   N©éà   é   g�íµ ÷Æ°>)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚintÚfloatr4   Úlistr   rF   Ú__static_attributes__© r$   r"   r,   r,   /   sa   † ñ	ð  #Ø Øñàðð ðð ð	ð
 ðð õðJ  f¡ð J°&÷ Jr$   r,   Úboxesc                 óT  • [         R                  " U SS9nUR                  UR                  p2[         R                  " [	        U 5       VVs/ s H3  u  pE[         R
                  " US S 2S S24   XC[         R                  US9PM5     snnSS9n[         R                  " Xa/SS9nU$ s  snnf )Nr   )Údimr
   )r   Úlayoutr   )r   r<   r   r   Ú	enumerateÚ	full_likeÚstrided)rU   Úconcat_boxesr   r   ÚiÚbÚidsÚroiss           r"   Ú_convert_to_roi_formatra   W   s”   € Ü—9’9˜U¨Ñ*€LØ ×'Ñ'¨×);Ñ);ˆEÜ
�)Š)ÜdmÐnsÔdtÔuÒdtÑ\`Ð\]Œ�Š˜š1˜b˜q˜b˜5™ 1¼%¿-¹-ÐPVÔ	WÑdtÒuØñ€Cô �9Š9�cÐ(¨aÑ0€DØ€Kùó	 	vs   Á:B$
ÚfeatureÚoriginal_sizec                 ó"  • U R                   SS  n/ n[        X!5       Hk  u  pE[        U5      [        U5      -  nS[        [        R                  " U5      R                  5       R                  5       5      -  nUR                  U5        Mm     US   $ )Néþÿÿÿr   r   )ÚshapeÚziprQ   r   r?   r>   ÚroundÚappend)rb   rc   r   Úpossible_scalesÚs1Ús2Úapprox_scaleÚscales           r"   Ú_infer_scalero   b   s   € à�=‰=˜˜Ð€DØ#%€OÜ�dÖ*‰ˆÜ˜R“y¤5¨£9Ñ,ˆØ”Uœ5Ÿ<š<¨Ó5×:Ñ:Ó<×BÑBÓDÓEÑEˆØ×Ñ˜uÖ%ñ +ð ˜1ÑÐr$   ÚfeaturesÚimage_shapesc                 ó(  • U(       d  [        S5      eSnSnU H!  n[        US   U5      n[        US   U5      nM#     XE4nU  Vs/ s H  n[        X‡5      PM     n	n[        R                  " [        R
                  " U	S   [        R                  S95      R                  5       * n
[        R                  " [        R
                  " U	S   [        R                  S95      R                  5       * n[        [        U
5      [        U5      UUS9nXœ4$ s  snf )Nzimages list should not be emptyr   r
   r8   r   ©r'   r(   )
Ú
ValueErrorr:   ro   r   r>   r?   Úfloat32Úitemr-   rP   )rp   rq   r'   r(   Úmax_xÚmax_yrf   Úoriginal_input_shapeÚfeatÚscalesÚlvl_minÚlvl_maxÚ
map_levelss                r"   Ú_setup_scalesr   m   sõ   € ö ÜÐ:Ó;Ð;Ø€EØ€EÛˆÜ�E˜!‘H˜eÓ$ˆÜ�E˜!‘H˜eÓ$Šñ ð "˜>ÐáCKÓLÂ8¸4Œl˜4Ö6Á8€FÐLô �zŠzœ%Ÿ,š, v¨a¡y¼¿¹ÑFÓG×LÑLÓNÐN€GÜ�zŠzœ%Ÿ,š, v¨b¡z¼¿¹ÑGÓH×MÑMÓOÐO€Gä ÜˆG‹ÜˆG‹Ø'Ø'ñ	€Jð ÐÐùò Ms   ÁDÚxÚfeatmap_namesc                 ól   • / nU R                  5        H  u  p4X1;   d  M  UR                  U5        M     U$ r+   )Úitemsri   )r€   r�   Ú
x_filteredÚkÚvs        r"   Ú_filter_inputr‡   ‰   s5   € à€JØ—‘–	‰ˆØÕØ×Ñ˜aÖ ñ ð Ðr$   r„   Úoutput_sizeÚsampling_ratior{   Úmapperc           	      óÞ  • Ub  Uc  [        S5      e[        U 5      n[        U5      nUS:X  a  [        U S   UUUS   US9$ U" U5      n[        U5      n	U S   R                  S   n
U S   R
                  U S   R                  pË[        R                  " U	U
4U-   UUS9n/ n[        [        X5      5       H�  u  nu  nn[        R                  " X�:H  5      S   nUU   n[        UUUUUS9n[        R                  " 5       (       a"  UR                  UR                  U5      5        Mr  UR                  UR
                  5      UU'   M’     [        R                  " 5       (       a  [!        XŽ5      nU$ )aµ  
Args:
    x_filtered (List[Tensor]): List of input tensors.
    boxes (List[Tensor[N, 4]]): boxes to be used to perform the pooling operation, in
        (x1, y1, x2, y2) format and in the image reference size, not the feature map
        reference. The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
    output_size (Union[List[Tuple[int, int]], List[int]]): size of the output
    sampling_ratio (int): sampling ratio for ROIAlign
    scales (Optional[List[float]]): If None, scales will be automatically inferred. Default value is None.
    mapper (Optional[LevelMapper]): If none, mapper will be automatically inferred. Default value is None.
Returns:
    result (Tensor)
z$scales and mapper should not be Noner
   r   )rˆ   Úspatial_scaler‰   r   )rt   r   ra   r   rf   r   r   r   r   rY   rg   r   ÚtorchvisionÚ_is_tracingri   rA   r#   )r„   rU   rˆ   r‰   r{   rŠ   Ú
num_levelsr`   r   Únum_roisÚnum_channelsr   r   ÚresultÚtracing_resultsr    Úper_level_featurern   Úidx_in_levelÚrois_per_levelÚresult_idx_in_levels                        r"   Ú_multiscale_roi_alignr˜   ’   s‹  € ð, �~˜™ÜÐ?Ó@Ð@ä�Z“€JÜ! %Ó(€Dà�QƒÜØ�q‰MØØ#Ø  ™)Ø)ñ
ð 	
ñ �E‹]€Fä�4‹y€HØ˜a‘=×&Ñ& qÑ)€Là˜q‘M×'Ñ'¨°A©×)=Ñ)=ˆ6Ü�[Š[àØð	
ð ñ		ð
 Øñ€Fð €OÜ-6´s¸:Ó7NÖ-OÑ)ˆÑ)Ð! 5Ü—{’{ 6¡?Ó3°AÑ6ˆØ˜lÑ+ˆä'ØØØ#ØØ)ñ
Ðô ×"Ò"×$Ñ$Ø×"Ñ"Ð#6×#9Ñ#9¸%Ó#@ÖAð $7×#9Ñ#9¸&¿,¹,Ó#GˆF�<Ó ñ- .Pô0 ×Ò× Ñ Ü# FÓ<ˆà€Mr$   c                   óÐ   ^ • \ rS rSrSr\\\      \\   S.r	SSS.S\\
   S\\\\   \\   4   S	\S
\S\4
U 4S jjjrS\\
\4   S\\   S\\\\4      S\4S jrS\
4S jrSrU =r$ )ÚMultiScaleRoIAlignéæ   a  
Multi-scale RoIAlign pooling, which is useful for detection with or without FPN.

It infers the scale of the pooling via the heuristics specified in eq. 1
of the `Feature Pyramid Network paper <https://arxiv.org/abs/1612.03144>`_.
They keyword-only parameters ``canonical_scale`` and ``canonical_level``
correspond respectively to ``224`` and ``k0=4`` in eq. 1, and
have the following meaning: ``canonical_level`` is the target level of the pyramid from
which to pool a region of interest with ``w x h = canonical_scale x canonical_scale``.

Args:
    featmap_names (List[str]): the names of the feature maps that will be used
        for the pooling.
    output_size (List[Tuple[int, int]] or List[int]): output size for the pooled region
    sampling_ratio (int): sampling ratio for ROIAlign
    canonical_scale (int, optional): canonical_scale for LevelMapper
    canonical_level (int, optional): canonical_level for LevelMapper

Examples::

    >>> m = torchvision.ops.MultiScaleRoIAlign(['feat1', 'feat3'], 3, 2)
    >>> i = OrderedDict()
    >>> i['feat1'] = torch.rand(1, 5, 64, 64)
    >>> i['feat2'] = torch.rand(1, 5, 32, 32)  # this feature won't be used in the pooling
    >>> i['feat3'] = torch.rand(1, 5, 16, 16)
    >>> # create some random bounding boxes
    >>> boxes = torch.rand(6, 4) * 256; boxes[:, 2:] += boxes[:, :2]
    >>> # original image size, before computing the feature maps
    >>> image_sizes = [(512, 512)]
    >>> output = m(i, [boxes], image_sizes)
    >>> print(output.shape)
    >>> torch.Size([6, 5, 3, 3])

)r{   r~   rI   rJ   rs   r�   rˆ   r‰   r'   r(   c                óÔ   >• [         TU ]  5         [        U 5        [        U[        5      (       a  X"4nXl        X0l        [        U5      U l        S U l	        S U l
        X@l        XPl        g r+   )Úsuperr4   r	   Ú
isinstancerP   r�   r‰   Útuplerˆ   r{   r~   r'   r(   )r3   r�   rˆ   r‰   r'   r(   Ú	__class__s         €r"   r4   ÚMultiScaleRoIAlign.__init__  s`   ø€ ô 	‰ÑÔÜ˜DÔ!Ü�k¤3×'Ñ'Ø&Ð4ˆKØ*ÔØ,ÔÜ  Ó-ˆÔØˆŒØˆŒØ.ÔØ.Õr$   r€   rU   rq   r   c                 ó,  • [        XR                  5      nU R                  b  U R                  c.  [	        XCU R
                  U R                  5      u  U l        U l        [        UUU R                  U R                  U R                  U R                  5      $ )a¨  
Args:
    x (OrderedDict[Tensor]): feature maps for each level. They are assumed to have
        all the same number of channels, but they can have different sizes.
    boxes (List[Tensor[N, 4]]): boxes to be used to perform the pooling operation, in
        (x1, y1, x2, y2) format and in the image reference size, not the feature map
        reference. The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
    image_shapes (List[Tuple[height, width]]): the sizes of each image before they
        have been fed to a CNN to obtain feature maps. This allows us to infer the
        scale factor for each one of the levels to be pooled.
Returns:
    result (Tensor)
)
r‡   r�   r{   r~   r   r'   r(   r˜   rˆ   r‰   )r3   r€   rU   rq   r„   s        r"   ÚforwardÚMultiScaleRoIAlign.forward!  s‚   € ô& # 1×&8Ñ&8Ó9ˆ
Ø�;‰;Ñ $§/¡/Ñ"9Ü+8Ø¨$×*>Ñ*>À×@TÑ@Tó,Ñ(ˆDŒK˜œô %ØØØ×ÑØ×ÑØ�K‰KØ�O‰Oó
ð 	
r$   c                 ó‚   • U R                   R                   SU R                   SU R                   SU R                   S3$ )Nz(featmap_names=z, output_size=z, sampling_ratio=Ú))r    rK   r�   rˆ   r‰   )r3   s    r"   Ú__repr__ÚMultiScaleRoIAlign.__repr__C  sM   € à�~‰~×&Ñ&Ð' °t×7IÑ7IÐ6Jð KØ×+Ñ+Ð,Ð,=¸d×>QÑ>QÐ=RÐRSðUð	
r$   )r(   r'   r�   r~   rˆ   r‰   r{   )rK   rL   rM   rN   rO   r   rR   rQ   r,   Ú__annotations__Ústrr   rP   rŸ   r4   Údictr   r£   r§   rS   Ú__classcell__)r    s   @r"   rš   rš   æ   sÔ   ø† ñ!ðF "*¨$¨u©+Ñ!6ÀhÈ{ÑF[Ñ\€Oð  #Ø ò/à˜C‘yð/ð ˜3  c¡
¨D°©IÐ5Ñ6ð/ð ð	/ð ð/ð ÷/ð /ð* 
à��V�Ñð 
ð �F‰|ð 
ð ˜5  c ™?Ñ+ð	 
ð
 
ô 
ðD
˜#÷ 
ò 
r$   rš   rH   )!Útypingr   r   r   Útorch.fxr�   r   r   Útorchvision.ops.boxesr   Úutilsr	   r   ÚjitÚunusedrR   r#   rP   rQ   r-   r,   ra   ro   ÚfxÚwraprŸ   r   r«   rª   r‡   r˜   ÚModulerš   rT   r$   r"   Ú<module>r¶      s  ðß "ã Û Û ß Ý *å 'Ý  ð ‡�×Ñð˜vð ¸¸f¹ð È&ó ó ðð, ØØñLØðLàðLð ðLð ð	Lð
 
õL÷%Jñ %JðP $ v¡,ð °6ô ð˜&ð °°c±ð ¸uô ð ‡�‡�ðØ�6‰lðØ*.¨u°S¸#°X©Ñ*?ðØRUðØhkðà
ˆ4�‰;˜Ð#Ñ$óó ðð6 ‡�‡�ð�T˜#˜v˜+Ñ&ð °t¸C±yð ÀTÈ&Á\ó ó ðð ‡�‡�ðPØ�V‘ðPà�‰<ðPð �c‘ðPð ð	Pð
 �T˜%‘[Ñ!ðPð �[Ñ!ðPð óPó ðPôfa
˜Ÿ™õ a
r$   