
    NgD                     R    d Z ddlZej        j         G d de                      ZdS )a  Matcher interface and Match class.

This module defines the Matcher interface and the Match object. The job of the
matcher is to match row and column indices based on the similarity matrix and
other optional parameters. Each column is matched to at most one row. There
are three possibilities for the matching:

1) match: A column matches a row.
2) no_match: A column does not match any row.
3) ignore: A column that is neither 'match' nor no_match.

The ignore case is regularly encountered in object detection: when an anchor has
a relatively small overlap with a ground-truth box, one neither wants to
consider this box a positive example (match) nor a negative example (no match).

The Match class is used to store the match results and it provides simple apis
to query the results.
    Nc                   p    e Zd ZdZdej        fdZd Zd Zd Z	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd ZdS )MatchzClass to store results from the matcher.

    This class is used to store the results from the matcher. It provides
    convenient methods to query the matching results.
    match_resultsc                     t          |j                  dk    rt          d          |j        t          j        t          j        fvrt          d          || _        dS )a  Constructs a Match object.

        Args:
            match_results: Integer tensor of shape [N] with (1) match_results[i]>=0,
                meaning that column i is matched with row match_results[i].
                (2) match_results[i]=-1, meaning that column i is not matched.
                (3) match_results[i]=-2, meaning that column i is ignored.

        Raises:
            ValueError: if match_results does not have rank 1 or is not an integer int32 scalar tensor
           z match_results should have rank 1z7match_results should be an int32 or int64 scalar tensorN)lenshape
ValueErrordtypetorchint32int64r   )selfr   s     [/var/www/html/ai-engine/env/lib/python3.11/site-packages/effdet/object_detection/matcher.py__init__zMatch.__init__,   s]     }"##q((?@@@u{EK&@@@VWWW*    c                     t          j        | j        dk                                                                              S )zReturns column indices that match to some row.

        The indices returned by this op are always sorted in increasing order.

        Returns:
            column_indices: int32 tensor of shape [K] with column indices.
        r   nonzeror   flattenlongr   s    r   matched_column_indiceszMatch.matched_column_indices>   s4     }T/"455==??DDFFFr   c                     | j         dk    S )zReturns column indices that are matched.

        Returns:
            column_indices: int32 tensor of shape [K] with column indices.
        r   r   r   s    r   matched_column_indicatorzMatch.matched_column_indicatorH   s     !Q&&r   c                 N    |                                                                  S z8Returns number (int32 scalar tensor) of matched columns.)r   numelr   s    r   num_matched_columnszMatch.num_matched_columnsP        **,,22444r   c                     t          j        | j        dk                                                                              S )zReturns column indices that do not match any row.

        The indices returned by this op are always sorted in increasing order.

        Returns:
          column_indices: int32 tensor of shape [K] with column indices.
        r   r   r   s    r   unmatched_column_indiceszMatch.unmatched_column_indicesT   s4     }T/2566>>@@EEGGGr   c                     | j         dk    S )zReturns column indices that are unmatched.

        Returns:
          column_indices: int32 tensor of shape [K] with column indices.
        r   r   r   s    r   unmatched_column_indicatorz Match.unmatched_column_indicator^        !R''r   c                 N    |                                                                  S )z:Returns number (int32 scalar tensor) of unmatched columns.)r$   r    r   s    r   num_unmatched_columnszMatch.num_unmatched_columnsf   s     ,,..44666r   c                     t          j        |                                                                                                           S )zReturns column indices that are ignored (neither Matched nor Unmatched).

        The indices returned by this op are always sorted in increasing order.

        Returns:
          column_indices: int32 tensor of shape [K] with column indices.
        )r   r   ignored_column_indicatorr   r   r   s    r   ignored_column_indiceszMatch.ignored_column_indicesj   s8     }T::<<==EEGGLLNNNr   c                     | j         dk    S )zReturns boolean column indicator where True means the column is ignored.

        Returns:
            column_indicator: boolean vector which is True for all ignored column indices.
        r   r   s    r   r+   zMatch.ignored_column_indicatort   r'   r   c                 N    |                                                                  S r   )r,   r    r   s    r   num_ignored_columnszMatch.num_ignored_columns|   r"   r   c                     t          j        d| j        k                                                                              S )zReturns column indices that are unmatched or ignored.

        The indices returned by this op are always sorted in increasing order.

        Returns:
            column_indices: int32 tensor of shape [K] with column indices.
        r   r   r   s    r   #unmatched_or_ignored_column_indicesz)Match.unmatched_or_ignored_column_indices   s4     }Q!3344<<>>CCEEEr   c                     t          j        | j        d|                                                                                                           S )a  Returns row indices that match some column.

        The indices returned by this op are ordered so as to be in correspondence with the output of
        matched_column_indicator().  For example if self.matched_column_indicator() is [0,2],
        and self.matched_row_indices() is [7, 3], then we know that column 0 was matched to row 7 and
        column 2 was matched to row 3.

        Returns:
            row_indices: int32 tensor of shape [K] with row indices.
        r   )r   gatherr   r   r   r   r   s    r   matched_row_indiceszMatch.matched_row_indices   s?     |D.43N3N3P3PQQYY[[``bbbr   c                 H   t          |t          j                  rt          j        |||gd          }n9t          j        t          j        ||g|j        |j                  |gd          }t          j        | j        dz   d          }t          j	        |d|          }|S )a  Gathers elements from `input_tensor` based on match results.

        For columns that are matched to a row, gathered_tensor[col] is set to input_tensor[match_results[col]].
        For columns that are unmatched, gathered_tensor[col] is set to unmatched_value. Finally, for columns that
        are ignored gathered_tensor[col] is set to ignored_value.

        Note that the input_tensor.shape[1:] must match with unmatched_value.shape
        and ignored_value.shape

        Args:
            input_tensor: Tensor to gather values from.
            unmatched_value: Constant tensor or python scalar value for unmatched columns.
            ignored_value: Constant tensor or python scalar for ignored columns.

        Returns:
            gathered_tensor: A tensor containing values gathered from input_tensor.
                The shape of the gathered tensor is [match_results.shape[0]] + input_tensor.shape[1:].
        r   )dim)r   device   )min)

isinstancer   Tensorcattensorr   r8   clampr   index_select)r   input_tensorunmatched_valueignored_valuegather_indicesgathered_tensors         r   gather_based_on_matchzMatch.gather_based_on_match   s    & mU\22 	& 9m_l%SYZ[[[LL !9m_=\EW`l`sttt&#$& & &L T%7!%;CCC,\1nMMr   N)__name__
__module____qualname____doc__r   r<   r   r   r   r!   r$   r&   r)   r,   r+   r0   r2   r5   rF    r   r   r   r   $   s         +el + + + +$G G G' ' '5 5 5H H H( ( (7 7 7O O O( ( (5 5 5F F Fc c c    r   r   )rJ   r   jitscriptobjectr   rK   r   r   <module>rO      sj    $  N N N N NF N N N N Nr   