ó
    Eñi¡‘  ã                   ó~  • S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	  S SK
r
S SK
JrJr  SSKJrJrJr  SSKJr  SS	KJr  SS
KJr  SSKJrJrJr  SSKJr  SSKJ r J!r!  SSK"J#r#J$r$  SSK%Jr&  SSKJ'r'J(r(  SSK)J*r*  SSK+J,r,J-r-  SSK.J/r/  / SQr0S\1\   S\4S jr2S r3S r4 " S S\Rj                  5      r6 " S S\Rj                  5      r7 " S  S!\Rj                  5      r8 " S" S#\Rj                  5      r9\S$S%.r: " S& S'\5      r; " S( S)\5      r<\" 5       \!" S*\;Rz                  4S+\$R|                  4S,9SS-S\$R|                  SS..S/\	\;   S0\?S1\	\@   S2\	\$   S3\	\@   S4\S\94S5 jj5       5       rA\" 5       \!" S*\<Rz                  4S+\$R|                  4S,9SS-SSSS..S/\	\<   S0\?S1\	\@   S2\	\$   S3\	\@   S4\S\94S6 jj5       5       rBg)7é    N)ÚOrderedDict)Úpartial)ÚAnyÚCallableÚOptional)ÚnnÚTensoré   )ÚboxesÚmiscÚsigmoid_focal_loss)ÚLastLevelP6P7)ÚObjectDetection)Ú_log_api_usage_onceé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_COCO_CATEGORIES)Ú_ovewrite_value_paramÚhandle_legacy_interface)Úresnet50ÚResNet50_Weightsé   )Ú_utils)Ú	_box_lossÚoverwrite_eps)ÚAnchorGenerator)Ú_resnet_fpn_extractorÚ_validate_trainable_layers)ÚGeneralizedRCNNTransform)Ú	RetinaNetÚRetinaNet_ResNet50_FPN_WeightsÚ!RetinaNet_ResNet50_FPN_V2_WeightsÚretinanet_resnet50_fpnÚretinanet_resnet50_fpn_v2ÚxÚreturnc                 ó0   • U S   nU SS   H  nX-   nM	     U$ )Nr   r   © )r'   ÚresÚis      Úc/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torchvision/models/detection/retinanet.pyÚ_sumr.   "   s'   € Ø
ˆA‰$€CØˆqˆr‹UˆØ‰gŠñ à€Jó    c                 óœ   • [        S5       H=  nS H4  nU SSU-   SU 3nU SU SU 3nX@;   d  M!  U R                  U5      X'   M6     M?     g )Né   )ÚweightÚbiaszconv.r   Ú.z.0.)ÚrangeÚpop)Ú
state_dictÚprefixr,   ÚtypeÚold_keyÚnew_keys         r-   Ú_v1_to_v2_weightsr<   )   sa   € Ü�1ŽXˆÛ&ˆDØ˜  a¨¡c U¨!¨D¨6Ð2ˆGØ˜  a S¨¨D¨6Ð2ˆGØÕ$Ø&0§n¡n°WÓ&=�
Ó#ó	 'ò r/   c                  ó\   • [        S S 5       5      n S[        U 5      -  n[        X5      nU$ )Nc              3   óZ   #   • U  H!  o[        US -  5      [        US-  5      4v •  M#     g7f)g‹r�ù¢(ô?g<n=¥þeù?N)Úint)Ú.0r'   s     r-   Ú	<genexpr>Ú%_default_anchorgen.<locals>.<genexpr>3   s,   é € ÐpÒXoÐSTœS  ^Ñ!3Ó4´c¸!¸nÑ:LÓ6MÕNÒXoùs   ‚)+)é    é@   é€   é   i   ))ç      à?ç      ð?g       @)ÚtupleÚlenr   )Úanchor_sizesÚaspect_ratiosÚanchor_generators      r-   Ú_default_anchorgenrN   2   s3   € ÜÑpÑXoÓpÓp€LØ&¬¨\Ó):Ñ:€MÜ& |ÓCÐØÐr/   c                   óh   ^ • \ rS rSrSrS	S\\S\R                  4      4U 4S jjjr	S r
S rSrU =r$ )
ÚRetinaNetHeadé9   aY  
A regression and classification head for use in RetinaNet.

Args:
    in_channels (int): number of channels of the input feature
    num_anchors (int): number of anchors to be predicted
    num_classes (int): number of classes to be predicted
    norm_layer (callable, optional): Module specifying the normalization layer to use. Default: None
Ú
norm_layer.c                 ó^   >• [         TU ]  5         [        XX4S9U l        [	        XUS9U l        g )N©rR   )ÚsuperÚ__init__ÚRetinaNetClassificationHeadÚclassification_headÚRetinaNetRegressionHeadÚregression_head)ÚselfÚin_channelsÚnum_anchorsÚnum_classesrR   Ú	__class__s        €r-   rV   ÚRetinaNetHead.__init__D   s2   ø€ Ü‰ÑÔÜ#>Ø kñ$
ˆÔ ô  7°{Ð\fÑgˆÕr/   c                 ót   • U R                   R                  XU5      U R                  R                  XX45      S.$ )N)ÚclassificationÚbbox_regression)rX   Úcompute_lossrZ   )r[   ÚtargetsÚhead_outputsÚanchorsÚmatched_idxss        r-   rd   ÚRetinaNetHead.compute_lossK   s<   € ð #×6Ñ6×CÑCÀGÐ[gÓhØ#×3Ñ3×@Ñ@ÀÐX_Ónñ
ð 	
r/   c                 óH   • U R                  U5      U R                  U5      S.$ )N)Ú
cls_logitsrc   ©rX   rZ   )r[   r'   s     r-   ÚforwardÚRetinaNetHead.forwardR   s$   € à"×6Ñ6°qÓ9Èd×NbÑNbÐcdÓNeÑfÐfr/   rl   ©N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   r   r   ÚModulerV   rd   rm   Ú__static_attributes__Ú__classcell__©r_   s   @r-   rP   rP   9   sE   ø† ññhÈ(ÐS[Ð\_Ðac×ajÑajÐ\jÑSkÑJl÷ hð hò
÷gð gr/   rP   c                   ó|   ^ • \ rS rSrSrSr  SS\\S\R                  4      4U 4S jjjr
U 4S jrS rS	 rS
rU =r$ )rW   éW   aJ  
A classification head for use in RetinaNet.

Args:
    in_channels (int): number of channels of the input feature
    num_anchors (int): number of anchors to be predicted
    num_classes (int): number of classes to be predicted
    norm_layer (callable, optional): Module specifying the normalization layer to use. Default: None
r   rR   .c           	      óî  >• [         T	U ]  5         / n[        S5       H'  nUR                  [        R
                  " XUS95        M)     [        R                  " U6 U l        U R                  R                  5        H™  n[        U[        R                  5      (       d  M$  [        R                  R                  R                  UR                  SS9  UR                   c  Me  [        R                  R                  R#                  UR                   S5        M›     [        R                  " XU-  SSSS9U l        [        R                  R                  R                  U R$                  R                  SS9  [        R                  R                  R#                  U R$                  R                   [&        R(                  " SU-
  U-  5      * 5        X0l        X l        [.        R0                  R2                  U l        g )	Nr1   rT   ç{®Gáz„?©Ústdr   r
   r   ©Úkernel_sizeÚstrideÚpadding)rU   rV   r5   ÚappendÚmisc_nn_opsÚConv2dNormActivationr   Ú
SequentialÚconvÚmodulesÚ
isinstanceÚConv2dÚtorchÚinitÚnormal_r2   r3   Ú	constant_rk   ÚmathÚlogr^   r]   Ú	det_utilsÚMatcherÚBETWEEN_THRESHOLDS)
r[   r\   r]   r^   Úprior_probabilityrR   r‡   Ú_Úlayerr_   s
            €r-   rV   Ú$RetinaNetClassificationHead.__init__d   s[  ø€ ô 	‰ÑÔàˆÜ�q–ˆAØ�K‰Kœ×8Ò8¸Ð^hÑiÖjñ ä—M’M 4Ð(ˆŒ	à—Y‘Y×&Ñ&Ö(ˆEÜ˜%¤§¡×+Ó+Ü—‘—‘×%Ñ% e§l¡l¸Ð%Ñ=Ø—:‘:Ó)Ü—H‘H—M‘M×+Ñ+¨E¯J©J¸Ö:ñ	 )ô Ÿ)š) K¸{Ñ1JÐXYÐbcÐmnÑoˆŒÜ�‰�‰×Ñ˜dŸo™o×4Ñ4¸$ÐÑ?Ü�‰�‰×Ñ §¡× 4Ñ 4´t·x²xÀÐEVÑAVÐZkÑ@kÓ7lÐ6lÔmà&ÔØ&Ôô
 #,×"3Ñ"3×"FÑ"FˆÕr/   c           	      ó|   >• UR                  SS 5      nUb  US:  a  [        X5        [        T	U ]  UUUUUUU5        g ©NÚversionr   ©Úgetr<   rU   Ú_load_from_state_dict©
r[   r7   r8   Úlocal_metadataÚstrictÚmissing_keysÚunexpected_keysÚ
error_msgsrš   r_   s
            €r-   r�   Ú1RetinaNetClassificationHead._load_from_state_dict…   óL   ø€ ð !×$Ñ$ Y°Ó5ˆà‰?˜g¨›kÜ˜jÔ1ä‰Ñ%ØØØØØØØõ	
r/   c           	      óP  • / nUS   n[        XU5       Hy  u  pgnUS:¬  n	U	R                  5       n
[        R                  " U5      nSUU	US   X‰      4'   X€R                  :g  nUR                  [        X|   X¼   SS9[        SU
5      -  5        M{     [        U5      [        U5      -  $ )Nrk   r   rH   ÚlabelsÚsum)Ú	reductionr   )
Úzipr¨   r‹   Ú
zeros_liker“   rƒ   r   Úmaxr.   rJ   )r[   re   rf   rh   Úlossesrk   Útargets_per_imageÚcls_logits_per_imageÚmatched_idxs_per_imageÚforeground_idxs_per_imageÚnum_foregroundÚgt_classes_targetÚvalid_idxs_per_images                r-   rd   Ú(RetinaNetClassificationHead.compute_lossž   sß   € àˆà! ,Ñ/ˆ
äORÐSZÐhtÖOuÑKÐÐ5Kà(>À!Ñ(CÐ%Ø6×:Ñ:Ó<ˆNô !&× 0Ò 0Ð1EÓ FÐð ð Ø)Ø! (Ñ+Ð,BÑ,]Ñ^ð`ñð $:×=TÑ=TÑ#TÐ ð �M‰MÜ"Ø(Ñ>Ø%Ñ;Ø#ñô
 �a˜Ó(ñ)öñ! Pvô2 �F‹|œc '›lÑ*Ð*r/   c                 óf  • / nU H•  nU R                  U5      nU R                  U5      nUR                  u  pVpxUR                  USU R                  Xx5      nUR                  SSSSS5      nUR                  USU R                  5      nUR                  U5        M—     [        R                  " USS9$ )Néÿÿÿÿr   r
   r1   r   r   ©Údim)
r‡   rk   ÚshapeÚviewr^   ÚpermuteÚreshaperƒ   r‹   Úcat)	r[   r'   Úall_cls_logitsÚfeaturesrk   ÚNr•   ÚHÚWs	            r-   rm   Ú#RetinaNetClassificationHead.forward¿   s«   € àˆãˆHØŸ™ 8Ó,ˆJØŸ™¨Ó4ˆJð $×)Ñ)‰JˆA�!Ø#Ÿ™¨¨B°×0@Ñ0@À!ÓGˆJØ#×+Ñ+¨A¨q°!°Q¸Ó:ˆJØ#×+Ñ+¨A¨r°4×3CÑ3CÓDˆJà×!Ñ! *Ö-ñ ô �yŠy˜¨QÑ/Ð/r/   )r“   rk   r‡   r]   r^   )r|   N)rp   rq   rr   rs   rt   Ú_versionr   r   r   ru   rV   r�   rd   rm   rv   rw   rx   s   @r-   rW   rW   W   sV   ø† ñð €Hð Ø9=ñGð ˜X c¨2¯9©9 nÑ5Ñ6÷Gð GõB
ò2+÷B0ð 0r/   rW   c                   ó”   ^ • \ rS rSrSrSrS\R                  0rSS\	\
S\R                  4      4U 4S jjjrU 4S jrS	 rS
 rSrU =r$ )rY   éÒ   a  
A regression head for use in RetinaNet.

Args:
    in_channels (int): number of channels of the input feature
    num_anchors (int): number of anchors to be predicted
    norm_layer (callable, optional): Module specifying the normalization layer to use. Default: None
r   Ú	box_coderrR   .c           	      óž  >• [         TU ]  5         / n[        S5       H'  nUR                  [        R
                  " XUS95        M)     [        R                  " U6 U l        [        R                  " XS-  SSSS9U l
        [        R                  R                  R                  U R                  R                  SS9  [        R                  R                  R                  U R                  R                   5        U R                  R#                  5        H˜  n[%        U[        R                  5      (       d  M$  [        R                  R                  R                  UR                  SS9  UR                   c  Me  [        R                  R                  R                  UR                   5        Mš     [&        R(                  " SS	9U l        S
U l        g )Nr1   rT   r
   r   r   r|   r}   ©rH   rH   rH   rH   ©ÚweightsÚl1)rU   rV   r5   rƒ   r„   r…   r   r†   r‡   rŠ   Úbbox_regr‹   rŒ   r�   r2   Úzeros_r3   rˆ   r‰   r‘   ÚBoxCoderrÈ   Ú
_loss_type)r[   r\   r]   rR   r‡   r•   r–   r_   s          €r-   rV   Ú RetinaNetRegressionHead.__init__â   s3  ø€ Ü‰ÑÔàˆÜ�q–ˆAØ�K‰Kœ×8Ò8¸Ð^hÑiÖjñ ä—M’M 4Ð(ˆŒ	äŸ	š	 +¸Q©ÈAÐVWÐabÑcˆŒÜ�‰�‰×Ñ˜dŸm™m×2Ñ2¸ÐÑ=Ü�‰�‰×Ñ˜TŸ]™]×/Ñ/Ô0à—Y‘Y×&Ñ&Ö(ˆEÜ˜%¤§¡×+Ó+Ü—‘—‘×%Ñ% e§l¡l¸Ð%Ñ=Ø—:‘:Ó)Ü—H‘H—M‘M×(Ñ(¨¯©Ö4ñ	 )ô #×+Ò+Ð4HÑIˆŒØˆ�r/   c           	      ó|   >• UR                  SS 5      nUb  US:  a  [        X5        [        T	U ]  UUUUUUU5        g r™   r›   rž   s
            €r-   r�   Ú-RetinaNetRegressionHead._load_from_state_dict÷   r¥   r/   c           
      óˆ  • / nUS   n[        XX45       H‹  u  pxpš[        R                  " U
S:¬  5      S   nUR                  5       nUS   X«      nX‹S S 24   nX›S S 24   n	UR	                  [        U R                  U R                  U	UU5      [        SU5      -  5        M�     [        U5      [        S[        U5      5      -  $ )Nrc   r   r   r   )rª   r‹   ÚwhereÚnumelrƒ   r   rÑ   rÈ   r¬   r.   rJ   )r[   re   rf   rg   rh   r­   rc   r®   Úbbox_regression_per_imageÚanchors_per_imager°   r±   r²   Úmatched_gt_boxes_per_images                 r-   rd   Ú$RetinaNetRegressionHead.compute_loss  sç   € àˆà&Ð'8Ñ9ˆägjØ göh
ÑcÐÐ:Kô ).¯ªÐ4JÈaÑ4OÓ(PÐQRÑ(SÐ%Ø6×<Ñ<Ó>ˆNð *;¸7Ñ)CÐDZÑDuÑ)vÐ&Ø(AÒ]^ÐB^Ñ(_Ð%Ø 1ÊQÐ2NÑ OÐð �M‰MÜØ—O‘OØ—N‘NØ%Ø.Ø-óô �a˜Ó(ñ)ö	ñh
ô0 �F‹|œc !¤S¨£\Ó2Ñ2Ð2r/   c                 ó>  • / nU H�  nU R                  U5      nU R                  U5      nUR                  u  pVpxUR                  USSXx5      nUR	                  SSSSS5      nUR                  USS5      nUR                  U5        Mƒ     [        R                  " USS9$ )Nr·   r1   r   r
   r   r   r¸   )	r‡   rÎ   rº   r»   r¼   r½   rƒ   r‹   r¾   )	r[   r'   Úall_bbox_regressionrÀ   rc   rÁ   r•   rÂ   rÃ   s	            r-   rm   ÚRetinaNetRegressionHead.forward0  s£   € à ÐãˆHØ"Ÿi™i¨Ó1ˆOØ"Ÿm™m¨OÓ<ˆOð )×.Ñ.‰JˆA�!Ø-×2Ñ2°1°b¸!¸QÓBˆOØ-×5Ñ5°a¸¸A¸qÀ!ÓDˆOØ-×5Ñ5°a¸¸QÓ?ˆOà×&Ñ& Ö7ñ ô �yŠyÐ,°!Ñ4Ð4r/   )rÑ   rÎ   rÈ   r‡   ro   )rp   rq   rr   rs   rt   rÅ   r‘   rÐ   Ú__annotations__r   r   r   ru   rV   r�   rd   rm   rv   rw   rx   s   @r-   rY   rY   Ò   s^   ø† ñð €Hð 	�Y×'Ñ'ð€Oñ¸XÀhÈsÐTV×T]ÑT]È~ÑF^Ñ=_÷ ð õ*
ò23÷@5ð 5r/   rY   c                   óÊ   ^ • \ rS rSrSr\R                  \R                  S.r             S
U 4S jjr	\
R                  R                  S 5       rS rS rSS jrS	rU =r$ )r"   iC  aü  
Implements RetinaNet.

The input to the model is expected to be a list of tensors, each of shape [C, H, W], one for each
image, and should be in 0-1 range. Different images can have different sizes.

The behavior of the model changes depending on if it is in training or evaluation mode.

During training, the model expects both the input tensors and targets (list of dictionary),
containing:
    - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with
      ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
    - labels (Int64Tensor[N]): the class label for each ground-truth box

The model returns a Dict[Tensor] during training, containing the classification and regression
losses.

During inference, the model requires only the input tensors, and returns the post-processed
predictions as a List[Dict[Tensor]], one for each input image. The fields of the Dict are as
follows:
    - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with
      ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
    - labels (Int64Tensor[N]): the predicted labels for each image
    - scores (Tensor[N]): the scores for each prediction

Args:
    backbone (nn.Module): the network used to compute the features for the model.
        It should contain an out_channels attribute, which indicates the number of output
        channels that each feature map has (and it should be the same for all feature maps).
        The backbone should return a single Tensor or an OrderedDict[Tensor].
    num_classes (int): number of output classes of the model (including the background).
    min_size (int): Images are rescaled before feeding them to the backbone:
        we attempt to preserve the aspect ratio and scale the shorter edge
        to ``min_size``. If the resulting longer edge exceeds ``max_size``,
        then downscale so that the longer edge does not exceed ``max_size``.
        This may result in the shorter edge beeing lower than ``min_size``.
    max_size (int): See ``min_size``.
    image_mean (Tuple[float, float, float]): mean values used for input normalization.
        They are generally the mean values of the dataset on which the backbone has been trained
        on
    image_std (Tuple[float, float, float]): std values used for input normalization.
        They are generally the std values of the dataset on which the backbone has been trained on
    anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature
        maps.
    head (nn.Module): Module run on top of the feature pyramid.
        Defaults to a module containing a classification and regression module.
    score_thresh (float): Score threshold used for postprocessing the detections.
    nms_thresh (float): NMS threshold used for postprocessing the detections.
    detections_per_img (int): Number of best detections to keep after NMS.
    fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
        considered as positive during training.
    bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be
        considered as negative during training.
    topk_candidates (int): Number of best detections to keep before NMS.

Example:

    >>> import torch
    >>> import torchvision
    >>> from torchvision.models.detection import RetinaNet
    >>> from torchvision.models.detection.anchor_utils import AnchorGenerator
    >>> # load a pre-trained model for classification and return
    >>> # only the features
    >>> backbone = torchvision.models.mobilenet_v2(weights=MobileNet_V2_Weights.DEFAULT).features
    >>> # RetinaNet needs to know the number of
    >>> # output channels in a backbone. For mobilenet_v2, it's 1280,
    >>> # so we need to add it here
    >>> backbone.out_channels = 1280
    >>>
    >>> # let's make the network generate 5 x 3 anchors per spatial
    >>> # location, with 5 different sizes and 3 different aspect
    >>> # ratios. We have a Tuple[Tuple[int]] because each feature
    >>> # map could potentially have different sizes and
    >>> # aspect ratios
    >>> anchor_generator = AnchorGenerator(
    >>>     sizes=((32, 64, 128, 256, 512),),
    >>>     aspect_ratios=((0.5, 1.0, 2.0),)
    >>> )
    >>>
    >>> # put the pieces together inside a RetinaNet model
    >>> model = RetinaNet(backbone,
    >>>                   num_classes=2,
    >>>                   anchor_generator=anchor_generator)
    >>> model.eval()
    >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
    >>> predictions = model(x)
)rÈ   Úproposal_matcherc                 ó`  >• [         TU ]  5         [        U 5        [        US5      (       d  [	        S5      eXl        [        U[        [        S 5      45      (       d  [        S[        U5       35      eUc
  [        5       nXpl        Uc(  [        UR                  UR                  5       S   U5      nX€l        U	c  [         R"                  " UUSS9n	X�l        [         R&                  " SS9U l        Uc  / S	QnUc  / S
Qn[+        X4XV40 UD6U l        X l        X°l        XÀl        Xðl        SU l        g )NÚout_channelsz†backbone should contain an attribute out_channels specifying the number of output channels (assumed to be the same for all the levels)zFanchor_generator should be of type AnchorGenerator or None instead of r   T)Úallow_low_quality_matchesrÊ   rË   )g
×£p=
ß?gÉv¾Ÿ/Ý?g–C‹lçûÙ?)gZd;ßOÍ?gyé&1¬Ì?gÍÌÌÌÌÌÌ?F)rU   rV   r   ÚhasattrÚ
ValueErrorÚbackboner‰   r   r9   Ú	TypeErrorrN   rM   rP   rã   Únum_anchors_per_locationÚheadr‘   r’   rá   rÐ   rÈ   r!   Ú	transformÚscore_threshÚ
nms_threshÚdetections_per_imgÚtopk_candidatesÚ_has_warned)r[   rç   r^   Úmin_sizeÚmax_sizeÚ
image_meanÚ	image_stdrM   rê   rá   rì   rí   rî   Úfg_iou_threshÚbg_iou_threshrï   Úkwargsr_   s                    €r-   rV   ÚRetinaNet.__init__¡  s@  ø€ ô* 	‰ÑÔÜ˜DÔ!ä�x ×0Ñ0Üð+óð ð
 !ŒäÐ*¬_¼dÀ4»jÐ,I×JÑJÜØXÔY]Ð^nÓYoÐXpÐqóð ð Ñ#Ü1Ó3ÐØ 0Ôà‰<Ü  ×!6Ñ!6Ð8H×8aÑ8aÓ8cÐdeÑ8fÐhsÓtˆDØŒ	àÑ#Ü(×0Ò0ØØØ*.ñ Ðð
 !1Ôä"×+Ò+Ð4HÑIˆŒàÑÚ.ˆJØÑÚ-ˆIÜ1°(ÀjÑfÐ_eÑfˆŒà(ÔØ$ŒØ"4ÔØ.Ôð !ˆÕr/   c                 ó,   • U R                   (       a  U$ U$ ro   )Útraining)r[   r­   Ú
detectionss      r-   Úeager_outputsÚRetinaNet.eager_outputsæ  s   € ð �=�=ØˆMàÐr/   c           
      ó¨  • / n[        X15       H¦  u  pVUS   R                  5       S:X  aP  UR                  [        R                  " UR                  S5      4S[        R                  UR                  S95        Ml  [        R                  " US   U5      nUR                  U R                  U5      5        M¨     U R                  R                  XX45      $ )Nr   r   r·   )ÚdtypeÚdevice)rª   r×   rƒ   r‹   ÚfullÚsizeÚint64r   Úbox_opsÚbox_iourá   rê   rd   )r[   re   rf   rg   rh   rÙ   r®   Úmatch_quality_matrixs           r-   rd   ÚRetinaNet.compute_lossî  s»   € àˆÜ47¸Ö4IÑ0ÐØ  Ñ)×/Ñ/Ó1°QÓ6Ø×#Ñ#Ü—J’JÐ 1× 6Ñ 6°qÓ 9Ð;¸RÄuÇ{Á{Ð[l×[sÑ[sÑtôñ ä#*§?¢?Ð3DÀWÑ3MÐO`Ó#aÐ Ø×Ñ × 5Ñ 5Ð6JÓ KÖLñ 5Jð �y‰y×%Ñ% g¸WÓSÐSr/   c                 ón  • US   nUS   n[        U5      n/ n[        U5       GH  nU V	s/ s H  o™U   PM	     n
n	U Vs/ s H  o»U   PM	     nnX(   X8   pí/ n/ n/ n[        X¬U5       GH*  u  nnnUR                  S   n[        R
                  " U5      R                  5       nUU R                  :„  nUU   n[        R                  " U5      S   n[        R                  " UU R                  S5      nUR                  U5      u  nnUU   n[        R                  " UUSS9nUU-  nU R                  R                  UU   UU   5      n[         R"                  " UU5      nUR%                  U5        UR%                  U5        UR%                  U5        GM-     [        R&                  " USS9n[        R&                  " USS9n[        R&                  " USS9n[         R(                  " UUUU R*                  5      nUS U R,                   nUR%                  UU   UU   UU   S.5        GM     U$ s  sn	f s  snf )	Nrk   rc   r·   r   Úfloor)Úrounding_moder¸   )r   Úscoresr§   )rJ   r5   rª   rº   r‹   ÚsigmoidÚflattenrì   rÖ   r‘   Ú	_topk_minrï   ÚtopkÚdivrÈ   Údecode_singler  Úclip_boxes_to_imagerƒ   r¾   Úbatched_nmsrí   rî   )r[   rf   rg   Úimage_shapesÚclass_logitsÚbox_regressionÚ
num_imagesrû   ÚindexÚbrÚbox_regression_per_imageÚclÚlogits_per_imagerÙ   Úimage_shapeÚimage_boxesÚimage_scoresÚimage_labelsÚbox_regression_per_levelÚlogits_per_levelÚanchors_per_levelr^   Úscores_per_levelÚ	keep_idxsÚ	topk_idxsÚnum_topkÚidxsÚanchor_idxsÚlabels_per_levelÚboxes_per_levelÚkeeps                                  r-   Úpostprocess_detectionsÚ RetinaNet.postprocess_detectionsý  sZ  € à# LÑ1ˆØ%Ð&7Ñ8ˆä˜Ó&ˆ
à.0ˆ
ä˜:×&ˆEÙ<JÓ'KºN°b¨5¬	¹NÐ$Ð'KÙ4@ÓA²L¨b 5¤	±LÐÐAØ-4©^¸\Ñ=P˜{àˆKØˆLØˆLäQTØ(Ð<M÷RÑMÐ(Ð*:Ð<Mð /×4Ñ4°RÑ8�ô $)§=¢=Ð1AÓ#B×#JÑ#JÓ#LÐ Ø,¨t×/@Ñ/@Ñ@�	Ø#3°IÑ#>Ð Ü!ŸKšK¨	Ó2°1Ñ5�	ô %×.Ò.¨y¸$×:NÑ:NÐPQÓR�Ø)9×)>Ñ)>¸xÓ)HÑ&Ð  $Ø% d™O�	ä#Ÿiši¨	°;ÈgÑV�Ø#,¨{Ñ#:Ð à"&§.¡.×">Ñ">Ø,¨[Ñ9Ð;LÈ[Ñ;Yó#�ô #*×"=Ò"=¸oÈ{Ó"[�à×"Ñ" ?Ô3Ø×#Ñ#Ð$4Ô5Ø×#Ñ#Ð$4×5ñ5Rô8  Ÿ)š) K°QÑ7ˆKÜ Ÿ9š9 \°qÑ9ˆLÜ Ÿ9š9 \°qÑ9ˆLô ×&Ò& {°LÀ,ÐPT×P_ÑP_Ó`ˆDØÐ1˜$×1Ñ1Ð2ˆDà×Ñà(¨Ñ.Ø*¨4Ñ0Ø*¨4Ñ0ñ÷ñ[ 'ðj Ðùòi (LùÚAs
   «H-¿H2c           	      ó&  • U R                   (       až  Uc  [        R                  " SS5        OƒU H}  nUS   n[        R                  " [        U[        R                  5      S5        [        R                  " [        UR                  5      S:H  =(       a    UR                  S   S:H  S	5        M     / nU H^  nUR                  S
S n[        R                  " [        U5      S:H  SUR                  S
S  35        UR                  US   US   45        M`     U R                  X5      u  pUb   [        U5       H‘  u  pƒUS   nUSS2SS24   USS2SS24   :*  n	U	R                  5       (       d  M8  [        R                  " U	R                  SS95      S   S   n
XJ   R                  5       n[        R                  " SSU SU S35        M“     U R                  UR                  5      n[        U[        R                  5      (       a  [        SU4/5      n[!        UR#                  5       5      nU R%                  U5      nU R'                  X5      n0 n/ nU R                   (       a0  Uc  [        R                  " SS5        GOU R)                  X-U5      nGO	U Vs/ s H&  nUR+                  S5      UR+                  S5      -  PM(     nnSnU H  nUU-  nM
     US   R+                  S5      nUU-  nU Vs/ s H  nUU-  PM
     nn0 nU H"  n[!        UU   R-                  USS95      UU'   M$     U Vs/ s H  n[!        UR-                  U5      5      PM     nnU R/                  UUUR0                  5      nU R                  R3                  UUR0                  U5      n[        R4                  R7                  5       (       a2  U R8                  (       d  [:        R<                  " S5        SU l        UU4$ U R?                  UU5      $ s  snf s  snf s  snf )a¾  
Args:
    images (list[Tensor]): images to be processed
    targets (list[Dict[Tensor]]): ground-truth boxes present in the image (optional)

Returns:
    result (list[BoxList] or dict[Tensor]): the output from the model.
        During training, it returns a dict[Tensor] which contains the losses.
        During testing, it returns list[BoxList] contains additional fields
        like `scores`, `labels` and `mask` (for Mask R-CNN models).

NFz0targets should not be none when in training moder   z+Expected target boxes to be of type Tensor.r   r·   r1   z5Expected target boxes to be a tensor of shape [N, 4].éþÿÿÿzJexpecting the last two dimensions of the Tensor to be H and W instead got r   r   r¸   zLAll bounding boxes should have positive height and width. Found invalid box z for target at index r4   Ú0r
   rk   zBRetinaNet always returns a (Losses, Detections) tuple in scriptingT) rú   r‹   Ú_assertr‰   r	   rJ   rº   rƒ   rë   Ú	enumerateÚanyrÖ   Útolistrç   Útensorsr   ÚlistÚvaluesrê   rM   rd   r  Úsplitr-  Úimage_sizesÚpostprocessÚjitÚis_scriptingrð   ÚwarningsÚwarnrü   )r[   Úimagesre   Útargetr   Úoriginal_image_sizesÚimgÚvalÚ
target_idxÚdegenerate_boxesÚbb_idxÚdegen_bbrÀ   rf   rg   r­   rû   r'   Únum_anchors_per_levelÚHWÚvÚHWAÚAÚhwÚsplit_head_outputsÚkÚaÚsplit_anchorss                               r-   rm   ÚRetinaNet.forward=  sÏ  € ð �=�=Ø‰Ü—’˜eÐ%WÕXã%�FØ" 7™O�EÜ—M’M¤*¨U´E·L±LÓ"AÐCpÔqÜ—M’MÜ˜EŸK™KÓ(¨AÑ-×F°%·+±+¸b±/ÀQÑ2FØOöñ &ð 79ÐÛˆCØ—)‘)˜B˜C�.ˆCÜ�MŠMÜ�C“˜A‘Ø\Ð]`×]fÑ]fÐgiÐgjÐ]kÐ\lÐmôð !×'Ñ'¨¨Q©°°Q±Ð(8Ö9ñ ð Ÿ.™.¨Ó9‰ˆð ÑÜ&/°Ö&8Ñ"�
Ø˜w™�Ø#(ª¨A©B¨¡<°5º¸B¸Q¸B¸±<Ñ#?Ð Ø#×'Ñ'×)Ó)ä"Ÿ[š[Ð)9×)=Ñ)=À!Ð)=Ð)DÓEÀaÑHÈÑK�FØ,1©M×,@Ñ,@Ó,B�HÜ—M’MØð.Ø.6¨ZÐ7LÈZÈLÐXYð[öñ '9ð —=‘= §¡Ó0ˆÜ�h¤§¡×-Ñ-Ü" S¨( OÐ#4Ó5ˆHô ˜Ÿ™Ó)Ó*ˆð —y‘y Ó*ˆð ×'Ñ'¨Ó9ˆàˆØ.0ˆ
Ø�=�=Ø‰Ü—’˜eÐ%WÖXð ×*Ñ*¨7À'ÓJ’ñ EMÓ$MÂH¸q Q§V¡V¨A£Y°·±¸³Ô%:ÁHÐ!Ð$MØˆBÛ*�Ø�a‘’ñ +à˜|Ñ,×1Ñ1°!Ó4ˆCØ�r‘	ˆAÙ6KÓ$LÒ6K° R¨!¤VÑ6KÐ!Ð$Lð ;=ÐÛ!�Ü(,¨\¸!©_×-BÑ-BÐCXÐ^_Ð-BÐ-`Ó(aÐ" 1Ó%ñ "áKRÓSÊ7ÀaœT !§'¡'Ð*?Ó"@ÖAÉ7ˆMÐSð ×4Ñ4Ð5GÈÐX^×XjÑXjÓkˆJØŸ™×3Ñ3°JÀ×@RÑ@RÐThÓiˆJä�9‰9×!Ñ!×#Ñ#Ø×#×#Ü—’ÐbÔcØ#'�Ô Ø˜:Ð%Ð%Ø×!Ñ! &¨*Ó5Ð5ùò/ %Nùò %Mùò Ts   Ê-PË4P	Ì3$P)rð   rM   rç   rÈ   rî   rê   rí   rá   rì   rï   rë   )i   i5  NNNNNgš™™™™™©?rG   i,  rG   gš™™™™™Ù?iè  ro   )rp   rq   rr   rs   rt   r‘   rÐ   r’   rß   rV   r‹   r<  Úunusedrü   rd   r-  rm   rv   rw   rx   s   @r-   r"   r"   C  s‹   ø† ñVðr ×'Ñ'Ø%×-Ñ-ñ€Oð ØØØàØØØØØØØØ÷%C!ðJ ‡Y�Y×Ññó ðòTò>÷@f6ò f6r/   r"   )r   r   )Ú
categoriesrñ   c                   óF   • \ rS rSr\" S\0 \ESSSSS00SS	S
S.ES9r\rSr	g)r#   i¬  zLhttps://download.pytorch.org/models/retinanet_resnet50_fpn_coco-eeacb38b.pthizJhttps://github.com/pytorch/vision/tree/main/references/detection#retinanetúCOCO-val2017Úbox_mapg333333B@gáz®Gñb@g�•C‹H`@zSThese weights were produced by following a similar training recipe as on the paper.©Ú
num_paramsÚrecipeÚ_metricsÚ_opsÚ
_file_sizeÚ_docs©ÚurlÚ
transformsÚmetar*   N©
rp   rq   rr   rs   r   r   Ú_COMMON_METAÚCOCO_V1ÚDEFAULTrv   r*   r/   r-   r#   r#   ¬  sN   † ÙØZØ"ð
Øð
à"ØbàØ˜tð!ðð
 Ø!Ønò
ñ€Gð" ƒGr/   r#   c                   óF   • \ rS rSr\" S\0 \ESSSSS00SS	S
S.ES9r\rSr	g)r$   iÁ  zOhttps://download.pytorch.org/models/retinanet_resnet50_fpn_v2_coco-5905b1c5.pthi—ÞFz+https://github.com/pytorch/vision/pull/5756rW  rX  g     ÀD@gV-²�c@gw¾Ÿ/Ab@zZThese weights were produced using an enhanced training recipe to boost the model accuracy.rY  r`  r*   Nrd  r*   r/   r-   r$   r$   Á  sN   † ÙØ]Ø"ð
Øð
à"ØCàØ˜tð!ðð
 Ø!Øuò
ñ€Gð" ƒGr/   r$   Ú
pretrainedÚpretrained_backbone)rÌ   Úweights_backboneT)rÌ   Úprogressr^   rk  Útrainable_backbone_layersrÌ   rl  r^   rk  rm  r÷   c           	      ó  • [         R                  U 5      n [        R                  " U5      nU b&  Sn[        SU[	        U R
                  S   5      5      nOUc  SnU SL=(       d    USLn[        XdSS5      nU(       a  [        R                  O[        R                  n[        X1US9n[        X„/ SQ[        S	S	5      S
9n[        X‚40 UD6n	U b?  U	R                  U R!                  USS95        U [         R"                  :X  a  [%        U	S5        U	$ )aä  
Constructs a RetinaNet model with a ResNet-50-FPN backbone.

.. betastatus:: detection module

Reference: `Focal Loss for Dense Object Detection <https://arxiv.org/abs/1708.02002>`_.

The input to the model is expected to be a list of tensors, each of shape ``[C, H, W]``, one for each
image, and should be in ``0-1`` range. Different images can have different sizes.

The behavior of the model changes depending on if it is in training or evaluation mode.

During training, the model expects both the input tensors and targets (list of dictionary),
containing:

    - boxes (``FloatTensor[N, 4]``): the ground-truth boxes in ``[x1, y1, x2, y2]`` format, with
      ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
    - labels (``Int64Tensor[N]``): the class label for each ground-truth box

The model returns a ``Dict[Tensor]`` during training, containing the classification and regression
losses.

During inference, the model requires only the input tensors, and returns the post-processed
predictions as a ``List[Dict[Tensor]]``, one for each input image. The fields of the ``Dict`` are as
follows, where ``N`` is the number of detections:

    - boxes (``FloatTensor[N, 4]``): the predicted boxes in ``[x1, y1, x2, y2]`` format, with
      ``0 <= x1 < x2 <= W`` and ``0 <= y1 < y2 <= H``.
    - labels (``Int64Tensor[N]``): the predicted labels for each detection
    - scores (``Tensor[N]``): the scores of each detection

For more details on the output, you may refer to :ref:`instance_seg_output`.

Example::

    >>> model = torchvision.models.detection.retinanet_resnet50_fpn(weights=RetinaNet_ResNet50_FPN_Weights.DEFAULT)
    >>> model.eval()
    >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
    >>> predictions = model(x)

Args:
    weights (:class:`~torchvision.models.detection.RetinaNet_ResNet50_FPN_Weights`, optional): The
        pretrained weights to use. See
        :class:`~torchvision.models.detection.RetinaNet_ResNet50_FPN_Weights`
        below for more details, and possible values. By default, no
        pre-trained weights are used.
    progress (bool): If True, displays a progress bar of the download to stderr. Default is True.
    num_classes (int, optional): number of output classes of the model (including the background)
    weights_backbone (:class:`~torchvision.models.ResNet50_Weights`, optional): The pretrained weights for
        the backbone.
    trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from final block.
        Valid values are between 0 and 5, with 5 meaning all backbone layers are trainable. If ``None`` is
        passed (the default) this value is set to 3.
    **kwargs: parameters passed to the ``torchvision.models.detection.RetinaNet``
        base class. Please refer to the `source code
        <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/retinanet.py>`_
        for more details about this class.

.. autoclass:: torchvision.models.detection.RetinaNet_ResNet50_FPN_Weights
    :members:
Nr^   rU  é[   é   r
   )rÌ   rl  rR   ©r   r
   r1   rF   ©Úreturned_layersÚextra_blocksT©rl  Ú
check_hashg        )r#   Úverifyr   r   rJ   rc  r    r„   ÚFrozenBatchNorm2dr   ÚBatchNorm2dr   r   r   r"   Úload_state_dictÚget_state_dictrf  r   )
rÌ   rl  r^   rk  rm  r÷   Ú
is_trainedrR   rç   Úmodels
             r-   r%   r%   Ö  s  € ôV -×3Ñ3°GÓ<€GÜ'×.Ò.Ð/?Ó@ÐàÑØÐÜ+¨M¸;ÌÈGÏLÉLÐYeÑLfÓHgÓh‰Ø	Ñ	Øˆà Ð$×DÐ(8ÀÐ(D€JÜ :¸:ÐbcÐefÓ gÐÞ2<”×.Ò.Ä"Ç.Á.€JäÐ 0ÐPZÑ[€Hä$ØºYÔUbÐcfÐhkÓUlñ€Hô �hÑ6¨vÑ6€EàÑØ×Ñ˜g×4Ñ4¸hÐSWÐ4ÐXÔYØÔ4×<Ñ<Ó<Ü˜% Ô%à€Lr/   c           	      óD  • [         R                  U 5      n [        R                  " U5      nU b&  Sn[        SU[	        U R
                  S   5      5      nOUc  SnU SL=(       d    USLn[        XdSS5      n[        X1S9n[        Xt/ SQ[        S	S
5      S9n[        5       n[        UR                  UR                  5       S   U[        [        R                   S5      S9n	SU	R"                  l        ['        Xr4X‰S.UD6n
U b  U
R)                  U R+                  USS95        U
$ )aj  
Constructs an improved RetinaNet model with a ResNet-50-FPN backbone.

.. betastatus:: detection module

Reference: `Bridging the Gap Between Anchor-based and Anchor-free Detection via Adaptive Training Sample Selection
<https://arxiv.org/abs/1912.02424>`_.

:func:`~torchvision.models.detection.retinanet_resnet50_fpn` for more details.

Args:
    weights (:class:`~torchvision.models.detection.RetinaNet_ResNet50_FPN_V2_Weights`, optional): The
        pretrained weights to use. See
        :class:`~torchvision.models.detection.RetinaNet_ResNet50_FPN_V2_Weights`
        below for more details, and possible values. By default, no
        pre-trained weights are used.
    progress (bool): If True, displays a progress bar of the download to stderr. Default is True.
    num_classes (int, optional): number of output classes of the model (including the background)
    weights_backbone (:class:`~torchvision.models.ResNet50_Weights`, optional): The pretrained weights for
        the backbone.
    trainable_backbone_layers (int, optional): number of trainable (not frozen) layers starting from final block.
        Valid values are between 0 and 5, with 5 meaning all backbone layers are trainable. If ``None`` is
        passed (the default) this value is set to 3.
    **kwargs: parameters passed to the ``torchvision.models.detection.RetinaNet``
        base class. Please refer to the `source code
        <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/retinanet.py>`_
        for more details about this class.

.. autoclass:: torchvision.models.detection.RetinaNet_ResNet50_FPN_V2_Weights
    :members:
Nr^   rU  ro  rp  r
   )rÌ   rl  rq  i   rF   rr  r   rC   rT   Úgiou)rM   rê   Tru  )r$   rw  r   r   rJ   rc  r    r   r   r   rN   rP   rã   ré   r   r   Ú	GroupNormrZ   rÑ   r"   rz  r{  )rÌ   rl  r^   rk  rm  r÷   r|  rç   rM   rê   r}  s              r-   r&   r&   =  s7  € ôZ 0×6Ñ6°wÓ?€GÜ'×.Ò.Ð/?Ó@ÐàÑØÐÜ+¨M¸;ÌÈGÏLÉLÐYeÑLfÓHgÓh‰Ø	Ñ	Øˆà Ð$×DÐ(8ÀÐ(D€JÜ :¸:ÐbcÐefÓ gÐäÐ 0ÑD€HÜ$ØºYÔUbÐcgÐilÓUmñ€Hô *Ó+ÐÜØ×ÑØ×1Ñ1Ó3°AÑ6ØÜœ2Ÿ<™<¨Ó,ñ	€Dð '-€D×ÑÔ#Ü�hÐdÐ>NÑdÐ]cÑd€EàÑØ×Ñ˜g×4Ñ4¸hÐSWÐ4ÐXÔYà€Lr/   )Cr�   r>  Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r‹   r   r	   Úopsr   r  r   r„   r   Úops.feature_pyramid_networkr   Útransforms._presetsr   Úutilsr   Ú_apir   r   r   Ú_metar   r   r   r   Úresnetr   r   Ú r‘   r   r   Úanchor_utilsr   Úbackbone_utilsr   r    rë   r!   Ú__all__r7  r.   r<   rN   ru   rP   rW   rY   r"   re  r#   r$   rf  ÚIMAGENET1K_V1Úboolr?   r%   r&   r*   r/   r-   Ú<module>r‘     sT  ðÛ Û Ý #Ý ß *Ñ *ã ß ç LÑ LÝ 8Ý 2Ý (ß 7Ñ 7Ý $ß Cß /Ý !ß ,Ý )ß MÝ /ò€ðˆD�‰Lð ˜Vô ò>òôg�B—I‘Iô gô<x0 "§)¡)ô x0ôvn5˜bŸi™iô n5ôb`6�—	‘	ô `6ðH #Øñ€ô [ô ô*¨ô ñ* ÓÙØÐ9×AÑAÐBØ+Ð-=×-KÑ-KÐLñð 9=ØØ!%Ø3C×3QÑ3QØ/3ò_àÐ4Ñ5ð_ð ð_ð ˜#‘ð	_ð
 Ð/Ñ0ð_ð  (¨™}ð_ð ð_ð ô_ó	ó ð
_ñD ÓÙØÐ<×DÑDÐEØ+Ð-=×-KÑ-KÐLñð <@ØØ!%Ø37Ø/3òEàÐ7Ñ8ðEð ðEð ˜#‘ð	Eð
 Ð/Ñ0ðEð  (¨™}ðEð ðEð ôEó	ó ñ
Er/   