
    kh=                     *   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y)    )DictListOptionalTupleN)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/teggl/fontify/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_msgsr7   typeold_keynew_keyr3   s               r4   r:   zRPNHead._load_from_state_dict*   s     !$$Y5?gk* B#HE$0#HIdV4j(*4..*AJw'	B 	%	
r5   xc                     g }g }|D ]S  }| j                  |      }|j                  | j                  |             |j                  | j                  |             U ||fS N)r"   r    r$   r%   )r.   rE   logitsbbox_regfeaturets         r4   forwardzRPNHead.forwardG   s`     	/G		'"AMM$//!,-OODNN1-.	/ xr5   )r   )__name__
__module____qualname____doc___versionintr   r:   r   r   r   rL   __classcell__r3   s   @r4   r   r      sW     H;C ;c ;D ;
: f  %Vd6l0J*K  r5   r   r2   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)r2   rU   rV   rW   rX   rY   s         r4   permute_and_flattenr_   Q   sE    JJq"aA&EMM!Q1a(EMM!R#ELr5   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   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;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""r5   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   rv   rw   	det_utilsBoxCoderrs   box_opsbox_ioubox_similarityMatcherrt   BalancedPositiveNegativeSamplerru   _pre_nms_top_n_post_nms_top_nr~   r   min_size)r.   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r3   s              r4   r   zRegionProposalNetwork.__init__   s     	 0	"++4HI &oo ) 1 1&*!
 'FFG[]no+-$(r5   c                 V    | j                   r| j                  d   S | j                  d   S Ntrainingtesting)r   r   r.   s    r4   r|   z#RegionProposalNetwork.pre_nms_top_n   s+    ==&&z22""9--r5   c                 V    | j                   r| j                  d   S | j                  d   S r   )r   r   r   s    r4   r}   z$RegionProposalNetwork.post_nms_top_n   s+    ==''
33##I..r5   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      )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   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*+##$>?;	@< '''r5   
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   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%//D4F4F4H!LM77=a78LAyHHY'(k!F	" yy""r5   	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   r[   r   r   )r   )rg   r   detachr^   	enumerater(   fullint64rh   	expand_asr   arangesigmoidrf   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                        r4   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   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** ;;uyy1Aq'IJ1M ;;uyy1Aq'IJ1Myy"24D!E1M'')
6q)"YY'9qA##-./0	

 !# <<Z=UW]^jWkl((r5   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   r[   r   ztargets should not be None)loss_objectnessloss_rpn_box_reg)listvaluesrw   rv   lenrg   rp   rs   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                        r4   rL   zRegionProposalNetwork.forwardP  s   0 )*'+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   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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:	44 sF{#4 $tCK012	4
 
tF|T#v+..	/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    r5   r4   <module>r      s    . .   $ B ! * !? bii ? Dv # # # # # RX #$v, #V #Y^_egm_mYn #2SEHHOO Sr5   