
    h=                        d dl mZ d dlZd dlmZmZ d dlmZ d dlm	Z
mZ ddlmZ ddlmZ dd	lmZ  G d
 dej&                        ZdededededededefdZdee   dee   deeef   fdZ G d dej                  j&                        Zy)    )OptionalN)nnTensor)
functional)boxesConv2dNormActivation   )_utils)AnchorGenerator)	ImageListc                   l     e Zd ZdZdZddededdf fdZ fdZd	ee	   de
ee	   ee	   f   fd
Z xZS )RPNHeada  
    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         |           g }t        |      D ]   }|j                  t	        ||dd              " t        j                  | | _        t        j                  ||dd      | _	        t        j                  ||dz  dd      | _
        | j                         D ]  }t        |t
        j                        st        j
                  j                  j                  |j                   d       |j"                  `t        j
                  j                  j%                  |j"                  d        y )	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          ^/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/models/detection/rpn.pyr   zRPNHead.__init__   s    z" 	iALL-k;TUbfgh	iMM5)	))K!TUV;aQWXY\\^ 	;E%+%%ell%=::)HHMM++EJJ:		;    c           	          |j                  dd       }||dk  r.dD ])  }	| d|	 }
| d|	 }|
|v s|j                  |
      ||<   + t        |   |||||||       y )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_msgsr4   typeold_keynew_keyr0   s               r1   r7   zRPNHead._load_from_state_dict*   s     !$$Y5?gk* B#HE$0#HIdV4j(*4..*AJw'	B 	%	
r2   xc                     g }g }|D ]S  }| j                  |      }|j                  | j                  |             |j                  | j                  |             U ||fS N)r   r   r!   r"   )r+   rB   logitsbbox_regfeaturets         r1   forwardzRPNHead.forwardG   s`     	/G		'"AMM$//!,-OODNN1-.	/ xr2   )r	   )__name__
__module____qualname____doc___versionintr   r7   listr   tuplerI   __classcell__r0   s   @r1   r   r      sW     H;C ;c ;D ;
: f  %Vd6l0J*K  r2   r   r/   NACHWr   c                     | j                  |d|||      } | j                  ddddd      } | j                  |d|      } | S )Nr   r   r   r	   r   )viewpermutereshape)r/   rT   rU   rV   rW   rX   s         r1   permute_and_flattenr^   Q   sE    JJq"aA&EMM!Q1a(EMM!R#ELr2   box_clsbox_regressionc           	         g }g }t        | |      D ]q  \  }}|j                  \  }}}}	|j                  d   }
|
dz  }||z  }t        ||||||	      }|j                  |       t        |||d||	      }|j                  |       s t	        j
                  |d      j                  dd      } t	        j
                  |d      j                  dd      }| |fS )Nr	   r   dimr   rZ   )zipshaper^   r   r%   catflattenr]   )r_   r`   box_cls_flattenedbox_regression_flattenedbox_cls_per_levelbox_regression_per_levelrT   AxCrW   rX   Ax4rU   rV   s                r1   concat_box_prediction_layersro   X   s    !
 8;7N7S 	B33(..31&,,Q/1H1H/0A1aAqQ  !23#67OQRTUWXZ[]^#_  ''(@A	B ii)q199!R@GYY7Q?GGANNN""r2   c                       e Zd ZdZej
                  ej                  ej                  dZ	 d"de	de
j                  dedededed	eeef   d
eeef   dededdf fdZdefdZdefdZdee   deeeef      deee   ee   f   fdZdedee   defdZdededeeeef      dee   deee   ee   f   f
dZdededee   dee   deeef   f
dZ	 d#ded eeef   deeeeef         deee   eeef   f   fd!Z xZS )$RegionProposalNetworka  
    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   t         |           || _        || _        t	        j
                  d      | _        t        j                  | _	        t	        j                  ||d      | _        t	        j                  ||      | _        || _        || _        |	| _        |
| _        d| _        y )N)      ?r   r   r   )weightsT)allow_low_quality_matchesgMbP?)r   r   ru   rv   	det_utilsBoxCoderrr   box_opsbox_ioubox_similarityMatcherrs   BalancedPositiveNegativeSamplerrt   _pre_nms_top_n_post_nms_top_nr}   r~   min_size)r+   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r0   s              r1   r   zRegionProposalNetwork.__init__   s     	 0	"++4HI &oo ) 1 1&*!
 'FFG[]no+-$(r2   c                 V    | j                   r| j                  d   S | j                  d   S Ntrainingtesting)r   r   r+   s    r1   r{   z#RegionProposalNetwork.pre_nms_top_n   s+    ==&&z22""9--r2   c                 V    | j                   r| j                  d   S | j                  d   S r   )r   r   r   s    r1   r|   z$RegionProposalNetwork.post_nms_top_n   s+    ==''
33##I..r2   anchorstargetsc                    g }g }t        ||      D ]J  \  }}|d   }|j                         dk(  rq|j                  }t        j                  |j
                  t        j                  |      }	t        j                  |j
                  d   ft        j                  |      }
n| j                  ||      }| j                  |      }||j                  d         }	|dk\  }
|
j                  t        j                        }
|| j                  j                  k(  }d|
|<   || j                  j                  k(  }d|
|<   |j                  |
       |j                  |	       M ||fS )Nr   r   dtypedevice)min)r           g      )re   numelr   r%   zerosrf   float32r   rs   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                  r1   assign_targets_to_anchorsz/RegionProposalNetwork.assign_targets_to_anchors   sq    474I 	@00(1H~~1$*11-2[[9J9P9PX]XeXent-u*#(;;0A0G0G0J/LTYTaTajp#q '+':':8EV'W$#445IJ
 .6l6H6HQ6H6O-P*#/1#4 #3#6#6U]]#6#K  *T-B-B-V-VV
/2 , #/$2G2G2Z2Z"Z48 1MM*+##$>?;	@< '''r2   
objectnessnum_anchors_per_levelc                 ,   g }d}|j                  |d      D ]e  }|j                  d   }t        j                  || j	                         d      }|j                  |d      \  }}	|j                  |	|z          ||z  }g t        j                  |d      S )Nr   r	   rb   )	splitrf   r   	_topk_minr{   topkr   r%   rg   )
r+   r   r   roffsetobr   r{   r.   	top_n_idxs
             r1   _get_top_n_idxz$RegionProposalNetwork._get_top_n_idx   s    ""#8!< 	"B((1+K%//D4F4F4H!LM77=a78LAyHHY'(k!F	" yy""r2   	proposalsimage_shapesc           
      (   |j                   d   }|j                  }|j                         }|j                  |d      }t	        |      D cg c]-  \  }}t        j                  |f|t
        j                  |      / }	}}t        j                  |	d      }	|	j                  dd      j                  |      }	| j                  ||      }
t        j                  ||      }|d d d f   }|||
f   }|	||
f   }	|||
f   }t        j                  |      }g }g }t        |||	|      D ]  \  }}}}t        j                  ||      }t        j                   || j"                        }||   ||   ||   }}}t        j$                  || j&                  k\        d   }||   ||   ||   }}}t        j(                  |||| j*                        }|d | j-                          }||   ||   }}|j/                  |       |j/                  |        ||fS c c}}w )Nr   rZ   r   r	   )r   )rf   r   detachr]   	enumerater%   fullint64rg   	expand_asr   arangesigmoidre   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                        r1   filter_proposalsz&RegionProposalNetwork.filter_proposals   s3    __Q'
!!&&(
''
B7
 S\\qRr
HNQEJJtSFC
 
 61%2&00< ''
4IJ	ll:f=4(		9 45
	9,-i23	--
3-0OVUa-b 	()E63	//yAE --eT]]CD!&tfTlCI36E ;;v):)::;A>D!&tfTlCI36E &&ufc4??KD /$--/0D!$K6Eu%')	(* L((S
s   2Hpred_bbox_deltasr   regression_targetsc                 ,   | j                  |      \  }}t        j                  t        j                  |d            d   }t        j                  t        j                  |d            d   }t        j                  ||gd      }|j	                         }t        j                  |d      }t        j                  |d      }t        j                  ||   ||   dd      |j                         z  }t        j                  ||   ||         }	|	|fS )a  
        Args:
            objectness (Tensor)
            pred_bbox_deltas (Tensor)
            labels (List[Tensor])
            regression_targets (List[Tensor])

        Returns:
            objectness_loss (Tensor)
            box_loss (Tensor)
        r   rb   gqq?sum)beta	reduction)	rt   r%   r   rg   rh   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
             r1   compute_lossz"RegionProposalNetwork.compute_loss+  s    .2-?-?-G** ;;uyy1Aq'IJ1M ;;uyy1Aq'IJ1Myy"24D!E1M'')
6q)"YY'9qA##-./0	

 !# <<Z=UW]^jWkl((r2   imagesfeaturesc                    t        |j                               }| j                  |      \  }}| j                  ||      }t	        |      }|D cg c]  }|d   j
                   }	}|	D 
cg c]  }
|
d   |
d   z  |
d   z   }}
t        ||      \  }}| j                  j                  |j                         |      }|j                  |dd      }| j                  |||j                  |      \  }}i }| j                  rZ|t        d      | j                  ||      \  }}| j                  j!                  ||      }| j#                  ||||      \  }}||d}||fS c c}w c c}
w )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   rZ   r   ztargets should not be None)loss_objectnessloss_rpn_box_reg)rP   valuesrv   ru   lenrf   ro   rr   decoder   r[   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                        r1   rI   zRegionProposalNetwork.forwardP  s   . )*'+yy':$
$''9\
CM.Naqtzz.N+.N=` a1!qt!3 a a'CJP`'a$
$ NN))*:*A*A*CWM	NN:r15	--iVEWEWYnov== !=>>'+'E'Egw'W$F$!%!6!67G!Q040A0A,f6H1-O- $3$4F f}/ /O as   E+E#)r   rD   )rJ   rK   rL   rM   r   r   r   r   __annotations__r   r   ModulefloatrO   dictstrr   r{   r|   rP   r   rQ   r   r   r   r   r   r   rI   rR   rS   s   @r1   rq   rq   q   s-   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r2   rq   )typingr   r%   r   r   torch.nnr   r   torchvision.opsr   r   r    r
   r   anchor_utilsr   
image_listr   r   r   rO   r^   rP   rQ   ro   rq    r2   r1   <module>r      s       $ B ! * !? bii ? Dv # # # # # RX #$v, #V #Y^_egm_mYn #2REHHOO Rr2   