Ë
    ëÓ^j€  ã                   ó   — d Z ddlZddlmc mZ ddlmZ dej                  dej                  fd„Z	dej                  dej                  fd„Z
dej                  d	ej                  deej                  ej                  f   fd
„Zdej                  d	ej                  dej                  fd„Zdej                  dej                  fd„Zdej                  dej                  dej                  fd„Zej                   j#                  e«      Zdej                  dej                  dej                  fd„Zej                   j#                  e«      Zy)z1Utilities for bounding box manipulation and GIoU.é    N)Úbox_areaÚxÚreturnc                 ó  — | j                  d«      \  }}}}|d|j                  d¬«      z  z
  |d|j                  d¬«      z  z
  |d|j                  d¬«      z  z   |d|j                  d¬«      z  z   g}t        j                  |d¬«      S )Néÿÿÿÿg      à?g        ©Úmin©Údim)ÚunbindÚclampÚtorchÚstack)r   Úx_cÚy_cÚwÚhÚbs         úc/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/rfdetr/utilities/box_ops.pyÚbox_cxcywh_to_xyxyr      s‘   € Ø—X‘X˜b“\�N€Cˆˆa�à	ˆs�Q—W‘W �WÓ%Ñ%Ñ	%Ø	ˆs�Q—W‘W �WÓ%Ñ%Ñ	%Ø	ˆs�Q—W‘W �WÓ%Ñ%Ñ	%Ø	ˆs�Q—W‘W �WÓ%Ñ%Ñ	%ð		€Aô �;‰;�q˜bÔ!Ð!ó    c                 óŒ   — | j                  d«      \  }}}}||z   dz  ||z   dz  ||z
  ||z
  g}t        j                  |d¬«      S )Nr   é   r
   )r   r   r   )r   Úx0Úy0Úx1Úy1r   s         r   Úbox_xyxy_to_cxcywhr   #   sN   € Ø—X‘X˜b“\�N€BˆˆB�Ø
ˆr‰'�Q‰˜˜b™ A™¨¨R©°2¸±7Ð<€AÜ�;‰;�q˜bÔ!Ð!r   Úboxes1Úboxes2c                 ó^  — t        | «      }t        |«      }t        j                  | dd…ddd…f   |dd…dd…f   «      }t        j                  | dd…ddd…f   |dd…dd…f   «      }||z
  j	                  d¬«      }|dd…dd…df   |dd…dd…df   z  }|dd…df   |z   |z
  }||z  }	|	|fS )a0  Compute pairwise IoU and union for two sets of boxes.

    Returns:
        iou: the NxM matrix containing the pairwise
            IoU values for every element in boxes1 and boxes2
        union: the NxM matrix containing the pairwise
            union values for every element in boxes1 and boxes2
    Nr   r   r   é   )r   r   Úmaxr	   r   )
r   r    Úarea1Úarea2ÚltÚrbÚwhÚinterÚunionÚious
             r   Úbox_iour,   *   sÆ   € ô �VÓ€EÜ�VÓ€Eä	�‰�6š!˜T 2 A 2˜+Ñ&¨ªq°"°1°"¨u©Ó	6€BÜ	�‰�6š!˜T 1¡2˜+Ñ&¨ªq°!±"¨u©Ó	6€Bà
ˆr‰'�‰˜QˆÓ	€BØŠq’!�Qˆw‰K˜"šQ¢ 1˜W™+Ñ%€Eà’!�T�'‰N˜UÑ" UÑ*€Eà
�%‰-€CØ�ˆ:Ðr   c                 ó6  — t        | |«      \  }}t        j                  | dd…ddd…f   |dd…dd…f   «      }t        j                  | dd…ddd…f   |dd…dd…f   «      }||z
  j	                  d¬«      }|dd…dd…df   |dd…dd…df   z  }|||z
  |z  z
  S )z»Generalized IoU from https://giou.stanford.edu/

    The boxes should be in [x0, y0, x1, y1] format.

    Returns a [N, M] pairwise matrix, where N = len(boxes1) and M = len(boxes2).
    Nr   r   r   r"   )r,   r   r	   r#   r   )r   r    r+   r*   r&   r'   r(   Úareas           r   Úgeneralized_box_iour/   B   s¬   € ô ˜ Ó(�J€Cˆä	�‰�6š!˜T 2 A 2˜+Ñ&¨ªq°"°1°"¨u©Ó	6€BÜ	�‰�6š!˜T 1¡2˜+Ñ&¨ªq°!±"¨u©Ó	6€Bà
ˆr‰'�‰˜QˆÓ	€BØŠa’�Aˆg‰;˜šAšq !˜G™Ñ$€Dà�$˜‘, $Ñ&Ñ&Ð&r   Úmasksc                 ó   — | j                  «       dk(  r!t        j                  d| j                  ¬«      S | j                  dd \  }}t        j
                  d|t        j                  | j                  ¬«      }t        j
                  d|t        j                  | j                  ¬«      }t        j                  ||d¬«      \  }}| |j                  d«      z  }|j                  d	«      j                  d
«      d   }|j                  | j                  «        d«      j                  d	«      j                  d
«      d   }| |j                  d«      z  }|j                  d	«      j                  d
«      d   }	|j                  | j                  «        d«      j                  d	«      j                  d
«      d   }
t        j                  ||
||	gd	«      S )zçCompute the bounding boxes around the provided masks.

    The masks should be in format [N, H, W] where N is the number of masks, (H, W) are the spatial dimensions.

    Returns a [N, 4] tensor, with the boxes in xyxy format.
    r   )r   é   )ÚdeviceéþÿÿÿN)Údtyper3   Úij)Úindexingr"   r   g    „×—A)Únumelr   Úzerosr3   ÚshapeÚarangeÚfloat32ÚmeshgridÚ	unsqueezeÚflattenr#   Úmasked_fillÚboolr	   r   )r0   r   r   Úyr   Úx_maskÚx_maxÚx_minÚy_maskÚy_maxÚy_mins              r   Úmasks_to_boxesrI   V   sm  € ð ‡{�{ƒ}˜ÒÜ�{‰{˜6¨%¯,©,Ô7Ð7à�;‰;�r�sÐ�D€A€qä�‰�Q˜¤§¡°u·|±|ÔD€AÜ�‰�Q˜¤§¡°u·|±|ÔD€AÜ�>‰>˜!˜Q¨Ô.�D€A€qà�Q—[‘[ “^Ñ#€FØ�N‰N˜1Ó×!Ñ! "Ó% aÑ(€EØ×Ñ §¡£˜°Ó4×<Ñ<¸QÓ?×CÑCÀBÓGÈÑJ€Eà�Q—[‘[ “^Ñ#€FØ�N‰N˜1Ó×!Ñ! "Ó% aÑ(€EØ×Ñ §¡£˜°Ó4×<Ñ<¸QÓ?×CÑCÀBÓGÈÑJ€Eä�;‰;˜˜u e¨UÐ3°QÓ7Ð7r   ÚinputsÚtargetsc                 óú   — | j                  «       } | j                  d«      } dt        j                  d| |«      z  }| j	                  d«      dd…df   |j	                  d«      ddd…f   z   }d|dz   |dz   z  z
  }|S )ag  Compute the DICE loss, similar to generalized IOU for masks.

    Args:
        inputs: A float tensor of arbitrary shape. The predictions for each example.
        targets: A float tensor with the same shape as inputs. Stores the binary
            classification label for each element in inputs (0 for the negative class and 1 for the positive class).
    r"   r   ú	nc,mc->nmr   N)Úsigmoidr?   r   ÚeinsumÚsum)rJ   rK   Ú	numeratorÚdenominatorÚlosss        r   Úbatch_dice_lossrT   q   s}   € ð �^‰^Ó€FØ�^‰^˜AÓ€FØ”E—L‘L ¨f°gÓ>Ñ>€IØ—*‘*˜R“.¢ D Ñ)¨G¯K©K¸«O¸DÂ!¸GÑ,DÑD€KØ˜i¨!™m°¸a±Ñ@Ñ@€DØ€Kr   c                 ó:  — | j                   d   }t        j                  | t        j                  | «      d¬«      }t        j                  | t        j
                  | «      d¬«      }t        j                  d||«      t        j                  d|d|z
  «      z   }||z  S )a†  Compute sigmoid cross-entropy loss for mask predictions.

    Args:
        inputs: A float tensor of arbitrary shape. The predictions for each example.
        targets: A float tensor with the same shape as inputs. Stores the binary
            classification label for each element in inputs (0 for the negative class and 1 for the positive class).

    Returns:
        Loss tensor.
    r"   Únone)Ú	reductionrM   )r:   ÚFÚ binary_cross_entropy_with_logitsr   Ú	ones_likeÚ
zeros_likerO   )rJ   rK   ÚhwÚposÚnegrS   s         r   Úbatch_sigmoid_ce_lossr_   „   s„   € ð 
�‰�a‰€Bä
×
,Ñ
,¨V´U·_±_ÀVÓ5LÐX^Ô
_€CÜ
×
,Ñ
,¨V´U×5EÑ5EÀfÓ5MÐY_Ô
`€Cä�<‰<˜ S¨'Ó2´U·\±\À+ÈsÐUVÐY`ÑU`Ó5bÑb€Dà�"‰9Ðr   )Ú__doc__r   Útorch.nn.functionalÚnnÚ
functionalrX   Útorchvision.ops.boxesr   ÚTensorr   r   Útupler,   r/   rI   rT   ÚjitÚscriptÚbatch_dice_loss_jitr_   Úbatch_sigmoid_ce_loss_jit© r   r   ú<module>rl      s<  ðñ" 8ã ß Ð Ý *ð"˜%Ÿ,™,ð "¨5¯<©<ó "ð"˜%Ÿ,™,ð "¨5¯<©<ó "ð�E—L‘Lð ¨%¯,©,ð ¸5ÀÇÁÈuÏ|É|ÐA[Ñ;\ó ð0' §¡ð '°e·l±lð 'ÀuÇ|Á|ó 'ð(8˜%Ÿ,™,ð 8¨5¯<©<ó 8ð6˜EŸL™Lð °5·<±<ð ÀEÇLÁLó ð  —i‘i×&Ñ& Ó7Ð ð %§,¡,ð ¸¿¹ð È%Ï,É,ó ð* "ŸI™I×,Ñ,Ð-BÓCÑ r   