
    v-j                    H   d dl mZ d dlZd dlmZ d dlmZ d dlZd dlm	Z	 d dl
m	c mZ d dlmZmZmZ d dlmZmZmZ d dlmZmZmZmZmZ d dlmZ d	d
lmZmZ d	dl m!Z!m"Z"  G d de	j#                  Z$ G d de	j#                  Z% G d de	j#                  Z& G d de	j#                  Z' G d de	j#                  Z( G d de'          Z) G d de	j#                  Z* G d de	j#                  Z+ G d de	j#                  Z, G d d          Z- G d  d!e-          Z. G d" d#e-          Z/ G d$ d%e/          Z0 G d& d'          Z1 G d( d)e-          Z2 G d* d+          Z3 G d, d-          Z4 G d. d/          Z5 G d0 d1e5          Z6 G d2 d3e	j#                  Z7dS )4    )annotationsN)Path)Any)CITYSCAPES_WEIGHT	OKS_SIGMA
RLE_WEIGHT)	crop_mask	xywh2xyxy	xyxy2xywh)RotatedTaskAlignedAssignerTaskAlignedAssigner	dist2bbox	dist2rboxmake_anchors)autocast   )bbox_iouprobiou)	bbox2dist	rbox2distc                  .     e Zd ZdZdd fdZddZ xZS )VarifocalLossa  Varifocal loss by Zhang et al.

    Implements the Varifocal Loss function for addressing class imbalance in object detection by focusing on
    hard-to-classify examples and balancing positive/negative samples.

    Attributes:
        gamma (float): The focusing parameter that controls how much the loss focuses on hard-to-classify examples.
        alpha (float): The balancing factor used to address class imbalance.

    References:
        https://arxiv.org/abs/2008.13367
           @      ?gammafloatalphac                d    t                                                       || _        || _        dS )zJInitialize the VarifocalLoss class with focusing and balancing parameters.N)super__init__r   r   selfr   r   	__class__s      V/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/ultralytics/utils/loss.pyr    zVarifocalLoss.__init__$   s+    



    
pred_scoretorch.Tensorgt_scorelabelreturnc                   | j         |                                                    | j                  z  d|z
  z  ||z  z   }t	          d          5  t          j        |                                |                                d          |z                      d          	                                }ddd           n# 1 swxY w Y   |S )z<Compute varifocal loss between predictions and ground truth.r   F)enablednone	reductionN)
r   sigmoidpowr   r   F binary_cross_entropy_with_logitsr   meansum)r"   r&   r(   r)   weightlosss         r$   forwardzVarifocalLoss.forward*   s    j002266tzBBBa%iPS[^cScce$$$ 	 	3J4D4D4F4FHXHXdjkkkntta 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 s   A$CCC)r   r   r   r   r   r   )r&   r'   r(   r'   r)   r'   r*   r'   __name__
__module____qualname____doc__r    r8   __classcell__r#   s   @r$   r   r      s`               	 	 	 	 	 	 	 	r%   r   c                  .     e Zd ZdZdd fdZddZ xZS )	FocalLossa  Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5).

    Implements the Focal Loss function for addressing class imbalance by down-weighting easy examples and focusing on
    hard negatives during training.

    Attributes:
        gamma (float): The focusing parameter that controls how much the loss focuses on hard-to-classify examples.
        alpha (torch.Tensor): The balancing factor used to address class imbalance.
          ?      ?r   r   r   c                    t                                                       || _        t          j        |          | _        dS )zBInitialize FocalLoss class with focusing and balancing parameters.N)r   r    r   torchtensorr   r!   s      r$   r    zFocalLoss.__init__A   s5    
\%((


r%   predr'   r)   r*   c                   t          j        ||d          }|                                }||z  d|z
  d|z
  z  z   }d|z
  | j        z  }||z  }| j        dk                                    rK| j                            |j        |j                  | _        || j        z  d|z
  d| j        z
  z  z   }||z  }|	                    d          
                                S )zACalculate focal loss with modulating factors for class imbalance.r-   r.   r         ?r   devicedtype)r2   r3   r0   r   r   anytorL   rM   r4   r5   )r"   rH   r)   r7   	pred_probp_tmodulating_factoralpha_factors           r$   r8   zFocalLoss.forwardG   s    1$PPP
 LLNN	i1u9Y"?? 3Y4:5!!JN!! 	!dkLLDJ 4:-Uq4:~0NNLL Dyy||!!!r%   )rC   rD   r9   )rH   r'   r)   r'   r*   r'   r:   r@   s   @r$   rB   rB   6   s`         ) ) ) ) ) ) )" " " " " " " "r%   rB   c                  .     e Zd ZdZdd fdZddZ xZS )DFLossz<Criterion class for computing Distribution Focal Loss (DFL).   reg_maxintr*   Nonec                V    t                                                       || _        dS )z6Initialize the DFL module with regularization maximum.N)r   r    rW   r"   rW   r#   s     r$   r    zDFLoss.__init__\   s$    r%   	pred_distr'   targetc                   |                     d| j        dz
  dz
            }|                                }|dz   }||z
  }d|z
  }t          j        ||                    d          d                              |j                  |z  t          j        ||                    d          d                              |j                  |z  z                       dd          S )	zZReturn sum of left and right DFL losses from https://ieeexplore.ieee.org/document/9792391.r   r   g{Gz?r-   r.   Tkeepdim)clamp_rW   longr2   cross_entropyviewshaper4   )r"   r\   r]   tltrwlwrs          r$   __call__zDFLoss.__call__a   s    q$,"2T"9::[[]]!V&[VOIrwwr{{fEEEJJ28TTWYYoiGGGLLRXVVY[[\
$r4$
 
 	!r%   rV   )rW   rX   r*   rY   )r\   r'   r]   r'   r*   r'   )r;   r<   r=   r>   r    rk   r?   r@   s   @r$   rU   rU   Y   s\        FF      

! 
! 
! 
! 
! 
! 
! 
!r%   rU   c                  .     e Zd ZdZdd fdZddZ xZS )BboxLosszACriterion class for computing training losses for bounding boxes.rV   rW   rX   c                    t                                                       |dk    rt          |          nd| _        dS )zLInitialize the BboxLoss module with regularization maximum and DFL settings.r   N)r   r    rU   dfl_lossr[   s     r$   r    zBboxLoss.__init__q   s6    +2Q;;wDr%   r\   r'   pred_bboxesanchor_pointstarget_bboxestarget_scorestarget_scores_sumfg_maskimgszstrider*   !tuple[torch.Tensor, torch.Tensor]c
                   |                     d          |                             d          }
t          ||         ||         dd          }d|z
  |
z                                   |z  }| j        ryt	          ||| j        j        dz
            }|                     ||                             d| j        j                  ||                   |
z  }|                                 |z  }nt	          ||          }||	z  }|ddd	d
fxx         |d         z  cc<   |ddd	d
fxx         |d         z  cc<   ||	z  }|ddd	d
fxx         |d         z  cc<   |ddd	d
fxx         |d         z  cc<   t          j        ||         ||         d          	                    dd          |
z  }|                                 |z  }||fS )z.Compute IoU and DFL losses for bounding boxes.r_   FT)xywhCIoUrJ   r   .r   N   r-   r.   r`   )
r5   	unsqueezer   rp   r   rW   re   r2   l1_lossr4   r"   r\   rq   rr   rs   rt   ru   rv   rw   rx   r6   iouloss_ioutarget_ltrbloss_dfls                  r$   r8   zBboxLoss.forwardv   s    ""2&&w/99"=={7+]7-C%VZ[[[3Y&(--//2CC = 	:#M=$-BWZ[B[\\K}}Yw%7%<%<RAV%W%WYdelYmnnqwwH||~~(99HH#M=AAK%.KQTT	"""eAh."""QTT	"""eAh."""!F*Ic14a4i   E!H,   c14a4i   E!H,   	)G,k'.BfUUUZZ[]gkZllouu   ||~~(99H!!r%   rl   rW   rX   r\   r'   rq   r'   rr   r'   rs   r'   rt   r'   ru   r'   rv   r'   rw   r'   rx   r'   r*   ry   r:   r@   s   @r$   rn   rn   n   sc        KKA A A A A A A
$" $" $" $" $" $" $" $"r%   rn   c                  2     e Zd ZdZdd fdZ	 dddZ xZS )RLELossa  Residual Log-Likelihood Estimation Loss.

    Attributes:
        size_average (bool): Option to average the loss by the batch_size.
        use_target_weight (bool): Option to use weighted loss.
        residual (bool): Option to add L1 loss and let the flow learn the residual error distribution.

    References:
        https://arxiv.org/abs/2107.11291
        https://github.com/open-mmlab/mmpose/blob/main/mmpose/models/losses/regression_loss.py
    Tuse_target_weightboolsize_averageresidualc                r    t                                                       || _        || _        || _        dS )aG  Initialize RLELoss with target weight and residual options.

        Args:
            use_target_weight (bool): Whether to use target weights for loss calculation.
            size_average (bool): Whether to average the loss over elements.
            residual (bool): Whether to include residual log-likelihood term.
        N)r   r    r   r   r   )r"   r   r   r   r#   s       r$   r    zRLELoss.__init__   s6     	(!2 r%   Nsigmar'   log_phierrortarget_weightr*   c                   t          j        |          }||                    d          z
  }| j        r/|t          j        |dz            t          j        |          z   z  }| j        r>|
J d            |                                dk    r|                    d          }||z  }| j        r|t          |          z  }|	                                S )a&  
        Args:
            sigma (torch.Tensor): Output sigma, shape (N, D).
            log_phi (torch.Tensor): Output log_phi, shape (N).
            error (torch.Tensor): Error, shape (N, D).
            target_weight (torch.Tensor): Weights across different joint types, shape (N).
        r   r}   NzD'target_weight' should not be None when 'use_target_weight' is True.)
rF   logr~   r   absr   dimr   lenr5   )r"   r   r   r   r   	log_sigmar7   s          r$   r8   zRLELoss.forward   s     Ie$$	7,,Q///= 	<EIeai((59U+;+;;;D! 	" ,,.t,,,  ""a'' - 7 7 : :M!D 	CIIDxxzzr%   )TTT)r   r   r   r   r   r   )N)
r   r'   r   r'   r   r'   r   r'   r*   r'   r:   r@   s   @r$   r   r      sl        
 
! ! ! ! ! ! ! nr        r%   r   c                  ,     e Zd ZdZd fdZddZ xZS )RotatedBboxLosszICriterion class for computing training losses for rotated bounding boxes.rW   rX   c                J    t                                          |           dS )zSInitialize the RotatedBboxLoss module with regularization maximum and DFL settings.N)r   r    r[   s     r$   r    zRotatedBboxLoss.__init__   s!    !!!!!r%   r\   r'   rq   rr   rs   rt   ru   rv   rw   rx   r*   ry   c
                   |                     d          |                             d          }
t          ||         ||                   }d|z
  |
z                                   |z  }| j        rt	          |dddf         ||dddf         | j        j        dz
            }|                     ||                             d| j        j                  ||                   |
z  }|                                 |z  }nt	          |dddf         ||dddf                   }||	z  }|dd	dd
fxx         |d         z  cc<   |dddd
fxx         |d	         z  cc<   ||	z  }|dd	dd
fxx         |d         z  cc<   |dddd
fxx         |d	         z  cc<   t          j        ||         ||         d          	                    dd          |
z  }|                                 |z  }||fS )z6Compute IoU and DFL losses for rotated bounding boxes.r_   rJ   .N      r   )rW   r   r}   r-   r.   Tr`   )
r5   r~   r   rp   r   rW   re   r2   r   r4   r   s                  r$   r8   zRotatedBboxLoss.forward   sN    ""2&&w/99"==k'*M',BCC3Y&(--//2CC = 	:#c2A2g&}S!A#X7NX\XeXmpqXq  K }}Yw%7%<%<RAV%W%WYdelYmnnqwwH||~~(99HH#M#rr'$:M=Y\^_`a^aYaKbccK%.KQTT	"""eAh."""QTT	"""eAh."""!F*Ic14a4i   E!H,   c14a4i   E!H,   	)G,k'.BfUUUZZ[]gkZllouu   ||~~(99H!!r%   r   r   r:   r@   s   @r$   r   r      sW        SS" " " " " "%" %" %" %" %" %" %" %"r%   r   c                  .     e Zd ZdZdd fdZddZ xZS )MultiChannelDiceLossz8Criterion class for computing multi-channel Dice losses.ư>r4   smoothr   r/   strc                d    t                                                       || _        || _        dS )zInitialize MultiChannelDiceLoss with smoothing and reduction options.

        Args:
            smooth (float): Smoothing factor to avoid division by zero.
            reduction (str): Reduction method ('mean', 'sum', or 'none').
        N)r   r    r   r/   )r"   r   r/   r#   s      r$   r    zMultiChannelDiceLoss.__init__  s-     	"r%   rH   r'   r]   r*   c                   |                                 |                                 k    s
J d            |                                }||z                      d          }|                    d          |                    d          z   }d|z  | j        z   || j        z   z  }d|z
  }|                    d          }| j        dk    r|                                S | j        dk    r|                                S |S )	zBCalculate multi-channel Dice loss between predictions and targets.z-the size of predict and target must be equal.)r}      r   r   rJ   r   r4   r5   )sizer0   r5   r   r4   r/   )r"   rH   r]   intersectionuniondice	dice_losss          r$   r8   zMultiChannelDiceLoss.forward  s    yy{{fkkmm+++-\+++||~~v**v*66V$$vzzfz'='==l"T[0UT[5HI$J	NNqN))	>V##>>###^u$$==??"r%   )r   r4   )r   r   r/   r   rH   r'   r]   r'   r*   r'   r:   r@   s   @r$   r   r     s\        BB	# 	# 	# 	# 	# 	# 	#       r%   r   c                  .     e Zd ZdZdd fdZddZ xZS )BCEDiceLossz;Criterion class for computing combined BCE and Dice losses.      ?
weight_bcer   weight_dicec                    t                                                       || _        || _        t	          j                    | _        t          d          | _        dS )zInitialize BCEDiceLoss with BCE and Dice weight factors.

        Args:
            weight_bce (float): Weight factor for BCE loss component.
            weight_dice (float): Weight factor for Dice loss component.
        r   )r   N)	r   r    r   r   nnBCEWithLogitsLossbcer   r   )r"   r   r   r#   s      r$   r    zBCEDiceLoss.__init__&  sP     	$&'))(222			r%   rH   r'   r]   r*   c                
   |j         \  }}}}t          |j         dd                   ||fk    rt          j        |||fd          }| j        |                     ||          z  | j        |                     ||          z  z   S )zECalculate combined BCE and Dice loss between predictions and targets.Nnearest)mode)rf   tupler2   interpolater   r   r   r   )r"   rH   r]   _mask_hmask_ws         r$   r8   zBCEDiceLoss.forward3  s    #z1ffbcc"##'777]6FF+;)LLLF$!7!77$:JTYYW[]cMdMd:dddr%   )r   r   )r   r   r   r   r   r:   r@   s   @r$   r   r   #  sd        EE3 3 3 3 3 3 3e e e e e e e er%   r   c                  ,     e Zd ZdZd fdZddZ xZS )KeypointLossz.Criterion class for computing keypoint losses.sigmasr'   r*   rY   c                V    t                                                       || _        dS )z7Initialize the KeypointLoss class with keypoint sigmas.N)r   r    r   )r"   r   r#   s     r$   r    zKeypointLoss.__init__>  s$    r%   	pred_kptsgt_kptskpt_maskareac                   |d         |d         z
                       d          |d         |d         z
                       d          z   }|j        d         t          j        |dk    d          dz   z  }|d| j        z                       d          |dz   z  dz  z  }|                    dd          dt          j        |           z
  |z  z                                  S )	zICalculate keypoint loss factor and Euclidean distance loss for keypoints..r   r}   .r   r   r   r   &.>r_   )r1   rf   rF   r5   r   re   expr4   )r"   r   r   r   r   dkpt_loss_factores           r$   r8   zKeypointLoss.forwardC  s     v055a88If<MPWX^P_<_;d;def;g;gg".+uyQA/N/N/NQU/UV!dk/&&q))TD[9A=>$$R++EIqbMM0AX/MNTTVVVr%   )r   r'   r*   rY   )
r   r'   r   r'   r   r'   r   r'   r*   r'   r:   r@   s   @r$   r   r   ;  s_        88     
W W W W W W W Wr%   r   c                  N    e Zd ZdZ	 dd d
Zd!dZd"dZd#dZd$dZd%dZ	d&dZ
dS )'v8DetectionLosszJCriterion class for computing training losses for YOLOv8 object detection.
   Nmodeltorch.nn.Moduletal_topkrX   	tal_topk2
int | Nonec                   t          |                                          j        }|j        }|j        d         }t          j        d          | _        || _        |j	        | _	        |j
        | _
        |j
        |j        dz  z   | _        |j        | _        || _        |j        dk    | _        t          |dd          | _        | j        4| j                            |                              ddd          | _        t%          || j
        dd	| j	                                        |
          | _        t+          |j                                      |          | _        t/          j        |j        t.          j        |          | _        dS )zVInitialize v8DetectionLoss with model parameters and task-aligned assignment settings.r_   r-   r.   r   r   class_weightsNr         @topknum_classesr   betarx   topk2rM   rL   )next
parametersrL   argsr   r   r   r   hyprx   ncrW   nouse_dflgetattrr   rO   re   r   tolistassignerrn   	bbox_lossrF   aranger   proj)r"   r   r   r   rL   hms          r$   r    zv8DetectionLoss.__init__Q  sT    e&&(())0JKO'&999h$$Q&yy1} %UOTBB)!%!3!6!6v!>!>!C!CAq"!M!MD+;%%''
 
 
 "!),,//77L%+fMMM			r%   targetsr'   
batch_sizescale_tensorr*   c                $   |j         \  }}|dk    r"t          j        |d|dz
  | j                  }n]|dddf                                         }|                    d          \  }}	|	                    t          j                  }	t          j        ||	                                |dz
  | j                  }t          j        |dz   t          j        | j                  }
|
	                    d|dz   t          j
        |                     |
                    d          }
t          j        || j                  |
|         z
  }|ddddf         |||f<   t          |d	dd
f                             |                    |d	dd
f<   |S )zJPreprocess targets by converting to tensor format and scaling coordinates.r   r   rL   NTreturn_countsrM   r   .r   )rf   rF   zerosrL   rc   uniquerO   int32maxscatter_add_	ones_likecumsumr   r
   mul_)r"   r   r   r   nlneout	batch_idxr   countsoffsets
within_idxs               r$   
preprocesszv8DetectionLoss.preprocesss  st   B77+j!R!VDKHHHCC1**,,I!((t(<<IAvYYU[Y11F+j&**,,Qt{SSSCk*q.
4;WWWG  IM5?93M3MNNNnnQ''Gb===	@RRJ)0ABBC	:%&%c#qs(m&8&8&F&FGGCQqSM
r%   rr   r\   c                   | j         ri|j        \  }}}|                    ||d|dz                                d                              | j                            |j                            }t          ||d          S )zUDecode predicted object bounding box coordinates from anchor points and distribution.r   r   F)r{   )	r   rf   re   softmaxmatmulr   typerM   r   )r"   rr   r\   bacs         r$   bbox_decodezv8DetectionLoss.bbox_decode  sz    < 	koGAq!!q!QQ77??BBII$)..YbYhJiJijjI M>>>>r%   predsdict[str, torch.Tensor]batchdict[str, Any]r   c                   t          j        d| j                  }|d                             ddd                                          |d                             ddd                                          }}t          |d         | j        d	          \  }}|j        }|j        d         }	t          j	        |d         d         j        dd
         | j        |          | j        d         z  }
t          j
        |d                             dd          |d                             dd          |d         fd          }|                     |                    | j                  |	|
g d                   }|                    dd          \  }}|                    dd                              d          }|                     ||          }|                     |                                                                |                                |z                      |j                  ||z  |||          \  }}}}}t-          |                                d          }|                     ||                    |                    }| j        
|| j        z  }|                                |z  |d<   |                                r)|                     |||||z  ||||
|	  	        \  |d<   |d<   |dxx         | j        j        z  cc<   |dxx         | j        j        z  cc<   |dxx         | j        j        z  cc<   |||||f||                                fS )zCalculate the sum of the loss for box, cls and dfl multiplied by batch size and return foreground mask and
        target indices.
        r   r   boxesr   r}   r   scoresfeatsr   NrK   r  r_   clsbboxesr   r   r   r   r   )r   r   Tr`           )rF   r   rL   permute
contiguousr   rx   rM   rf   rG   catre   r  rO   splitr5   gt_r  r   detachr0   r
  r   r   r   r   r   boxr  dfl)r"   r  r  r7   pred_distripred_scoresrr   stride_tensorrM   r   rw   r   	gt_labels	gt_bboxesmask_gtrq   r   rs   rt   rv   target_gt_idxru   bce_losss                          r$   get_assigned_targets_and_lossz-v8DetectionLoss.get_assigned_targets_and_loss  sR    {1T[111'N""1a++6688(O##Aq!,,7799 ! (4E'NDKQT'U'U$}! &q)
U7^A.4QRR8TYZZZ]a]hij]kk )U;/44R;;U5\=N=NrST=U=UW\]eWfgijkk//'**T["9"9:TYZfZfZfTg/hh&}}VQ77	9--4-0044S99 &&}kBBBF--  ((**!!M177	HHM)C
 C
?=--   1 1 3 3Q77 88K)9)9%)@)@AA)**H,,..#44Q ;;== 	#~~-!
  
 DGT!W 	Q48<Q48<Q48<m]M=QKKMM
 	
r%   Fdict[str, torch.Tensor] | tuple[torch.Tensor, dict[str, torch.Tensor]]c                @    t          |t                    r|d         n|S ),Parse model predictions to extract features.r   )
isinstancer   r"   r  s     r$   parse_outputzv8DetectionLoss.parse_output  s!     &eU33>uQxx>r%   ry   c                T    |                      |                     |          |          S )LCalculate the sum of the loss for box, cls and dfl multiplied by batch size.r7   r2  r"   r  r  s      r$   rk   zv8DetectionLoss.__call__  s&     yy**5115999r%   c                x    |d         j         d         }|                     ||          dd         \  }}||z  |fS )z0Calculate detection loss using assigned targets.r  r   r   N)rf   r,  )r"   r  r  r   r7   loss_detachs         r$   r7   zv8DetectionLoss.loss  sF    7^)!,
 >>ueLLQRRPkj +--r%   r   Nr   r   r   rX   r   r   r   r'   r   rX   r   r'   r*   r'   )rr   r'   r\   r'   r*   r'   )r  r  r  r  r*   r   )r  r-  r*   r'   )r  r-  r  r  r*   ry   r  r  r  r  r*   ry   )r;   r<   r=   r>   r    r  r  r,  r2  rk   r7    r%   r$   r   r   N  s        TT SW N  N  N  N  ND   $? ? ? ?>
 >
 >
 >
@? ? ? ?: : : :. . . . . .r%   r   c                  P     e Zd ZdZ	 d d! fd
Zd"dZed#d            Zd$dZ xZ	S )%v8SegmentationLosszFCriterion class for computing training losses for YOLOv8 segmentation.r   Nr   r   r   rX   r   r   c                    t                                          |||           |j        j        | _        t          dd          | _        dS )zWInitialize the v8SegmentationLoss class with model parameters and mask overlap setting.r   )r   r   N)r   r    r   overlap_maskoverlapr   bcedice_lossr"   r   r   r   r#   s       r$   r    zv8SegmentationLoss.__init__  sH     	)444z.'3CHHHr%   r  r  r  r*   ry   c           
        |d                              ddd                                          |d         }}t          j        d| j                  }t          |t                    rt          |          dk    r|\  }}nd}|                     ||          \  \  }}}	}
}
}}
|d         |d         |d         c|d<   |d<   |d	<   |j	        \  }}
}}|
                                rN|d
                             | j                                                  }t          |j	        dd                   ||fk    r%t          j        ||j	        dd         dd          }t          j        |d         d         j	        dd         | j        |j                  | j        d         z  }|                     ||||	|d                             dd          |||          |d<   |P|d                             | j                  }t          j        |                                | j                                       dd	dd                                          }| j        r2|dk    }d||                    d                              |          <   no|d                             d          }t5          |          D ]D}|||k             }t          |          dk    r"d||dd|
                    d          dk    f<   E|                     ||          |d<   |dxx         | j        j        z  cc<   nd|dxx         |dz  
                                |dz  
                                z   z  cc<   |%|dxx         |dz  
                                z  cc<   |dxx         | j        j        z  cc<   ||z  |                                fS )zFCalculate and return the combined loss for detection and segmentation.mask_coefficientr   r}   r   protor   r   Nr   masksr   bilinearF)r   align_cornersr  rK   r  r_   	sem_masks)r   r   r   )r  r  rF   r   rL   r0  r   r   r,  rf   r5   rO   r   r2   r   rG   rM   rx   calculate_segmentation_lossre   one_hotrc   r   rB  r~   	expand_asrangerC  r   r"  r!  )r"   r  r  
pred_masksrG  r7   pred_semanticrv   r*  rs   r   det_lossr   r   r   rH  rw   rK  	mask_zeror  iinstance_mask_is                         r$   r7   zv8SegmentationLoss.loss  s   !"45==aAFFQQSSUZ[bUcE
{1T[111eU## 	!E

a#( E== MEIEgEghmotEuEuB5-1x$,QK!hqk!Qa$q'(-%
Avv;;== *	5'N%%dk2288::EU[%&&66*:::eU[-=J^cddd U7^A.4QRR8T^Tdeeehlhstuhvv  66k"''A..	 	DG (!+.11$+>>	Iinn&6&6DGLLLTTUVXY[\^_``ffhh	< 	M %
IMNIi11!44>>yIIJJ %k 2 7 7 ; ;I":.. M M*/	Q*?//144$KL	!QQQ(;(;(;(B(Ba(G"GHH++M9EEQQ48<' GGG	((JN+?+?+A+AAAGGG(QMA-22444Q48<j $++--//r%   gt_maskr'   rH   rG  xyxyr   c                    t          j        d||          }t          j        || d          }t	          ||                              d          |z                                  S )aO  Compute the instance segmentation loss for a single image.

        Args:
            gt_mask (torch.Tensor): Ground truth mask of shape (N, H, W), where N is the number of objects.
            pred (torch.Tensor): Predicted mask coefficients of shape (N, 32).
            proto (torch.Tensor): Prototype masks of shape (32, H, W).
            xyxy (torch.Tensor): Ground truth bounding boxes in xyxy format, normalized to [0, 1], of shape (N, 4).
            area (torch.Tensor): Area of each ground truth bounding box of shape (N,).

        Returns:
            (torch.Tensor): The calculated mask loss for a single image.

        Notes:
            The function uses the equation pred_mask = torch.einsum('in,nhw->ihw', pred, proto) to produce the
            predicted masks from the prototype masks and predicted mask coefficients.
        zin,nhw->ihwr-   r.   )r   r}   r   )rF   einsumr2   r3   r	   r4   r5   )rV  rH   rG  rW  r   	pred_maskr7   s          r$   single_mask_lossz#v8SegmentationLoss.single_mask_loss*  sa    ( Le<<	1)WPVWWW$%%**v*66=BBDDDr%   rv   rH  r*  rs   r  rP  rw   c	                   |j         \  }	}	}
}d}||g d         z  }t          |          dddf                             d          }|t          j        ||
||
g|j                  z  }t          t          |||||||                    D ]\  }}|\  }}}}}}}|                                r||         }| j	        r3||dz   
                    ddd          k    }|                                }n%||
                    d          |k             |         }||                     |||         |||         ||                   z  }||dz                                  |dz                                  z   z  }||                                z  S )	a  Calculate the loss for instance segmentation.

        Args:
            fg_mask (torch.Tensor): A binary tensor of shape (BS, N_anchors) indicating which anchors are positive.
            masks (torch.Tensor): Ground truth masks of shape (BS, H, W) if `overlap` is False, otherwise (BS, ?, H, W).
            target_gt_idx (torch.Tensor): Indexes of ground truth objects for each anchor of shape (BS, N_anchors).
            target_bboxes (torch.Tensor): Ground truth bounding boxes for each anchor of shape (BS, N_anchors, 4).
            batch_idx (torch.Tensor): Batch indices of shape (N_labels_in_batch, 1).
            proto (torch.Tensor): Prototype masks of shape (BS, 32, H, W).
            pred_masks (torch.Tensor): Predicted masks for each anchor of shape (BS, N_anchors, 32).
            imgsz (torch.Tensor): Size of the input image as a tensor of shape (2), i.e., (H, W).

        Returns:
            (torch.Tensor): The calculated loss for instance segmentation.

        Notes:
            The batch loss can be computed for improved speed at higher memory usage.
            For example, pred_mask can be computed as follows:
                pred_mask = torch.einsum('in,nhw->ihw', pred, proto)  # (i, 32) @ (32, 160, 160) -> (i, 160, 160)
        r   r  .r}   Nr   r   r_   )rf   r   prodrF   rG   rL   	enumerateziprN   rB  re   r   r[  r5   )r"   rv   rH  r*  rs   r  rG  rP  rw   r   r   r   r7   target_bboxes_normalizedmareamxyxyrT  single_i	fg_mask_itarget_gt_idx_ipred_masks_iproto_imxyxy_imarea_imasks_imask_idxrV  s                              r$   rL  z.v8SegmentationLoss.calculate_segmentation_lossB  s   >  %{1ff $153F#F  233CG<AA!DD )5<QW8Xafam+n+n+nn$S-UTY[`bg%h%hii 	C 	CKAx[cXIgwQX}} C*95< G%(Q,)<)<RA)F)FFG%mmooGG#INN2$6$6!$;<XFG--\)4gwy?QSZ[dSe   ))Z!^,@,@,B,BBBgkkmm##r%   r9  r:  r<  )rV  r'   rH   r'   rG  r'   rW  r'   r   r'   r*   r'   )rv   r'   rH  r'   r*  r'   rs   r'   r  r'   rG  r'   rP  r'   rw   r'   r*   r'   )
r;   r<   r=   r>   r    r7   staticmethodr[  rL  r?   r@   s   @r$   r?  r?    s        PP SWI I I I I I I:0 :0 :0 :0x E E E \E.=$ =$ =$ =$ =$ =$ =$ =$r%   r?  c                  V     e Zd ZdZdd fdZddZedd            ZddZd dZ	 xZ
S )!
v8PoseLosszICriterion class for computing training losses for YOLOv8 pose estimation.r   r   r   r   rX   r   c                   t                                          |||           |j        d         j        | _        t	          j                    | _        | j        ddgk    }| j        d         }|r1t          j        t                    
                    | j                  nt          j        || j                  |z  }t          |          | _        dS )zQInitialize v8PoseLoss with model parameters and keypoint-specific loss functions.r_      r   r   r   )r   N)r   r    r   	kpt_shaper   r   bce_poserF   
from_numpyr   rO   rL   onesr   keypoint_loss)r"   r   r   r   is_posenkptr   r#   s          r$   r    zv8PoseLoss.__init__  s    )444R2,...RG+~a @Gx!),,//<<<UZX\eiepMqMqMqtxMx)888r%   r  r  r  r*   ry   c           	     2   |d                              ddd                                          }t          j        d| j                  }|                     ||          \  \  }}}}}	}
}|
d         |
d         |
d         c|d<   |d<   |d<   |j        d         }t          j        |d	         d         j        dd
         | j        |j                  | j	        d         z  }| 
                    | |j        |dg| j        R            }|                                r|d                             | j                                                                                  }|dxx         |d         z  cc<   |dxx         |d         z  cc<   |                     ||||d                             dd          |	||          \  |d<   |d<   |dxx         | j        j        z  cc<   |dxx         | j        j        z  cc<   ||z  |                                fS );Calculate the total loss and detach it for pose estimation.kptsr   r}   r   r   r   r   r   r  NrK   r_   	keypointsr   r   r  )r  r  rF   r   rL   r,  rf   rG   rM   rx   kpts_decodere   rq  r5   rO   r   clonecalculate_keypoints_lossr   posekobjr!  )r"   r  r  r   r7   rv   r*  rs   rr   r&  rR  r   r   rw   r{  s                  r$   r7   zv8PoseLoss.loss  s/   &M))!Q22==??	{1T[111..ue<< 	[M-}xYZ %-QK!hqk!Qa$q'_Q'
U7^A.4QRR8T]Tcdddgkgrstguu $$]NIN:r4cTXTb4c4c4cdd	 ;;== 	k*--dk::@@BBHHJJIfq)fq)#<<k"''A..   DGT!W 	Q48= Q48= j $++--//r%   rr   r'   r   c                    |                                 }|dddfxx         dz  cc<   |dxx         | dddgf         dz
  z  cc<   |dxx         | ddd	gf         dz
  z  cc<   |S )
0Decode predicted keypoints to image coordinates..Nr}   r   r   r   r   r   r   r}  rr   r   ys      r$   r|  zv8PoseLoss.kpts_decode  s     OO	#rr'


c


	&			]111qc6*S00				&			]111qc6*S00			r%   r{  r  r*  rH  c           
     R   |                                 }t          |          }t          j        |d          d                                         }t          j        |||j        d         |j        d         f|j                  }|                                }t          j        |dz   t          j        |j                  }	|		                    d|dz   t          j
        |                     |	                    d          }	t          j        t          |          |j                  |	|         z
  }
||||
f<   |                    d                              d          }|                    d|                    dd|j        d         |j        d                             }|S )	a  Select target keypoints for each anchor based on batch index and target ground truth index.

        Args:
            keypoints (torch.Tensor): Ground truth keypoints, shape (N_kpts_in_batch, N_kpts_per_object, kpts_dim).
            batch_idx (torch.Tensor): Batch index tensor for keypoints, shape (N_kpts_in_batch, 1).
            target_gt_idx (torch.Tensor): Index tensor mapping anchors to ground truth objects, shape (BS, N_anchors).
            masks (torch.Tensor): Binary mask tensor indicating object presence, shape (BS, N_anchors).

        Returns:
            (torch.Tensor): Selected keypoints tensor, shape (BS, N_anchors, N_kpts_per_object, kpts_dim).
        Tr   r   r}   r   r   r   r_   )flattenr   rF   r   r   r   rf   rL   rc   r   r   r   r   r~   gatherexpand)r"   r{  r  r*  rH  r   max_kptsbatched_keypointsbatch_idx_longr  r  target_gt_idx_expandedselected_keypointss                r$   _select_target_keypointsz#v8PoseLoss._select_target_keypoints  s   $ %%''	ZZ
 <	>>>qAEEGG "K9?1#5yq7IJS\Sc
 
 

 #))+j1nEJyGWXXXQ 2EON4S4STTT..##\#i..9IJJJWUcMdd
8A.*45 "/!8!8!<!<!F!Fr!J!J /55%,,RY_Q5GYZI[\\
 
 "!r%   r&  rs   c           	     r   |                      ||||          }|dddfxx         |                    dddd          z  cc<   d}	d}
|                                r||z  }||         }t          ||                   ddddf                             dd          }||         }|j        d         d	k    r|d
         dk    nt          j        |d         d          }|                     ||||          }	|j        d         d	k    r.| 	                    |d
         |
                                          }
|	|
fS )a  Calculate the keypoints loss for the model.

        This function calculates the keypoints loss and keypoints object loss for a given batch. The keypoints loss is
        based on the difference between the predicted keypoints and ground truth keypoints. The keypoints object loss is
        a binary classification loss that classifies whether a keypoint is present or not.

        Args:
            masks (torch.Tensor): Binary mask tensor indicating object presence, shape (BS, N_anchors).
            target_gt_idx (torch.Tensor): Index tensor mapping anchors to ground truth objects, shape (BS, N_anchors).
            keypoints (torch.Tensor): Ground truth keypoints, shape (N_kpts_in_batch, N_kpts_per_object, kpts_dim).
            batch_idx (torch.Tensor): Batch index tensor for keypoints, shape (N_kpts_in_batch, 1).
            stride_tensor (torch.Tensor): Stride tensor for anchors, shape (N_anchors, 1).
            target_bboxes (torch.Tensor): Ground truth boxes in (x1, y1, x2, y2) format, shape (BS, N_anchors, 4).
            pred_kpts (torch.Tensor): Predicted keypoints, shape (BS, N_anchors, N_kpts_per_object, kpts_dim).

        Returns:
            kpts_loss (torch.Tensor): The keypoints loss.
            kpts_obj_loss (torch.Tensor): The keypoints object loss.
        .Nr}   r   r_   r   Tr`   r   .r}   r   )r  re   rN   r   r]  rf   rF   	full_likeru  rr  r   )r"   rH  r*  r{  r  r&  rs   r   r  	kpts_losskpts_obj_lossgt_kptr   pred_kptr   s                  r$   r~  z#v8PoseLoss.calculate_keypoints_loss  sY   < "::9iQ^`eff 	37###}'9'9!RA'F'FF###	99;; 		R]*M'.F]5122111abb59>>q$>OOD 'H.4l2.>!.C.Cvf~**Y_`fYgimInInH**8VXtLLI~b!Q&& $hv.>@P@P Q Q-''r%   )r   r   )r   r   r   rX   r   rX   r<  rr   r'   r   r'   r*   r'   )
r{  r'   r  r'   r*  r'   rH  r'   r*   r'   )rH  r'   r*  r'   r{  r'   r  r'   r&  r'   rs   r'   r   r'   r*   ry   )r;   r<   r=   r>   r    r7   rl  r|  r  r~  r?   r@   s   @r$   rn  rn    s        SS9 9 9 9 9 9 9#0 #0 #0 #0J    \-" -" -" -"^1( 1( 1( 1( 1( 1( 1( 1(r%   rn  c                  X     e Zd ZdZ	 d!d" fd
Zd#dZed$d            Zd%dZd&d Z	 xZ
S )'
PoseLoss26z_Criterion class for computing training losses for YOLOv8 pose estimation with RLE loss support.r   Nr   r   r   rX   r   r   c                
   t                                          |||           | j        ddgk    }| j        d         }d| _        t	          |j        d         d          r|j        d         j        nd| _        | j        t          d                              | j	                  | _        |r1t          j        t                                        | j	                  nt          j        || j	        	          | _        dS dS )
zdInitialize PoseLoss26 with model parameters and keypoint-specific loss functions including RLE loss.rp  r   r   Nr_   
flow_modelT)r   r   )r   r    rq  rle_losshasattrr   r  r   rO   rL   rF   rs  r   rt  target_weights)r"   r   r   r   rv  rw  r#   s         r$   r    zPoseLoss26.__init__#  s     	)444.RG+~a 8?BQ]8^8^h%+b/44dh?&#d;;;>>t{KKDM@Gq ,,//<<<UZX\eiepMqMqMq  '&r%   r  r  r  r*   ry   c           	        |d                              ddd                                          }t          j        | j        rdnd| j                  }|                     ||          \  \  }}}}}	}
}|
d         |
d         |
d         c|d<   |d<   |d	<   |j        d         }t          j        |d
         d         j        dd         | j        |j	                  | j
        d         z  } |j        |dg| j        R  }| j        r|                    dd          j|d                              ddd                                          }|                    |d| j        d         d          }t          j        ||gd          }|                     ||          }|                                r|d                             | j                                                                                  }|dxx         |d         z  cc<   |dxx         |d         z  cc<   |                     ||||d                             dd          |	||          }|d         |d<   |d         |d<   | j        |d         |d<   |dxx         | j        j        z  cc<   |dxx         | j        j        z  cc<   | j        |dxx         | j        j        z  cc<   ||z  |                                fS )ry  rz  r   r}   r      r   r   r   r   r  NrK   r_   
kpts_sigmar   r{  r   r   r  )r  r  rF   r   r  rL   r,  rf   rG   rM   rx   re   rq  getr  r|  r5   rO   r   r}  r~  r   r  r  rler!  )r"   r  r  r   r7   rv   r*  rs   rr   r&  rR  r   r   rw   
pred_sigmar{  keypoints_losss                    r$   r7   zPoseLoss26.loss2  s   &M))!Q22==??	{%AAAdk
 
 
 ..ue<< 	[M-}xYZ %-QK!hqk!Qa$q'_Q'
U7^A.4QRR8T]Tcdddgkgrstguu"IN:rCDNCCC	= 	CUYY|T::F|,44Q1==HHJJJ#R9JANNJ	9j"9rBBBI$$]I>>	 ;;== 	,k*--dk::@@BBHHJJIfq)fq)!::k"''A.. N %Q'DG$Q'DG}((+QQ48= Q48= =$GGGtx|#GGGj $++--//r%   rr   r'   r   c                    |                                 }|dxx         | dddgf         z  cc<   |dxx         | dddgf         z  cc<   |S )r  r   Nr   r   r   r  r  s      r$   r|  zPoseLoss26.kpts_decodee  sb     OO	&			]111qc6**				&			]111qc6**			r%   r  r  r   c                   |                                 s|dddf                                         S ||         }||         }|ddddf         }|ddddf         }|ddddf         }| j                            d                              |j        d         d          }	|	|         }	|                                }||z
  |dz   z  }
|
                                s|dddf                                         S t          j	        |
          t          j
        |
          z                       d	           }|                                 s|dddf                                         S |
|         }
|
                    d
d          }
||         }|	|         }	| j                            |
          }|                     |||
|	          S )a  Calculate the RLE (Residual Log-likelihood Estimation) loss for keypoints.

        Args:
            pred_kpt (torch.Tensor): Predicted kpts with sigma, shape (N, num_keypoints, kpts_dim) where kpts_dim >= 4.
            gt_kpt (torch.Tensor): Ground truth keypoints, shape (N, num_keypoints, kpts_dim).
            kpt_mask (torch.Tensor): Mask for valid keypoints, shape (N, num_keypoints).

        Returns:
            (torch.Tensor): The RLE loss.
        .Nr   r}   r   r   r   r_   r   id   )rN   r5   r  r~   repeatrf   r0   numelrF   isnanisinfclampr  log_probr  )r"   r  r  r   pred_kpt_visiblegt_kpt_visiblepred_coordsr  	gt_coordsr  r   
valid_maskr   s                r$   calculate_rle_losszPoseLoss26.calculate_rle_lossm  s    ||~~ 	+C!G$((***#H-)&qqq!A#v.%aaaf-
"111ac6*	,66q99@@PQARTUVV'1''))
y(Z$->?{{}} 	+C!G$((*** {5))EK,>,>>CCCKKK
~~ 	+C!G$((***j!D#&&
+
'
3/**511}}Z%HHHr%   rH  r*  r{  r  r&  rs   /tuple[torch.Tensor, torch.Tensor, torch.Tensor]c           	     H   |                      ||||          }|dddfxx         |                    dddd          z  cc<   d}	d}
d}|                                rA||z  }||         }t          ||                   ddddf                             dd          }||         }|j        d         d	k    r|d
         dk    nt          j        |d         d          }|                     ||||          }	| j	        O|j        d         dk    s|j        d         dk    r-| 
                    |||          }|                    d          }|j        d         d	k    s|j        d         dk    r.|                     |d
         |                                          }
|	|
|fS )aG  Calculate the keypoints loss for the model.

        This function calculates the keypoints loss and keypoints object loss for a given batch. The keypoints loss is
        based on the difference between the predicted keypoints and ground truth keypoints. The keypoints object loss is
        a binary classification loss that classifies whether a keypoint is present or not.

        Args:
            masks (torch.Tensor): Binary mask tensor indicating object presence, shape (BS, N_anchors).
            target_gt_idx (torch.Tensor): Index tensor mapping anchors to ground truth objects, shape (BS, N_anchors).
            keypoints (torch.Tensor): Ground truth keypoints, shape (N_kpts_in_batch, N_kpts_per_object, kpts_dim).
            batch_idx (torch.Tensor): Batch index tensor for keypoints, shape (N_kpts_in_batch, 1).
            stride_tensor (torch.Tensor): Stride tensor for anchors, shape (N_anchors, 1).
            target_bboxes (torch.Tensor): Ground truth boxes in (x1, y1, x2, y2) format, shape (BS, N_anchors, 4).
            pred_kpts (torch.Tensor): Predicted keypoints, shape (BS, N_anchors, N_kpts_per_object, kpts_dim).

        Returns:
            kpts_loss (torch.Tensor): The keypoints loss.
            kpts_obj_loss (torch.Tensor): The keypoints object loss.
            rle_loss (torch.Tensor): The RLE loss.
        .Nr}   r   r_   r   Tr`   r   r  r   r   r   )min)r  re   rN   r   r]  rf   rF   r  ru  r  r  r  rr  r   )r"   rH  r*  r{  r  r&  rs   r   r  r  r  r  r  r   r  r   s                   r$   r~  z#PoseLoss26.calculate_keypoints_loss  s   > "::9iQ^`eff 	37###}'9'9!RA'F'FF###	99;; 	R]*M'.F]5122111abb59>>q$>OOD 'H.4l2.>!.C.Cvf~**Y_`fYgimInInH**8VXtLLI}(hnR.@A.E.EXZI[_`I`I`228VXNN#>>a>00~b!Q&&(.*<*A*A $hv.>@P@P Q Q-11r%   r9  r:  r<  r  )r  r'   r  r'   r   r'   r*   r'   )rH  r'   r*  r'   r{  r'   r  r'   r&  r'   rs   r'   r   r'   r*   r  )r;   r<   r=   r>   r    r7   rl  r|  r  r~  r?   r@   s   @r$   r  r     s        ii SW      10 10 10 10f    \(I (I (I (IT62 62 62 62 62 62 62 62r%   r  c                      e Zd ZdZd
dZd	S )v8ClassificationLosszACriterion class for computing training losses for classification.r  r   r  r  r*   ry   c                    t          |t          t          f          r|d         n|}t          j        ||d         d          }||                                fS )zDCompute the classification loss between predictions and true labels.r   r  r4   r.   )r0  listr   r2   rd   r!  )r"   r  r  r7   s       r$   rk   zv8ClassificationLoss.__call__  sO    &utUm<<Ga%ueElfEEET[[]]""r%   Nr  r   r  r  r*   ry   )r;   r<   r=   r>   rk   r=  r%   r$   r  r    s.        KK# # # # # #r%   r  c                  F     e Zd ZdZdd fdZddZddZddZd dZ xZ	S )!	v8OBBLosszdCalculates losses for object detection, classification, and box distribution in rotated YOLO models.r   Nr   r   r   r   c                   t                                          ||           t          || j        dd| j                                        |          | _        t          | j                  	                    | j
                  | _        dS )z^Initialize v8OBBLoss with model, assigner, and rotated bbox loss; model must be de-paralleled.r   r   r   r   N)r   r    r   r   rx   r   r   r   rW   rO   rL   r   rD  s       r$   r    zv8OBBLoss.__init__  s    2222;%%''
 
 
 )6699$+FFr%   r   r'   r   rX   r   r*   c                8   |j         d         dk    rt          j        |dd| j                  }ni|dddf                                         }|                    d          \  }}|                    t          j                  }t          j        ||                                d| j                  }|ddddf         	                                }|dddd	f         
                    |           t          j        |dz   t          j        | j        
          }	|	                    d|dz   t          j        |                     |	                    d          }	t          j        t          |          | j                  |	|         z
  }
||||
f<   |S )z7Preprocess targets for oriented bounding box detection.r   r  r   NTr   r   r   r   r   )rf   rF   r   rL   rc   r   rO   r   r   r}  r   r   r   r   r   r   )r"   r   r   r   r  r  r   r  packed_targetsr  r  s              r$   r  zv8OBBLoss.preprocess  st   =q  +j!Qt{CCCCC1**,,I!((t(<<IAvYYU[Y11F+j&**,,$+NNNC$QQQU^1133N111ac6"''555k*q.
4;WWWG  IM5?93M3MNNNnnQ''Gc'll4;GGG'R[J\\J)7C	:%&
r%   r  r  r  ry   c                x	   t          j        d| j                  }|d                             ddd                                          |d                             ddd                                          |d                             ddd                                          }}}t          |d	         | j        d
          \  }}|j        d         }	|j        }
t          j	        |d	         d         j        dd         | j        |
          | j        d         z  }	 |d         
                    dd          }t          j        ||d         
                    dd          |d         
                    dd          fd          }|dddf         t          |d                   z  |dddf         t          |d                   z  }}||dk    |dk    z           }|                     |                    | j                  |	|g d                   }|                    dd          \  }}|                    dd                              d          }n"# t$          $ r}t'          d          |d}~ww xY w|                     |||          }|                                                                }|dddfxx         |z  cc<   |                     |                                                                |                    |j                  ||z  |||          \  }}}}}t5          |                                d          }|                     ||                    |
                                                    |z  |d<   |                                rr|dddfxx         |z  cc<   |                     |||||||||	  	        \  |d<   |d<   |                    d          |         }|                     |||||          |d<   n%|dxx         |dz                                  z  cc<   |dxx         | j        j        z  cc<   |dxx         | j        j         z  cc<   |dxx         | j        j!        z  cc<   |dxx         | j        j"        z  cc<   ||	z  |                                fS )zBCalculate and return the loss for oriented bounding box detection.r   r   r  r   r}   r   r  angler  r   NrK   r  r_   r  r  r   r  r  )r   r   Tr`   r  uh  ERROR ❌ OBB dataset incorrectly formatted or not a OBB dataset.
This error can occur when incorrectly training a 'OBB' model on a 'detect' dataset, i.e. 'yolo train model=yolo26n-obb.pt data=dota8.yaml'.
Verify your dataset is a correctly formatted 'OBB' dataset using 'data=dota8.yaml' as an example.
See https://docs.ultralytics.com/datasets/obb/ for help..r   )#rF   r   rL   r  r  r   rx   rf   rM   rG   re   r  r   r  rO   r  r5   r   RuntimeError	TypeErrorr  r}  r!  r   r0   r
  r   r   r   calculate_angle_lossr   r"  r  r#  r  )r"   r  r  r7   r$  r%  
pred_anglerr   r&  r   rM   rw   r  r   rwrhr'  r(  r)  r   rq   bboxes_for_assignerr   rs   rt   rv   ru   r6   s                               r$   r7   zv8OBBLoss.loss  s   {1T[111'N""1a++6688(O##Aq!,,7799'N""1a++6688 #-[
 (4E'NDKQT'U'U$}%a(
!U7^A.4QRR8TYZZZ]a]hij]kk	k*//A66IiE%L,=,=b!,D,DeHoFZFZ[]_`FaFa bdeffGQQQT]U58__4gaaadmeERSHoo6UBrQw2734Googjj&=&=zX]^j^j^jXkollG#*==#;#; IymmAtm4488==GG 	 	 	[  	 &&}k:NN)//1188::C!G$$$5$$$6:mm  ((**$$Y_55M)7
 7
3=-!   1 1 3 3Q77 ((;(8(8(?(?@@DDFFIZZQ ;;== 	.#rr'"""m3"""#~~!
  
 DGT!W #&&r**73F//]GV=N DGG GGG
Q++---GGGQ48<Q48<Q48<Q48>!j $++--//s    D:I 
I:%I55I:rr   r\   r  c                0   | j         ri|j        \  }}}|                    ||d|dz                                d                              | j                            |j                            }t          j	        t          |||          |fd          S )a  Decode predicted object bounding box coordinates from anchor points and distribution.

        Args:
            anchor_points (torch.Tensor): Anchor points, (h*w, 2).
            pred_dist (torch.Tensor): Predicted rotated distance, (bs, h*w, 4).
            pred_angle (torch.Tensor): Predicted angle, (bs, h*w, 1).

        Returns:
            (torch.Tensor): Predicted rotated bounding boxes with angles, (bs, h*w, 5).
        r   r   r_   r   )r   rf   re   r  r	  r   r
  rM   rF   r  r   )r"   rr   r\   r  r  r  r  s          r$   r  zv8OBBLoss.bbox_decodeM  s     < 	koGAq!!q!QQ77??BBII$)..YbYhJiJijjIy)Iz=II:V\^____r%   r   c                   |d         }|d         }|d         }	|d         }
t          j        |dz   |dz   z            }t          j        |dz   |dz  z            }|	|
z
  }|t          j        |t          j        z            t          j        z  z
  }t          j        d||         z            dz  }||         |z  }||z  }|                                |z  S )a  Calculate oriented angle loss.

        Args:
            pred_bboxes (torch.Tensor): Predicted bounding boxes with shape [N, 5] (x, y, w, h, theta).
            target_bboxes (torch.Tensor): Target bounding boxes with shape [N, 5] (x, y, w, h, theta).
            fg_mask (torch.Tensor): Foreground mask indicating valid predictions.
            weight (torch.Tensor): Loss weights for each prediction.
            target_scores_sum (torch.Tensor): Sum of target scores for normalization.
            lambda_val (int): Controls the sensitivity to aspect ratio.

        Returns:
            (torch.Tensor): The calculated angle loss.
        r  ).r   ).r   r   r}   )rF   r   r   roundmathpisinr5   )r"   rq   rs   rv   r6   ru   
lambda_valw_gth_gt
pred_thetatarget_thetalog_arscale_weightdelta_thetadelta_theta_wrappedang_losss                   r$   r  zv8OBBLoss.calculate_angle_loss_  s     V$V$ (
$V,D4KD4K899y619Q!?@@ </)EKdg8M,N,NQUQX,XX9Q!4W!==>>!C(83f$||~~ 111r%   r9  r   r   r   r   r;  r<  )rr   r'   r\   r'   r  r'   r*   r'   )r   )
r;   r<   r=   r>   r    r  r7   r  r  r?   r@   s   @r$   r  r    s        nnG G G G G G G   $O0 O0 O0 O0b` ` ` `$2 2 2 2 2 2 2 2r%   r  c                  "    e Zd ZdZddZddZdS )E2EDetectLossGCriterion class for computing training losses for end-to-end detection.r   r   c                ^    t          |d          | _        t          |d          | _        dS )zcInitialize E2EDetectLoss with one-to-many and one-to-one detection losses using the provided model.r   r  r   N)r   one2manyone2one)r"   r   s     r$   r    zE2EDetectLoss.__init__  s.    ';;;&uq999r%   r  r   r  r  r*   ry   c                    t          |t                    r|d         n|}|d         }|                     ||          }|d         }|                     ||          }|d         |d         z   |d         |d         z   fS )r4  r   r  r  r   )r0  r   r  r  )r"   r  r  r  loss_one2manyr  loss_one2ones          r$   rk   zE2EDetectLoss.__call__  s    &ue44?a%$h66	"||GU33Q,q/1=3ClSTo3UUUr%   Nr   r   r  )r;   r<   r=   r>   r    rk   r=  r%   r$   r  r    sH        QQ: : : :
V V V V V Vr%   r  c                  6    e Zd ZdZefddZddZddZddZdS )E2ELossr  r   r   c                     ||d          | _          ||dd          | _        d| _        d| _        d| _        | j        | j        z
  | _        | j        | _        d	| _        d
S )z]Initialize E2ELoss with one-to-many and one-to-one detection losses using the provided model.r   r     r   )r   r   r   rJ   g?g?N)r  r  updatestotalo2mo2oo2m_copy	final_o2m)r"   r   loss_fns      r$   r    zE2ELoss.__init__  si    333wuqA>>>
:(r%   r  r   r  r  r*   ry   c                   | j                             |          }|d         |d         }}| j                             ||          }| j                            ||          }|d         | j        z  |d         | j        z  z   |d         fS )r4  r  r  r   r   )r  r2  r7   r  r  r  )r"   r  r  r  r  r  r  s          r$   rk   zE2ELoss.__call__  s    **511!*-uY/?'**8U;;|((%88Q$(*\!_tx-GGVWXXr%   rY   c                    | xj         dz  c_         |                     | j                   | _        t          | j        | j        z
  d          | _        dS )zUUpdate the weights for one-to-many and one-to-one losses based on the decay schedule.r   r   N)r  decayr  r   r  r  )r"   s    r$   updatezE2ELoss.update  sE    ::dl++tzDH,a00r%   r   c                    t          d|t          | j        j        j        dz
  d          z  z
  d          | j        | j        z
  z  | j        z   S )zSCalculate the decayed weight for one-to-many loss based on the current update step.r   r   )r   r  r   epochsr  r  )r"   xs     r$   r  zE2ELoss.decay  sL    1q3t|/6:A>>>>BBdmVZVdFdehlhvvvr%   Nr  r  )r*   rY   )r*   r   )	r;   r<   r=   r>   r   r    rk   r  r  r=  r%   r$   r  r    s|        QQ7F     Y Y Y Y1 1 1 1w w w w w wr%   r  c                  <    e Zd ZdZdddZddZddZddZddZdS )TVPDetectLosszOCriterion class for computing training losses for text-visual prompt detection.r   Nr   r   r   r   c                    t          |||          | _        | j        j        | _        | j        j        | _        | j        j        | _        | j        j        | _        dS )z^Initialize TVPDetectLoss with task-prompt and visual-prompt criteria using the provided model.N)	r   vp_criterionr   r   ori_ncr   ori_norW   ori_reg_max)r"   r   r   r   s       r$   r    zTVPDetectLoss.__init__  sQ    +E8YGG$('*'*,4r%   r*   r  c                6    | j                             |          S )r/  )r  r2  r1  s     r$   r2  zTVPDetectLoss.parse_output  s     --e444r%   r  r   r  ry   c                T    |                      |                     |          |          S )4Calculate the loss for text-visual prompt detection.r5  r6  s      r$   rk   zTVPDetectLoss.__call__  $    yy**5115999r%   c                4   | j         |d         j        d         k    r7t          j        d| j        j        d          }||                                fS |                     |          |d<   |                     ||          }|d         d         }||d         fS )r  r  r   r   TrL   requires_gradr   r  rf   rF   r   r  rL   r!  _get_vp_features)r"   r  r  r7   vp_lossbox_losss         r$   r7   zTVPDetectLoss.loss      ;%//222;q):)AQUVVVD&&//66h##E5111:a=##r%   list[torch.Tensor]c                    |d         }|j         d         }|| j        _        || j        j        dz  z   | j        _        || j        j        _        |S )z5Extract visual-prompt features from the model output.r  r   r   )rf   r  r   rW   r   r   r   )r"   r  r  vncs       r$   r  zTVPDetectLoss._get_vp_features  sO    xl1o""T%6%>%BB14".r%   r9  r  )r*   r  r  r<  )r  r  r*   r  )	r;   r<   r=   r>   r    r2  rk   r7   r  r=  r%   r$   r  r    s        YY5 5 5 5 55 5 5 5: : : :	$ 	$ 	$ 	$     r%   r  c                  6     e Zd ZdZdd fdZddZddZ xZS )TVPSegmentLosszRCriterion class for computing training losses for text-visual prompt segmentation.r   r   r   c                    t                                          |           t          ||          | _        | j        j        | _        dS )z_Initialize TVPSegmentLoss with task-prompt and visual-prompt criteria using the provided model.N)r   r    r?  r  r   )r"   r   r   r#   s      r$   r    zTVPSegmentLoss.__init__  s>    .uh??$(r%   r  r   r  r  r*   ry   c                T    |                      |                     |          |          S )7Calculate the loss for text-visual prompt segmentation.r5  r6  s      r$   rk   zTVPSegmentLoss.__call__  r  r%   c                4   | j         |d         j        d         k    r7t          j        d| j        j        d          }||                                fS |                     |          |d<   |                     ||          }|d         d         }||d         fS )r  r  r   r   Tr  r   r}   r  )r"   r  r  r7   r	  cls_losss         r$   r7   zTVPSegmentLoss.loss  r  r%   )r   r  r  )r;   r<   r=   r>   r    rk   r7   r?   r@   s   @r$   r  r    sp        \\) ) ) ) ) ) ): : : :	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$r%   r  c                  B     e Zd ZdZd
 fdZd Zd Zd Zd Zd	 Z	 xZ
S )SemanticSegmentationLosszLoss function for semantic segmentation using cross-entropy and Dice terms.

    Attributes:
        nc (int): Number of semantic classes.
        ce (nn.CrossEntropyLoss): Cross-entropy loss with ignore_index=255.
    r   r   c           	     |   t                                                       |j        d         }|j        | _        t	          |                                          j        | _        t	          |                                          j        | _        t          t          t          |j        dd          pd                    j                                        }|dv o| j        t          t                    k    | _        | j        dk    rt#          j                    | _        dS t#          j        d                              | j        | j                  | _        | j        rWt-          j        t                                        | j        | j                  }| j                            d	|d
           dS dS )zInitialize semantic segmentation loss.

        Args:
            model (torch.nn.Module): Model containing the SemanticSegment head.
        r_   data >   
cityscapescityscapes8r      )ignore_indexrK   r6   F)
persistentN)r   r    r   r   r   r   rL   rM   r   r   r   r   stemlowerr   r   use_cityscapes_weightr   r   ceCrossEntropyLossrO   rF   rs  register_buffer)r"   r   r   	data_namer6   r#   s        r$   r    z!SemanticSegmentationLoss.__init__  sz    	KO$5++--..5%**,,--3
WUZ<<BCCDDIOOQQ	%.2O%O%uTXT[_bct_u_uTu"7a<<*,,DGGG)s;;;>>dkY]Yc>ddDG) L)*;<<??t{Z^Zd?ee''&U'KKKKKL Lr%   c                   |j         dd         |k    rlt          j        |                                                    d          |d                              d                              t          j                  S |S )z4Resize masks to match prediction spatial dimensions.r   Nr   )r   r   )	rf   r2   r   r   r~   squeezerO   rF   r   )r"   rH  target_shapes      r$   _resize_masksz&SemanticSegmentationLoss._resize_masks  so    ;qrr?l**ekkmm55a88|R[\\\ddefggjjkpkvww r%   c                   | j         dk    rQ|                    d          }|dk    }|                    d          |         }||                                         }nX|                    dddd                              d| j                   }|                    d                                          }|                     ||          S )zLCompute cross-entropy on flattened pixels to avoid the CUDA nll_loss2d path.r   r_   r  r   r}   r   )r   reshaper   r  rc   r#  )r"   r  rH  flatvalidlogitsr]   s          r$   _ce_lossz!SemanticSegmentationLoss._ce_loss  s    7a<<==$$DCKE]]2&&u-F%[&&((FF]]1aA..66r47CCF]]2&&++--Fwwvv&&&r%   c           
        | j         dk    r|                     ||          S |                    d          }|dk    }|                                s|                                dz  S t          j        |d          }||                                         }|                                	                    dddd                              d| j                   |         }t          j        | j         |j        t          j                  }|                    d||                    d|d	d	d	f                                       d                     |                    d          }	t          j        || j         
                              |j        t          j                  }
|	|
z   }dd|z  dz   |dz   z  z
                                  S )z*Compute Dice loss excluding ignore pixels.r   r_   r  r   r   r}   r   rK   N)	minlengthrJ   r   )r   _binary_dice_lossr,  rN   r5   r2   r  rc   r   r  rF   r   rL   float32r   r  r(  bincountrO   r4   )r"   r  rH  flat_targetr.  	pred_softr]   	flat_predr   pred_sum
target_sumcardinalitys               r$   
_dice_lossz#SemanticSegmentationLoss._dice_loss)  s   7a<<))%777mmB''s"yy{{ 	#99;;?"Ie+++	U#((**OO%%--aAq99AA"dgNNuU	{475<u}UUU!!!VY-=-=a4-Q-Q-Y-YZ[-\-\]]]==Q=''^Fdg>>>AA]b]jAkk
+s\)C/K#4EFFLLNNNr%   c                :   |dk                                     }|                    d                                          }|dk                                     }||z  |z                                  }||z   |z                                  }dd|z  dz   |dz   z  z
  S )zCompute Dice loss for single-class (binary) segmentation.

        Pixels with value 255 are excluded from Dice terms to match BCE valid-pixel filtering.
        r  r   rJ   r   )r   r(  r0   r5   )r"   r  rH  r.  r7  r]   r   r;  s           r$   r3  z*SemanticSegmentationLoss._binary_dice_loss<  s    
 #$$&&MM!$$,,..	1*##%%!F*U27799!F*e388::cL(3.;3DEEEr%   c                   d}t          |t                    r|\  }}|d                             |j                  }|j        dd         |j        dd         k    r%t          j        ||j        dd         dd          }|                     ||          }|                     ||          }||z   }t          j
        d|j        |j        	          }|c|j        dd         |j        dd         k    r%t          j        ||j        dd         dd          }|                     ||          d
z  }||z  }t          j        |||g                                          }	||j        d         z  |	fS )a  Compute semantic segmentation loss with optional auxiliary loss.

        Args:
            preds (torch.Tensor | tuple): Main logits [B, nc, H', W'], or (main, aux) tuple.
            batch (dict): Batch dict with 'semantic_mask' [B, H, W] containing class IDs (255=ignore).

        Returns:
            (tuple[torch.Tensor, torch.Tensor]): (total_loss * batch_size, detached loss items [ce, dice, aux]).
        Nsemantic_maskr}   r   rI  F)r   r   rJ  r  rK   g?r   )r0  r   rO   rL   rf   r2   r   r0  r<  rF   rG   rM   stackr!  )
r"   r  r  
aux_logitsrH  ce_lossr   r  aux_loss
loss_itemss
             r$   r8   z SemanticSegmentationLoss.forwardH  st    
eU## 	& %E:o&))%,77;qrr?ek!""o--M%ek!""oJ^cdddE --u--OOE511	)# <ELNNN!#u{12266]:EKOR\lqrrr
}}Z77#=HXE['9h!?@@GGII
u{1~%z11r%   r  )r;   r<   r=   r>   r    r*  r0  r<  r3  r8   r?   r@   s   @r$   r  r    s         L L L L L L,  
' 
' 
'O O O&
F 
F 
F!2 !2 !2 !2 !2 !2 !2r%   r  )8
__future__r   r  pathlibr   typingr   rF   torch.nnr   torch.nn.functional
functionalr2   ultralytics.utils.metricsr   r   r   ultralytics.utils.opsr	   r
   r   ultralytics.utils.talr   r   r   r   r   ultralytics.utils.torch_utilsr   metricsr   r   talr   r   Moduler   rB   rU   rn   r   r   r   r   r   r   r?  rn  r  r  r  r  r  r  r  r  r=  r%   r$   <module>rR     s   # " " " " "                              N N N N N N N N N N A A A A A A A A A A u u u u u u u u u u u u u u 2 2 2 2 2 2 & & & & & & & & % % % % % % % %    BI   @ "  "  "  "  "	  "  "  "F! ! ! ! !RY ! ! !*," ," ," ," ,"ry ," ," ,"^3 3 3 3 3bi 3 3 3l," ," ," ," ,"h ," ," ,"^    29   Be e e e e") e e e0W W W W W29 W W W&R. R. R. R. R. R. R. R.j\$ \$ \$ \$ \$ \$ \$ \$~[( [( [( [( [( [( [( [(|m2 m2 m2 m2 m2 m2 m2 m2`# # # # # # # #b2 b2 b2 b2 b2 b2 b2 b2JV V V V V V V V$ w  w  w  w  w  w  w  wF' ' ' ' ' ' ' 'T$ $ $ $ $] $ $ $2r2 r2 r2 r2 r2ry r2 r2 r2 r2 r2r%   