§
    ù²Åj‹X  ã                   ó  — d dl Z d dlmZ d dlmZmZmZmZ d dlZd dlm	Z	m
Z
 d dlmZ d dlmZmZmZmZ  G d„ d¦  «        Zej        j        d	e
d
e
de
de
fd„¦   «         Z G d„ d¦  «        Z G d„ d¦  «        Z G d„ d¦  «        Z G d„ de¦  «        Zde	j        deddfd„Zde	j        deeef         dee         fd„Zej        j         de
defd„¦   «         Z!de
dededefd „Z"	 d(d!e#d"ed#e
d$e
d%e
d&eee#ef                  de
fd'„Z$dS ))é    N)ÚOrderedDict)ÚDictÚListÚOptionalÚTuple)ÚnnÚTensor)Ú
functional)Úcomplete_box_iou_lossÚdistance_box_iou_lossÚFrozenBatchNorm2dÚgeneralized_box_iou_lossc                   ój   — e Zd ZdZdededdfd„Zdee         de	ee         ee         f         fd„Z
dS )	ÚBalancedPositiveNegativeSamplerz`
    This class samples batches, ensuring that they contain a fixed proportion of positives
    Úbatch_size_per_imageÚpositive_fractionÚreturnNc                 ó"   — || _         || _        dS )z¼
        Args:
            batch_size_per_image (int): number of elements to be selected per image
            positive_fraction (float): percentage of positive elements per batch
        N)r   r   )Úselfr   r   s      úTC:\Projects\Historical\venv\Lib\site-packages\torchvision/models/detection/_utils.pyÚ__init__z(BalancedPositiveNegativeSampler.__init__   s   € ð %9ˆÔ!Ø!2ˆÔÐÐó    Úmatched_idxsc                 óH  — g }g }|D �]—}t          j        |dk    ¦  «        d         }t          j        |dk    ¦  «        d         }t          | j        | j        z  ¦  «        }t          |                     ¦   «         |¦  «        }| j        |z
  }t          |                     ¦   «         |¦  «        }t          j        |                     ¦   «         |j        ¬¦  «        d|…         }	t          j        |                     ¦   «         |j        ¬¦  «        d|…         }
||	         }||
         }t          j	        |t           j
        ¬¦  «        }t          j	        |t           j
        ¬¦  «        }d||<   d||<   |                     |¦  «         |                     |¦  «         �Œ™||fS )a  
        Args:
            matched_idxs: list of tensors containing -1, 0 or positive values.
                Each tensor corresponds to a specific image.
                -1 values are ignored, 0 are considered as negatives and > 0 as
                positives.

        Returns:
            pos_idx (list[tensor])
            neg_idx (list[tensor])

        Returns two lists of binary masks for each image.
        The first list contains the positive elements that were selected,
        and the second list the negative example.
        é   r   ©ÚdeviceN©Údtype)ÚtorchÚwhereÚintr   r   ÚminÚnumelÚrandpermr   Ú
zeros_likeÚuint8Úappend)r   r   Úpos_idxÚneg_idxÚmatched_idxs_per_imageÚpositiveÚnegativeÚnum_posÚnum_negÚperm1Úperm2Úpos_idx_per_imageÚneg_idx_per_imageÚpos_idx_per_image_maskÚneg_idx_per_image_masks                  r   Ú__call__z(BalancedPositiveNegativeSampler.__call__   s•  € ð  ˆØˆØ&2ð 	3ñ 	3Ð"Ý”{Ð#9¸QÒ#>Ñ?Ô?ÀÔBˆHÝ”{Ð#9¸QÒ#>Ñ?Ô?ÀÔBˆHå˜$Ô3°dÔ6LÑLÑMÔMˆGå˜(Ÿ.š.Ñ*Ô*¨GÑ4Ô4ˆGØÔ/°'Ñ9ˆGå˜(Ÿ.š.Ñ*Ô*¨GÑ4Ô4ˆGõ ”N 8§>¢>Ñ#3Ô#3¸H¼OÐLÑLÔLÈXÈgÈXÔVˆEÝ”N 8§>¢>Ñ#3Ô#3¸H¼OÐLÑLÔLÈXÈgÈXÔVˆEà (¨¤ÐØ (¨¤Ðõ &+Ô%5Ð6LÕTYÔT_Ð%`Ñ%`Ô%`Ð"Ý%*Ô%5Ð6LÕTYÔT_Ð%`Ñ%`Ô%`Ð"à89Ð"Ð#4Ñ5Ø89Ð"Ð#4Ñ5à�NŠNÐ1Ñ2Ô2Ð2Ø�NŠNÐ1Ñ2Ô2Ð2Ñ2à˜ÐÐr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   Úfloatr   r   r	   r   r6   © r   r   r   r      s   € € € € € ðð ð3¨Sð 3ÀUð 3Ètð 3ð 3ð 3ð 3ð.  T¨&¤\ð . °e¸DÀ¼LÈ$ÈvÌ,Ð<VÔ6Wð . ð . ð . ð . ð . ð . r   r   Úreference_boxesÚ	proposalsÚweightsr   c                 óf  — |d         }|d         }|d         }|d         }|dd…df                               d¦  «        }|dd…df                               d¦  «        }|dd…df                               d¦  «        }	|dd…df                               d¦  «        }
| dd…df                               d¦  «        }| dd…df                               d¦  «        }| dd…df                               d¦  «        }| dd…df                               d¦  «        }|	|z
  }|
|z
  }|d|z  z   }|d|z  z   }||z
  }||z
  }|d|z  z   }|d|z  z   }|||z
  z  |z  }|||z
  z  |z  }|t          j        ||z  ¦  «        z  }|t          j        ||z  ¦  «        z  }t          j        ||||fd¬¦  «        }|S )z÷
    Encode a set of proposals with respect to some
    reference boxes

    Args:
        reference_boxes (Tensor): reference boxes
        proposals (Tensor): boxes to be encoded
        weights (Tensor[4]): the weights for ``(x, y, w, h)``
    r   r   é   é   Nç      à?©Údim)Ú	unsqueezer    ÚlogÚcat)r=   r>   r?   ÚwxÚwyÚwwÚwhÚproposals_x1Úproposals_y1Úproposals_x2Úproposals_y2Úreference_boxes_x1Úreference_boxes_y1Úreference_boxes_x2Úreference_boxes_y2Ú	ex_widthsÚ
ex_heightsÚex_ctr_xÚex_ctr_yÚ	gt_widthsÚ
gt_heightsÚgt_ctr_xÚgt_ctr_yÚ
targets_dxÚ
targets_dyÚ
targets_dwÚ
targets_dhÚtargetss                               r   Úencode_boxesrb   J   s
  € ð 
�Œ€BØ	�Œ€BØ	�Œ€BØ	�Œ€Bà˜Q˜Q˜Q ˜T”?×,Ò,¨QÑ/Ô/€LØ˜Q˜Q˜Q ˜T”?×,Ò,¨QÑ/Ô/€LØ˜Q˜Q˜Q ˜T”?×,Ò,¨QÑ/Ô/€LØ˜Q˜Q˜Q ˜T”?×,Ò,¨QÑ/Ô/€Là(¨¨¨¨A¨Ô.×8Ò8¸Ñ;Ô;ÐØ(¨¨¨¨A¨Ô.×8Ò8¸Ñ;Ô;ÐØ(¨¨¨¨A¨Ô.×8Ò8¸Ñ;Ô;ÐØ(¨¨¨¨A¨Ô.×8Ò8¸Ñ;Ô;Ðð ˜|Ñ+€IØ Ñ,€JØ˜c I™oÑ-€HØ˜c JÑ.Ñ.€Hà"Ð%7Ñ7€IØ#Ð&8Ñ8€JØ! C¨)¡OÑ3€HØ! C¨*Ñ$4Ñ4€Hà�x (Ñ*Ñ+¨iÑ7€JØ�x (Ñ*Ñ+¨jÑ8€JØ•e”i 	¨IÑ 5Ñ6Ô6Ñ6€JØ•e”i 
¨ZÑ 7Ñ8Ô8Ñ8€JåŒi˜ Z°¸ZÐHÈaÐPÑPÔP€GØ€Nr   c                   óÚ   — e Zd ZdZ ej        d¦  «        fdeeeeef         deddfd„Zde	e
         d	e	e
         de	e
         fd
„Zde
d	e
de
fd„Zde
de	e
         de
fd„Zde
de
de
fd„ZdS )ÚBoxCoderz~
    This class encodes and decodes a set of bounding boxes into
    the representation used for training the regressors.
    g     @O@r?   Úbbox_xform_clipr   Nc                 ó"   — || _         || _        dS )za
        Args:
            weights (4-element tuple)
            bbox_xform_clip (float)
        N)r?   re   )r   r?   re   s      r   r   zBoxCoder.__init__€   s   € ð ˆŒØ.ˆÔÐÐr   r=   r>   c                 óÊ   — d„ |D ¦   «         }t          j        |d¬¦  «        }t          j        |d¬¦  «        }|                      ||¦  «        }|                     |d¦  «        S )Nc                 ó,   — g | ]}t          |¦  «        ‘ŒS r<   )Úlen©Ú.0Úbs     r   ú
<listcomp>z#BoxCoder.encode.<locals>.<listcomp>Œ   s   € Ð;Ð;Ð; a�3˜q™6œ6Ð;Ð;Ð;r   r   rD   )r    rH   Úencode_singleÚsplit)r   r=   r>   Úboxes_per_imagera   s        r   ÚencodezBoxCoder.encode‹   sf   € Ø;Ð;¨?Ð;Ñ;Ô;ˆÝœ) O¸Ð;Ñ;Ô;ˆÝ”I˜i¨QÐ/Ñ/Ô/ˆ	Ø×$Ò$ _°iÑ@Ô@ˆØ�}Š}˜_¨aÑ0Ô0Ð0r   c                 ó|   — |j         }|j        }t          j        | j        ||¬¦  «        }t          |||¦  «        }|S )zÑ
        Encode a set of proposals with respect to some
        reference boxes

        Args:
            reference_boxes (Tensor): reference boxes
            proposals (Tensor): boxes to be encoded
        ©r   r   )r   r   r    Ú	as_tensorr?   rb   )r   r=   r>   r   r   r?   ra   s          r   rn   zBoxCoder.encode_single’   sB   € ð  Ô%ˆØ Ô'ˆÝ”/ $¤,°eÀFÐKÑKÔKˆÝ˜°	¸7ÑCÔCˆàˆr   Ú	rel_codesÚboxesc                 ó¸  — t          j        t          |t          t          f¦  «        d¦  «         t          j        t          |t           j        ¦  «        d¦  «         d„ |D ¦   «         }t          j        |d¬¦  «        }d}|D ]}||z  }Œ|dk    r|                     |d¦  «        }|                      ||¦  «        }|dk    r|                     |dd¦  «        }|S )Nz2This function expects boxes of type list or tuple.z5This function expects rel_codes of type torch.Tensor.c                 ó8   — g | ]}|                      d ¦  «        ‘ŒS )r   ©Úsizerj   s     r   rm   z#BoxCoder.decode.<locals>.<listcomp>«   s"   € Ð4Ð4Ð4¨˜1Ÿ6š6 !™9œ9Ð4Ð4Ð4r   r   rD   éÿÿÿÿé   )	r    Ú_assertÚ
isinstanceÚlistÚtupler	   rH   ÚreshapeÚdecode_single)r   ru   rv   rp   Úconcat_boxesÚbox_sumÚvalÚ
pred_boxess           r   ÚdecodezBoxCoder.decode¢   só   € ÝŒÝ�u�t¥U˜mÑ,Ô,Ø@ñ	
ô 	
ð 	
õ 	ŒÝ�y¥%¤,Ñ/Ô/ØCñ	
ô 	
ð 	
ð 5Ð4¨eÐ4Ñ4Ô4ˆÝ”y ¨AÐ.Ñ.Ô.ˆØˆØ"ð 	ð 	ˆCØ�s‰NˆGˆGØ�QŠ;ˆ;Ø!×)Ò)¨'°2Ñ6Ô6ˆIØ×'Ò'¨	°<Ñ@Ô@ˆ
Ø�QŠ;ˆ;Ø#×+Ò+¨G°R¸Ñ;Ô;ˆJØÐr   c                 ó   — |                      |j        ¦  «        }|dd…df         |dd…df         z
  }|dd…df         |dd…df         z
  }|dd…df         d|z  z   }|dd…df         d|z  z   }| j        \  }}}	}
|dd…ddd…f         |z  }|dd…ddd…f         |z  }|dd…ddd…f         |	z  }|dd…ddd…f         |
z  }t          j        || j        ¬¦  «        }t          j        || j        ¬¦  «        }||dd…df         z  |dd…df         z   }||dd…df         z  |dd…df         z   }t          j        |¦  «        |dd…df         z  }t          j        |¦  «        |dd…df         z  }t          j        d|j        |j        ¬	¦  «        |z  }t          j        d|j        |j        ¬	¦  «        |z  }||z
  }||z
  }||z   }||z   }t          j	        ||||fd¬
¦  «         
                    d¦  «        }|S )zÙ
        From a set of original boxes and encoded relative box offsets,
        get the decoded boxes.

        Args:
            rel_codes (Tensor): encoded boxes
            boxes (Tensor): reference boxes.
        NrA   r   rB   r   rC   r|   )Úmaxrs   rD   )Útor   r?   r    Úclampre   ÚexpÚtensorr   ÚstackÚflatten)r   ru   rv   ÚwidthsÚheightsÚctr_xÚctr_yrI   rJ   rK   rL   ÚdxÚdyÚdwÚdhÚ
pred_ctr_xÚ
pred_ctr_yÚpred_wÚpred_hÚc_to_c_hÚc_to_c_wÚpred_boxes1Úpred_boxes2Úpred_boxes3Úpred_boxes4r†   s                             r   r‚   zBoxCoder.decode_single·   sm  € ð —’˜œÑ)Ô)ˆà�q�q�q˜!�t”˜u Q Q Q¨ Tœ{Ñ*ˆØ˜˜˜˜1˜”+  a a a¨ d¤Ñ+ˆØ�a�a�a˜�d”˜c F™lÑ*ˆØ�a�a�a˜�d”˜c G™mÑ+ˆàœ‰ˆˆB��BØ�q�q�q˜!˜$˜Q˜$�wÔ "Ñ$ˆØ�q�q�q˜!˜$˜Q˜$�wÔ "Ñ$ˆØ�q�q�q˜!˜$˜Q˜$�wÔ "Ñ$ˆØ�q�q�q˜!˜$˜Q˜$�wÔ "Ñ$ˆõ Œ[˜ Ô!5Ð6Ñ6Ô6ˆÝŒ[˜ Ô!5Ð6Ñ6Ô6ˆà˜&    D œ/Ñ)¨E°!°!°!°T°'¬NÑ:ˆ
Ø˜' ! ! ! T 'Ô*Ñ*¨U°1°1°1°d°7¬^Ñ;ˆ
Ý”˜2‘” ¨¨¨¨4¨¤Ñ0ˆÝ”˜2‘” ¨¨¨¨D¨Ô!1Ñ1ˆõ ”< ¨:Ô+;ÀFÄMÐRÑRÔRÐU[Ñ[ˆÝ”< ¨:Ô+;ÀFÄMÐRÑRÔRÐU[Ñ[ˆà  8Ñ+ˆØ  8Ñ+ˆØ  8Ñ+ˆØ  8Ñ+ˆÝ”[ +¨{¸KÈÐ!UÐ[\Ð]Ñ]Ô]×eÒeÐfgÑhÔhˆ
ØÐr   )r7   r8   r9   r:   ÚmathrG   r   r;   r   r   r	   rq   rn   r‡   r‚   r<   r   r   rd   rd   z   s*  € € € € € ðð ð T\ÐSWÔS[Ð\gÑShÔShð	/ð 	/Ø˜U E¨5°%Ð7Ô8ð	/ØKPð	/à	ð	/ð 	/ð 	/ð 	/ð1 d¨6¤lð 1¸tÀF¼|ð 1ÐPTÐU[ÔP\ð 1ð 1ð 1ð 1ð¨Vð Àð È6ð ð ð ð ð  ð ¨t°F¬|ð Àð ð ð ð ð*) vð )°fð )Àð )ð )ð )ð )ð )ð )r   rd   c                   óL   — e Zd ZdZddeddfd„Zdededefd	„Zd
ededefd„ZdS )ÚBoxLinearCoderz´
    The linear box-to-box transform defined in FCOS. The transformation is parameterized
    by the distance from the center of (square) src box to 4 edges of the target box.
    TÚnormalize_by_sizer   Nc                 ó   — || _         dS )zq
        Args:
            normalize_by_size (bool): normalize deltas by the size of src (anchor) boxes.
        N)r¥   )r   r¥   s     r   r   zBoxLinearCoder.__init__é   s   € ð
 "3ˆÔÐÐr   r=   r>   c                 ór  — d|d         |d         z   z  }d|d         |d         z   z  }||d         z
  }||d         z
  }|d         |z
  }|d         |z
  }t          j        ||||fd¬¦  «        }	| j        rA|d         |d         z
  }
|d         |d         z
  }t          j        |
||
|fd¬¦  «        }|	|z  }	|	S )aC  
        Encode a set of proposals with respect to some reference boxes

        Args:
            reference_boxes (Tensor): reference boxes
            proposals (Tensor): boxes to be encoded

        Returns:
            Tensor: the encoded relative box offsets that can be used to
            decode the boxes.

        rC   ©.r   ©.rA   ©.r   ©.rB   r{   rD   )r    rŽ   r¥   )r   r=   r>   Úreference_boxes_ctr_xÚreference_boxes_ctr_yÚtarget_lÚtarget_tÚtarget_rÚtarget_bra   Úreference_boxes_wÚreference_boxes_hÚreference_boxes_sizes                r   rq   zBoxLinearCoder.encodeð   s  € ð !$ °vÔ'>ÀÐQWÔAXÑ'XÑ YÐØ # °vÔ'>ÀÐQWÔAXÑ'XÑ YÐð )¨9°VÔ+<Ñ<ˆØ(¨9°VÔ+<Ñ<ˆØ˜VÔ$Ð'<Ñ<ˆØ˜VÔ$Ð'<Ñ<ˆå”+˜x¨°8¸XÐFÈBÐOÑOÔOˆàÔ!ð 	5Ø /°Ô 7¸/È&Ô:QÑ QÐØ /°Ô 7¸/È&Ô:QÑ QÐÝ#(¤;Ø"Ð$5Ð7HÐJ[Ð\Ðbdð$ñ $ô $Ð ð Ð 4Ñ4ˆGØˆr   ru   rv   c                 ó¨  — |                      |j        ¬¦  «        }d|d         |d         z   z  }d|d         |d         z   z  }| j        rA|d         |d         z
  }|d         |d         z
  }t          j        ||||fd¬¦  «        }||z  }||d         z
  }||d         z
  }	||d         z   }
||d         z   }t          j        ||	|
|fd¬¦  «        }|S )	aØ  
        From a set of original boxes and encoded relative box offsets,
        get the decoded boxes.

        Args:
            rel_codes (Tensor): encoded boxes
            boxes (Tensor): reference boxes.

        Returns:
            Tensor: the predicted boxes with the encoded relative box offsets.

        .. note::
            This method assumes that ``rel_codes`` and ``boxes`` have same size for 0th dimension. i.e. ``len(rel_codes) == len(boxes)``.

        r   rC   r¨   r©   rª   r«   r{   rD   )rŠ   r   r¥   r    rŽ   )r   ru   rv   r’   r“   Úboxes_wÚboxes_hÚlist_box_sizerž   rŸ   r    r¡   r†   s                r   r‡   zBoxLinearCoder.decode  sþ   € ð$ —’˜yœ�Ñ/Ô/ˆà�u˜V”} u¨V¤}Ñ4Ñ5ˆØ�u˜V”} u¨V¤}Ñ4Ñ5ˆàÔ!ð 	2Ø˜F”m e¨F¤mÑ3ˆGØ˜F”m e¨F¤mÑ3ˆGå!œK¨°'¸7ÀGÐ(LÐRTÐUÑUÔUˆMØ! MÑ1ˆIà˜i¨Ô/Ñ/ˆØ˜i¨Ô/Ñ/ˆØ˜i¨Ô/Ñ/ˆØ˜i¨Ô/Ñ/ˆå”[ +¨{¸KÈÐ!UÐ[]Ð^Ñ^Ô^ˆ
ØÐr   )T)	r7   r8   r9   r:   Úboolr   r	   rq   r‡   r<   r   r   r¤   r¤   ã   s˜   € € € € € ðð ð
3ð 3¨$ð 3¸$ð 3ð 3ð 3ð 3ð! fð !¸ð !ÀFð !ð !ð !ð !ðF$ ð $¨vð $¸&ð $ð $ð $ð $ð $ð $r   r¤   c            	       óf   — e Zd ZdZdZdZeedœZddedede	d	d
fd„Z
ded	efd„Zdededed	d
fd„Zd
S )ÚMatchera‰  
    This class assigns to each predicted "element" (e.g., a box) a ground-truth
    element. Each predicted element will have exactly zero or one matches; each
    ground-truth element may be assigned to zero or more predicted elements.

    Matching is based on the MxN match_quality_matrix, that characterizes how well
    each (ground-truth, predicted)-pair match. For example, if the elements are
    boxes, the matrix may contain box IoU overlap values.

    The matcher returns a tensor of size N containing the index of the ground-truth
    element m that matches to prediction n. If there is no match, a negative value
    is returned.
    r{   éþÿÿÿ)ÚBELOW_LOW_THRESHOLDÚBETWEEN_THRESHOLDSFÚhigh_thresholdÚlow_thresholdÚallow_low_quality_matchesr   Nc                 ó~   — d| _         d| _        t          j        ||k    d¦  «         || _        || _        || _        dS )a¯  
        Args:
            high_threshold (float): quality values greater than or equal to
                this value are candidate matches.
            low_threshold (float): a lower quality threshold used to stratify
                matches into three levels:
                1) matches >= high_threshold
                2) BETWEEN_THRESHOLDS matches in [low_threshold, high_threshold)
                3) BELOW_LOW_THRESHOLD matches in [0, low_threshold)
            allow_low_quality_matches (bool): if True, produce additional matches
                for predictions that have only low-quality match candidates. See
                set_low_quality_matches_ for more details.
        r{   r¼   z)low_threshold should be <= high_thresholdN)r½   r¾   r    r}   r¿   rÀ   rÁ   )r   r¿   rÀ   rÁ   s       r   r   zMatcher.__init__Q  sJ   € ð $&ˆÔ Ø"$ˆÔÝŒ�m ~Ò5Ð7bÑcÔcÐcØ,ˆÔØ*ˆÔØ)BˆÔ&Ð&Ð&r   Úmatch_quality_matrixc                 óÚ  — |                      ¦   «         dk    r/|j        d         dk    rt          d¦  «        ‚t          d¦  «        ‚|                     d¬¦  «        \  }}| j        r|                     ¦   «         }nd}|| j        k     }|| j        k    || j        k     z  }| j        ||<   | j	        ||<   | j        r/|€t          j        dd¦  «         n|                      |||¦  «         |S )a‰  
        Args:
            match_quality_matrix (Tensor[float]): an MxN tensor, containing the
            pairwise quality between M ground-truth elements and N predicted elements.

        Returns:
            matches (Tensor[int64]): an N tensor where N[i] is a matched gt in
            [0, M - 1] or a negative value indicating that prediction i could not
            be matched.
        r   zENo ground-truth boxes available for one of the images during trainingzANo proposal boxes available for one of the images during trainingrD   NFzall_matches should not be None)r$   ÚshapeÚ
ValueErrorr‰   rÁ   ÚclonerÀ   r¿   r½   r¾   r    r}   Úset_low_quality_matches_)r   rÃ   Úmatched_valsÚmatchesÚall_matchesÚbelow_low_thresholdÚbetween_thresholdss          r   r6   zMatcher.__call__f  s  € ð  ×%Ò%Ñ'Ô'¨1Ò,Ð,à#Ô)¨!Ô,°Ò1Ð1Ý Ð!hÑiÔiÐiå Ð!dÑeÔeÐeð !5× 8Ò 8¸QÐ 8Ñ ?Ô ?Ñˆ�gØÔ)ð 	Ø!Ÿ-š-™/œ/ˆKˆKàˆKð +¨TÔ-?Ò?ÐØ*¨dÔ.@Ò@À\ÐTXÔTgÒEgÑhÐØ'+Ô'?ˆÐ#Ñ$Ø&*Ô&=ˆÐ"Ñ#àÔ)ð 	ZØÐ"Ý”˜eÐ%EÑFÔFÐFÐFà×-Ò-¨g°{ÐDXÑYÔYÐYàˆr   rÊ   rË   c                 ó¢   — |                      d¬¦  «        \  }}t          j        ||dd…df         k    ¦  «        }|d         }||         ||<   dS )ax  
        Produce additional matches for predictions that have only low-quality matches.
        Specifically, for each ground-truth find the set of predictions that have
        maximum overlap with it (including ties); for each prediction in that set, if
        it is unmatched, then match it to the ground-truth with which it has the highest
        quality value.
        r   rD   N)r‰   r    r!   )r   rÊ   rË   rÃ   Úhighest_quality_foreach_gtÚ_Ú gt_pred_pairs_of_highest_qualityÚpred_inds_to_updates           r   rÈ   z Matcher.set_low_quality_matches_Ž  sm   € ð )=×(@Ò(@ÀQÐ(@Ñ(GÔ(GÑ%Ð" Aå+0¬;Ð7KÐOiÐjkÐjkÐjkÐmqÐjqÔOrÒ7rÑ+sÔ+sÐ(ð ?¸qÔAÐØ'2Ð3FÔ'GˆÐ#Ñ$Ð$Ð$r   )F)r7   r8   r9   r:   r½   r¾   r"   Ú__annotations__r;   r¹   r   r	   r6   rÈ   r<   r   r   r»   r»   :  sß   € € € € € ðð ð ÐØÐð  #Ø!ðð €Oð
Cð C uð C¸Uð CÐ_cð CÐptð Cð Cð Cð Cð*&¨Vð &¸ð &ð &ð &ð &ðPH°ð HÀVð HÐcið HÐnrð Hð Hð Hð Hð Hð Hr   r»   c                   ó<   ‡ — e Zd Zdeddfˆ fd„Zdedefˆ fd„Zˆ xZS )Ú
SSDMatcherÚ	thresholdr   Nc                 óP   •— t          ¦   «                              ||d¬¦  «         d S )NF)rÁ   )Úsuperr   )r   rÖ   Ú	__class__s     €r   r   zSSDMatcher.__init__¥  s(   ø€ Ý‰Œ×Ò˜ IÈÐÑOÔOÐOÐOÐOr   rÃ   c                 óô   •— t          ¦   «                              |¦  «        }|                     d¬¦  «        \  }}t          j        |                     d¦  «        t          j        |j        ¬¦  «        ||<   |S )Nr   rD   r   rs   )rØ   r6   r‰   r    Úarangerz   Úint64r   )r   rÃ   rÊ   rÐ   Úhighest_quality_pred_foreach_gtrÙ   s        €r   r6   zSSDMatcher.__call__¨  sx   ø€ Ý‘'”'×"Ò"Ð#7Ñ8Ô8ˆð .B×-EÒ-EÈ!Ð-EÑ-LÔ-LÑ*ˆÐ*Ý38´<Ø+×0Ò0°Ñ3Ô3½5¼;ÐOnÔOuð4
ñ 4
ô 4
ˆÐ/Ñ0ð ˆr   )r7   r8   r9   r;   r   r	   r6   Ú__classcell__)rÙ   s   @r   rÕ   rÕ   ¤  s   ø€ € € € € ðP %ð P¨Dð Pð Pð Pð Pð Pð Pð	¨Vð 	¸ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	r   rÕ   ÚmodelÚepsc                 ól   — |                       ¦   «         D ]}t          |t          ¦  «        r||_        ŒdS )að  
    This method overwrites the default eps values of all the
    FrozenBatchNorm2d layers of the model with the provided value.
    This is necessary to address the BC-breaking change introduced
    by the bug-fix at pytorch/vision#2933. The overwrite is applied
    only when the pretrained weights are loaded to maintain compatibility
    with previous versions.

    Args:
        model (nn.Module): The model on which we perform the overwrite.
        eps (float): The new value of eps.
    N)Úmodulesr~   r   rà   )rß   rà   Úmodules      r   Úoverwrite_epsrä   ´  s@   € ð —-’-‘/”/ð ð ˆÝ�fÕ/Ñ0Ô0ð 	ØˆFŒJøðð r   rz   c                 ó   — | j         }|                      ¦   «          t          j        ¦   «         5  t	          |                      ¦   «         ¦  «        j        }t          j        dd|d         |d         f|¬¦  «        } | |¦  «        }t          |t          j	        ¦  «        rt          d|fg¦  «        }d„ |                     ¦   «         D ¦   «         }ddd¦  «         n# 1 swxY w Y   |r|                      ¦   «          |S )a‹  
    This method retrieves the number of output channels of a specific model.

    Args:
        model (nn.Module): The model for which we estimate the out_channels.
            It should return a single Tensor or an OrderedDict[Tensor].
        size (Tuple[int, int]): The size (wxh) of the input.

    Returns:
        out_channels (List[int]): A list of the output channels of the model.
    r   rB   r   r   Ú0c                 ó8   — g | ]}|                      d ¦  «        ‘ŒS )r   ry   )rk   Úxs     r   rm   z)retrieve_out_channels.<locals>.<listcomp>Ü  s"   € Ð=Ð=Ð= a˜Ÿš˜q™	œ	Ð=Ð=Ð=r   N)ÚtrainingÚevalr    Úno_gradÚnextÚ
parametersr   Úzerosr~   r	   r   ÚvaluesÚtrain)rß   rz   Úin_trainingr   Útmp_imgÚfeaturesÚout_channelss          r   Úretrieve_out_channelsrõ   Æ  s)  € ð ”.€KØ	‡J‚J�L„L€Lå	Œ‰Œð >ð >å�e×&Ò&Ñ(Ô(Ñ)Ô)Ô0ˆÝ”+˜q ! T¨!¤W¨d°1¬gÐ6¸vÐFÑFÔFˆØ�5˜‘>”>ˆÝ�h¥¤Ñ-Ô-ð 	6Ý" S¨( OÐ#4Ñ5Ô5ˆHØ=Ð=¨8¯?ª?Ñ+<Ô+<Ð=Ñ=Ô=ˆð>ð >ð >ñ >ô >ð >ð >ð >ð >ð >ð >øøøð >ð >ð >ð >ð ð Ø�Š‰ŒˆàÐs   ¯B"CÃC!Ã$C!Úvc                 ó   — | S ©Nr<   )rö   s    r   Ú_fake_cast_onnxrù   ä  s   € à€Hr   ÚinputÚ	orig_kvalÚaxisc                 ó‚  — t           j                             ¦   «         s#t          ||                      |¦  «        ¦  «        S t          j        | ¦  «        |                              d¦  «        }t          j        t          j        t          j        |g|j	        ¬¦  «        |fd¦  «        ¦  «        }t          |¦  «        S )ay  
    ONNX spec requires the k-value to be less than or equal to the number of inputs along
    provided dim. Certain models use the number of elements along a particular axis instead of K
    if K exceeds the number of elements along that axis. Previously, python's min() function was
    used to determine whether to use the provided k-value or the specified dim axis value.

    However, in cases where the model is being exported in tracing mode, python min() is
    static causing the model to be traced incorrectly and eventually fail at the topk node.
    In order to avoid this situation, in tracing mode, torch.min() is used instead.

    Args:
        input (Tensor): The original input tensor.
        orig_kval (int): The provided k-value.
        axis(int): Axis along which we retrieve the input size.

    Returns:
        min_kval (int): Appropriately selected k-value.
    r   r   )r    ÚjitÚ
is_tracingr#   rz   Ú_shape_as_tensorrF   rH   r�   r   rù   )rú   rû   rü   Úaxis_dim_valÚmin_kvals        r   Ú	_topk_minr  é  s�   € õ& Œ9×ÒÑ!Ô!ð 0Ý�9˜eŸjšj¨Ñ.Ô.Ñ/Ô/Ð/ÝÔ)¨%Ñ0Ô0°Ô6×@Ò@ÀÑCÔC€LÝŒy�œ¥E¤L°)°ÀLÔDVÐ$WÑ$WÔ$WÐYeÐ#fÐhiÑjÔjÑkÔk€HÝ˜8Ñ$Ô$Ð$r   ÚtypeÚ	box_coderÚanchors_per_imageÚmatched_gt_boxes_per_imageÚbbox_regression_per_imageÚcnfc                 óú  — t          j        | dv d| › �¦  «         | dk    r-|                     ||¦  «        }t          j        ||d¬¦  «        S | dk    r>|                     ||¦  «        }|�d|v r|d         nd}t          j        ||d|¬	¦  «        S |                     ||¦  «        }|�d
|v r|d
         nd}	| dk    rt          ||d|	¬¦  «        S | dk    rt          ||d|	¬¦  «        S t          ||d|	¬¦  «        S )N)Úl1Ú	smooth_l1ÚciouÚdiouÚgiouzUnsupported loss: r  Úsum)Ú	reductionr  Úbetag      ð?)r  r  rà   gH¯¼šò×z>r  )r  rà   r  )
r    r}   rn   ÚFÚl1_lossÚsmooth_l1_lossr‚   r   r   r   )
r  r  r  r  r  r	  Útarget_regressionr  Úbbox_per_imagerà   s
             r   Ú	_box_lossr    sK  € õ 
„M�$ÐEÐEÐGbÐ\`ÐGbÐGbÑcÔcÐcàˆt‚|€|Ø%×3Ò3Ð4NÐPaÑbÔbÐÝŒyÐ2Ð4EÐQVÐWÑWÔWÐWØ	�Ò	Ð	Ø%×3Ò3Ð4NÐPaÑbÔbÐØ!˜o°&¸C°-°-ˆs�6Œ{ˆ{ÀSˆÝÔÐ 9Ð;LÐX]ÐdhÐiÑiÔiÐià"×0Ò0Ð1JÐL]Ñ^Ô^ˆØ˜O°¸°°ˆc�%ŒjˆjÀ$ˆØ�6Š>ˆ>Ý(¨Ð9SÐ_dÐjmÐnÑnÔnÐnØ�6Š>ˆ>Ý(¨Ð9SÐ_dÐjmÐnÑnÔnÐnå'¨Ð8RÐ^cÐilÐmÑmÔmÐmr   rø   )%r¢   Úcollectionsr   Útypingr   r   r   r   r    r   r	   Útorch.nnr
   r  Útorchvision.opsr   r   r   r   r   rþ   Ú_script_if_tracingrb   rd   r¤   r»   rÕ   ÚModuler;   rä   r"   rõ   Úunusedrù   r  Ústrr  r<   r   r   ú<module>r!     s  ðØ €€€Ø #Ð #Ð #Ð #Ð #Ð #Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ø uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uÐ uð< ð < ð < ð < ð < ñ < ô < ð < ð~ „Ôð, &ð ,°Vð ,Àfð ,ÐQWð ,ð ,ð ,ñ Ôð,ð^fð fð fð fð fñ fô fð fðRTð Tð Tð Tð Tñ Tô Tð TðngHð gHð gHð gHð gHñ gHô gHð gHðTð ð ð ð �ñ ô ð ð ˜œð ¨ð °4ð ð ð ð ð$ ¤ð °%¸¸S¸´/ð ÀdÈ3Äið ð ð ð ð< „Ôð�vð  #ð ð ð ñ Ôðð%�Vð %¨ð %°3ð %¸3ð %ð %ð %ð %ð@ '+ðnð nØ
ðnàðnð ðnð !'ð	nð
  &ðnð 
�$�s˜E�zÔ"Ô	#ðnð ðnð nð nð nð nð nr   