CoolFace
Apppublic

Mjolnir65/FasterRCNN

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
rpn.cpython-311.pyc168 linesDownload Raw Back to __pycache__
1�

2���g�=��,�ddlmZmZmZmZddlZddlmZmZddlm	Z3ddlmZ
mZddlmZddlmZdd	lmZGd4�dej��Zded
edededededefd�Zdeedeedeeeffd�ZGd�dejj��ZdS)�)�Dict�List�Optional�TupleN)�nn�Tensor)�5functional)�boxes�Conv2dNormActivation�)�_utils)�AnchorGenerator)�	ImageListc���eZdZdZdZddededdf�fd�
Z�fd	�Zd6ee	de7ee	ee	ffd�Z�xZS)
�RPNHeada8    Adds a simple RPN Head with classification and regression heads9 10    Args:11        in_channels (int): number of channels of the input feature12        num_anchors (int): number of anchors to be predicted13        conv_depth (int, optional): number of convolutions14    �r�in_channels�num_anchors�returnNc	�v��t�����g}t|��D](}|�t	||dd������)tj|�|_tj||dd���|_	tj||dzdd���|_15|���D]x}t|t16j��r\tjj�|jd���|j�*tjj�|jd���ydS)	N�)�kernel_size�17norm_layerr)r�stride�g{�G�z�?)�stdr)�super�__init__�range�appendrr�18Sequential�conv�Conv2d�19cls_logits�	bbox_pred�modules�20isinstance�torch�init�normal_�weight�bias�	constant_)�selfrr�21conv_depth�convs�_�layer�	__class__s       ��L/home/gaurav/Documents/assignment-3-gaurav-behera/q1/modify/detection/rpn.pyrzRPNHead.__init__s!���
�����������z�"�"�	i�	i�A��L�L�-�k�;�TU�bf�g�g�g�h�h�h�h��M�5�)��	��)�K��!�TU�V�V�V�����;��a��Q�WX�Y�Y�Y����\�\�^�^�	;�	;�E��%���+�+�
;���
�%�%�e�l��%�=�=�=��:�)��H�M�+�+�E�J��:�:�:��		;�	;�c	����|�dd��}|�|dkr/dD],}	|�d|	��}22|�d|	��}|23|vr|�|24��||<�-t���|||||||��dS)N�versionr)r+r,zconv.z	conv.0.0.)�get�popr�_load_from_state_dict)
r.�25state_dict�prefix�local_metadata�strict�missing_keys�unexpected_keys�26error_msgsr7�type�old_key�new_keyr3s
            �r4r:zRPNHead._load_from_state_dict*s����!�$�$�Y��5�5���?�g��k�k�*�
B�
B��#�0�0�$�0�0��#�4�4�d�4�4���j�(�(�*4�.�.��*A�*A�J�w�'��
���%�%��������	27�	28�	29�	30�	31r5�xc���g}g}|D]g}|�|��}|�|�|����|�|�|�����h||fS�N)r"r r$r%)r.rE�logits�bbox_reg�feature�ts      r4�forwardzRPNHead.forwardGsv�������	/�	/�G��	�	�'�"�"�A��M�M�$�/�/�!�,�,�-�-�-��O�O�D�N�N�1�-�-�.�.�.�.��x��r5)r)
�__name__�32__module__�__qualname__�__doc__�_version�intrr:rrrrL�
__classcell__�r3s@r4rrs�����������H�
;�
;�C�
;�c�
;�D�
;�
;�
;�
;�
;�
;�33�34�35�36�37�: ��f�� �%��V��d�6�l�0J�*K� � � � � � � � r5rr2�N�A�C�H�Wrc��|�|d|||��}|�ddddd��}|�|d|��}|S)N�����rrrrr)�view�permute�reshape)r2rUrVrWrXrYs      r4�permute_and_flattenr_QsN���J�J�q�"�a��A�&�&�E��M�M�!�Q��1�a�(�(�E��M�M�!�R��#�#�E��Lr5�box_cls�box_regressionc	���g}g}t||��D]z\}}|j\}}}}	|jd}38|39dz}||z}t||||||	��}|�|��t|||d||	��}|�|���{t	j|d����dd��}t	j|d����dd��}||fS)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�AxCrXrY�Ax4rVrWs
             r4�concat_box_prediction_layersrpXs����!��408;�7�N�7S�7S�	B�	B�3��3�(�.���3��1�&�,�Q�/���1�H���1�H��/�0A�1�a��A�q�Q�Q��� � �!2�3�3�3�#6�7O�QR�TU�WX�Z[�]^�#_�#_� � �'�'�(@�A�A�A�A��i�)�q�1�1�1�9�9�!�R�@�@�G��Y�7�Q�?�?�?�G�G��A�N�N�N��N�"�"r5c�t��eZdZdZejejejd�Z	d#de	de41jdedede
d	ed42eee
fdeee
fded
eddf�fd�
Zde
fd�Zde
fd�Zdeedeeeefdeeeeeffd�Zdedee
defd�Zdededeee
e
fdee
deeeeeff43d�Zdededeedeedeeeff44d�Z	d$d ed!eeefdeeeeefdeeeeeefffd"�Z�xZS)%�RegionProposalNetworka�45    Implements Region Proposal Network (RPN).46 47    Args:48        anchor_generator (AnchorGenerator): module that generates the anchors for a set of feature49            maps.50        head (nn.Module): module that computes the objectness and regression deltas51        fg_iou_thresh (float): minimum IoU between the anchor and the GT box so that they can be52            considered as positive during training of the RPN.53        bg_iou_thresh (float): maximum IoU between the anchor and the GT box so that they can be54            considered as negative during training of the RPN.55        batch_size_per_image (int): number of anchors that are sampled during training of the RPN56            for computing the loss57        positive_fraction (float): proportion of positive anchors in a mini-batch during training58            of the RPN59        pre_nms_top_n (Dict[str, int]): number of proposals to keep before applying NMS. It should60            contain two fields: training and testing, to allow for different values depending61            on training or evaluation62        post_nms_top_n (Dict[str, int]): number of proposals to keep after applying NMS. It should63            contain two fields: training and testing, to allow for different values depending64            on training or evaluation65        nms_thresh (float): NMS threshold used for postprocessing the RPN proposals66        score_thresh (float): only return proposals with an objectness score greater than score_thresh67 68    )�	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�69nms_thresh�score_threshrNc�l��t�����||_||_t	jd���|_tj|_	t	j70||d���|_t	j||��|_
||_||_|	|_|71|_d|_dS)N)��?r�r�r�)�weightsT)�allow_low_quality_matchesg����MbP?)rrrwrx�	det_utils�BoxCoderrs�box_ops�box_iou�box_similarity�Matcherrt�BalancedPositiveNegativeSamplerru�_pre_nms_top_n�_post_nms_top_nrr��min_size)r.rwrxryrzr{r|r}r~rr�r3s           �r4rzRegionProposalNetwork.__init__�s����	�������� 0�����	�"�+�4H�I�I�I���&�o��� )� 1���&*�!72�!73�!74���'�F�G[�]n�o�o���+���-���$���(�����
�
�
r5c�D�|jr
|jdS|jdS�N�training�testing)r�r��r.s r4r}z#RegionProposalNetwork.pre_nms_top_n�s(���=�	3��&�z�2�2��"�9�-�-r5c�D�|jr
|jdS|jdSr�)r�r�r�s r4r~z$RegionProposalNetwork.post_nms_top_n�s(���=�	4��'�75�3�3��#�I�.�.r5�anchors�targetsc��g}g}t||��D�]B\}}|d}|���dkr[|j}tj|jtj|���}	tj|jdftj|���}76n�|�||��}|�|��}||�	d���}	|dk}77|78�79tj���}80||jjk}
d|81|
<||jjk}d|82|<|�
|83��|�
|	����D||fS)Nr84r��dtype�device)�min)r�rvg�)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�85bg_indices�inds_to_discards               r4�assign_targets_to_anchorsz/RegionProposalNetwork.assign_targets_to_anchors�s�������47���4I�4I�	@�	@�0��0�(��1�H��~�~���1�$�$�*�1��-2�[�9J�9P�X]�Xe�nt�-u�-u�-u�*�#(�;�0A�0G��0J�/L�TY�Ta�jp�#q�#q�#q� � �'+�':�':�8�EV�'W�'W�$�#�4�4�5I�J�J��86.6�l�6H�6H�Q�6H�6O�6O�-P�*�#/�1�#4� �#3�#6�#6�U�]�#6�#K�#K� �*�T�-B�-V�V�87�/2� ��,�#/�$�2G�2Z�"Z��48� ��1��M�M�*�+�+�+��#�#�$>�?�?�?�?��'�'�'r5�88objectness�num_anchors_per_levelc�@�g}d}|�|d��D]n}|jd}tj||���d��}|�|d���\}}	|�|	|z��||z
}�otj|d���S)Nrrrc)	�splitrgr��	_topk_minr}�topkr r(rh)89r.r�r��r�offset�obrr}r1�	top_n_idxs90          r4�_get_top_n_idxz$RegionProposalNetwork._get_top_n_idx�s��������"�"�#8�!�<�<�	"�	"�B��(�1�+�K�%�/��D�4F�4F�4H�4H�!�L�L�M��7�7�=�a�7�8�8�L�A�y�
�H�H�Y��'�(�(�(��k�!�F�F��y���"�"�"�"r5�	proposals�image_shapesc�2��|jd}|j�|���}|�|d��}�fd�t	|��D��}tj|d��}|�dd���|��}|�||��}tj	|����}|dd�df}	||	|f}||	|f}||	|f}tj91|��}92g}g}t||93||��D]�\}
}}}tj
|
|��}
tj|
|j��}|
|||||}}}
tj||jk��d}|
|||||}}}
tj|
|||j��}|d|����}|
|||}}
|�|
��|�|����||fS)Nrr[c�\��g|](\}}tj|f|tj������)S)r�)r(�full�int64)�.0�idx�nr�s   �r4�94<listcomp>z:RegionProposalNetwork.filter_proposals.<locals>.<listcomp>sA���95�96�97�HN��Q�E�J��t�S���F�C�C�C�98�99�100r5r)r�)rgr��detachr^�	enumerater(rh�	expand_asr��arange�sigmoidrfr��clip_boxes_to_image�remove_small_boxesr��wherer��batched_nmsrr~r )r.r�r�r�r��101num_images�levelsr��image_range�	batch_idx�objectness_prob�final_boxes�final_scoresr102�scores�lvl�	img_shape�keepr�s                  @r4�filter_proposalsz&RegionProposalNetwork.filter_proposals�sF����_�Q�'�103��!���&�&�(�(�104��'�'�105�B�7�7�106�107�108�109�110�R[�\q�Rr�Rr�111�112�113����6�1�%�%������2�&�&�0�0��<�<���'�'�114�4I�J�J�	��l�:�f�=�=�=������4��(�	��	�9� 4�5�115��	�9�,�-���i��2�3�	��-�116�3�3������-0��O�V�Ua�-b�-b�	(�	(�)�E�6�3�	��/��y�A�A�E��-�e�T�]�C�C�D�!&�t��f�T�l�C��I�3�6�E��;�v��):�:�;�;�A�>�D�!&�t��f�T�l�C��I�3�6�E��&�u�f�c�4�?�K�K�D��/�$�-�-�/�/�/�0�D�!�$�K����6�E����u�%�%�%�����'�'�'�'��L�(�(r5�pred_bbox_deltasr��regression_targetsc�V�|�|��\}}tjtj|d�����d}tjtj|d�����d}tj||gd���}|���}tj|d���}tj|d���}tj||||dd���|���z}tj||||��}	|	|fS)a117        Args:118            objectness (Tensor)119            pred_bbox_deltas (Tensor)120            labels (List[Tensor])121            regression_targets (List[Tensor])122 123        Returns:124            objectness_loss (Tensor)125            box_loss (Tensor)126        rrcg�q�q�?�sum)�beta�	reduction)	rur(r�rhri�F�smooth_l1_lossr�� binary_cross_entropy_with_logits)127r.r�r�r�r��sampled_pos_inds�sampled_neg_inds�sampled_inds�box_loss�objectness_losss128          r4�compute_lossz"RegionProposalNetwork.compute_loss+s.��.2�-?�-?��-G�-G�*��*� �;�u�y�1A�q�'I�'I�'I�J�J�1�M�� �;�u�y�1A�q�'I�'I�'I�J�J�1�M���y�"2�4D�!E�1�M�M�M���'�'�)�)�129���6�q�)�)�)��"�Y�'9�q�A�A�A���#��-�.��/�0���	130�131�132�133�
�
�
!�
!�#���<�Z��=U�W]�^j�Wk�l�l����(�(r5�images�featuresc���t|�����}|�|��\}}|�||��}t	|��}d�|D��}d�|D��}	t||��\}}|j�|���|��}134|135�	|dd��}136|�137|138||j|	��\}}i}
|jre|�td���|�||��\}}|j�||��}|�||||��\}}||d�}
||
fS)a=139        Args:140            images (ImageList): images for which we want to compute the predictions141            features (Dict[str, Tensor]): features computed from the images that are142                used for computing the predictions. Each tensor in the list143                correspond to different feature levels144            targets (List[Dict[str, Tensor]]): ground-truth boxes present in the image (optional).145                If provided, each element in the dict should contain a field `boxes`,146                with the locations of the ground-truth boxes.147 148        Returns:149            boxes (List[Tensor]): the predicted boxes from the RPN, one Tensor per150                image.151            losses (Dict[str, Tensor]): the losses for the model during training. During152                testing, it is an empty dict.153        c�(�g|]}|dj��S)r)rg)r��os  r4r�z1RegionProposalNetwork.forward.<locals>.<listcomp>ks��.N�.N�.N�a�q��t�z�.N�.N�.Nr5c�B�g|]}|d|dz|dz��S)rrr�)r��ss  r4r�z1RegionProposalNetwork.forward.<locals>.<listcomp>ls-�� a� a� a���1���!���q��t�!3� a� a� ar5r[rNztargets should not be None)�loss_objectness�loss_rpn_box_reg)�list�valuesrxrw�lenrprs�decoder�r\r��image_sizesr��154ValueErrorr��encoder�)r.r�r�r�r�r�r�r��#num_anchors_per_level_shape_tensorsr�r�r155r��lossesr�r�r�r�r�s                   r4rLzRegionProposalNetwork.forwardOs���0����)�)�*�*��'+�y�y��':�':�$�156�$��'�'���9�9����\�\�157�.N�.N�:�.N�.N�.N�+� a� a�=`� a� a� a��'C�J�P`�'a�'a�$�158�$��N�)�)�*:�*A�*A�*C�*C�W�M�M�	��N�N�:�r�1�5�5�	��-�-�i��V�EW�Yn�o�o�
��v����=�	��� �!=�>�>�>�'+�'E�'E�g�w�'W�'W�$�F�$�!%��!6�!6�7G��!Q�!Q��04�0A�0A��,�f�6H�1�1�-�O�-�$3�$4���F��f�}�r5)rvrG)rMrNrOrPr�r�r�r��__annotations__rr�Module�floatrRr�strrr}r~rrrr�r�r�r�rrrLrSrTs@r4rrrrqs����������6�'�%�-�"�B���O�&"�#�#�)�#��i�#�159�#��
#�"�#�!�#��C��H�~�#��S�#�X��#��#��#�160�#�#�#�#�#�#�J.�s�.�.�.�.�161/��/�/�/�/�162$(��F�|�$(�.2�4��V��3D�.E�$(�	�t�F�|�T�&�\�)�	*�$(�$(�$(�$(�L	#��	#��S�	�	#�V\�	#�	#�	#�	#�7)��7)��7)��5��c��?�+�	7)�163 $�C�y�7)�164�t�F�|�T�&�\�)�	*�
7)�7)�7)�7)�r")� �")�4:�")�DH��L�")�fj�kq�fr�")�	�v�v�~�	�")�")�")�")�P6:�	2�2��2��s�F�{�#�2��$�t�C��K�0�1�2�	2�165166�t�F�|�T�#�v�+�.�.�	/�2�2�2�2�2�2�2�2r5rr)�typingrrrrr(rr�torch.nnr	r��torchvision.opsr167r�r�r
r��anchor_utilsr�168image_listrr�rrRr_rprrr�r5r4�<module>rs���.�.�.�.�.�.�.�.�.�.�.�.�������������$�$�$�$�$�$�B�B�B�B�B�B�B�B�!�!�!�!�!�!�*�)�)�)�)�)�!�!�!�!�!�!�? �? �? �? �? �b�i�? �? �? �D�v��#��#��#��#��#��RX�����#�$�v�,�#��V��#�Y^�_e�gm�_m�Yn�#�#�#�#�2P�P�P�P�P�E�H�O�P�P�P�P�Pr5