
    kh                         d Z ddlZddlmZ defdZdedefd	Zdedefd
Zej                  j                   G d de	             Z
y)zRegion Similarity Calculators for BoxLists.

Region Similarity Calculators compare a pairwise measure of similarity
between the boxes in two BoxLists.
    N   )BoxListboxlistc                     | j                         j                  dd      \  }}}}||z
  j                  d      ||z
  j                  d      z  }|S )zComputes area of boxes.

    Args:
        boxlist: BoxList holding N boxes

    Returns:
        a tensor with shape [N] representing box areas.
       r   dim)boxeschunksqueeze)r   y_minx_miny_maxx_maxouts         p/var/www/teggl/fontify/venv/lib/python3.12/site-packages/effdet/object_detection/region_similarity_calculator.pyarear      sU     ")!6!6qa!6!@E5%5=
!
!!
$'>'>q'A
ACJ    boxlist1boxlist2c                 ,   | j                         j                  dd      \  }}}}|j                         j                  dd      \  }}}}	t        j                  ||j	                               }
t        j
                  ||j	                               }t        j                  |
|z
  d      }t        j                  ||	j	                               }t        j
                  ||j	                               }t        j                  ||z
  d      }||z  S )zCompute pairwise intersection areas between boxes.

    Args:
        boxlist1: BoxList holding N boxes
        boxlist2: BoxList holding M boxes

    Returns:
        a tensor with shape [N, M] representing pairwise intersections
    r   r   r   r   )min)r
   r   torchr   tmaxclamp)r   r   y_min1x_min1y_max1x_max1y_min2x_min2y_max2x_max2all_pairs_min_ymaxall_pairs_max_yminintersect_heightsall_pairs_min_xmaxall_pairs_max_xminintersect_widthss                   r   intersectionr+   &   s     &.^^%5%;%;A1%;%E"FFFF%-^^%5%;%;A1%;%E"FFFF6688:66688:6$69K$KQRS6688:66688:6{{#58J#JPQR///r   c                     t        | |      }t        |       }t        |      }|j                  d      |j                  d      z   |z
  }t        j                  |dk(  t        j
                  |      ||z        S )zComputes pairwise intersection-over-union between box collections.

    Args:
        boxlist1: BoxList holding N boxes
        boxlist2: BoxList holding M boxes

    Returns:
        a tensor with shape [N, M] representing pairwise iou scores.
    r   r   g        )r+   r   	unsqueezer   where
zeros_like)r   r   intersectionsareas1areas2unionss         r   iour4   ;   sp     !84M(^F(^Fa 6#3#3A#66FF;;}+U-=-=m-Lm^dNdeer   c                   &    e Zd ZdZd ZdedefdZy)IouSimilarityzClass to compute similarity based on Intersection over Union (IOU) metric.

    This class computes pairwise similarity between two BoxLists based on IOU.
    c                      y )N )selfs    r   __init__zIouSimilarity.__init__R   s    r   r   r   c                     t        ||      S )a$  Computes matrix of pairwise similarity between BoxLists.

        This op (to be overridden) computes a measure of pairwise similarity between
        the boxes in the given BoxLists. Higher values indicate more similarity.

        Note that this method simply measures similarity and does not explicitly
        perform a matching.

        Args:
            boxlist1: BoxList holding N boxes.
            boxlist2: BoxList holding M boxes.

        Returns:
            a (float32) tensor of shape [N, M] with pairwise similarity score.
        )r4   )r9   r   r   s      r   comparezIouSimilarity.compareU   s      8X&&r   N)__name__
__module____qualname____doc__r:   r   r<   r8   r   r   r6   r6   L   s    ' '7 'r   r6   )r@   r   box_listr   r   r+   r4   jitscriptobjectr6   r8   r   r   <module>rE      sl   
  ' 07 0g 0*f' fW f" 'F ' 'r   