Ë
    þÍ:j q  ã                   óÌ  — d dl Z d dlmZ d dlmZmZ d dlZd dlmc m	Z
 d dlmZmZ ddlmZ ddlmZ ddlmZ d	d
lmZmZmZ d	dlmZ d	dlmZmZ d	dlmZmZmZ ddl mZ! ddl"m#Z# ddl$m%Z% ddl&m'Z' ddgZ( G d„ de«      Z)dejT                  fd„Z+ G d„ dejT                  «      Z, G d„ dejT                  «      Z- G d„ de-«      Z. G d„ de-«      Z/ G d „ d!ejT                  «      Z0 G d"„ d#ejT                  «      Z1d$ed%e2d&e3fd'„Z4 e«        ed(e)jj                  fd)ejl                  f¬*«      dd+dejl                  dd,œd-ee)   d.e2d/ee3   d0ee   d1ee3   d2ed3e0fd4„«       «       Z7y)5é    N)ÚOrderedDict)ÚAnyÚOptional)ÚnnÚTensoré   )Úboxes)ÚObjectDetection)Ú_log_api_usage_onceé   )Úregister_modelÚWeightsÚWeightsEnum)Ú_COCO_CATEGORIES)Ú_ovewrite_value_paramÚhandle_legacy_interface)ÚVGGÚvgg16ÚVGG16_Weightsé   )Ú_utils)ÚDefaultBoxGenerator)Ú_validate_trainable_layers)ÚGeneralizedRCNNTransformÚSSD300_VGG16_WeightsÚssd300_vgg16c                   ó@   — e Zd Z edededddddiidd	d
dœ¬«      ZeZy)r   zBhttps://download.pytorch.org/models/ssd300_vgg16_coco-b556d3b4.pthiâÙ)r   r   zMhttps://github.com/pytorch/vision/tree/main/references/detection#ssd300-vgg16zCOCO-val2017Úbox_mapgš™™™™9@gçû©ñÒmA@gV-²�ÿ`@zSThese weights were produced by following a similar training recipe as on the paper.)Ú
num_paramsÚ
categoriesÚmin_sizeÚrecipeÚ_metricsÚ_opsÚ
_file_sizeÚ_docs)ÚurlÚ
transformsÚmetaN)Ú__name__Ú
__module__Ú__qualname__r   r
   r   ÚCOCO_V1ÚDEFAULT© ó    úu/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchvision/models/detection/ssd.pyr   r      sG   „ ÙØPØ"à"Ø*ØØeàØ˜tð!ðð
 Ø!Ønñ
ô€Gð$ �Gr0   Úconvc                 óL  — | j                  «       D ]‘  }t        |t        j                  «      sŒt        j                  j
                  j                  |j                  «       |j                  €Œ^t        j                  j
                  j                  |j                  d«       Œ“ y )Ng        )
ÚmodulesÚ
isinstancer   ÚConv2dÚtorchÚinitÚxavier_uniform_ÚweightÚbiasÚ	constant_)r2   Úlayers     r1   Ú_xavier_initr>   2   se   € Ø—‘“ò 9ˆÜ�eœRŸY™YÕ'Ü�H‰H�M‰M×)Ñ)¨%¯,©,Ô7Ø�z‰zÑ%Ü—‘—‘×'Ñ'¨¯
©
°CÕ8ñ	9r0   c                   óX   ‡ — e Zd Zdee   dee   defˆ fd„Zdee   deeef   fd„Z	ˆ xZ
S )ÚSSDHeadÚin_channelsÚnum_anchorsÚnum_classesc                 óh   •— t         ‰| �  «        t        |||«      | _        t	        ||«      | _        y ©N)ÚsuperÚ__init__ÚSSDClassificationHeadÚclassification_headÚSSDRegressionHeadÚregression_head)ÚselfrA   rB   rC   Ú	__class__s       €r1   rG   zSSDHead.__init__;   s/   ø€ Ü‰ÑÔÜ#8¸ÀkÐS^Ó#_ˆÔ Ü0°¸kÓJˆÕr0   ÚxÚreturnc                 óH   — | j                  |«      | j                  |«      dœS )N)Úbbox_regressionÚ
cls_logits)rK   rI   )rL   rN   s     r1   ÚforwardzSSDHead.forward@   s(   € à#×3Ñ3°AÓ6Ø×2Ñ2°1Ó5ñ
ð 	
r0   )r*   r+   r,   ÚlistÚintrG   r   ÚdictÚstrrS   Ú__classcell__©rM   s   @r1   r@   r@   :   sJ   ø„ ðK D¨¡Ið K¸DÀ¹Ið KÐTWõ Kð

˜˜f™ð 
¨$¨s°F¨{Ñ*;÷ 
r0   r@   c                   óf   ‡ — e Zd Zdej                  defˆ fd„Zdededefd„Zde	e   defd„Z
ˆ xZS )	ÚSSDScoringHeadÚmodule_listÚnum_columnsc                 ó>   •— t         ‰| �  «        || _        || _        y rE   )rF   rG   r\   r]   )rL   r\   r]   rM   s      €r1   rG   zSSDScoringHead.__init__H   s   ø€ Ü‰ÑÔØ&ˆÔØ&ˆÕr0   rN   ÚidxrO   c                 óž   — t        | j                  «      }|dk  r||z  }|}t        | j                  «      D ]  \  }}||k(  sŒ ||«      }Œ |S )zr
        This is equivalent to self.module_list[idx](x),
        but torchscript doesn't support this yet
        r   )Úlenr\   Ú	enumerate)rL   rN   r_   Ú
num_blocksÚoutÚiÚmodules          r1   Ú_get_result_from_module_listz+SSDScoringHead._get_result_from_module_listM   s_   € ô
 ˜×)Ñ)Ó*ˆ
Ø�Š7Ø�:ÑˆCØˆÜ" 4×#3Ñ#3Ó4ò 	 ‰IˆAˆvØ�C‹xÙ˜Q“i‘ð	 ð ˆ
r0   c                 ód  — g }t        |«      D ]Š  \  }}| j                  ||«      }|j                  \  }}}}	|j                  |d| j                  ||	«      }|j                  ddddd«      }|j                  |d| j                  «      }|j                  |«       ŒŒ t        j                  |d¬«      S )Néÿÿÿÿr   r   é   r   r   ©Údim)
rb   rg   ÚshapeÚviewr]   ÚpermuteÚreshapeÚappendr7   Úcat)
rL   rN   Úall_resultsre   ÚfeaturesÚresultsÚNÚ_ÚHÚWs
             r1   rS   zSSDScoringHead.forward[   s®   € Øˆä$ Q›<ò 		(‰KˆAˆxØ×7Ñ7¸À!ÓDˆGð !Ÿ™‰JˆAˆq�!�QØ—l‘l 1 b¨$×*:Ñ*:¸A¸qÓAˆGØ—o‘o a¨¨A¨q°!Ó4ˆGØ—o‘o a¨¨T×-=Ñ-=Ó>ˆGà×Ñ˜wÕ'ð		(ô �y‰y˜¨!Ô,Ð,r0   )r*   r+   r,   r   Ú
ModuleListrU   rG   r   rg   rT   rS   rX   rY   s   @r1   r[   r[   G   sK   ø„ ð' B§M¡Mð 'Àõ 'ð
¨fð ¸3ð À6ó ð-˜˜f™ð -¨&÷ -r0   r[   c                   ó8   ‡ — e Zd Zdee   dee   defˆ fd„Zˆ xZS )rH   rA   rB   rC   c           	      óâ   •— t        j                  «       }t        ||«      D ]0  \  }}|j                  t        j                  |||z  dd¬«      «       Œ2 t        |«       t        ‰| �  ||«       y )Nr   r   ©Úkernel_sizeÚpadding©r   rz   Úziprq   r6   r>   rF   rG   )rL   rA   rB   rC   rR   ÚchannelsÚanchorsrM   s          €r1   rG   zSSDClassificationHead.__init__m   sf   ø€ Ü—]‘]“_ˆ
Ü!$ [°+Ó!>ò 	dÑˆH�gØ×ÑœbŸi™i¨°+ÀÑ2GÐUVÐ`aÔbÕcð	dä�ZÔ Ü‰Ñ˜ [Õ1r0   ©r*   r+   r,   rT   rU   rG   rX   rY   s   @r1   rH   rH   l   s+   ø„ ð2 D¨¡Ið 2¸DÀ¹Ið 2ÐTW÷ 2ñ 2r0   rH   c                   ó4   ‡ — e Zd Zdee   dee   fˆ fd„Zˆ xZS )rJ   rA   rB   c           	      óâ   •— t        j                  «       }t        ||«      D ]0  \  }}|j                  t        j                  |d|z  dd¬«      «       Œ2 t        |«       t        ‰| �  |d«       y )Nrj   r   r   r}   r€   )rL   rA   rB   Úbbox_regr‚   rƒ   rM   s         €r1   rG   zSSDRegressionHead.__init__v   sb   ø€ Ü—=‘=“?ˆÜ!$ [°+Ó!>ò 	XÑˆH�gØ�O‰OœBŸI™I h°°G±ÈÐTUÔVÕWð	Xä�XÔÜ‰Ñ˜ 1Õ%r0   r„   rY   s   @r1   rJ   rJ   u   s#   ø„ ð& D¨¡Ið &¸DÀ¹I÷ &ñ &r0   rJ   c                   ób  ‡ — e Zd ZdZej
                  ej                  dœZ	 	 	 	 	 	 	 	 	 d dej                  de
deeef   dedeee      deee      d	eej                     d
ededededededefˆ fd„Zej&                  j(                  deeef   deeeef      deeeef   eeeef      f   fd„«       Zdeeeef      deeef   dee   dee   deeef   f
d„Z	 d!dee   deeeeef         deeeef   eeeef      f   fd„Zdeeef   dee   deeeef      deeeef      fd„Zˆ xZS )"ÚSSDaM  
    Implements SSD architecture from `"SSD: Single Shot MultiBox Detector" <https://arxiv.org/abs/1512.02325>`_.

    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, but they will be resized
    to a fixed size before passing it to the backbone.

    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 for each detection

    Args:
        backbone (nn.Module): the network used to compute the features for the model.
            It should contain an out_channels attribute with the list of the output channels of
            each feature map. The backbone should return a single Tensor or an OrderedDict[Tensor].
        anchor_generator (DefaultBoxGenerator): module that generates the default boxes for a
            set of feature maps.
        size (Tuple[int, int]): the width and height to which images will be rescaled before feeding them
            to the backbone.
        num_classes (int): number of output classes of the model (including the background).
        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
        head (nn.Module, optional): Module run on top of the backbone features. 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.
        iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
            considered as positive during training.
        topk_candidates (int): Number of best detections to keep before NMS.
        positive_fraction (float): a number between 0 and 1 which indicates the proportion of positive
            proposals used during the training of the classification head. It is used to estimate the negative to
            positive ratio.
    )Ú	box_coderÚproposal_matcherÚbackboneÚanchor_generatorÚsizerC   Ú
image_meanÚ	image_stdÚheadÚscore_threshÚ
nms_threshÚdetections_per_imgÚ
iou_threshÚtopk_candidatesÚpositive_fractionÚkwargsc                 óä  •— t         ‰| �  «        t        | «       || _        || _        t        j                  d¬«      | _        |€¥t        |d«      r|j                  }nt        j                  ||«      }t        |«      t        |j                  «      k7  r.t        dt        |«      › dt        |j                  «      › d�«      ‚| j                  j                  «       }t        |||«      }|| _        t        j"                  |«      | _        |€g d¢}|€g d¢}t'        t)        |«      t+        |«      ||fd	|d
œ|¤Ž| _        || _        |	| _        |
| _        || _        d|z
  |z  | _        d| _        y )N)ç      $@rš   ç      @r›   )ÚweightsÚout_channelsz5The length of the output channels from the backbone (zA) do not match the length of the anchor generator aspect ratios (ú))g
×£p=
ß?gÉv¾Ÿ/Ý?g–C‹lçûÙ?)gZd;ßOÍ?gyé&1¬Ì?gÍÌÌÌÌÌÌ?r   )Úsize_divisibleÚ
fixed_sizeg      ð?F)rF   rG   r   rŒ   r�   Ú	det_utilsÚBoxCoderrŠ   Úhasattrr�   Úretrieve_out_channelsra   Úaspect_ratiosÚ
ValueErrorÚnum_anchors_per_locationr@   r‘   Ú
SSDMatcherr‹   r   ÚminÚmaxÚ	transformr’   r“   r”   r–   Úneg_to_pos_ratioÚ_has_warned)rL   rŒ   r�   rŽ   rC   r�   r�   r‘   r’   r“   r”   r•   r–   r—   r˜   r�   rB   rM   s                    €r1   rG   zSSD.__init__º   s›  ø€ ô" 	‰ÑÔÜ˜DÔ!à ˆŒà 0ˆÔä"×+Ñ+Ð4JÔKˆŒàˆ<Ü�x Ô0Ø'×4Ñ4‘ä(×>Ñ>¸xÈÓN�ä�<Ó ¤CÐ(8×(FÑ(FÓ$GÒGÜ ØKÌCÐP\ÓL]ÐK^ð  _`ô  adð  eu÷  eCñ  eCó  aDð  `Eð  EFð  Góð ð ×/Ñ/×HÑHÓJˆKÜ˜<¨°kÓBˆDØˆŒ	ä )× 4Ñ 4°ZÓ @ˆÔàÐÚ.ˆJØÐÚ-ˆIÜ1Ü�‹I”s˜4“y *¨ið
ØHIÐVZñ
Ø^dñ
ˆŒð )ˆÔØ$ˆŒØ"4ˆÔØ.ˆÔØ!$Ð'8Ñ!8Ð<MÑ MˆÔð !ˆÕr0   ÚlossesÚ
detectionsrO   c                 ó"   — | j                   r|S |S rE   )Útraining)rL   r®   r¯   s      r1   Úeager_outputszSSD.eager_outputsö   s   € ð �=Š=ØˆMàÐr0   ÚtargetsÚhead_outputsrƒ   Úmatched_idxsc                 óò  — |d   }|d   }d}g }g }	t        |||||«      D �]  \  }
}}}}t        j                  |dk\  «      d   }||   }||j                  «       z  }|
d   |   }||d d …f   }||d d …f   }| j                  j                  ||«      }|j                  t        j                  j                  j                  ||d¬«      «       t        j                  |j                  d«      f|
d   j                  |
d   j                  ¬«      }|
d   |   ||<   |	j                  |«       �Œ t        j                  |«      }t        j                  |	«      }	|j                  d	«      }t        j                   |j#                  d	|«      |	j#                  d	«      d
¬«      j#                  |	j                  «       «      }|	dkD  }| j$                  |j'                  dd¬«      z  }|j)                  «       }t+        d«       ||<   |j-                  dd¬«      \  }}|j-                  d«      d   |k  }t/        d|«      }|j'                  «       |z  ||   j'                  «       ||   j'                  «       z   |z  dœS )NrQ   rR   r   r	   Úsum)Ú	reductionÚlabels©ÚdtypeÚdeviceri   Únoner   T)ÚkeepdimÚinf)Ú
descending)rQ   Úclassification)r�   r7   ÚwhereÚnumelrŠ   Úencode_singlerq   r   Ú
functionalÚsmooth_l1_lossÚzerosrŽ   r»   r¼   ÚstackÚFÚcross_entropyrn   r¬   r·   ÚcloneÚfloatÚsortrª   )rL   r³   r´   rƒ   rµ   rQ   rR   Únum_foregroundÚ	bbox_lossÚcls_targetsÚtargets_per_imageÚbbox_regression_per_imageÚcls_logits_per_imageÚanchors_per_imageÚmatched_idxs_per_imageÚforeground_idxs_per_imageÚ!foreground_matched_idxs_per_imageÚmatched_gt_boxes_per_imageÚtarget_regressionÚgt_classes_targetrC   Úcls_lossÚforeground_idxsÚnum_negativeÚnegative_lossÚvaluesr_   Úbackground_idxsrv   s                                r1   Úcompute_losszSSD.compute_lossÿ   sµ  € ð 'Ð'8Ñ9ˆØ! ,Ñ/ˆ
ð ˆØˆ	Øˆô �˜/¨:°wÀÓMó	2ñ 
ØØ%Ø ØØ"ô ).¯©Ð4JÈaÑ4OÓ(PÐQRÑ(SÐ%Ø0FÐG`Ñ0aÐ-ØÐ?×EÑEÓGÑGˆNð *;¸7Ñ)CÐDeÑ)fÐ&Ø(AÐB[Ò]^ÐB^Ñ(_Ð%Ø 1Ð2KÊQÐ2NÑ OÐØ $§¡× <Ñ <Ð=WÐYjÓ kÐØ×ÑÜ—‘×#Ñ#×2Ñ2Ð3LÐN_ÐkpÐ2Óqôô
 !&§¡Ø%×*Ñ*¨1Ó-Ð/Ø'¨Ñ1×7Ñ7Ø(¨Ñ2×9Ñ9ô!Ðð
 <MÈXÑ;VØ1ñ<ÐÐ7Ñ8ð ×ÑÐ0Ö1ð=	2ô@ —K‘K 	Ó*ˆ	Ü—k‘k +Ó.ˆð !—o‘o bÓ)ˆÜ—?‘? :§?¡?°2°{Ó#CÀ[×EUÑEUÐVXÓEYÐekÔl×qÑqØ×ÑÓó
ˆð
 &¨™/ˆØ×,Ñ,¨×/BÑ/BÀ1ÈdÐ/BÓ/SÑSˆà Ÿ™Ó(ˆÜ*/°«,¨ˆ�oÑ&Ø#×(Ñ(¨°tÐ(Ó<‰ˆ�àŸ(™( 1›+ a™.¨<Ñ7ˆä��>Ó"ˆà(Ÿ}™}›°Ñ2Ø'¨Ñ8×<Ñ<Ó>ÀÈ/ÑAZ×A^ÑA^ÓA`Ñ`ÐdeÑeñ
ð 	
r0   Úimagesc           	      óÎ  — | j                   r±|€t        j                  dd«       n˜|D ]“  }|d   }t        |t        j                  «      rOt        j                  t        |j                  «      dk(  xr |j                  d   dk(  d|j                  › d�«       Œqt        j                  dd	t        |«      › d�«       Œ• g }|D ]\  }|j                  d
d  }t        j                  t        |«      dk(  d|j                  d
d  › �«       |j                  |d   |d   f«       Œ^ | j                  ||«      \  }}|�›t        |«      D ]�  \  }}|d   }|d d …dd …f   |d d …d d…f   k  }	|	j                  «       sŒ3t        j                  |	j                  d¬«      «      d   d   }
||
   j                  «       }t        j                  dd|› d|› d�«       Œ� | j                  |j                  «      }t        |t        j                  «      rt!        d|fg«      }t#        |j%                  «       «      }| j'                  |«      }| j)                  ||«      }i }g }| j                   råg }|€t        j                  dd«       �nt+        ||«      D ]¥  \  }}|d   j-                  «       dk(  rQ|j                  t        j.                  |j1                  d«      fdt        j2                  |j4                  ¬«      «       Œmt7        j8                  |d   |«      }|j                  | j;                  |«      «       Œ§ | j=                  ||||«      }nD| j?                  |||j@                  «      }| j                  jC                  ||j@                  |«      }t        jD                  jG                  «       r,| jH                  stK        jL                  d«       d| _$        ||fS | jO                  ||«      S )NFz0targets should not be none when in training moder	   r   ri   rj   z:Expected target boxes to be a tensor of shape [N, 4], got ú.z0Expected target boxes to be of type Tensor, got éþÿÿÿzJexpecting the last two dimensions of the Tensor to be H and W instead got r   r   rk   zLAll bounding boxes should have positive height and width. Found invalid box z for target at index Ú0rº   z<SSD always returns a (Losses, Detections) tuple in scriptingT)(r±   r7   Ú_assertr5   r   ra   rm   Útyperq   r«   rb   ÚanyrÂ   ÚtolistrŒ   Útensorsr   rT   rß   r‘   r�   r�   rÃ   ÚfullrŽ   Úint64r¼   Úbox_opsÚbox_iour‹   rá   Úpostprocess_detectionsÚimage_sizesÚpostprocessÚjitÚis_scriptingr­   ÚwarningsÚwarnr²   )rL   râ   r³   Útargetr	   Úoriginal_image_sizesÚimgÚvalÚ
target_idxÚdegenerate_boxesÚbb_idxÚdegen_bbrt   r´   rƒ   r®   r¯   rµ   rÔ   rÑ   Úmatch_quality_matrixs                        r1   rS   zSSD.forwardF  sÉ  € ð �=Š=ØˆÜ—‘˜eÐ%WÕXà%ò p�FØ" 7™O�EÜ! %¬¯©Ô6ÜŸ™Ü §¡Ó,°Ñ1ÒJ°e·k±kÀ"±oÈÑ6JØXÐY^×YdÑYdÐXeÐefÐgõô
 Ÿ™ eÐ/_Ô`dÐejÓ`kÐ_lÐlmÐ-nÕoðpð 79ÐØò 	:ˆCØ—)‘)˜B˜C�.ˆCÜ�M‰MÜ�C“˜A‘Ø\Ð]`×]fÑ]fÐgiÐgjÐ]kÐ\lÐmôð !×'Ñ'¨¨Q©°°Q±Ð(8Õ9ð	:ð Ÿ.™.¨°Ó9‰ˆ�ð ÐÜ&/°Ó&8ò 
Ñ"�
˜FØ˜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ð[õð
ð —=‘= §¡Ó0ˆÜ�h¤§¡Ô-Ü" S¨( OÐ#4Ó5ˆHä˜Ÿ™Ó)Ó*ˆð —y‘y Ó*ˆð ×'Ñ'¨°Ó9ˆàˆØ.0ˆ
Ø�=Š=ØˆLØˆÜ—‘˜eÐ%WÖXä<?ÀÈÓ<Qò 
UÑ8Ð%Ð'8Ø(¨Ñ1×7Ñ7Ó9¸QÒ>Ø$×+Ñ+Ü!ŸJ™JØ!2×!7Ñ!7¸Ó!:Ð <¸bÌÏÉÐ\m×\tÑ\tôôð
 !ä+2¯?©?Ð;LÈWÑ;UÐWhÓ+iÐ(Ø ×'Ñ'¨×(=Ñ(=Ð>RÓ(SÕTð
Uð ×*Ñ*¨7°LÀ'È<ÓX‘à×4Ñ4°\À7ÈF×L^ÑL^Ó_ˆJØŸ™×3Ñ3°JÀ×@RÑ@RÐThÓiˆJä�9‰9×!Ñ!Ô#Ø×#Ò#Ü—‘Ð\Ô]Ø#'�Ô Ø˜:Ð%Ð%Ø×!Ñ! &¨*Ó5Ð5r0   Úimage_anchorsÚimage_shapesc           
      óØ  — |d   }t        j                  |d   d¬«      }|j                  d«      }|j                  }g }t	        ||||«      D �]™  \  }	}
}}| j
                  j                  |	|«      }	t        j                  |	|«      }	g }g }g }t        d|«      D ]¶  }|
d d …|f   }|| j                  kD  }||   }|	|   }t        j                  || j                  d«      }|j                  |«      \  }}||   }|j                  |«       |j                  |«       |j                  t!        j"                  ||t         j$                  |¬«      «       Œ¸ t!        j&                  |d¬«      }t!        j&                  |d¬«      }t!        j&                  |d¬«      }t        j(                  |||| j*                  «      }|d | j,                   }|j                  ||   ||   ||   dœ«       �Œœ |S )	NrQ   rR   ri   rk   r   r   )Ú
fill_valuer»   r¼   )r	   Úscoresr¹   )rÉ   ÚsoftmaxrŽ   r¼   r�   rŠ   Údecode_singlerî   Úclip_boxes_to_imageÚranger’   r¡   Ú	_topk_minr–   Útopkrq   r7   Ú	full_likerí   rr   Úbatched_nmsr“   r”   )rL   r´   r   r  rQ   Úpred_scoresrC   r¼   r¯   r	   r  rƒ   Úimage_shapeÚimage_boxesÚimage_scoresÚimage_labelsÚlabelÚscoreÚ	keep_idxsÚboxÚnum_topkÚidxsÚkeeps                          r1   rð   zSSD.postprocess_detectionsž  sú  € ð 'Ð'8Ñ9ˆÜ—i‘i ¨\Ñ :ÀÔCˆà!×&Ñ& rÓ*ˆØ×#Ñ#ˆà.0ˆ
ä36°ÈÐUbÐdpÓ3qó %	Ñ/ˆE�6˜7 KØ—N‘N×0Ñ0°¸Ó@ˆEÜ×/Ñ/°°{ÓCˆEàˆKØˆLØˆLÜ˜q +Ó.ò p�Øšq %˜xÑ(�à! D×$5Ñ$5Ñ5�	Ø˜iÑ(�Ø˜IÑ&�ô %×.Ñ.¨u°d×6JÑ6JÈAÓN�Ø#Ÿj™j¨Ó2‘��tØ˜$‘i�à×"Ñ" 3Ô'Ø×#Ñ# EÔ*Ø×#Ñ#¤E§O¡O°EÀeÔSX×S^ÑS^ÐgmÔ$nÕoðpô   Ÿ)™) 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ñöð?%	ðL Ðr0   )	NNNg{®Gáz„?gÍÌÌÌÌÌÜ?éÈ   g      à?i�  g      Ð?rE   )r*   r+   r,   Ú__doc__r¡   r¢   ÚMatcherÚ__annotations__r   ÚModuler   ÚtuplerU   r   rT   rÌ   r   rG   r7   ró   ÚunusedrV   rW   r   r²   rá   rS   rð   rX   rY   s   @r1   r‰   r‰   ~   s}  ø„ ñ4ðn ×'Ñ'Ø%×-Ñ-ñ€Oð -1Ø+/Ø$(Ø"Ø Ø"%ØØ"Ø#'ñ:!à—)‘)ð:!ð .ð:!ð �C˜�H‰oð	:!ð
 ð:!ð ˜T %™[Ñ)ð:!ð ˜D ™KÑ(ð:!ð �r—y‘yÑ!ð:!ð ð:!ð ð:!ð  ð:!ð ð:!ð ð:!ð !ð:!ð õ:!ðx ‡Y�Y×ÑðØ˜3 ˜;Ñ'ðØ59¸$¸sÀF¸{Ñ:KÑ5Lðà	ˆt�C˜�KÑ  $ t¨C°¨KÑ'8Ñ"9Ð9Ñ	:òó ððE
à�d˜3 ˜;Ñ'Ñ(ðE
ð ˜3 ˜;Ñ'ðE
ð �f‘ð	E
ð
 ˜6‘lðE
ð 
ˆc�6ˆkÑ	óE
ðP RVñV6Ø˜6‘lðV6Ø-5°d¸4ÀÀVÀÑ;LÑ6MÑ-NðV6à	ˆt�C˜�KÑ  $ t¨C°¨KÑ'8Ñ"9Ð9Ñ	:óV6ðp1Ø   f Ñ-ð1Ø>BÀ6¹lð1ØZ^Ð_dÐehÐjmÐemÑ_nÑZoð1à	ˆd�3˜�;ÑÑ	 ÷1r0   r‰   c                   óV   ‡ — e Zd Zdej                  defˆ fd„Zdedee	ef   fd„Z
ˆ xZS )ÚSSDFeatureExtractorVGGrŒ   Úhighresc                 ó  •— t         ‰| �  «        d„ t        |«      D «       \  }}}}}d||   _        t	        j
                  t        j                  d«      dz  «      | _        t	        j                  |d | Ž | _
        t	        j                  t	        j                  t	        j                  ddd¬«      t	        j                  d¬	«      t	        j                  ddd
dd¬«      t	        j                  d¬	«      «      t	        j                  t	        j                  ddd¬«      t	        j                  d¬	«      t	        j                  ddd
dd¬«      t	        j                  d¬	«      «      t	        j                  t	        j                  ddd¬«      t	        j                  d¬	«      t	        j                  ddd
¬«      t	        j                  d¬	«      «      t	        j                  t	        j                  ddd¬«      t	        j                  d¬	«      t	        j                  ddd
¬«      t	        j                  d¬	«      «      g«      }|r{|j                  t	        j                  t	        j                  ddd¬«      t	        j                  d¬	«      t	        j                  ddd¬«      t	        j                  d¬	«      «      «       t        |«       t	        j                  t	        j                   d
ddd¬«      t	        j                  ddd
dd¬«      t	        j                  d¬	«      t	        j                  ddd¬«      t	        j                  d¬	«      «      }t        |«       |j#                  dt	        j                  g ||d ¢|‘­Ž «       || _        y )Nc              3   ó\   K  — | ]$  \  }}t        |t        j                  «      sŒ!|–— Œ& y ­wrE   )r5   r   Ú	MaxPool2d)Ú.0re   r=   s      r1   ú	<genexpr>z2SSDFeatureExtractorVGG.__init__.<locals>.<genexpr>Ö  s&   è ø€ Ò.x±X°Q¸ÔXbÐchÔjl×jvÑjvÕXw¬qÑ.xùs   ‚",¥,Ti   é   i   é   r   )r~   )Úinplacer   r   )r~   r   Ústrideé€   rj   F)r~   r+  r   Ú	ceil_modeé   )rA   r�   r~   r   Údilation)rA   r�   r~   r   ri   )rF   rG   rb   r-  r   Ú	Parameterr7   ÚonesÚscale_weightÚ
Sequentialrt   rz   r6   ÚReLUrq   r>   r%  ÚinsertÚextra)	rL   rŒ   r"  rw   Úmaxpool3_posÚmaxpool4_posr6  ÚfcrM   s	           €r1   rG   zSSDFeatureExtractorVGG.__init__Ó  s¥  ø€ Ü‰ÑÔá.xÄÈ8ÓATÔ.xÑ+ˆˆ1ˆl˜L¨!ð ,0ˆ�ÑÔ(ô ŸL™L¬¯©°C«¸2Ñ)=Ó>ˆÔô Ÿ™ x°°Ð'>Ð?ˆŒô —‘ä—‘Ü—I‘I˜d C°QÔ7Ü—G‘G DÔ)Ü—I‘I˜c 3°A¸qÈÔKÜ—G‘G DÔ)ó	ô —‘Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)Ü—I‘I˜c 3°A¸qÈÔKÜ—G‘G DÔ)ó	ô —‘Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)ó	ô —‘Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)ó	ð'ó
ˆñ8 à�L‰LÜ—‘Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)Ü—I‘I˜c 3°AÔ6Ü—G‘G DÔ)ó	ôô 	�UÔä�]‰]Ü�L‰L Q¨q¸!ÀuÔMÜ�I‰I #°DÀaÐQRÐ]^Ô_Ü�G‰G˜DÔ!Ü�I‰I $°TÀqÔIÜ�G‰G˜DÔ!ó
ˆô 	�RÔØ�‰ØÜ�M‰Mð Ø˜, rÐ*ðàòô	
ð ˆ�
r0   rN   rO   c           	      óV  — | j                  |«      }| j                  j                  dddd«      t        j                  |«      z  }|g}| j
                  D ]  } ||«      }|j                  |«       Œ t        t        |«      D ��cg c]  \  }}t        |«      |f‘Œ c}}«      S c c}}w )Nr   ri   )
rt   r2  rn   rÉ   Ú	normalizer6  rq   r   rb   rW   )rL   rN   ÚrescaledÚoutputÚblockre   Úvs          r1   rS   zSSDFeatureExtractorVGG.forward  s–   € à�M‰M˜!ÓˆØ×$Ñ$×)Ñ)¨!¨R°°AÓ6¼¿¹ÀQ»ÑGˆØ�ˆð —Z‘Zò 	ˆEÙ�a“ˆAØ�M‰M˜!Õð	ô ´I¸fÓ4E×F©D¨A¨qœS ›V QšKÓFÓGÐGùÓFs   ÂB%
)r*   r+   r,   r   r  ÚboolrG   r   rV   rW   rS   rX   rY   s   @r1   r!  r!  Ò  s=   ø„ ðF §¡ð F°Tõ FðPH˜ð H D¨¨f¨Ñ$5÷ Hr0   r!  rŒ   r"  Útrainable_layersc           	      ó¸  — | j                   } dgt        | «      D ��cg c]"  \  }}t        |t        j                  «      sŒ!|‘Œ$ c}}d d z   }t        |«      }t        j                  d|cxk  xr |k  nc d|› d|› �«       |dk(  rt        | «      n|||z
     }| d | D ](  }|j                  «       D ]  }|j                  d«       Œ Œ* t        | |«      S c c}}w )Nr   ri   z,trainable_layers should be in the range [0, z]. Instead got F)rt   rb   r5   r   r%  ra   r7   rç   Ú
parametersÚrequires_grad_r!  )	rŒ   r"  rA  re   ÚbÚstage_indicesÚ
num_stagesÚfreeze_beforeÚ	parameters	            r1   Ú_vgg_extractorrJ  )  sè   € Ø× Ñ €Hà�C¬°8Ó)<×\¡  AÄ
È1ÌbÏlÉlÕ@[š1Ó\Ð]`Ð^`ÐaÑa€MÜ�]Ó#€Jô 
‡M�MØ	ÐÖ+ Ô+Ø
6°z°lÀ/ÐRbÐQcÐdôð &6¸Ò%:”C˜”MÀÈjÐ[kÑNkÑ@l€Mà�n�}Ð%ò ,ˆØŸ™›ò 	,ˆIØ×$Ñ$ UÕ+ñ	,ð,ô " (¨GÓ4Ð4ùó ]s   �"CÁ CÚ
pretrainedÚpretrained_backbone)rœ   Úweights_backboneT)rœ   ÚprogressrC   rM  Útrainable_backbone_layersrœ   rN  rC   rM  rO  r˜   rO   c                 ó
  — t         j                  | «      } t        j                  |«      }d|v rt        j                  d«       | �&d}t        d|t        | j                  d   «      «      }n|€d}t        | duxs |du|dd«      }t        ||¬	«      }t        |d
|«      }t        dgddgddgddgdgdggg d¢g d¢¬«      }g d¢g d¢dœ}i |¥|¥}t        ||d|fi |¤Ž}	| �"|	j                  | j                  |d¬«      «       |	S )a4  The SSD300 model is based on the `SSD: Single Shot MultiBox Detector
    <https://arxiv.org/abs/1512.02325>`_ paper.

    .. betastatus:: detection module

    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, but they will be resized
    to a fixed size before passing it to the backbone.

    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 for each detection

    Example:

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

    Args:
        weights (:class:`~torchvision.models.detection.SSD300_VGG16_Weights`, optional): The pretrained
                weights to use. See
                :class:`~torchvision.models.detection.SSD300_VGG16_Weights`
                below for more details, and possible values. By default, no
                pre-trained weights are used.
        progress (bool, optional): 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.VGG16_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 4.
        **kwargs: parameters passed to the ``torchvision.models.detection.SSD``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/detection/ssd.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.detection.SSD300_VGG16_Weights
        :members:
    rŽ   z?The size of the model is already fixed; ignoring the parameter.NrC   r    é[   é   rj   )rœ   rN  Fr   r   )gìQ¸…ë±?g333333Ã?g…ëQ¸Õ?gR¸…ëQà?g®Gázæ?g×£p=
×ë?gÍÌÌÌÌÌð?)é   é   é    é@   éd   é,  )ÚscalesÚsteps)g;pÎˆÒÞÞ?gÌ°�N]Ý?g|
€ñÚ?)çp?r[  r[  )r�   r�   )rX  rX  T)rN  Ú
check_hash)r   Úverifyr   rõ   rö   r   ra   r)   r   r   rJ  r   r‰   Úload_state_dictÚget_state_dict)
rœ   rN  rC   rM  rO  r˜   rŒ   r�   ÚdefaultsÚmodels
             r1   r   r   =  sH  € ôR #×)Ñ)¨'Ó2€GÜ$×+Ñ+Ð,<Ó=Ðà�ÑÜ�‰ÐWÔXàÐØÐÜ+¨M¸;ÌÈGÏLÉLÐYeÑLfÓHgÓh‰Ø	Ð	Øˆä :Ø�tÐÒ;Ð/°tÐ;Ð=VÐXYÐ[\ó!Ðô
 Ð-¸ÔA€HÜ˜h¨Ð/HÓI€HÜ*Ø
ˆˆq�!ˆf�q˜!�f˜q !˜f q c¨A¨3Ð/Ú9Ú'ôÐò 2Ú<ñ€Hð
 )�XÐ( Ð(€FÜ�Ð*¨J¸ÑNÀvÑN€EàÐØ×Ñ˜g×4Ñ4¸hÐSWÐ4ÓXÔYà€Lr0   )8rõ   Úcollectionsr   Útypingr   r   r7   Útorch.nn.functionalr   rÅ   rÉ   r   Úopsr	   rî   Útransforms._presetsr
   Úutilsr   Ú_apir   r   r   Ú_metar   r   r   r   Úvggr   r   r   Ú r¡   Úanchor_utilsr   Úbackbone_utilsr   r«   r   Ú__all__r   r  r>   r@   r[   rH   rJ   r‰   r!  r@  rU   rJ  r-   ÚIMAGENET1K_FEATURESr   r/   r0   r1   ú<module>rp     sŸ  ðÛ Ý #ß  ã ß Ð ß å #Ý 2Ý (ß 7Ñ 7Ý $ß Cß +Ñ +Ý !Ý -Ý 6Ý /ð Øð€ô˜;ô ð,9�r—y‘yó 9ô

ˆb�i‰iô 

ô"-�R—Y‘Yô "-ôJ2˜Nô 2ô&˜ô &ôQˆ"�)‰)ô Qôh
TH˜RŸY™Yô THðn5˜Sð 5¨4ð 5À3ó 5ñ( ÓÙØÐ/×7Ñ7Ð8Ø+¨]×-NÑ-NÐOôð /3ØØ!%Ø0=×0QÑ0QØ/3òhàÐ*Ñ+ðhð ðhð ˜#‘ð	hð
 ˜}Ñ-ðhð  (¨™}ðhð ðhð 	òhó	ó ñ
hr0   