
    Ng2                         d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ dZ G d de          ZdS )a  Base target assigner module.

The job of a TargetAssigner is, for a given set of anchors (bounding boxes) and
groundtruth detections (bounding boxes), to assign classification and regression
targets to each anchor as well as weights to each anchor (specifying, e.g.,
which anchors should not contribute to training loss).

It assigns classification/regression targets by performing the following steps:
1) Computing pairwise similarity between anchors and groundtruth boxes using a
  provided RegionSimilarity Calculator
2) Computing a matching based on the similarity matrix using a provided Matcher
3) Assigning regression targets based on the matching and a provided BoxCoder
4) Assigning classification targets based on the matching and groundtruth labels

Note that TargetAssigners only operate on detections from a single
image at a time, so any logic for applying a TargetAssigner to multiple
images must be handled externally.
    N)Optional   )box_list)IouSimilarity)ArgMaxMatcher)Match)BoxList)FasterRcnnBoxCoder	keypointsc                       e Zd ZdZddefdedededede	e         d	e
fd
ZddedefdZdededefdZdej        fdZdefdZdefdZdefdZd ZdS )TargetAssignerzATarget assigner to compute classification and regression targets.g      ?Nsimilarity_calcmatcher	box_codernegative_class_weightunmatched_cls_targetkeypoints_field_namec                 n    || _         || _        || _        || _        ||| _        nd| _        || _        dS )aj  Construct Object Detection Target Assigner.

        Args:
            similarity_calc: a RegionSimilarityCalculator

            matcher: Matcher used to match groundtruth to anchors.

            box_coder: BoxCoder used to encode matching groundtruth boxes with respect to anchors.

            negative_class_weight: classification weight to be associated to negative
                anchors (default: 1.0). The weight must be in [0., 1.].

            unmatched_cls_target: a float32 tensor with shape [d_1, d_2, ..., d_k]
                which is consistent with the classification target for each
                anchor (and can be empty for scalar targets).  This shape must thus be
                compatible with the groundtruth labels that are passed to the "assign"
                function (which have shape [num_gt_boxes, d_1, d_2, ..., d_k]).
                If set to None, unmatched_cls_target is set to be [0] for each anchor.

        Raises:
            ValueError: if similarity_calc is not a RegionSimilarityCalculator or
                if matcher is not a Matcher or if box_coder is not a BoxCoder
        N        )_similarity_calc_matcher
_box_coder_negative_class_weight_unmatched_cls_target_keypoints_field_name)selfr   r   r   r   r   r   s          c/var/www/html/ai-engine/env/lib/python3.11/site-packages/effdet/object_detection/target_assigner.py__init__zTargetAssigner.__init__2   sI    4 !0#&;#+)=D&&)+D&%9"""    anchorsgroundtruth_boxesc                 t   t          |t          j                  st          d          t          |t          j                  st          d          | j                            ||          }| j                            |          }|                     |||          }| 	                    ||          }|||fS )a  Assign classification and regression targets to each anchor.

        For a given set of anchors and groundtruth detections, match anchors
        to groundtruth_boxes and assign classification and regression targets to
        each anchor as well as weights based on the resulting match (specifying,
        e.g., which anchors should not contribute to training loss).

        Anchors that are not matched to anything are given a classification target
        of self._unmatched_cls_target which can be specified via the constructor.

        Args:
            anchors: a BoxList representing N anchors

            groundtruth_boxes: a BoxList representing M groundtruth boxes

            groundtruth_labels:  a tensor of shape [M, d_1, ... d_k]
                with labels for each of the ground_truth boxes. The subshape
                [d_1, ... d_k] can be empty (corresponding to scalar inputs).  When set
                to None, groundtruth_labels assumes a binary problem where all
                ground_truth boxes get a positive label (of 1).

            groundtruth_weights: a float tensor of shape [M] indicating the weight to
                assign to all anchors match to a particular groundtruth box. The weights
                must be in [0., 1.]. If None, all weights are set to 1.

            **params: Additional keyword arguments for specific implementations of the Matcher.

        Returns:
            cls_targets: a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k],
                where the subshape [d_1, ..., d_k] is compatible with groundtruth_labels
                which has shape [num_gt_boxes, d_1, d_2, ... d_k].

            cls_weights: a float32 tensor with shape [num_anchors]

            reg_targets: a float32 tensor with shape [num_anchors, box_code_dimension]

            reg_weights: a float32 tensor with shape [num_anchors]

            match: a matcher.Match object encoding the match between anchors and groundtruth boxes,
                with rows corresponding to groundtruth boxes and columns corresponding to anchors.

        Raises:
            ValueError: if anchors or groundtruth_boxes are not of type box_list.BoxList
        zanchors must be an BoxListz$groundtruth_boxes must be an BoxList)

isinstancer   r	   
ValueErrorr   comparer   match_create_regression_targets_create_classification_targets)	r   r    r!   groundtruth_labelsgroundtruth_weightsmatch_quality_matrixr&   reg_targetscls_targetss	            r   assignzTargetAssigner.assignV   s    Z '8#344 	;9:::+X-=>> 	ECDDD  $4<<=NPWXX##$89955g?PRWXX99:LeTT K..r   r&   c                    |                                 }t          j        d|          }|                    |                                ||          }t          j        |          }|                    | j                  rs|	                    | j                  }t          j        d|j
        dd         z   |          }	|                    ||	|	          }
|                    | j        |
           | j                            ||          }|                     |                              |j        j
        d         d          }|                                }t          j        |                    d          ||          }|S )aO  Returns a regression target for each anchor.

        Args:
            anchors: a BoxList representing N anchors

            groundtruth_boxes: a BoxList representing M groundtruth_boxes

            match: a matcher.Match object

        Returns:
            reg_targets: a float32 tensor with shape [N, box_code_dimension]
        )r      deviceunmatched_valueignored_value)r   r   Nr   )r2   torchzerosgather_based_on_matchboxesr   r	   	has_fieldr   	get_fieldshape	add_fieldr   encode_default_regression_targetrepeatmatch_resultsmatched_column_indicatorwhere	unsqueeze)r   r    r!   r&   r2   zero_boxmatched_gt_boxesmatched_gt_boxlistgroundtruth_keypointszero_kpmatched_keypointsmatched_reg_targetsunmatched_ignored_reg_targetsmatched_anchors_maskr,   s                  r   r'   z)TargetAssigner._create_regression_targets   s    !!;vf555 66##%%xx 7 Y Y%-.>??&&t'ABB 	X$5$?$?@Z$[$[!k$)>)DQRR)H"HQWXXXG % ; ;%wg !< !W !W(()CEVWWW"o445GQQ(,(G(G(O(O(V(VW\WjWpqrWsuv(w(w%$==??k"6"@"@"C"CEXZwxxr   r2   c                 ^    t          j        d| j                                        |          S )a  Returns the default target for anchors to regress to.

        Default regression targets are set to zero (though in this implementation what
        these targets are set to should not matter as the regression weight of any box
        set to regress to the default target is zero).

        Returns:
            default_target: a float32 tensor with shape [1, box_code_dimension]
        r   r1   )r6   r7   r   	code_size)r   r2   s     r   r?   z)TargetAssigner._default_regression_target   s)     {1do7799&IIIIr   c                 F    |                     || j        | j                  S )a|  Create classification targets for each anchor.

        Assign a classification target of for each anchor to the matching
        groundtruth label that is provided by match.  Anchors that are not matched
        to anything are given the target self._unmatched_cls_target

        Args:
            groundtruth_labels:  a tensor of shape [num_gt_boxes, d_1, ... d_k]
                with labels for each of the ground_truth boxes. The subshape
                [d_1, ... d_k] can be empty (corresponding to scalar labels).
            match: a matcher.Match object that provides a matching between anchors
                and groundtruth boxes.

        Returns:
            a float32 tensor with shape [num_anchors, d_1, d_2 ... d_k], where the
            subshape [d_1, ..., d_k] is compatible with groundtruth_labels which has
            shape [num_gt_boxes, d_1, d_2, ... d_k].
        r3   )r8   r   )r   r)   r&   s      r   r(   z-TargetAssigner._create_classification_targets   s2    & ** 6dF` + b b 	br   c                 2    |                     |dd          S )aq  Set regression weight for each anchor.

        Only positive anchors are set to contribute to the regression loss, so this
        method returns a weight of 1 for every positive anchor and 0 for every
        negative anchor.

        Args:
            match: a matcher.Match object that provides a matching between anchors and groundtruth boxes.
            groundtruth_weights: a float tensor of shape [M] indicating the weight to
                assign to all anchors match to a particular groundtruth box.

        Returns:
            a float32 tensor with shape [num_anchors] representing regression weights.
        r   r5   r4   )r8   r   r&   r*   s      r   _create_regression_weightsz)TargetAssigner._create_regression_weights   s!     **+>bbd*eeer   c                 <    |                     |d| j                  S )ar  Create classification weights for each anchor.

        Positive (matched) anchors are associated with a weight of
        positive_class_weight and negative (unmatched) anchors are associated with
        a weight of negative_class_weight. When anchors are ignored, weights are set
        to zero. By default, both positive/negative weights are set to 1.0,
        but they can be adjusted to handle class imbalance (which is almost always
        the case in object detection).

        Args:
            match: a matcher.Match object that provides a matching between anchors and groundtruth boxes.
            groundtruth_weights: a float tensor of shape [M] indicating the weight to
                assign to all anchors match to a particular groundtruth box.

        Returns:
            a float32 tensor with shape [num_anchors] representing classification weights.
        r   rR   )r8   r   rS   s      r   _create_classification_weightsz-TargetAssigner._create_classification_weights   s.    $ **r4C^ + ` ` 	`r   c                     | j         S )z\Get BoxCoder of this TargetAssigner.

        Returns:
            BoxCoder object.
        )r   )r   s    r   r   zTargetAssigner.box_coder  s     r   )NN)__name__
__module____qualname____doc__KEYPOINTS_FIELD_NAMEr   r   r
   floatr   strr   r	   r.   r   r'   r6   r2   r?   r(   rT   rV   r    r   r   r   r   /   s]       KK 14]a-A": ": ": ":Zl ":(-":KSTY?":'*": ": ": ":HC/ C/g C/' C/ C/ C/ C/J' g ^c    @
J 
J 
J 
J 
Jb b b b b.f f f f f"`E ` ` ` `*    r   r   )r[   r6   typingr    r   region_similarity_calculatorr   argmax_matcherr   r   r   r	   r   r
   r\   objectr   r_   r   r   <module>re      s    $              7 7 7 7 7 7 ) ) ) ) ) )             ) ) ) ) ) )" [ [ [ [ [V [ [ [ [ [r   