ó
    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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)  SSK*J+r+J,r,  SSK-J.r.  / SQr/ " S S\R`                  5      r1 " S S\R`                  5      r2 " S S\R`                  5      r3 " S S\R`                  5      r4 " S S\5      r5\" 5       \"" S \5Rl                  4S!\%Rn                  4S"9SS#S\%Rn                  SS$.S%\	\5   S&\8S'\	\9   S(\	\%   S)\	\9   S*\S+\44S, jj5       5       r:g)-é    N)ÚOrderedDict)Úpartial)ÚAnyÚCallableÚOptional)ÚnnÚTensoré   )ÚboxesÚgeneralized_box_iou_lossÚ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)ÚAnchorGenerator)Ú_resnet_fpn_extractorÚ_validate_trainable_layers)ÚGeneralizedRCNNTransform)ÚFCOSÚFCOS_ResNet50_FPN_WeightsÚfcos_resnet50_fpnc                   óÚ   ^ • \ rS rSrSrS\R                  0rSS\S\S\S\	\   SS	4
U 4S
 jjjr
S\\\\4      S\\\4   S\\   S\\   S\\\4   4
S jrS\\   S\\\4   4S jrSrU =r$ )ÚFCOSHeadé   a8  
A regression and classification head for use in FCOS.

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
    num_convs (Optional[int]): number of conv layer of head. Default: 4.
Ú	box_coderÚin_channelsÚnum_anchorsÚnum_classesÚ	num_convsÚreturnNc                 ó˜   >• [         TU ]  5         [        R                  " SS9U l        [        XX45      U l        [        XU5      U l        g )NT©Únormalize_by_size)	ÚsuperÚ__init__Ú	det_utilsÚBoxLinearCoderr'   ÚFCOSClassificationHeadÚclassification_headÚFCOSRegressionHeadÚregression_head)Úselfr(   r)   r*   r+   Ú	__class__s        €Ú^/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torchvision/models/detection/fcos.pyr1   ÚFCOSHead.__init__.   s?   ø€ Ü‰ÑÔÜ"×1Ò1ÀDÑIˆŒÜ#9¸+ÐT_Ó#kˆÔ Ü1°+ÈIÓVˆÕó    ÚtargetsÚhead_outputsÚanchorsÚmatched_idxsc                 óV  • US   nUS   nUS   n/ n/ n	[        X5       H¨  u  p«[        U
S   5      S:X  a>  U
S   R                  [        U5      45      nU
S   R                  [        U5      S45      nO*U
S   UR                  SS9   nU
S   UR                  SS9   nS	XËS:  '   UR	                  U5        U	R	                  U5        Mª     [
        R                  " U	5      [
        R                  " U5      [
        R                  " U5      p8n	US:¬  nUR                  5       R                  5       n[
        R                  " U5      nS
XÎXŽ   4'   [        X\SS9nU R                  R                  Xc5      n[        UU   Xž   SS9nU R                  R                  X95      n[        U5      S:X  a#  UR                  UR                  5       S S	 5      nO{US S 2S S 2SS/4   nUS S 2S S 2SS/4   n[
        R                   " UR#                  S	S9S   UR%                  S	S9S   -  UR#                  S	S9S   UR%                  S	S9S   -  -  5      nUR'                  SS9n[(        R*                  R-                  UU   UU   SS9nU[%        SU5      -  U[%        SU5      -  U[%        SU5      -  S.$ )NÚ
cls_logitsÚbbox_regressionÚbbox_ctrnessÚlabelsr   r   é   )Úminéÿÿÿÿç      ð?Úsum)Ú	reductionr   r   r
   ©Údim)ÚclassificationrC   rD   )ÚzipÚlenÚ	new_zerosÚclipÚappendÚtorchÚstackrJ   ÚitemÚ
zeros_liker   r'   Údecoder   ÚencodeÚsizeÚsqrtrG   ÚmaxÚsqueezer   Ú
functionalÚ binary_cross_entropy_with_logits)r8   r=   r>   r?   r@   rB   rC   rD   Úall_gt_classes_targetsÚall_gt_boxes_targetsÚtargets_per_imageÚmatched_idxs_per_imageÚgt_classes_targetsÚgt_boxes_targetsÚforegroud_maskÚnum_foregroundÚloss_clsÚ
pred_boxesÚloss_bbox_regÚbbox_reg_targetsÚgt_ctrness_targetsÚ
left_rightÚ
top_bottomÚpred_centernessÚloss_bbox_ctrnesss                            r:   Úcompute_lossÚFCOSHead.compute_loss4   s  € ð " ,Ñ/ˆ
Ø&Ð'8Ñ9ˆØ# NÑ3ˆà!#ÐØ!ÐÜ9<¸WÖ9SÑ5ÐÜÐ$ XÑ.Ó/°1Ó4Ø%6°xÑ%@×%JÑ%JÌCÐPfÓLgÐKiÓ%jÐ"Ø#4°WÑ#=×#GÑ#GÌÐMcÓIdÐfgÐHhÓ#iÑ à%6°xÑ%@ÐAW×A\ÑA\ÐabÐA\ÐAcÑ%dÐ"Ø#4°WÑ#=Ð>T×>YÑ>YÐ^_Ð>YÐ>`Ñ#aÐ Ø=?Ð¸Ñ9Ñ:Ø"×)Ñ)Ð*<Ô=Ø ×'Ñ'Ð(8Ö9ñ :Tô �KŠKÐ,Ó-Ü�KŠKÐ.Ó/Ü�KŠK˜Ó ð 7>Ðð 0°1Ñ4ˆØ'×+Ñ+Ó-×2Ñ2Ó4ˆô #×-Ò-¨jÓ9ÐØUXÐÐ+AÑ+QÐQÑRÜ% jÐPUÑVˆð —^‘^×*Ñ*¨?ÓDˆ
ô 1Ø�~Ñ&Ø Ñ0Øñ
ˆð  Ÿ>™>×0Ñ0°ÓOÐäÐÓ  AÓ%Ø!1×!;Ñ!;Ð<L×<QÑ<QÓ<SÐTWÐUWÐ<XÓ!YÑà)ª!ªQ°°A°¨,Ñ7ˆJØ)ª!ªQ°°A°¨,Ñ7ˆJÜ!&§¢Ø—‘ B�Ð'¨Ñ*¨Z¯^©^À¨^Ð-CÀAÑ-FÑFØ—>‘> b�>Ð)¨!Ñ,¨z¯~©~À"¨~Ð/EÀaÑ/HÑHñJó"Ðð '×.Ñ.°1Ð.Ð5ˆÜŸM™M×JÑJØ˜NÑ+Ð-?ÀÑ-OÐ[`ð Kð 
Ðð
 '¬¨Q°Ó)?Ñ?Ø,¬s°1°nÓ/EÑEØ-´°A°~Ó0FÑFñ
ð 	
r<   Úxc                 óV   • U R                  U5      nU R                  U5      u  p4UUUS.$ )N)rB   rC   rD   )r5   r7   )r8   rs   rB   rC   rD   s        r:   ÚforwardÚFCOSHead.forward   s8   € Ø×-Ñ-¨aÓ0ˆ
Ø(,×(<Ñ(<¸QÓ(?Ñ%ˆà$Ø.Ø(ñ
ð 	
r<   )r'   r5   r7   )rF   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r2   r3   Ú__annotations__Úintr   r1   ÚlistÚdictÚstrr	   rq   ru   Ú__static_attributes__Ú__classcell__©r9   s   @r:   r%   r%      sá   ø† ñð 	�Y×-Ñ-ð€OñW Cð W°cð WÈð WÐX`ÐadÑXeð WÐnr÷ Wð WðI
à�d˜3 ˜;Ñ'Ñ(ðI
ð ˜3 ˜;Ñ'ðI
ð �f‘ð	I
ð
 ˜6‘lðI
ð 
ˆc�6ˆkÑ	ôI
ðV
˜˜f™ð 
¨$¨s°F¨{Ñ*;÷ 
ò 
r<   r%   c                   ó’   ^ • \ rS rSrSr   SS\S\S\S\S\S	\\S
\	R                  4      SS4U 4S jjjrS\\   S\4S jrSrU =r$ )r4   é‰   a´  
A classification head for use in FCOS.

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.
    num_convs (Optional[int]): number of conv layer. Default: 4.
    prior_probability (Optional[float]): probability of prior. Default: 0.01.
    norm_layer: Module specifying the normalization layer to use.
Nr(   r)   r*   r+   Úprior_probabilityÚ
norm_layer.r,   c                 óF  >• [         T
U ]  5         X0l        X l        Uc  [	        [
        R                  S5      n/ n[        U5       Hd  nUR                  [
        R                  " XSSSS95        UR                  U" U5      5        UR                  [
        R                  " 5       5        Mf     [
        R                  " U6 U l        U R                  R                  5        HŠ  n	[        U	[
        R                  5      (       d  M$  [        R
                  R                   R#                  U	R$                  SS9  [        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        g )Né    r
   r   ©Úkernel_sizeÚstrideÚpaddingç{®Gáz„?©Ústdr   )r0   r1   r*   r)   r   r   Ú	GroupNormÚrangerS   ÚConv2dÚReLUÚ
SequentialÚconvÚchildrenÚ
isinstancerT   ÚinitÚnormal_ÚweightÚ	constant_ÚbiasrB   ÚmathÚlog)r8   r(   r)   r*   r+   r†   r‡   r–   Ú_Úlayerr9   s             €r:   r1   ÚFCOSClassificationHead.__init__–   sz  ø€ ô 	‰ÑÔà&ÔØ&ÔàÑÜ ¤§¡¨rÓ2ˆJàˆÜ�yÖ!ˆAØ�K‰KœŸ	š	 +ÈÐRSÐ]^Ñ_Ô`Ø�K‰K™
 ;Ó/Ô0Ø�K‰KœŸš›	Ö"ñ "ô —M’M 4Ð(ˆŒ	à—Y‘Y×'Ñ'Ö)ˆEÜ˜%¤§¡×+Ó+Ü—‘—‘×%Ñ% e§l¡l¸Ð%Ñ=Ü—‘—‘×'Ñ'¨¯
©
°AÖ6ñ *ô
 Ÿ)š) K¸{Ñ1JÐXYÐbcÐmnÑoˆŒÜ�‰�‰×Ñ˜dŸo™o×4Ñ4¸$ÐÑ?Ü�‰�‰×Ñ §¡× 4Ñ 4´t·x²xÀÐEVÑAVÐZkÑ@kÓ7lÐ6lÕmr<   rs   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$ )NrH   r   r
   rF   r   r   rL   )
r–   rB   ÚshapeÚviewr*   ÚpermuteÚreshaperS   rT   Úcat)	r8   rs   Úall_cls_logitsÚfeaturesrB   ÚNr    ÚHÚWs	            r:   ru   ÚFCOSClassificationHead.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<   )rB   r–   r)   r*   )rF   rŽ   N)rw   rx   ry   rz   r{   r}   Úfloatr   r   r   ÚModuler1   r~   r	   ru   r�   r‚   rƒ   s   @r:   r4   r4   ‰   s™   ø† ñ
ð" Ø#'Ø9=ñnàðnð ðnð ð	nð
 ðnð !ðnð ˜X c¨2¯9©9 nÑ5Ñ6ðnð 
÷nð nðB0˜˜f™ð 0¨&÷ 0ò 0r<   r4   c                   óŽ   ^ • \ rS rSrSr  SS\S\S\S\\S\R                  4      4U 4S jjjr
S	\\   S
\\\4   4S jrSrU =r$ )r6   éÉ   aì  
A regression head for use in FCOS, which combines regression branch and center-ness branch.
This can obtain better performance.

Reference: `FCOS: A simple and strong anchor-free object detector <https://arxiv.org/abs/2006.09214>`_.

Args:
    in_channels (int): number of channels of the input feature
    num_anchors (int): number of anchors to be predicted
    num_convs (Optional[int]): number of conv layer. Default: 4.
    norm_layer: Module specifying the normalization layer to use.
r(   r)   r+   r‡   .c                 óH  >• [         TU ]  5         Uc  [        [        R                  S5      n/ n[        U5       Hd  nUR                  [        R                  " XSSSS95        UR                  U" U5      5        UR                  [        R                  " 5       5        Mf     [        R                  " U6 U l
        [        R                  " XS-  SSSS9U l        [        R                  " XS-  SSSS9U l        U R                  U R                  4 Hh  n[        R                  R                  R                  UR                   SS9  [        R                  R                  R#                  UR$                  5        Mj     U R                  R'                  5        H‰  n[)        U[        R                  5      (       d  M$  [        R                  R                  R                  UR                   SS9  [        R                  R                  R#                  UR$                  5        M‹     g )Nr‰   r
   r   rŠ   rF   rŽ   r�   )r0   r1   r   r   r‘   r’   rS   r“   r”   r•   r–   Úbbox_regrD   rT   r™   rš   r›   Úzeros_r�   r—   r˜   )	r8   r(   r)   r+   r‡   r–   r    r¡   r9   s	           €r:   r1   ÚFCOSRegressionHead.__init__×   s{  ø€ ô 	‰ÑÔàÑÜ ¤§¡¨rÓ2ˆJàˆÜ�yÖ!ˆAØ�K‰KœŸ	š	 +ÈÐRSÐ]^Ñ_Ô`Ø�K‰K™
 ;Ó/Ô0Ø�K‰KœŸš›	Ö"ñ "ô —M’M 4Ð(ˆŒ	äŸ	š	 +¸Q©ÈAÐVWÐabÑcˆŒÜŸIšI kÀ±?ÐPQÐZ[ÐefÑgˆÔØ—m‘m T×%6Ñ%6Ó7ˆEÜ�H‰H�M‰M×!Ñ! %§,¡,°DÐ!Ñ9Ü�H‰H�M‰M× Ñ  §¡Ö,ñ 8ð —Y‘Y×'Ñ'Ö)ˆEÜ˜%¤§¡×+Ó+Ü—‘—‘×%Ñ% e§l¡l¸Ð%Ñ=Ü—‘—‘×$Ñ$ U§Z¡ZÖ0ò *r<   rs   r,   c                 ób  • / n/ nU Hü  nU R                  U5      n[        R                  R                  U R	                  U5      5      nU R                  U5      nUR                  u  p‰p«UR                  USSX«5      nUR                  SSSSS5      nUR                  USS5      nUR                  U5        UR                  USSX«5      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[        R                  " USS94$ )NrH   rF   r   r
   r   r   rL   )r–   r   r^   Úrelur´   rD   r¤   r¥   r¦   r§   rS   rT   r¨   )r8   rs   Úall_bbox_regressionÚall_bbox_ctrnessrª   Úbbox_featurerC   rD   r«   r    r¬   r­   s               r:   ru   ÚFCOSRegressionHead.forwardõ   s,  € Ø ÐØÐãˆHØŸ9™9 XÓ.ˆLÜ Ÿm™m×0Ñ0°·±¸|Ó1LÓMˆOØ×,Ñ,¨\Ó:ˆLð )×.Ñ.‰JˆA�!Ø-×2Ñ2°1°b¸!¸QÓBˆOØ-×5Ñ5°a¸¸A¸qÀ!ÓDˆOØ-×5Ñ5°a¸¸QÓ?ˆOØ×&Ñ& Ô7ð (×,Ñ,¨Q°°A°qÓ<ˆLØ'×/Ñ/°°1°a¸¸AÓ>ˆLØ'×/Ñ/°°2°qÓ9ˆLØ×#Ñ# LÖ1ñ! ô$ �yŠyÐ,°!Ñ4´e·i²iÐ@PÐVWÑ6XÐXÐXr<   )rD   r´   r–   )rF   N)rw   rx   ry   rz   r{   r}   r   r   r   r°   r1   r~   r	   Útupleru   r�   r‚   rƒ   s   @r:   r6   r6   É   s{   ø† ñð" Ø9=ñ1àð1ð ð1ð ð	1ð
 ˜X c¨2¯9©9 nÑ5Ñ6÷1ð 1ð<Y˜˜f™ð Y¨%°¸°Ñ*?÷ Yò Yr<   r6   c                   ól  ^ • \ rS rSrSrS\R                  0r           S S\R                  S\
S\
S\
S\\\      S	\\\      S
\\   S\\R                     S\S\S\S\
S\
4U 4S jjjr\R"                  R$                  S\\\4   S\\\\4      S\\\\4   \\\\4      4   4S j5       rS\\\\4      S\\\4   S\\   S\\
   S\\\4   4
S jrS\\\\   4   S\\\      S\\\
\
4      S\\\\4      4S jr S!S\\   S\\\\\4         S\\\\4   \\\\4      4   4S jjrSrU =r$ )"r!   i  aB  
Implements FCOS.

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, regression
and centerness 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. For FCOS, only set one anchor for per position of each level, the width and height equal to
        the stride of feature map, and set aspect ratio = 1.0, so the center of anchor is equivalent to the point
        in FCOS paper.
    head (nn.Module): Module run on top of the feature pyramid.
        Defaults to a module containing a classification and regression module.
    center_sampling_radius (int): radius of the "center" of a groundtruth box,
        within which all anchor points are labeled positive.
    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.
    topk_candidates (int): Number of best detections to keep before NMS.

Example:

    >>> import torch
    >>> import torchvision
    >>> from torchvision.models.detection import FCOS
    >>> 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
    >>> # FCOS 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=((8,), (16,), (32,), (64,), (128,)),
    >>>     aspect_ratios=((1.0,),)
    >>> )
    >>>
    >>> # put the pieces together inside a FCOS model
    >>> model = FCOS(
    >>>     backbone,
    >>>     num_classes=80,
    >>>     anchor_generator=anchor_generator,
    >>> )
    >>> model.eval()
    >>> x = [torch.rand(3, 300, 400), torch.rand(3, 500, 400)]
    >>> predictions = model(x)
r'   Úbackboner*   Úmin_sizeÚmax_sizeÚ
image_meanÚ	image_stdÚanchor_generatorÚheadÚcenter_sampling_radiusÚscore_threshÚ
nms_threshÚdetections_per_imgÚtopk_candidatesc                 óÒ  >• [         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  SnS[        U5      -  n[        UU5      nXpl        U R                  R                  5       S   S:w  a  [	        SUR                  5       S    35      eUc(  [        UR                  UR                  5       S   U5      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        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)zIanchor_generator should be of type AnchorGenerator or None, instead  got ))é   )é   )r‰   )é@   )é€   ))rI   r   r   zFanchor_generator.num_anchors_per_location()[0] should be 1 instead of Tr.   )g
×£p=
ß?gÉv¾Ÿ/Ý?g–C‹lçûÙ?)gZd;ßOÍ?gyé&1¬Ì?gÍÌÌÌÌÌÌ?F)r0   r1   r   ÚhasattrÚ
ValueErrorr¿   r˜   r   ÚtypeÚ	TypeErrorrP   rÄ   Únum_anchors_per_locationr%   rÌ   rÅ   r2   r3   r'   r    Ú	transformrÆ   rÇ   rÈ   rÉ   rÊ   Ú_has_warned)r8   r¿   r*   rÀ   rÁ   rÂ   rÃ   rÄ   rÅ   rÆ   rÇ   rÈ   rÉ   rÊ   ÚkwargsÚanchor_sizesÚaspect_ratiosr9   s                    €r:   r1   ÚFCOS.__init__m  s–  ø€ ô& 	‰ÑÔÜ˜DÔ!ä�x ×0Ñ0Üð+óð ð
 !ŒäÐ*¬_¼dÀ4»jÐ,I×JÑJÜØ[Ô\`ÐaqÓ\rÐ[sÐtóð ð Ñ#Ø>ˆLØ%¬¨LÓ(9Ñ9ˆMÜ.¨|¸]ÓKÐØ 0ÔØ× Ñ ×9Ñ9Ó;¸AÑ>À!ÓCÜØXÐYi÷  ZCñ  ZCó  ZEð  FGñ  ZHð  YIð  Jóð ð ‰<Ü˜H×1Ñ1Ð3C×3\Ñ3\Ó3^Ð_`Ñ3aÐcnÓoˆDØŒ	ä"×1Ò1ÀDÑIˆŒàÑÚ.ˆJØÑÚ-ˆIÜ1°(ÀjÑfÐ_eÑfˆŒà&<Ô#Ø(ÔØ$ŒØ"4ÔØ.Ôð !ˆÕr<   ÚlossesÚ
detectionsr,   c                 ó,   • U R                   (       a  U$ U$ ©N)Útraining)r8   rÜ   rÝ   s      r:   Úeager_outputsÚFCOS.eager_outputs¯  s   € ð �=�=ØˆMàÐr<   r=   r>   r?   Únum_anchors_per_levelc           
      ó,  • / n[        X15       GHg  u  pgUS   R                  5       S:X  aP  UR                  [        R                  " UR                  S5      4S[        R                  UR                  S95        Mm  US   nUS S 2S S24   US S 2SS 24   -   S-  n	US S 2S S24   US S 2SS 24   -   S-  n
US S 2S4   US S 2S4   -
  nU
S S 2S S S 24   U	S S S 2S S 24   -
  R                  5       R                  SS9R                  U R                  US S 2S 4   -  :  nU
R                  SS9R                  SS9u  pÞUR                  SS9R                  SS9u  nnnn[        R                  " Xß-
  UU-
  UU-
  UU-
  /SS9nUUR                  SS9R                  S:„  -  nUS-  nSUS US   & US	-  n[!        S
5      UUS   * S & UR                  SS9R                  nUUUS S 2S 4   :„  UUS S 2S 4   :  -  -  nUS S 2S4   US S 2S4   -
  US S 2S4   US S 2S4   -
  -  nUR#                  [        R$                  5      SUS S S 24   -
  -  nUR                  SS9u  nnSUUS:  '   UR                  U5        GMj     U R&                  R)                  XX55      $ )Nr   r   rH   )ÚdtypeÚdevicer   rL   r   rF   rÍ   Úinfr
   g    „×—Agñhãˆµøä>)rO   ÚnumelrS   rT   ÚfullrZ   Úint64ræ   Úabs_r\   ÚvaluesrÆ   Ú	unsqueezeÚunbindrU   rG   r¯   ÚtoÚfloat32rÅ   rq   )r8   r=   r>   r?   rã   r@   Úanchors_per_imagerb   Úgt_boxesÚ
gt_centersÚanchor_centersrÙ   Úpairwise_matchrs   ÚyÚx0Úy0Úx1Úy1Úpairwise_distÚlower_boundÚupper_boundÚgt_areasÚ
min_valuesÚmatched_idxs                            r:   rq   ÚFCOS.compute_loss¸  s  € ð ˆÜ47¸×4IÑ0ÐØ  Ñ)×/Ñ/Ó1°QÓ6Ø×#Ñ#Ü—J’JÐ 1× 6Ñ 6°qÓ 9Ð;¸RÄuÇ{Á{Ð[l×[sÑ[sÑtôñ à(¨Ñ1ˆHØ"¢1 b q b 5™/¨H²Q¸¹°U©OÑ;¸qÑ@ˆJØ/²°2°A°2°Ñ6Ð9JÊ1ÈaÉbÈ5Ñ9QÑQÐUVÑVˆNØ,ªQ°¨TÑ2Ð5FÂqÈ!ÀtÑ5LÑLˆLà,ªQ°²a¨ZÑ8¸:ÀdÊAÊqÀjÑ;QÑQ×WÑWÓY×]Ñ]Øð ^ð ç‰f�t×2Ñ2°\Â!ÀTÀ'Ñ5JÑJñKˆNð "×+Ñ+°Ð+Ð2×9Ñ9¸aÐ9Ð@‰DˆAØ%×/Ñ/°AÐ/Ð6×=Ñ=À!Ð=ÐD‰NˆB��B˜Ü!ŸKšK¨©°°R±¸¸a¹ÀÀaÁÐ(HÈaÑPˆMð ˜m×/Ñ/°AÐ/Ð6×=Ñ=ÀÑAÑAˆNð '¨Ñ*ˆKØ67ˆKÐ2Ð/°Ñ2Ð3Ø&¨Ñ*ˆKÜ8=¸e»ˆKÐ.¨rÑ2Ð2Ð4Ð5Ø)×-Ñ-°!Ð-Ð4×;Ñ;ˆMØ˜}¨{º1¸d¸7Ñ/CÑCÈÐXcÒdeÐgkÐdkÑXlÑHlÑmÑmˆNð !¢ A ™¨²!°Q°$©Ñ7¸HÂQÈÀT¹NÈXÒVWÐYZÐVZÉ^Ñ<[Ñ\ˆHØ+×.Ñ.¬u¯}©}Ó=ÀÀxÐPTÒVWÐPWÑGXÑAXÑYˆNØ&4×&8Ñ&8¸QÐ&8Ð&?Ñ#ˆJ˜Ø-/ˆK˜
 TÑ)Ñ*à×Ñ ×,ñK 5JðN �y‰y×%Ñ% g¸WÓSÐSr<   Úimage_shapesc                 ó  • US   nUS   nUS   n[        U5      n/ n[        U5       GHE  n	U V
s/ s H  oªU	   PM	     nn
U Vs/ s H  oÌU	   PM	     nnU Vs/ s H  oîU	   PM	     nnX)   X9   nn/ n/ n/ n[        X½UU5       GHV  u  nnnnUR                  S   n[        R
                  " [        R                  " U5      [        R                  " U5      -  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MY     [        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MH     U$ s  sn
f s  snf s  snf )
NrB   rC   rD   rH   r   Úfloor)Úrounding_moderL   )r   ÚscoresrE   )rP   r’   rO   r¤   rT   r[   ÚsigmoidÚflattenrÇ   Úwherer2   Ú	_topk_minrÊ   ÚtopkÚdivr'   rX   Úbox_opsÚclip_boxes_to_imagerS   r¨   Úbatched_nmsrÈ   rÉ   )#r8   r>   r?   r  Úclass_logitsÚbox_regressionÚbox_ctrnessÚ
num_imagesrÝ   ÚindexÚbrÚbox_regression_per_imageÚclÚlogits_per_imageÚbcÚbox_ctrness_per_imagerñ   Úimage_shapeÚimage_boxesÚimage_scoresÚimage_labelsÚbox_regression_per_levelÚlogits_per_levelÚbox_ctrness_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ÚFCOS.postprocess_detectionsé  s¦  € ð $ LÑ1ˆØ%Ð&7Ñ8ˆØ" >Ñ2ˆä˜Ó&ˆ
à.0ˆ
ä˜:×&ˆEÙ<JÓ'KºN°b¨5¬	¹NÐ$Ð'KÙ4@ÓA²L¨b 5¤	±LÐÐAÙ9DÓ$Eº°2¨¤Y¹Ð!Ð$EØ-4©^¸\Ñ=P˜{ÐàˆKØˆLØˆLähkØ(Ð<QÐSd÷iÑdÐ(Ð*:Ð<QÐSdð /×4Ñ4°RÑ8�ô $)§:¢:Ü—M’MÐ"2Ó3´e·m²mÐDYÓ6ZÑZó$ç‘'“)ð !ð -¨t×/@Ñ/@Ñ@�	Ø#3°IÑ#>Ð Ü!ŸKšK¨	Ó2°1Ñ5�	ô %×.Ò.¨y¸$×:NÑ:NÐPQÓR�Ø)9×)>Ñ)>¸xÓ)HÑ&Ð  $Ø% d™O�	ä#Ÿiši¨	°;ÈgÑV�Ø#,¨{Ñ#:Ð à"&§.¡.×"7Ñ"7Ø,¨[Ñ9Ð;LÈ[Ñ;Yó#�ô #*×"=Ò"=¸oÈ{Ó"[�à×"Ñ" ?Ô3Ø×#Ñ#Ð$4Ô5Ø×#Ñ#Ð$4×5ñ9iô<  Ÿ)š) 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ñ÷ña 'ðp Ðùòo (LùÚAùÚ$Es   °I4ÁI9ÁI>Úimagesc           	      ó¸  • 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	UR                   S
3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U Vs/ s H%  oÿR)                  S5      UR)                  S5      -  PM'     nn0 n/ nU R                   (       a/  Uc  [        R                  " SS5        O¬U R+                  X-UU5      nO˜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 )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   rH   rF   z:Expected target boxes to be a tensor of shape [N, 4], got Ú.éþÿÿÿzJexpecting the last two dimensions of the Tensor to be H and W instead got r   r   rL   zLAll bounding boxes should have positive height and width. Found invalid box z for target at index Ú0r
   z=FCOS always returns a (Losses, Detections) tuple in scriptingT) rà   rT   Ú_assertr˜   r	   rP   r¤   rS   rÖ   Ú	enumerateÚanyr	  Útolistr¿   Útensorsr   r~   rì   rÅ   rÄ   rZ   rq   Úsplitr,  Úimage_sizesÚpostprocessÚjitÚis_scriptingr×   ÚwarningsÚwarnrá   )r8   r.  r=   Útargetr   Úoriginal_image_sizesÚimgÚvalÚ
target_idxÚdegenerate_boxesÚbb_idxÚdegen_bbrª   r>   r?   rs   rã   rÜ   rÝ   Úsplit_head_outputsÚkÚaÚsplit_anchorss                          r:   ru   ÚFCOS.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ØTÐUZ×U`ÑU`ÐTaÐabÐcöñ &ð 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ØØfÐgoÐfpð  qFð  GQð  FRð  RSð  Töñ '9ð —=‘= §¡Ó0ˆÜ�h¤§¡×-Ñ-Ü" S¨( OÐ#4Ó5ˆHä˜Ÿ™Ó)Ó*ˆð —y‘y Ó*ˆð ×'Ñ'¨Ó9ˆá@HÓ IÂ¸1§¡¨£¨Q¯V©V°A«YÔ!6ÁÐÐ IàˆØ.0ˆ
Ø�=�=Ø‰Ü—’˜eÐ%WÕXð ×*Ñ*¨7À'ÐK`Óa‘ð ;=ÐÛ!�Ü(,¨\¸!©_×-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×!Ñ!×#Ñ#Ø×#×#Ü—’Ð]Ô^Ø#'�Ô Ø˜:Ð%Ð%Ø×!Ñ! &¨*Ó5Ð5ùò5 !Jùò Ts   É!,OÌ$O)r×   rÄ   r¿   r'   rÆ   rÉ   rÅ   rÈ   rÇ   rÊ   rÖ   )i   i5  NNNNg      ø?gš™™™™™É?g333333ã?éd   iè  rß   )rw   rx   ry   rz   r{   r2   r3   r|   r   r°   r}   r   r~   r¯   r   r1   rT   r;  Úunusedr   r€   r	   r½   rá   rq   r,  ru   r�   r‚   rƒ   s   @r:   r!   r!     s›  ø† ñXðv 	�Y×-Ñ-ð€Oð ØØ,0Ø+/à6:Ø$(Ø(+Ø!ØØ"%Ø#ñ!@!à—)‘)ð@!ð ð@!ð
 ð@!ð ð@!ð ˜T %™[Ñ)ð@!ð ˜D ™KÑ(ð@!ð # ?Ñ3ð@!ð �r—y‘yÑ!ð@!ð !&ð@!ð ð@!ð ð@!ð  ð@!ð  ÷!@!ð @!ðD ‡Y�Y×ÑðØ˜3 ˜;Ñ'ðØ59¸$¸sÀF¸{Ñ:KÑ5Lðà	ˆt�C˜�KÑ  $ t¨C°¨KÑ'8Ñ"9Ð9Ñ	:óó ðð/Tà�d˜3 ˜;Ñ'Ñ(ð/Tð ˜3 ˜;Ñ'ð/Tð �f‘ð	/Tð
  $ C™yð/Tð 
ˆc�6ˆkÑ	ô/TðbCØ   d¨6¡lÐ!2Ñ3ðCØ>BÀ4ÈÁ<Ñ>PðCØ`dÐejÐknÐpsÐksÑetÑ`uðCà	ˆd�3˜�;ÑÑ	 ôCðP 6:ñ^6à�V‘ð^6ð ˜$˜t C¨ KÑ0Ñ1Ñ2ð^6ð 
ˆt�C˜�KÑ  $ t¨C°¨KÑ'8Ñ"9Ð9Ñ	:÷	^6ó ^6r<   r!   c                   óB   • \ rS rSr\" S\S\SSSSS00S	S
SS.S9r\rSr	g)r"   i�  zGhttps://download.pytorch.org/models/fcos_resnet50_fpn_coco-99b0c9b7.pthi eì)r   r   zShttps://github.com/pytorch/vision/tree/main/references/detection#fcos-resnet-50-fpnzCOCO-val2017Úbox_mapgš™™™™™C@g´Èv¾Ÿ`@gôýÔxéæ^@zSThese weights were produced by following a similar training recipe as on the paper.)Ú
num_paramsÚ
categoriesrÀ   ÚrecipeÚ_metricsÚ_opsÚ
_file_sizeÚ_docs)ÚurlÚ
transformsÚmeta© N)
rw   rx   ry   rz   r   r   r   ÚCOCO_V1ÚDEFAULTr�   rZ  r<   r:   r"   r"   �  sG   † ÙØUØ"à"Ø*ØØkàØ˜tð!ðð
 Ø!Ønñ
ñ€Gð$ ƒGr<   r"   Ú
pretrainedÚpretrained_backbone)ÚweightsÚweights_backboneT)r_  Úprogressr*   r`  Útrainable_backbone_layersr_  ra  r*   r`  rb  rØ   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	$ )a5  
Constructs a FCOS model with a ResNet-50-FPN backbone.

.. betastatus:: detection module

Reference: `FCOS: Fully Convolutional One-Stage Object Detection <https://arxiv.org/abs/1904.01355>`_.
           `FCOS: A simple and strong anchor-free object detector <https://arxiv.org/abs/2006.09214>`_.

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.fcos_resnet50_fpn(weights=FCOS_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.FCOS_ResNet50_FPN_Weights`, optional): The
        pretrained weights to use. See
        :class:`~torchvision.models.detection.FCOS_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
    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) resnet 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. Default: None
    **kwargs: parameters passed to the ``torchvision.models.detection.FCOS``
        base class. Please refer to the `source code
        <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/fcos.py>`_
        for more details about this class.

.. autoclass:: torchvision.models.detection.FCOS_ResNet50_FPN_Weights
    :members:
Nr*   rQ  é[   é   r
   )r_  ra  r‡   )r   r
   rF   é   )Úreturned_layersÚextra_blocksT)ra  Ú
check_hash)r"   Úverifyr   r   rP   rY  r   Úmisc_nn_opsÚFrozenBatchNorm2dr   ÚBatchNorm2dr   r   r   r!   Úload_state_dictÚget_state_dict)
r_  ra  r*   r`  rb  rØ   Ú
is_trainedr‡   r¿   Úmodels
             r:   r#   r#   ¥  sø   € ôX (×.Ñ.¨wÓ7€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ô �Ñ1¨&Ñ1€EàÑØ×Ñ˜g×4Ñ4¸hÐSWÐ4ÐXÔYà€Lr<   );rž   r=  Úcollectionsr   Ú	functoolsr   Útypingr   r   r   rT   r   r	   Úopsr   r  r   r   rk  r   Úops.feature_pyramid_networkr   Útransforms._presetsr   Úutilsr   Ú_apir   r   r   Ú_metar   r   r   r   Úresnetr   r   Ú r2   Úanchor_utilsr   Úbackbone_utilsr   r   rÖ   r    Ú__all__r°   r%   r4   r6   r!   r"   r[  ÚIMAGENET1K_V1Úboolr}   r#   rZ  r<   r:   Ú<module>r‚     s`  ðÛ Û Ý #Ý ß *Ñ *ã ß ç fÓ fÝ 8Ý 2Ý (ß 7Ñ 7Ý $ß Cß /Ý !Ý )ß MÝ /ò€ôg
ˆr�y‰yô g
ôT=0˜RŸY™Yô =0ô@BY˜Ÿ™ô BYôJ~6ˆ2�9‰9ô ~6ôB ô ñ, ÓÙØÐ4×<Ñ<Ð=Ø+Ð-=×-KÑ-KÐLñð 48ØØ!%Ø3C×3QÑ3QØ/3ò]àÐ/Ñ0ð]ð ð]ð ˜#‘ð	]ð
 Ð/Ñ0ð]ð  (¨™}ð]ð ð]ð 
ô]ó	ó ñ
]r<   