
    ڧg=                     ,   d dl mZmZmZ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dS )    )DictListOptionalTupleN)nnTensor)
functional)boxesConv2dNormActivation   )_utils)AnchorGenerator)	ImageListc                        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
       r   in_channelsnum_anchorsreturnNc           	      v   t                                                       g }t          |          D ](}|                    t	          ||dd                      )t          j        | | _        t          j        ||dd          | _	        t          j        ||dz  dd          | _
        |                                 D ]x}t          |t
          j                  r\t          j        j                            |j        d           |j        *t          j        j                            |j        d           yd S )	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/ai-engine/env/lib/python3.11/site-packages/torchvision/models/detection/rpn.pyr   zRPNHead.__init__   s!   z"" 	i 	iALL-k;TUbfggghhhhM5)	)K!TUVVV;aQWXYYY\\^^ 	; 	;E%++ ;%%el%===:)HM++EJ:::		; 	;    c           	          |                     dd           }||dk     r/dD ],}	| d|	 }
| d|	 }|
|v r|                    |
          ||<   -t                                          |||||||           d S )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_msgsr7   typeold_keynew_keyr3   s               r4   r:   zRPNHead._load_from_state_dict*   s     !$$Y55?gkk* B B#00$00#44d44j((*4..*A*AJw'%%	
 	
 	
 	
 	
r5   xc                     g }g }|D ]g}|                      |          }|                    |                     |                     |                    |                     |                     h||fS N)r"   r    r$   r%   )r.   rE   logitsbbox_regfeaturets         r4   forwardzRPNHead.forwardG   sv     	/ 	/G		'""AMM$//!,,---OODNN1--....xr5   )r   )__name__
__module____qualname____doc___versionintr   r:   r   r   r   rL   __classcell__r3   s   @r4   r   r      s          H; ;C ;c ;D ; ; ; ; ; ;
 
 
 
 
: f  %Vd6l0J*K                r5   r   r2   NACHWr   c                     |                      |d|||          } |                     ddddd          } |                     |d|          } | S )Nr   r   r   r   r   )viewpermutereshape)r2   rU   rV   rW   rX   rY   s         r4   permute_and_flattenr_   Q   sN    JJq"aA&&EMM!Q1a((EMM!R##ELr5   box_clsbox_regressionc           	         g }g }t          | |          D ]z\  }}|j        \  }}}}	|j        d         }
|
dz  }||z  }t          ||||||	          }|                    |           t          |||d||	          }|                    |           {t	          j        |d                              dd          } t	          j        |d                              dd          }| |fS )Nr   r   dimr   r[   )zipshaper_   r    r(   catflattenr^   )r`   ra   box_cls_flattenedbox_regression_flattenedbox_cls_per_levelbox_regression_per_levelrU   AxCrX   rY   Ax4rV   rW   s                r4   concat_box_prediction_layersrp   X   s   !
 8;7N7S7S 	B 	B33(.31&,Q/1H1H/0A1aAqQQ  !2333#67OQRTUWXZ[]^#_#_  ''(@AAAA i)q11199!R@@GY7Q???GGANNNN""r5   c                   t    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                 l   t                                                       || _        || _        t	          j        d          | _        t          j        | _	        t	          j
        ||d          | _        t	          j        ||          | _        || _        || _        |	| _        |
| _        d| _        d S )N)      ?r   r   r   )weightsT)allow_low_quality_matchesgMbP?)r   r   rw   rx   	det_utilsBoxCoderrs   box_opsbox_ioubox_similarityMatcherrt   BalancedPositiveNegativeSamplerru   _pre_nms_top_n_post_nms_top_nr   r   min_size)r.   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r3   s              r4   r   zRegionProposalNetwork.__init__   s     	 0	"+4HIII &o ) 1&*!
 !
 !
 'FG[]noo+-$(r5   c                 D    | j         r| j        d         S | j        d         S Ntrainingtesting)r   r   r.   s    r4   r}   z#RegionProposalNetwork.pre_nms_top_n   s(    = 	3&z22"9--r5   c                 D    | j         r| j        d         S | j        d         S r   )r   r   r   s    r4   r~   z$RegionProposalNetwork.post_nms_top_n   s(    = 	4'
33#I..r5   anchorstargetsc                    g }g }t          ||          D ]B\  }}|d         }|                                dk    r[|j        }t          j        |j        t          j        |          }	t          j        |j        d         ft          j        |          }
n|                     ||          }|                     |          }||	                    d                   }	|dk    }
|

                    t          j                  }
|| j        j        k    }d|
|<   || j        j        k    }d|
|<   |                    |
           |                    |	           D||fS )Nr
   r   dtypedevice)min)r   rv   g      )rf   numelr   r(   zerosrg   float32r   rt   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                  r4   assign_targets_to_anchorsz/RegionProposalNetwork.assign_targets_to_anchors   s    474I4I 	@ 	@00(1H~~1$$*1-2[9J9PX]Xent-u-u-u*#(;0A0G0J/LTYTajp#q#q#q  '+':':8EV'W'W$#445IJJ
 .6l6H6HQ6H6O6O-P*#/1#4 #3#6#6U]#6#K#K  *T-B-VV
/2 , #/$2G2Z"Z48 1MM*+++##$>????'''r5   
objectnessnum_anchors_per_levelc                 @   g }d}|                     |d          D ]n}|j        d         }t          j        ||                                 d          }|                    |d          \  }}	|                    |	|z              ||z  }ot          j        |d          S )Nr   r   rc   )	splitrg   r   	_topk_minr}   topkr    r(   rh   )
r.   r   r   roffsetobr   r}   r1   	top_n_idxs
             r4   _get_top_n_idxz$RegionProposalNetwork._get_top_n_idx   s    ""#8!<< 	" 	"B(1+K%/D4F4F4H4H!LLM77=a788LAyHHY'(((k!FFy""""r5   	proposalsimage_shapesc                 2   |j         d         }|j        |                                }|                    |d          }fdt	          |          D             }t          j        |d          }|                    dd                              |          }|                     ||          }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 |                                          }||         ||         }}|                    |           |                    |           ||fS )Nr   r[   c                 \    g | ](\  }}t          j        |f|t           j                   )S )r   )r(   fullint64).0idxnr   s      r4   
<listcomp>z:RegionProposalNetwork.filter_proposals.<locals>.<listcomp>   sA     
 
 
HNQEJtSFCCC
 
 
r5   r   )r   )rg   r   detachr^   	enumerater(   rh   	expand_asr   arangesigmoidrf   r   clip_boxes_to_imageremove_small_boxesr   wherer   batched_nmsr   r~   r    )r.   r   r   r   r   
num_imageslevelsr   image_range	batch_idxobjectness_probfinal_boxesfinal_scoresr
   scoreslvl	img_shapekeepr   s                     @r4   filter_proposalsz&RegionProposalNetwork.filter_proposals   sF    _Q'
!&&((
''
B77

 
 
 
R[\qRrRr
 
 
 61%%2&&00<< ''
4IJJ	l:f===4(		9 45
	9,-i23	-
33-0OVUa-b-b 	( 	()E63	/yAAE -eT]CCD!&tfTlCI36E ;v)::;;A>D!&tfTlCI36E &ufc4?KKD /$--///0D!$K6Eu%%%''''L((r5   pred_bbox_deltasr   regression_targetsc                 V   |                      |          \  }}t          j        t          j        |d                    d         }t          j        t          j        |d                    d         }t          j        ||gd          }|                                }t          j        |d          }t          j        |d          }t          j        ||         ||         dd          |                                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   rc   gqq?sum)beta	reduction)	ru   r(   r   rh   ri   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
             r4   compute_lossz"RegionProposalNetwork.compute_loss+  s.    .2-?-?-G-G** ;uy1Aq'I'I'IJJ1M ;uy1Aq'I'I'IJJ1My"24D!E1MMM''))
6q)))"Y'9qAAA#-./0	
 
 

 !!# <Z=UW]^jWkll((r5   imagesfeaturesc                    t          |                                          }|                     |          \  }}|                     ||          }t	          |          }d |D             }d |D             }	t          ||          \  }}| j                            |                                |          }
|
	                    |dd          }
| 
                    |
||j        |	          \  }}i }| j        re|t          d          |                     ||          \  }}| j                            ||          }|                     ||||          \  }}||d}||fS )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.
        c                 (    g | ]}|d          j         S )r   )rg   )r   os     r4   r   z1RegionProposalNetwork.forward.<locals>.<listcomp>m  s    .N.N.Naqtz.N.N.Nr5   c                 B    g | ]}|d          |d         z  |d         z  S )r   r   r    )r   ss     r4   r   z1RegionProposalNetwork.forward.<locals>.<listcomp>n  s-     a a a1!qt!3 a a ar5   r[   r   Nztargets should not be None)loss_objectnessloss_rpn_box_reg)listvaluesrx   rw   lenrp   rs   decoder   r\   r   image_sizesr   
ValueErrorr   encoder   )r.   r   r   r   r   r   r   r   #num_anchors_per_level_shape_tensorsr   r   r
   r   lossesr   r   r   r   r   s                      r4   rL   zRegionProposalNetwork.forwardP  s   0 ))**'+yy':':$
$''99\\
.N.N:.N.N.N+ a a=` a a a'CJP`'a'a$
$ N))*:*A*A*C*CWMM	NN:r155	--iVEWYnoov= 	 !=>>>'+'E'Egw'W'W$F$!%!6!67G!Q!Q040A0A,f6H1 1-O- $3$4 F f}r5   )rv   rG   )rM   rN   rO   rP   r   r   r   r   __annotations__r   r   ModulefloatrR   r   strr   r}   r~   r   r   r   r   r   r   r   r   r   rL   rS   rT   s   @r4   rr   rr   q   s        6 '%-"B O& "# #)# i#
 # # "# !# 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) 7) 7) 7)r#) #)4:#)DHL#)fjkqfr#)	vv~	#) #) #) #)R 6:	4 44 sF{#4 $tCK012	4
 
tF|T#v+..	/4 4 4 4 4 4 4 4r5   rr   )typingr   r   r   r   r(   r   r   torch.nnr	   r   torchvision.opsr
   r   r    r   r   anchor_utilsr   
image_listr   r   r   rR   r_   rp   rr   r   r5   r4   <module>r     s   . . . . . . . . . . . .          $ $ $ $ $ $ B B B B B B B B ! ! ! ! ! ! * ) ) ) ) ) ! ! ! ! ! !?  ?  ?  ?  ? bi ?  ?  ? Dv # # # # # RX    #$v, #V #Y^_egm_mYn # # # #2S S S S SEHO S S S S Sr5   