
    ^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 )Ng      ?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    XXb\NCa	sQWWW%%	%	sQWWW%%	%	sQWWW%%	%	sQWWW%%	%		A ;;qb!!    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    XXb\NBB
r'QbAR27<A;;qb!!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!T2A2+&q"1"u	6B	6!T12+&q!"u	6B
r'Q	Bq!QwK"Q1W+%E!T'NU"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 )zGeneralized 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     (JC	6!T2A2+&q"1"u	6B	6!T12+&q!"u	6B
r'Q	BaAg;Aq!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 )zCompute 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      )deviceN)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;;rsDAqQu||DAQu||DA>>!Q.DAqQ[[^#FNN1!!"%a(E4<<Q?CCBGJEQ[[^#FNN1!!"%a(E4<<Q?CCBGJE;;ueU3Q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ELLfg>>I**R.D)GKKOD!G,DDK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

,
,VU__V5LX^
_C

,
,VU5E5Ef5MY_
`C<<S'2U\\+sUVY`U`5bbD"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<< "ELL %,, 5u||A[;\ 0' 'ell 'u|| '(8%,, 85<< 86ELL 5<< ELL   ii&&7 %,,  %,, * "II,,-BC r   