
    h=                     "   S SK Jr  S SK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  SS	KJr   " S
 S\R&                  5      rS\S\S\S\S\S\S\4S jrS\\   S\\   S\\\4   4S jr " S S\R                  R&                  5      rg)    )OptionalN)nnTensor)
functional)boxesConv2dNormActivation   )_utils)AnchorGenerator)	ImageListc                   ~   ^  \ rS rSrSrSrSS\S\SS4U 4S jjjrU 4S	 jrS
\	\
   S\\	\
   \	\
   4   4S jrSrU =r$ )RPNHead   z
Adds a simple RPN Head with classification and regression heads

Args:
    in_channels (int): number of channels of the input feature
    num_anchors (int): number of anchors to be predicted
    conv_depth (int, optional): number of convolutions
   in_channelsnum_anchorsreturnNc           
      z  > [         TU ]  5         / n[        U5       H  nUR                  [	        XSS S95        M     [
        R                  " U6 U l        [
        R                  " XSSS9U l	        [
        R                  " XS-  SSS9U l
        U 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     g )	N   )kernel_size
norm_layerr	   )r   stride   g{Gz?)stdr   )super__init__rangeappendr   r   
SequentialconvConv2d
cls_logits	bbox_predmodules
isinstancetorchinitnormal_weightbias	constant_)selfr   r   
conv_depthconvs_layer	__class__s          X/var/www/auris/envauris/lib/python3.13/site-packages/torchvision/models/detection/rpn.pyr   RPNHead.__init__   s    z"ALL-kTUbfgh #MM5)	))K!TUV;aQWXY\\^E%++%%ell%=::)HHMM++EJJ:	 $    c           	         > UR                  SS 5      nUb  US:  a1  S H+  n	U SU	 3n
U SU	 3nX;   d  M  UR                  U
5      X'   M-     [        TU ]  UUUUUUU5        g )Nversionr   )r)   r*   zconv.z	conv.0.0.)getpopr   _load_from_state_dict)r,   
state_dictprefixlocal_metadatastrictmissing_keysunexpected_keys
error_msgsr6   typeold_keynew_keyr1   s               r2   r9   RPNHead._load_from_state_dict*   s     !$$Y5?gk*#HE$0#HIdV4(*4..*AJ'	 + 	%	
r4   xc                     / n/ nU HT  nU R                  U5      nUR                  U R                  U5      5        UR                  U R                  U5      5        MV     X#4$ N)r    r   r"   r#   )r,   rE   logitsbbox_regfeaturets         r2   forwardRPNHead.forwardG   sY    G		'"AMM$//!,-OODNN1-.  r4   )r#   r"   r    )r	   )__name__
__module____qualname____firstlineno____doc___versionintr   r9   listr   tuplerL   __static_attributes____classcell__r1   s   @r2   r   r      sa     H;C ;c ;D ; ;
: f  %Vd6l0J*K    r4   r   r0   NACHWr   c                 ~    U R                  USX4U5      n U R                  SSSSS5      n U R                  USU5      n U $ )Nr   r   r   r	   r   )viewpermutereshape)r0   rZ   r[   r\   r]   r^   s         r2   permute_and_flattenrd   Q   sC    JJq"aA&EMM!Q1a(EMM!R#ELr4   box_clsbox_regressionc           	         / n/ n[        X5       Hi  u  pEUR                  u  pgpUR                  S   n
U
S-  nX{-  n[        XFXX5      nUR                  U5        [        XVUSX5      nUR                  U5        Mk     [        R
                  " USS9R                  SS5      n [        R
                  " USS9R                  SS5      nX4$ )Nr	   r   dimr   r`   )zipshaperd   r   r&   catflattenrc   )re   rf   box_cls_flattenedbox_regression_flattenedbox_cls_per_levelbox_regression_per_levelrZ   AxCr]   r^   Ax4r[   r\   s                r2   concat_box_prediction_layersru   X   s    !
 8;77S3(..&,,Q/1HH/0AaAQ  !23#67OTUWXZ[#_  ''(@A 8T ii)q199!R@GYY7Q?GGANN""r4   c                   $  ^  \ rS rSrSr\R                  \R                  \R                  S.r	 S$S\
S\R                  S\S\S\S	\S
\\\4   S\\\4   S\S\SS4U 4S jjjrS\4S jrS\4S jrS\\   S\\\\4      S\\\   \\   4   4S jrS\S\\   S\4S jrS\S\S\\\\4      S\\   S\\\   \\   4   4
S jrS\S\S\\   S\\   S\\\4   4
S jr S%S \S!\\\4   S\\\\\4         S\\\   \\\4   4   4S" jjrS#rU =r$ )&RegionProposalNetworkq   aw  
Implements Region Proposal Network (RPN).

Args:
    anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature
        maps.
    head (nn.Module): module that computes the objectness and regression deltas
    fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be
        considered as positive during training of the RPN.
    bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be
        considered as negative during training of the RPN.
    batch_size_per_image (int): number of anchors that are sampled during training of the RPN
        for computing the loss
    positive_fraction (float): proportion of positive anchors in a mini-batch during training
        of the RPN
    pre_nms_top_n (Dict[str, int]): number of proposals to keep before applying NMS. It should
        contain two fields: training and testing, to allow for different values depending
        on training or evaluation
    post_nms_top_n (Dict[str, int]): number of proposals to keep after applying NMS. It should
        contain two fields: training and testing, to allow for different values depending
        on training or evaluation
    nms_thresh (float): NMS threshold used for postprocessing the RPN proposals
    score_thresh (float): only return proposals with an objectness score greater than score_thresh

)	box_coderproposal_matcherfg_bg_sampleranchor_generatorheadfg_iou_threshbg_iou_threshbatch_size_per_imagepositive_fractionpre_nms_top_npost_nms_top_n
nms_threshscore_threshr   Nc                 @  > [         TU ]  5         Xl        X l        [        R
                  " SS9U l        [        R                  U l	        [        R                  " UUSS9U l        [        R                  " XV5      U l        Xpl        Xl        Xl        Xl        SU l        g )N)      ?r   r   r   )weightsT)allow_low_quality_matchesgMbP?)r   r   r|   r}   	det_utilsBoxCoderry   box_opsbox_ioubox_similarityMatcherrz   BalancedPositiveNegativeSamplerr{   _pre_nms_top_n_post_nms_top_nr   r   min_size)r,   r|   r}   r~   r   r   r   r   r   r   r   r1   s              r2   r   RegionProposalNetwork.__init__   s     	 0	"++4HI &oo ) 1 1&*!
 'FFG[o+-$(r4   c                 `    U R                   (       a  U R                  S   $ U R                  S   $ Ntrainingtesting)r   r   r,   s    r2   r   #RegionProposalNetwork.pre_nms_top_n   s+    ==&&z22""9--r4   c                 `    U R                   (       a  U R                  S   $ U R                  S   $ r   )r   r   r   s    r2   r   $RegionProposalNetwork.post_nms_top_n   s+    ==''
33##I..r4   anchorstargetsc                    / n/ n[        X5       GH;  u  pVUS   nUR                  5       S:X  am  UR                  n[        R                  " UR
                  [        R                  US9n	[        R                  " UR
                  S   4[        R                  US9n
OU R                  Xu5      nU R                  U5      nX|R                  SS9   n	US:  n
U
R                  [        R                  S9n
XR                  R                  :H  nSX'   XR                  R                  :H  nSX'   UR                  U
5        UR                  U	5        GM>     X44$ )Nr   r   dtypedevice)min)r           g      )rk   numelr   r&   zerosrl   float32r   rz   clamptoBELOW_LOW_THRESHOLDBETWEEN_THRESHOLDSr   )r,   r   r   labelsmatched_gt_boxesanchors_per_imagetargets_per_imagegt_boxesr   matched_gt_boxes_per_imagelabels_per_imagematch_quality_matrixmatched_idxs
bg_indicesinds_to_discards                  r2   assign_targets_to_anchors/RegionProposalNetwork.assign_targets_to_anchors   sV    474I0(1H~~1$*11-2[[9J9P9PX]XeXent-u*#(;;0A0G0G0J/LTYTaTajp#q '+':':8'W$#445IJ
 .66H6HQ6H6O-P*#/1#4 #3#6#6U]]#6#K  *-B-B-V-VV
/2 , #/2G2G2Z2Z"Z48 1MM*+##$>?; 5J< ''r4   
objectnessnum_anchors_per_levelc                     / nSnUR                  US5       H`  nUR                  S   n[        R                  " XPR	                  5       S5      nUR                  USS9u  pUR                  X-   5        XF-  nMb     [        R                  " USS9$ )Nr   r	   rh   )	splitrl   r   	_topk_minr   topkr   r&   rm   )
r,   r   r   roffsetobr   r   r/   	top_n_idxs
             r2   _get_top_n_idx$RegionProposalNetwork._get_top_n_idx   s    ""#8!<B((1+K%//4F4F4H!LM77=a78LAHHY'(!F = yy""r4   	proposalsimage_shapesc                 $   UR                   S   nUR                  nUR                  5       nUR                  US5      n[	        U5       VVs/ s H+  u  px[
        R                  " U4U[
        R                  US9PM-     n	nn[
        R                  " U	S5      n	U	R                  SS5      R                  U5      n	U R                  X$5      n
[
        R                  " XVS9nUS S 2S 4   nX,U
4   nXU
4   n	XU
4   n[
        R                  " U5      n/ n/ n[        XX5       H  u  nnnn[        R                  " UU5      n[        R                   " UU R"                  5      nUU   UU   UU   nnn[
        R$                  " UU R&                  :  5      S   nUU   UU   UU   nnn[        R(                  " UUUU R*                  5      nUS U R-                  5        nUU   UU   nnUR/                  U5        UR/                  U5        M     X4$ s  snnf )Nr   r`   r   r	   )r   )rl   r   detachrc   	enumerater&   fullint64rm   	expand_asr   arangesigmoidrk   r   clip_boxes_to_imageremove_small_boxesr   wherer   batched_nmsr   r   r   )r,   r   r   r   r   
num_imagesr   idxnlevelsr   image_range	batch_idxobjectness_probfinal_boxesfinal_scoresr   scoreslvl	img_shapekeeps                        r2   filter_proposals&RegionProposalNetwork.filter_proposals   s    __Q'
!!&&(
''
B7
 S\\qRr
RrEJJtSFCRr 	 
 61%2&00< ''
J	ll:=4(	9 45
9,-23	--
3-0V-b)E63	//yAE --eT]]CD!&tfTlCI36E ;;v):)::;A>D!&tfTlCI36E &&ufc4??KD /$--/0D!$K6Eu%') .c* ((S
s   2Hpred_bbox_deltasr   regression_targetsc                    U R                  U5      u  pV[        R                  " [        R                  " USS95      S   n[        R                  " [        R                  " USS95      S   n[        R                  " XV/SS9nUR	                  5       n[        R                  " USS9n[        R                  " USS9n[
        R                  " X%   XE   SSS9UR                  5       -  n[
        R                  " X   X7   5      n	X4$ )z
Args:
    objectness (Tensor)
    pred_bbox_deltas (Tensor)
    labels (List[Tensor])
    regression_targets (List[Tensor])

Returns:
    objectness_loss (Tensor)
    box_loss (Tensor)
r   rh   gqq?sum)beta	reduction)	r{   r&   r   rm   rn   Fsmooth_l1_lossr    binary_cross_entropy_with_logits)
r,   r   r   r   r   sampled_pos_indssampled_neg_indssampled_indsbox_lossobjectness_losss
             r2   compute_loss"RegionProposalNetwork.compute_loss+  s     .2-?-?-G* ;;uyy1Aq'IJ1M ;;uyy1Aq'IJ1Myy"2!E1M'')
6q)"YY'9qA##.0	

 !# <<Z=UW]Wkl((r4   imagesfeaturesc                    [        UR                  5       5      nU R                  U5      u  pEU R                  X5      n[	        U5      nU Vs/ s H  oS   R
                  PM     n	nU	 V
s/ s H  oS   U
S   -  U
S   -  PM     nn
[        XE5      u  pEU R                  R                  UR                  5       U5      nUR                  USS5      nU R                  XUR                  U5      u  p0 nU R                  (       aY  Uc  [        S5      eU R                  Xc5      u  nnU R                  R!                  UU5      nU R#                  XEUU5      u  nnUUS.nX4$ s  snf s  sn
f )a  
Args:
    images (ImageList): images for which we want to compute the predictions
    features (Dict[str, Tensor]): features computed from the images that are
        used for computing the predictions. Each tensor in the list
        correspond to different feature levels
    targets (List[Dict[str, Tensor]]): ground-truth boxes present in the image (optional).
        If provided, each element in the dict should contain a field `boxes`,
        with the locations of the ground-truth boxes.

Returns:
    boxes (List[Tensor]): the predicted boxes from the RPN, one Tensor per
        image.
    losses (Dict[str, Tensor]): the losses for the model during training. During
        testing, it is an empty dict.
r   r	   r   r`   r   ztargets should not be None)loss_objectnessloss_rpn_box_reg)rU   valuesr}   r|   lenrl   ru   ry   decoder   ra   r   image_sizesr   
ValueErrorr   encoder   )r,   r   r   r   r   r   r   r   o#num_anchors_per_level_shape_tensorssr   r   r   r   lossesr   r   r   r   r   s                        r2   rL   RegionProposalNetwork.forwardP  sm   . )*'+yy':$
''9\
CM.N:atzz:+.N=` a=`1!qt!3=` a'CJ'a$
 NN))*:*A*A*CWM	NN:r15	--iVEWEWYno== !=>>'+'E'Eg'W$F$!%!6!67G!Q040A0Af6H1-O- $3$4F }/ /O as   E+E$)r   r   r|   ry   r   r{   r}   r   r   rz   r   )r   rG   ) rN   rO   rP   rQ   rR   r   r   r   r   __annotations__r   r   ModulefloatrT   dictstrr   r   r   rU   r   rV   r   r   r   r   r   r   rL   rW   rX   rY   s   @r2   rw   rw   q   s7   6 ''%--"BBO& "#)# ii#
 # # "# !# CH~# S#X# # # 
# #J.s .
/ /
$(F|$(.24V3D.E$(	tF|T&\)	*$(L	# 	#S	 	#V\ 	#7)7) 7) 5c?+	7)
  $Cy7) 
tF|T&\)	*7)r#) #)4:#)DHL#)fjkqfr#)	vv~	#)R 6:	33 sF{#3 $tCK012	3
 
tF|T#v+..	/3 3r4   rw   )typingr   r&   r   r   torch.nnr   r   torchvision.opsr   r   r    r
   r   anchor_utilsr   
image_listr   r  r   rT   rd   rU   rV   ru   rw    r4   r2   <module>r     s       $ B ! * !? bii ? Dv # # # # # RX #$v, #V #Y^_egm_mYn #2REHHOO Rr4   