Ë
    þÍ:j}.  ã                   óˆ  — d dl mZmZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 ddl
mZ ddlmZ ej                  j                  d	ed
ee   defd„«       Z	 	 	 d(dededededef
d„Z G d„ d«      Zdee   defd„Zdedee   defd„Zej.                  j0                  dee   deeeef      dededeee   ef   f
d„«       Zej.                  j0                  deeef   dee   dee   fd„«       Zej.                  j0                  d ee   dee   d!ee   d"ed#eee      d$ee   defd%„«       Z G d&„ d'ej>                  «      Z y))é    )ÚOptionalÚUnionN)ÚnnÚTensor)Úbox_areaé   )Ú_log_api_usage_onceé   )Ú	roi_alignÚlevelsÚunmerged_resultsÚreturnc           	      óZ  — |d   }|j                   |j                  }}t        j                  | j	                  d«      |j	                  d«      |j	                  d«      |j	                  d«      f||¬«      }t        t        |«      «      D ]ž  }t        j                  | |k(  «      d   j                  dddd«      }|j                  |j	                  d«      ||   j	                  d«      ||   j	                  d«      ||   j	                  d«      «      }|j                  d|||   «      }Œ  |S )Nr   r
   r   é   ©ÚdtypeÚdeviceéÿÿÿÿ)r   r   ÚtorchÚzerosÚsizeÚrangeÚlenÚwhereÚviewÚexpandÚscatter)r   r   Úfirst_resultr   r   ÚresÚlevelÚindexs           úl/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchvision/ops/poolers.pyÚ_onnx_merge_levelsr#      s"  € à# AÑ&€LØ ×&Ñ&¨×(;Ñ(;ˆ6€EÜ
�+‰+Ø	�‰�Q‹˜×*Ñ*¨1Ó-¨|×/@Ñ/@ÀÓ/CÀ\×EVÑEVÐWXÓEYÐZÐbgÐpvô€Cô ”sÐ+Ó,Ó-ò =ˆÜ—‘˜F e™OÓ,¨QÑ/×4Ñ4°R¸¸A¸qÓAˆØ—‘Ø�J‰J�q‹MØ˜UÑ#×(Ñ(¨Ó+Ø˜UÑ#×(Ñ(¨Ó+Ø˜UÑ#×(Ñ(¨Ó+ó	
ˆð �k‰k˜!˜UÐ$4°UÑ$;Ó<‰ð=ð €Jó    Úk_minÚk_maxÚcanonical_scaleÚcanonical_levelÚepsc                 ó    — t        | ||||«      S ©N)ÚLevelMapper)r%   r&   r'   r(   r)   s        r"   ÚinitLevelMapperr-   %   s   € ô �u˜e _°oÀsÓKÐKr$   c                   óJ   — e Zd ZdZ	 	 	 ddededededef
d„Zdee   d	efd
„Z	y)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                 óJ   — || _         || _        || _        || _        || _        y r+   )r%   r&   Ús0Úlvl0r)   )Úselfr%   r&   r'   r(   r)   s         r"   Ú__init__zLevelMapper.__init__;   s'   € ð ˆŒ
ØˆŒ
Ø!ˆŒØ#ˆŒ	Øˆ�r$   Úboxlistsr   c           
      óP  — t        j                  t        j                  |D �cg c]  }t        |«      ‘Œ c}«      «      }t        j                  | j
                  t        j                  || j                  z  «      z   t        j                  | j                  |j                  ¬«      z   «      }t        j                  || j                  | j                  ¬«      }|j                  t         j                  «      | j                  z
  j                  t         j                  «      S c c}w )z<
        Args:
            boxlists (list[BoxList])
        ©r   )ÚminÚmax)r   ÚsqrtÚcatr   Úfloorr1   Úlog2r0   Útensorr)   r   Úclampr%   r&   ÚtoÚint64)r2   r4   ÚboxlistÚsÚtarget_lvlss        r"   Ú__call__zLevelMapper.__call__I   s¾   € ô �J‰J”u—y‘yÀ8Ö!L¸¤(¨7Õ"3Ò!LÓMÓNˆô —k‘k $§)¡)¬e¯j©j¸¸T¿W¹W¹Ó.EÑ"EÌÏÉÐUY×U]ÑU]Ðef×elÑelÔHmÑ"mÓnˆÜ—k‘k +°4·:±:À4Ç:Á:ÔNˆØ—‘œuŸ{™{Ó+¨d¯j©jÑ8×<Ñ<¼U¿[¹[ÓIÐIùò "Ms   £D#N©éà   é   g�íµ ÷Æ°>)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚfloatr3   Úlistr   rD   © r$   r"   r,   r,   /   sa   „ ñ	ð  #Ø Øñàðð ðð ð	ð
 ðð óðJ  f¡ð J°&ô Jr$   r,   Úboxesc                 óf  — t        j                  | d¬«      }|j                  |j                  }}t        j                  t	        | «      D ��cg c]6  \  }}t        j
                  |d d …d d…f   ||t         j                  |¬«      ‘Œ8 c}}d¬«      }t        j                  ||gd¬«      }|S c c}}w )Nr   )Údimr
   )r   Úlayoutr   )r   r:   r   r   Ú	enumerateÚ	full_likeÚstrided)rP   Úconcat_boxesr   r   ÚiÚbÚidsÚroiss           r"   Ú_convert_to_roi_formatr\   W   s—   € Ü—9‘9˜U¨Ô*€LØ ×'Ñ'¨×);Ñ);ˆE€FÜ
�)‰)ÜdmÐnsÓdt×uÑ\`Ð\]Ð_`Œ�‰˜š1˜b˜q˜b˜5™ 1¨E¼%¿-¹-ÐPVÖ	WÓuØô€Cô �9‰9�c˜<Ð(¨aÔ0€DØ€Kùó	 	vs   Á;B-
ÚfeatureÚoriginal_sizec                 ó   — | j                   dd  }g }t        ||«      D ]j  \  }}t        |«      t        |«      z  }dt        t        j                  |«      j                  «       j                  «       «      z  }|j                  |«       Œl |d   S )Néþÿÿÿr   r   )ÚshapeÚziprM   r   r=   r<   ÚroundÚappend)r]   r^   r   Úpossible_scalesÚs1Ús2Úapprox_scaleÚscales           r"   Ú_infer_scalerj   b   sˆ   € à�=‰=˜˜Ð€DØ#%€OÜ�d˜MÓ*ò &‰ˆˆBÜ˜R“y¤5¨£9Ñ,ˆØ”Uœ5Ÿ<™<¨Ó5×:Ñ:Ó<×BÑBÓDÓEÑEˆØ×Ñ˜uÕ%ð&ð ˜1ÑÐr$   ÚfeaturesÚimage_shapesc                 ó$  — |st        d«      ‚d}d}|D ]   }t        |d   |«      }t        |d   |«      }Œ" ||f}| D �cg c]  }t        ||«      ‘Œ }	}t        j                  t        j
                  |	d   t        j                  ¬«      «      j                  «        }
t        j                  t        j
                  |	d   t        j                  ¬«      «      j                  «        }t        t        |
«      t        |«      ||¬«      }|	|fS c c}w )Nzimages list should not be emptyr   r
   r6   r   ©r'   r(   )
Ú
ValueErrorr8   rj   r   r<   r=   Úfloat32Úitemr-   rL   )rk   rl   r'   r(   Úmax_xÚmax_yra   Úoriginal_input_shapeÚfeatÚscalesÚlvl_minÚlvl_maxÚ
map_levelss                r"   Ú_setup_scalesrz   m   s   € ñ ÜÐ:Ó;Ð;Ø€EØ€EØò %ˆÜ�E˜!‘H˜eÓ$ˆÜ�E˜!‘H˜eÓ$‰ð%ð " 5˜>ÐàCKÖL¸4Œl˜4Ð!5Õ6ÐL€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                 óf   — g }| j                  «       D ]  \  }}||v sŒ|j                  |«       Œ |S r+   )Úitemsrd   )r{   r|   Ú
x_filteredÚkÚvs        r"   Ú_filter_inputr‚   ‰   s>   € à€JØ—‘“	ò !‰ˆˆ1Ø�ÒØ×Ñ˜aÕ ð!ð Ðr$   r   Úoutput_sizeÚsampling_ratiorv   Úmapperc                 óÐ  — |�|€t        d«      ‚t        | «      }t        |«      }|dk(  rt        | d   |||d   |¬«      S  ||«      }t        |«      }	| d   j                  d   }
| d   j
                  | d   j                  }}t        j                  |	|
f|z   ||¬«      }g }t        t        | |«      «      D ]‹  \  }\  }}t        j                  ||k(  «      d   }||   }t        |||||¬«      }t        j                  «       r!|j                  |j                  |«      «       Œn|j                  |j
                  «      ||<   Œ� t        j                  «       rt!        ||«      }|S )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   )ro   r   r\   r   ra   r   r   r   r   rT   rb   r   ÚtorchvisionÚ_is_tracingrd   r?   r#   )r   rP   rƒ   r„   rv   r…   Ú
num_levelsr[   r   Únum_roisÚnum_channelsr   r   ÚresultÚtracing_resultsr    Úper_level_featureri   Ú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€EÜ�[‰[àØð	
ð ñ		ð
 Øô€Fð €OÜ-6´s¸:ÀvÓ7NÓ-Oò HÑ)ˆÑ)Ð! 5Ü—{‘{ 6¨U¡?Ó3°AÑ6ˆØ˜lÑ+ˆä'ØØØ#ØØ)ô
Ðô ×"Ñ"Ô$Ø×"Ñ"Ð#6×#9Ñ#9¸%Ó#@ÕAð $7×#9Ñ#9¸&¿,¹,Ó#GˆF�<Ò ð-Hô0 ×ÑÔ Ü# F¨OÓ<ˆà€Mr$   c                   ó¾   ‡ — e Zd ZdZeee      ee   dœZdddœdee	   de
eee   ee   f   ded	ed
ef
ˆ fd„Zdee	ef   dee   deeeef      defd„Zde	fd„Zˆ xZS )ÚMultiScaleRoIAligna{  
    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])

    )rv   ry   rF   rG   rn   r|   rƒ   r„   r'   r(   c                óÔ   •— t         ‰| �  «        t        | «       t        |t        «      r||f}|| _        || _        t        |«      | _        d | _	        d | _
        || _        || _        y r+   )Úsuperr3   r	   Ú
isinstancerL   r|   r„   Útuplerƒ   rv   ry   r'   r(   )r2   r|   rƒ   r„   r'   r(   Ú	__class__s         €r"   r3   zMultiScaleRoIAlign.__init__  sg   ø€ ô 	‰ÑÔÜ˜DÔ!Ü�k¤3Ô'Ø&¨Ð4ˆKØ*ˆÔØ,ˆÔÜ  Ó-ˆÔØˆŒØˆŒØ.ˆÔØ.ˆÕr$   r{   rP   rl   r   c                 ó,  — t        || j                  «      }| j                  �| j                  €/t	        ||| j
                  | j                  «      \  | _        | _        t        ||| j                  | j                  | j                  | j                  «      S )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|   rv   ry   rz   r'   r(   r“   rƒ   r„   )r2   r{   rP   rl   r   s        r"   ÚforwardzMultiScaleRoIAlign.forward!  s†   € ô& # 1 d×&8Ñ&8Ó9ˆ
Ø�;‰;Ð $§/¡/Ð"9Ü+8Ø˜L¨$×*>Ñ*>À×@TÑ@Tó,Ñ(ˆDŒK˜œô %ØØØ×ÑØ×ÑØ�K‰KØ�O‰Oó
ð 	
r$   c                 ó‚   — | j                   j                  › d| j                  › d| j                  › d| j                  › d�S )Nz(featmap_names=z, output_size=z, sampling_ratio=ú))rš   rH   r|   rƒ   r„   )r2   s    r"   Ú__repr__zMultiScaleRoIAlign.__repr__C  sM   € à�~‰~×&Ñ&Ð' °t×7IÑ7IÐ6Jð KØ×+Ñ+Ð,Ð,=¸d×>QÑ>QÐ=RÐRSðUð	
r$   )rH   rI   rJ   rK   r   rN   rM   r,   Ú__annotations__Ústrr   rL   r™   r3   Údictr   rœ   rŸ   Ú__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•   rE   )!Útypingr   r   r   Útorch.fxrˆ   r   r   Útorchvision.ops.boxesr   Úutilsr	   r   ÚjitÚunusedrN   r#   rL   rM   r-   r,   r\   rj   ÚfxÚwrapr™   rz   r¢   r¡   r‚   r“   ÚModuler•   rO   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$   