§
    ù²Åj×/  ã                   óÆ  — d dl mZmZmZ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        j        dee
         deeeef                  dededeee         ef         f
d„¦   «         Zej        j        d eee
f         d!ee         dee
         fd"„¦   «         Zej        j        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 dS ),é    )ÚDictÚListÚOptionalÚTupleÚUnionN)ÚnnÚTensor©Úbox_areaé   )Ú_log_api_usage_onceé   )Ú	roi_alignÚlevelsÚunmerged_resultsÚreturnc           	      óÌ  — |d         }|j         |j        }}t          j        |                      d¦  «        |                     d¦  «        |                     d¦  «        |                     d¦  «        f||¬¦  «        }t          t          |¦  «        ¦  «        D ]É}t          j        | |k    ¦  «        d                              dddd¦  «        }| 	                    |                     d¦  «        ||                              d¦  «        ||                              d¦  «        ||                              d¦  «        ¦  «        }| 
                    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           úHC:\Projects\Historical\venv\Lib\site-packages\torchvision/ops/poolers.pyÚ_onnx_merge_levelsr'      sL  € à# AÔ&€LØ Ô&¨Ô(;ˆ6€EÝ
Œ+Ø	�Š�Q‰Œ˜×*Ò*¨1Ñ-Ô-¨|×/@Ò/@ÀÑ/CÔ/CÀ\×EVÒEVÐWXÑEYÔEYÐZÐbgÐpvðñ ô €Cõ •sÐ+Ñ,Ô,Ñ-Ô-ð =ð =ˆÝ”˜F ešOÑ,Ô,¨QÔ/×4Ò4°R¸¸A¸qÑAÔAˆØ—’Ø�JŠJ�q‰MŒ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/   r0   s        r&   ÚinitLevelMapperr4   %   s   € õ �u˜e _°oÀsÑKÔKÐKr(   c                   óR   — 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	dS )r3   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-   r.   r/   r0   c                 óL   — || _         || _        || _        || _        || _        d S r2   )r,   r-   Ús0Úlvl0r0   )Úselfr,   r-   r.   r/   r0   s         r&   Ú__init__zLevelMapper.__init__;   s+   € ð ˆŒ
ØˆŒ
Ø!ˆŒØ#ˆŒ	ØˆŒˆˆr(   Úboxlistsr   c                 óÚ  — t          j        t          j        d„ |D ¦   «         ¦  «        ¦  «        }t          j        | j        t          j        || j        z  ¦  «        z   t          j        | j        |j	        ¬¦  «        z   ¦  «        }t          j
        || j        | j        ¬¦  «        }|                     t           j        ¦  «        | j        z
                       t           j        ¦  «        S )z<
        Args:
            boxlists (list[BoxList])
        c                 ó,   — g | ]}t          |¦  «        ‘ŒS © r
   )Ú.0Úboxlists     r&   ú
<listcomp>z(LevelMapper.__call__.<locals>.<listcomp>O   s    € Ð!LÐ!LÐ!L¸¥(¨7Ñ"3Ô"3Ð!LÐ!LÐ!Lr(   ©r   )ÚminÚmax)r   ÚsqrtÚcatÚfloorr8   Úlog2r7   Útensorr0   r   Úclampr,   r-   ÚtoÚint64)r9   r;   ÚsÚtarget_lvlss       r&   Ú__call__zLevelMapper.__call__I   sµ   € õ ŒJ•u”yÐ!LÐ!LÀ8Ð!LÑ!LÔ!LÑMÔMÑNÔNˆõ ”k $¤)­e¬j¸¸T¼W¹Ñ.EÔ.EÑ"EÍÌÐUYÔU]ÐefÔelÐHmÑHmÔHmÑ"mÑnÔnˆÝ”k +°4´:À4Ä:ÐNÑNÔNˆØ—’�uœ{Ñ+Ô+¨d¬jÑ8×<Ò<½U¼[ÑIÔIÐIr(   N©r)   r*   r+   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚintÚfloatr:   r   r	   rO   r>   r(   r&   r3   r3   /   s    € € € € € ð	ð 	ð  #Ø Øðð àðð ðð ð	ð
 ðð ðð ð ð ðJ  f¤ð J°&ð Jð Jð Jð Jð Jð Jr(   r3   Úboxesc                 óä   ‡‡— t          j        | d¬¦  «        }|j        |j        cŠŠt          j        ˆˆfd„t	          | ¦  «        D ¦   «         d¬¦  «        }t          j        ||gd¬¦  «        }|S )Nr   )Údimc           	      ót   •— g | ]4\  }}t          j        |d d …d d…f         |‰t           j        ‰¬¦  «        ‘Œ5S )Nr   )r   Úlayoutr   )r   Ú	full_likeÚstrided)r?   ÚiÚbr   r   s      €€r&   rA   z*_convert_to_roi_format.<locals>.<listcomp>[   sH   ø€ ÐuÐuÐuÑ\`Ð\]Ð_`�Œ˜˜1˜1˜1˜b˜q˜b˜5œ 1¨E½%¼-ÐPVÐ	WÑ	WÔ	WÐuÐuÐur(   r   )r   rF   r   r   Ú	enumerate)rW   Úconcat_boxesÚidsÚroisr   r   s       @@r&   Ú_convert_to_roi_formatrd   W   s†   øø€ Ý”9˜U¨Ð*Ñ*Ô*€LØ Ô'¨Ô);€M€FˆEÝ
Œ)ØuÐuÐuÐuÐuÕdmÐnsÑdtÔdtÐuÑuÔuØðñ ô €Cõ Œ9�c˜<Ð(¨aÐ0Ñ0Ô0€DØ€Kr(   ÚfeatureÚoriginal_sizec                 óX  — | j         dd …         }g }t          ||¦  «        D ]�\  }}t          |¦  «        t          |¦  «        z  }dt          t          j        |¦  «                             ¦   «                              ¦   «         ¦  «        z  }|                     |¦  «         Œ‚|d         S )Néþÿÿÿr   r   )ÚshapeÚziprV   r   rI   rH   ÚroundÚappend)re   rf   r   Úpossible_scalesÚs1Ús2Úapprox_scaleÚscales           r&   Ú_infer_scalerr   b   s    € àŒ=˜˜˜Ô€DØ#%€OÝ�d˜MÑ*Ô*ð &ð &‰ˆˆBÝ˜R‘y”y¥5¨¡9¤9Ñ,ˆØ•U�5œ<¨Ñ5Ô5×:Ò:Ñ<Ô<×BÒBÑDÔDÑEÔEÑEˆØ×Ò˜uÑ%Ô%Ð%Ð%Ø˜1ÔÐr(   ÚfeaturesÚimage_shapesc                 óB  ‡— |st          d¦  «        ‚d}d}|D ].}t          |d         |¦  «        }t          |d         |¦  «        }Œ/||fŠˆfd„| D ¦   «         }t          j        t          j        |d         t          j        ¬¦  «        ¦  «                             ¦   «          }t          j        t          j        |d         t          j        ¬¦  «        ¦  «                             ¦   «          }	t          t          |¦  «        t          |	¦  «        ||¬¦  «        }
||
fS )Nzimages list should not be emptyr   r   c                 ó0   •— g | ]}t          |‰¦  «        ‘ŒS r>   )rr   )r?   ÚfeatÚoriginal_input_shapes     €r&   rA   z!_setup_scales.<locals>.<listcomp>z   s$   ø€ ÐLÐLÐL¸4�l˜4Ð!5Ñ6Ô6ÐLÐLÐLr(   rB   r   ©r.   r/   )	Ú
ValueErrorrD   r   rH   rI   Úfloat32Úitemr4   rU   )rs   rt   r.   r/   Úmax_xÚmax_yri   ÚscalesÚlvl_minÚlvl_maxÚ
map_levelsrx   s              @r&   Ú_setup_scalesrƒ   m   s!  ø€ ð ð <ÝÐ:Ñ;Ô;Ð;Ø€EØ€EØð %ð %ˆÝ�E˜!”H˜eÑ$Ô$ˆÝ�E˜!”H˜eÑ$Ô$ˆˆØ! 5˜>ÐàLÐLÐLÐLÀ8ÐLÑLÔL€Fõ Œz�%œ, v¨a¤y½¼ÐFÑFÔFÑGÔG×LÒLÑNÔNÐN€GÝŒz�%œ, v¨b¤z½¼ÐGÑGÔGÑHÔH×MÒMÑOÔOÐO€Gå ÝˆG‰ŒÝˆG‰ŒØ'Ø'ð	ñ ô €Jð �:ÐÐr(   ÚxÚfeatmap_namesc                 óp   — g }|                       ¦   «         D ]\  }}||v r|                     |¦  «         Œ|S r2   )Úitemsrl   )r„   r…   Ú
x_filteredÚkÚvs        r&   Ú_filter_inputr‹   ‰   sH   € à€JØ—’‘	”	ð !ð !‰ˆˆ1Ø�ÐÐØ×Ò˜aÑ Ô Ð øØÐr(   rˆ   Úoutput_sizeÚsampling_ratior   Úmapperc                 ó6  — |�|€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        ¦  «        ||<   Œœ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)
    Nz$scales and mapper should not be Noner   r   )rŒ   Úspatial_scaler�   r   )rz   r   rd   r   ri   r   r   r   r   r`   rj   r   ÚtorchvisionÚ_is_tracingrl   rK   r'   )rˆ   rW   rŒ   r�   r   rŽ   Ú
num_levelsrc   r   Únum_roisÚnum_channelsr   r   ÚresultÚtracing_resultsr$   Úper_level_featurerq   Úidx_in_levelÚrois_per_levelÚresult_idx_in_levels                        r&   Ú_multiscale_roi_alignrœ   ’   sÙ  € ð, €~˜˜ÝÐ?Ñ@Ô@Ð@å�Z‘”€JÝ! %Ñ(Ô(€Dà�Q‚€ÝØ�qŒMØØ#Ø  œ)Ø)ð
ñ 
ô 
ð 	
ð ˆV�E‰]Œ]€Få�4‰yŒy€HØ˜a”=Ô& qÔ)€Là˜q”MÔ'¨°A¬Ô)=ˆ6€EÝŒ[àØð	
ð ñ		ð
 Øðñ ô €Fð €OÝ-6µs¸:ÀvÑ7NÔ7NÑ-OÔ-Oð Hð HÑ)ˆÑ)Ð! 5Ý”{ 6¨U¢?Ñ3Ô3°AÔ6ˆØ˜lÔ+ˆå'ØØØ#ØØ)ð
ñ 
ô 
Ðõ Ô"Ñ$Ô$ð 
	HØ×"Ò"Ð#6×#9Ò#9¸%Ñ#@Ô#@ÑAÔAÐAÐAð $7×#9Ò#9¸&¼,Ñ#GÔ#GˆF�<Ñ Ð åÔÑ Ô ð =Ý# 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])

    )r   r‚   r)   r*   ry   r…   rŒ   r�   r.   r/   c                ó  •— t          ¦   «                              ¦   «          t          | ¦  «         t          |t          ¦  «        r||f}|| _        || _        t          |¦  «        | _        d | _	        d | _
        || _        || _        d S r2   )Úsuperr:   r   Ú
isinstancerU   r…   r�   ÚtuplerŒ   r   r‚   r.   r/   )r9   r…   rŒ   r�   r.   r/   Ú	__class__s         €r&   r:   zMultiScaleRoIAlign.__init__  s‰   ø€ õ 	‰Œ×ÒÑÔÐÝ˜DÑ!Ô!Ð!Ý�k¥3Ñ'Ô'ð 	5Ø&¨Ð4ˆKØ*ˆÔØ,ˆÔÝ  Ñ-Ô-ˆÔØˆŒØˆŒØ.ˆÔØ.ˆÔÐÐr(   r„   rW   rt   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…   r   r‚   rƒ   r.   r/   rœ   rŒ   r�   )r9   r„   rW   rt   rˆ   s        r&   ÚforwardzMultiScaleRoIAlign.forward!  s|   € õ& # 1 dÔ&8Ñ9Ô9ˆ
ØŒ;Ð $¤/Ð"9Ý+8Ø˜L¨$Ô*>ÀÔ@Tñ,ô ,Ñ(ˆDŒK˜œõ %ØØØÔØÔØŒKØŒOñ
ô 
ð 	
r(   c                 óP   — | j         j        › d| j        › d| j        › d| j        › d�S )Nz(featmap_names=z, output_size=z, sampling_ratio=ú))r£   rQ   r…   rŒ   r�   )r9   s    r&   Ú__repr__zMultiScaleRoIAlign.__repr__C  sV   € àŒ~Ô&ð Uð U°tÔ7Ið Uð UØÔ+ðUð UØ>BÔ>QðUð Uð Uð	
r(   )rQ   rR   rS   rT   r   r   rV   r3   Ú__annotations__Ústrr   rU   r   r:   r   r	   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ž   rP   )!Útypingr   r   r   r   r   r   Útorch.fxr‘   r   r	   Útorchvision.ops.boxesr   Úutilsr   r   ÚjitÚunusedr'   rU   rV   r4   r3   rd   rr   ÚfxÚwraprƒ   rª   r‹   rœ   ÚModulerž   r>   r(   r&   ú<module>rµ      s;  ðØ 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5Ð 5à €€€Ø €€€Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð Ð Ð Ø *Ð *Ð *Ð *Ð *Ð *à 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø  Ð  Ð  Ð  Ð  Ð  ð „Ôð˜vð ¸¸f¼ð È&ð ð ð ñ Ôðð, ØØðLð LØðLàðLð ðLð ð	Lð
 
ðLð Lð Lð Lð%Jð %Jð %Jð %Jð %Jñ %Jô %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ð Pñ „ðPðfa
ð a
ð a
ð a
ð a
˜œñ a
ô a
ð a
ð a
ð a
r(   