CoolFace
Apppublic

Mjolnir65/FasterRCNN

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

2���gS.���ddlZddlmZmZddlZddlmZmZddlmZGd�dej	��Z3Gd�d	ej	��ZdS)4�N)�List�Optional)�nn�Tensor�)�	ImageListc�r��eZdZdZdeejiZ		d�fd�	Zej	ej5d��fdeedeed	ej
d6ej7def8d�Zd	ej
d9ej10fd
�Zdeefd�Zdeeedeeedeefd�Zdedeedeefd�Z�xZS)�AnchorGeneratora�11    Module that generates anchors for a set of feature maps and12    image sizes.13 14    The module support computing anchors at multiple sizes and aspect ratios15    per feature map. This module assumes aspect ratio = height / width for16    each anchor.17 18    sizes and aspect_ratios should have the same number of elements, and it should19    correspond to the number of feature maps.20 21    sizes[i] and aspect_ratios[i] can have an arbitrary number of elements,22    and AnchorGenerator will output a set of sizes[i] * aspect_ratios[i] anchors23    per spatial location for feature map i.24 25    Args:26        sizes (Tuple[Tuple[int]]):27        aspect_ratios (Tuple[Tuple[float]]):28    �cell_anchors�)��i�)��?��?g@c����t�����t|dttf��st	d�|D����}t|dttf��s|ft|��z}|�_|�_�fd�t||��D���_	dS)Nrc3�K�|]}|fV��dS)N�)�.0�ss  �U/home/gaurav/Documents/assignment-3-gaurav-behera/q1/modify/detection/anchor_utils.py�	<genexpr>z+AnchorGenerator.__init__.<locals>.<genexpr>,s$����.�.�1�1�$�.�.�.�.�.�.�c�B��g|]\}}��||����Sr)�generate_anchors)r�size�aspect_ratio�selfs   �r�29<listcomp>z,AnchorGenerator.__init__.<locals>.<listcomp>2s:���30�31�32�:L�$��D�!�!�$��5�5�33�34�35r)36�super�__init__�37isinstance�list�tuple�len�sizes�
aspect_ratios�zipr)rr&r'�	__class__s`  �rr!zAnchorGenerator.__init__#s�����38	���������%��(�T�5�M�2�2�	/��.�.��.�.�.�.�.�E��-��*�T�5�M�:�:�	:�*�,�s�5�z�z�9�M���39�*���40�41�42�43�PS�TY�[h�Pi�Pi�44�45�46����r�cpu�scalesr'�dtype�device�returnc��tj|||���}tj|||���}tj|��}d|z}|dd�df|ddd�fz�d��}|dd�df|ddd�fz�d��}tj||||gd���dz}	|	���S)N�r,r-r�������dim�)�torch�	as_tensor�sqrt�view�stack�round)47rr+r'r,r-�h_ratios�w_ratios�ws�hs�base_anchorss48          rrz AnchorGenerator.generate_anchors:s������u�V�D�D�D����
�U�6�R�R�R�
��:�m�,�,���x�<���q�q�q�$�w��&��q�q�q��/�1�
7�
7��
;�
;���q�q�q�$�w��&��q�q�q��/�1�
7�
7��
;�
;���{�R�C�"��b�"�#5�1�=�=�=��A���!�!�#�#�#rc�<�����fd�|jD��|_dS)Nc�>��g|]}|��������S)r0)�to)r�cell_anchorr-r,s  ��rrz4AnchorGenerator.set_cell_anchors.<locals>.<listcomp>Ms)���m�m�m�K�[�^�^�%��^�G�G�m�m�mr)r)rr,r-s ``r�set_cell_anchorsz AnchorGenerator.set_cell_anchorsLs-����m�m�m�m�m�[_�[l�m�m�m����rc�J�d�t|j|j��D��S)Nc�R�g|]$\}}t|��t|��z��%Sr�r%)rr�as   rrz<AnchorGenerator.num_anchors_per_location.<locals>.<listcomp>Ps,��P�P�P�D�A�q��A����Q����P�P�Pr)r(r&r'�rs r�num_anchors_per_locationz(AnchorGenerator.num_anchors_per_locationOs%��P�P�C��49�D�<N�,O�,O�P�P�P�Pr�50grid_sizes�stridesc	�,�g}|j}tj|dud��tjt|��t|��cxkot|��kncd��t	|||��D�]\}}}|\}}	|\}51}|j}tjd|	tj|���|z}
tjd|tj|���|52z}tj||
d���\}}|�	d��}|�	d��}tj53||||fd�	��}|�|�ddd54��|�ddd55��z�	dd56������|S)Nzcell_anchors should not be Nonez�Anchors should be Tuple[Tuple[int]] because each feature map could potentially have different sizes and aspect ratios. There needs to be a match between the number of feature maps passed and the number of sizes / aspect ratios specified.rr0�ij��indexingr1rr2�)
rr5�_assertr%r(r-�arange�int32�meshgrid�reshaper9�appendr8)rrKrL�anchorsrr�strider?�grid_height�57grid_width�
stride_height�stride_widthr-�shifts_x�shifts_y�shift_y�shift_x�shiftss                  r�grid_anchorszAnchorGenerator.grid_anchorsTs������(��
�
�l�$�.�0Q�R�R�R�
�
��58�O�O�s�7�|�|�@�@�@�@�s�<�/@�/@�@�@�@�@�
U�	59�	60�	61�+.�j�'�<�*P�*P�	a�	a�&�D�&�,�&*�#�K��*0�'�M�<�!�(�F��|�A�z���V�T�T�T�Wc�c�H��|�A�{�%�+�f�U�U�U�Xe�e�H�$�~�h��4�P�P�P��G�W��o�o�b�)�)�G��o�o�b�)�)�G��[�'�7�G�W�!E�1�M�M�M�F�
�N�N�F�K�K��A�q�1�1�L�4E�4E�a��Q�4O�4O�O�X�X�Y[�]^�_�_�`�`�`�`��r�62image_list�feature_mapsc��63��d�|D��}|jjdd��|dj|djc}�64�65�fd�|D��}|�|�66��|�||��}g}t
t|j����D]#}d�|D��}	|�	|	���$d�|D��}|S)Nc�,�g|]}|jdd���S������N��shape�r�feature_maps  rrz+AnchorGenerator.forward.<locals>.<listcomp>t�$��M�M�M��k�'����,�M�M�Mrrirc���g|]�}tjdtj������d|dz��tjdtj������d|dz��g���S)rr0rr)r5�empty�int64�fill_)r�gr-�67image_sizes  ��rrz+AnchorGenerator.forward.<locals>.<listcomp>ws����68�69�70�71���B�e�k�&�A�A�A�G�G�72�ST�
�YZ�[\�Y]�H]�^�^���B�e�k�&�A�A�A�G�G�73�ST�
�YZ�[\�Y]�H]�^�^�
�74�75�76rc��g|]}|��Srr)r�anchors_per_feature_maps  rrz+AnchorGenerator.forward.<locals>.<listcomp>�s��u�u�u�<S� 7�u�u�urc�6�g|]}tj|����Sr)r5�cat)r�anchors_per_images  rrz+AnchorGenerator.forward.<locals>.<listcomp>�s$��Q�Q�Q�4E�5�9�.�/�/�Q�Q�Qr)77�tensorsrkr,r-rDrc�ranger%�image_sizesrW)rrdrerKr,rL�anchors_over_all_feature_mapsrX�_�anchors_in_imager-rts          @@r�forwardzAnchorGenerator.forwardss����M�M��M�M�M�78��'�-�b�c�c�2�79�$�Q��-�|�A��/E�
��v�80�81�82�83�84�85 �86�87�88��	
���e�V�,�,�,�(,�(9�(9�*�g�(N�(N�%�,.���s�:�1�2�2�3�3�	-�	-�A�u�u�Wt�u�u�u���N�N�+�,�,�,�,�Q�Q��Q�Q�Q���r)rr)�__name__�89__module__�__qualname__�__doc__rr5r�__annotations__r!�float32r-�int�floatr,rrDrJrcrr��
__classcell__�r)s@rr90r9192s����������*	��U�\�*��O�!�(�93�94�95�96�97�98�6#�]�+�u�|�E�2�2�$�$��S�	�$��E�{�$��{�	$�99��$�100�
$�$�$�$�$n�e�k�n�5�<�n�n�n�n�Q�$�s�)�Q�Q�Q�Q�101�t�D��I����d�6�l�AS��X\�]c�Xd�����>�)��4��<��D�QW�L��������rr102c
���eZdZdZ					ddeeededed	eeed103eeedef�fd�
Z	e104je105jd
��fdede106j
de107jdeefd�Zdeefd�Ze108jfdeeedeede109j
defd�Zdefd�Zdedeedeefd�Z�xZS)�DefaultBoxGeneratora�110    This module generates the default boxes of SSD for a set of feature maps and image sizes.111 112    Args:113        aspect_ratios (List[List[int]]): A list with all the aspect ratios used in each feature map.114        min_ratio (float): The minimum scale :math:`	ext{s}_{	ext{min}}` of the default boxes used in the estimation115            of the scales of each feature map. It is used only if the ``scales`` parameter is not provided.116        max_ratio (float): The maximum scale :math:`	ext{s}_{	ext{max}}`  of the default boxes used in the estimation117            of the scales of each feature map. It is used only if the ``scales`` parameter is not provided.118        scales (List[float]], optional): The scales of the default boxes. If not provided it will be estimated using119            the ``min_ratio`` and ``max_ratio`` parameters.120        steps (List[int]], optional): It's a hyper-parameter that affects the tiling of default boxes. If not provided121            it will be estimated from the data.122        clip (bool): Whether the standardized values of default boxes should be clipped between 0 and 1. The clipping123            is applied while the boxes are encoded in format ``(cx, cy, w, h)``.124    �333333�?��������?NTr'�	min_ratio�	max_ratior+�steps�clipc�������t�����|�/t|��t|��krtd���||_||_||_t|���|�R�dkrB|�z125����fd�t���D��|_|j�	d��n�|g|_n||_|�126���|_dS)Nz3aspect_ratios and steps should have the same lengthrc�,��g|]}��|z�dz127zz��S)rr)r�kr��num_outputs�range_ratios  ���rrz0DefaultBoxGenerator.__init__.<locals>.<listcomp>�s-���m�m�m�UV�y�;��?�k�C�>O�+P�P�m�m�mrr)r r!r%�128ValueErrorr'r�r�r{r+rW�_generate_wh_pairs�	_wh_pairs)129rr'r�r�r+r�r�r�r�r)s130  `    @@�rr!zDefaultBoxGenerator.__init__�s�������	�����������]�!3�!3�s�5�z�z�!A�!A��R�S�S�S�*�����131���	��-�(�(���>��Q���'�)�3��m�m�m�m�m�m�Z_�`k�Zl�Zl�m�m�m�����"�"�3�'�'�'�'�(�)�4���� �D�K��0�0��=�=����rr*r�r,r-r.c���g}t|��D]�}|j|}tj|j||j|dzz��}||g||gg}|j|D]Q}	tj|	��}132|j||133z}|j||134z}|�||g||gg���R|�tj|||�������|S)Nrr0)	r{r+�mathr7r'�extendrWr5r6)
rr�r,r-r�r��s_k�	s_prime_k�wh_pairs�ar�sq_ar�w�hs
             rr�z&DefaultBoxGenerator._generate_wh_pairs�s���#%�	��{�#�#�
	T�
	T�A��+�a�.�C��	�$�+�a�.�4�;�q�1�u�3E�"E�F�F�I��c�135�Y�	�$:�;�H��(��+�
2�
2���	�"�
�
���K��N�U�*���K��N�U�*�����!�Q��!�Q�� 0�1�1�1�1����U�_�X�U�6�R�R�R�S�S�S�S��rc�$�d�|jD��S)Nc�8�g|]}ddt|��zz��S)r4rG)r�rs  rrz@DefaultBoxGenerator.num_anchors_per_location.<locals>.<listcomp>�s&��;�;�;�1��A��A���J��;�;�;r)r'rIs rrJz,DefaultBoxGenerator.num_anchors_per_location�s��;�;��(:�;�;�;�;rrKrtc���g}t|��D�]�\}}|j�-|d|j|z}|d|j|z}n|\}}tjd|d��dz|z�|���}	tjd|d��dz|z�|���}136tj|137|	d���\}}|�d��}|�d��}tj||ft|j	|��zd����dd	��}
|j138r"|j	|�dd�139��n|j	|}|�|d|dzd��}tj
|
|fd���}|�|�����tj
|d���S)Nrrr�r,rNrOr1r2r4)�min�max)�	enumerater�r5rSrBrUrVr9r%r�r��clamp�repeatrxrW)rrKrtr,�
default_boxesr��f_k�x_f_k�y_f_kr^r_r`rarb�_wh_pairr��default_boxs                 r�_grid_default_boxesz'DefaultBoxGenerator._grid_default_boxes�s����
��140�+�+�	.�	.�F�A�s��z�%�"�1�
��141�1�
�5��"�1�
��142�1�
�5���"���u���a��Q��0�0�3�6�%�?�C�C�%�C�P�P�H���a��Q��0�0�3�6�%�?�C�C�%�C�P�P�H�$�~�h��4�P�P�P��G�W��o�o�b�)�)�G��o�o�b�)�)�G��[�'�7�!3�c�$�.��:K�6L�6L�!L�RT�U�U�U�]�]�^`�bc�d�d�F�@D�	�`�t�~�a�(�.�.�1�!�.�<�<�<�t�~�^_�O`�H�����A���Q���!�<�<�H��)�V�X�$6�A�>�>�>�K�� � ��-�-�-�-��y��A�.�.�.�.rc143�d�|jj�d|j�d|j�d|j�d|j�d�144}|S)Nz(aspect_ratios=z, clip=z	, scales=z, steps=�))r)r�r'r�r+r�)rrs  r�__repr__zDefaultBoxGenerator.__repr__�sf���~�&�
�
�!�/�
�
��i�
�
���
�
��z�	
�
�
�	145��rrdrec146��d�|D��}|jjdd�}|dj|dj}}|�|||���}|�|��}g}t
j|d|dg|j���}	|jD]r}147|}t
j	|dd�dd�fd|dd�dd�fzz148|	z|dd�dd�fd|dd�dd�fzz|	zgd	��}|�149|���s|S)150Nc�,�g|]}|jdd���Srhrjrls  rrz/DefaultBoxGenerator.forward.<locals>.<listcomp>�rnrrirr�r)r-r4rr1)rzrkr,r-r�rBr5�tensorr|rxrW)rrdrerKrtr,r-r��dboxes�x_y_sizer~�dboxes_in_images            rr�zDefaultBoxGenerator.forward�sL��M�M��M�M�M�151��'�-�b�c�c�2�152�$�Q��-�|�A��/E�v���0�0��Z�u�0�U�U�
�%�(�(��0�0�
����<��A��153�1�
� >�}�G[�\�\�\���'�		+�		+�A�+�O�#�i�$�Q�Q�Q����U�+�c�O�A�A�A�q�r�r�E�4J�.J�J�h�V�$�Q�Q�Q����U�+�c�O�A�A�A�q�r�r�E�4J�.J�J�h�V�����O�
�M�M�/�*�*�*�*��
r)r�r�NNT)r�r�r�r�rr�r�r�boolr!r5r�r-r,rr�rJr��strr�rr�r�r�s@rr�r��s����������( ��(,�%)��>�>��D��I��>��>��	>�154��e��%�>���S�	�"�
>��>�>�>�>�>�>�>6;�]�[g�[`�[g�hm�[n�[n�����',�{��LQ�L��	
�f������(<�$�s�)�<�<�<�<�X]�Wd�/�/��t�C�y�/�/�7;�C�y�/�IN��/�	�/�/�/�/�:	�#�	�	�	�	��)��4��<��D�QW�L��������rr�)r��typingrrr5rrrdr�Moduler155r�rrr�<module>r�s�������!�!�!�!�!�!�!�!�������������!�!�!�!�!�!�{�{�{�{�{�b�i�{�{�{�|D�D�D�D�D�"�)�D�D�D�D�Dr