
    3jh#                        S SK JrJrJr  S SKrSSKJr   SS\\R                     S\\R                     S\\R                     S\\R                     S	\\R                     S
\S\	S\\R                  \\\	4   4   4S jjr
g)    )DictListTupleN   )mean_iou_bbox
pred_boxespred_labelspred_scoresgt_boxes	gt_labels	n_classes	thresholdreturnc           	         [        U 5      [        U5      s=:X  a5  [        U5      s=:X  a%  [        U5      s=:X  a  [        U5      :X  d   [        e  [        e/ n[        U5       H)  u  pUR                  U/U	R	                  S5      -  5        M+     [
        R                  " US5      n
[
        R                  " US5      n[
        R                  " XzR                  [
        R                  S9nUR	                  S5      U
R	                  S5      s=:X  a  UR	                  S5      :X  d   [        e  [        e/ n[        U5       H)  u  pUR                  U/U	R	                  S5      -  5        M+     [
        R                  " U S5      n[
        R                  " US5      n[
        R                  " US5      n[
        R                  " XR                  [
        R                  S9nUR	                  S5      UR	                  S5      s=:X  a1  UR	                  S5      s=:X  a  UR	                  S5      :X  d   [        e  [        e[
        R                  " US-
  UR                  UR                  S9n[        SU5       GH   nXU:H     nXU:H     n[
        R                  " UR	                  S5      [
        R                  UR                  S9nUUU:H     nXU:H     nUUU:H     nUR	                  S5      nUS:X  a  M  [
        R                  " USSS9u  nnUU   nUU   n[
        R                  " U4UR                  UR                  S9n[
        R                  " U4UR                  UR                  S9n[        U5       H  nUU   R                  S5      nUU   n UUU :H     n!U!R	                  S5      S:X  a  SUU'   M@  [!        UU!5      n"[
        R"                  " U"R%                  S5      SS9u  n#n$[
        R                  " [        UR	                  S5      5      UR                  [
        R                  S9UU :H     U$   n%U#R'                  5       U:  a  UU%   S:X  a  SUU'   SUU%'   M  SUU'   M  SUU'   M     [
        R(                  " USS9n&[
        R(                  " USS9n'U&U&U'-   S-   -  n(U&U
R	                  S5      -  n)[
        R*                  " SS	S
S9R-                  5       n*[
        R                  " [        U*5      U
R                  U
R                  S9n+[        U*5       H=  u  nn,U)U,:  n-U-R/                  5       (       a  U(U-   R#                  5       U+U'   M8  SU+U'   M?     U+R1                  5       UUS-
  '   GM     UR1                  5       n.[        UR-                  5       5       VV/s0 s H  u  nn/US-   [3        U/5      _M     n0nn/U.U04$ s  sn/nf )a)  Calculate the Mean Average Precision (mAP) of detected objects.

Code altered from https://github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection/blob/master/utils.py#L271.
Background class (0 index) is excluded.

Args:
    pred_boxes: a torch.Tensor list of predicted bounding boxes.
    pred_labels: a torch.Tensor list of predicted labels.
    pred_scores: a torch.Tensor list of predicted labels' scores.
    gt_boxes: a torch.Tensor list of ground truth bounding boxes.
    gt_labels: a torch.Tensor list of ground truth labels.
    n_classes: the number of classes.
    threshold: count as a positive if the overlap is greater than the threshold.

Returns:
    mean average precision (mAP), list of average precisions for each class.

Examples:
    >>> boxes, labels, scores = torch.tensor([[100, 50, 150, 100.]]), torch.tensor([1]), torch.tensor([.7])
    >>> gt_boxes, gt_labels = torch.tensor([[100, 50, 150, 100.]]), torch.tensor([1])
    >>> mean_average_precision([boxes], [labels], [scores], [gt_boxes], [gt_labels], 2)
    (tensor(1.), {1: 1.0})

r   )devicedtyper   )r   r   T)dim
descending)r   g|=g?g?)startendstepg        )lenAssertionError	enumerateextendsizetorchcattensorr   longzerosr   rangeuint8sort	unsqueezer   maxsqueezeitemcumsumarangetolistanymeanfloat)1r   r	   r
   r   r   r   r   	gt_imagesilabels	_gt_boxes
_gt_labels
_gt_imagespred_images_pred_boxes_pred_labels_pred_scores_pred_imagesaverage_precisionscgt_class_imagesgt_class_boxesgt_class_boxes_detectedpred_class_imagespred_class_boxespred_class_scoresn_class_detectionssort_indgt_positivesfalse_positivesdthis_detection_box
this_imageobject_boxesoverlapsmax_overlapindoriginal_indcumul_gt_positivescumul_false_positivescumul_precisioncumul_recallrecall_thresholds
precisionstrecalls_above_tmean_apvap_dicts1                                                    _/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/metrics/mean_average_precision.pymean_average_precisionrZ      s   D z?c+.e#k2Bec(meWZ[dWee f Iy)	!v{{1~-. * 		(A&I9a(Ji0@0@

SJ??1!2Hjooa6HH I K{+	A3Q/0 ,))J*K99[!,L99[!,L<<4F4FejjYLQ;#3#3A#6f,:K:KA:NfR^RcRcdeRff g 	Q 2 2+:K:K 1i $1_5"?3 #(++!!!$U[[AWAW#

 )):;&q'89():;-2215" ',jj1BVZ&[#8-h7+H5 {{!)9)?)?HXH_H_
  ++!)9)?)?HXH_H_
 )*A!1!!4!>!>q!A*1-J */Z*GHL  #q(%&" %%7FH$yy)9)9!)<!DK !<<n))!,-6M6T6T\a\f\f+--02L !I-*<8A=&'LO<=+L9 *+OA& &'"C +H #\\,A> %_! D,!66>
 *INN1,== "LLqcDKKM[[#&7"8)BRBRZcZiZij
/0DAq*a/O""$$ / @ D D F
1 #
1 1 %/OO$51q5!g !l !%%'G ,55G5N5N5P+QR+Q41aq1ueAh+QGRG Ss   (W)g      ?)typingr   r   r   r   mean_iour   Tensorintr.   rZ        rY   <module>ra      s   $ % $  # \U\\"\ell#\ ell#\ 5<< 	\
 ELL!\ \ \ 5<<c5j))*\r`   