o

    hG                     @   sx   d dl mZ d dlm  mZ ddlmZ 				d
ddZ				d
ddZ					dd	d
Z	G dd dej
ZdS )    N   )weight_reduce_lossmeanc                 C   s6   t j| ||dd}|dur| }t||||d}|S )a  Calculate the CrossEntropy loss.

    Args:
        pred (torch.Tensor): The prediction with shape (N, C), C is the number
            of classes.
        label (torch.Tensor): The gt label of the prediction.
        weight (torch.Tensor, optional): Sample-wise loss weight.
        reduction (str): The method used to reduce the loss.
        avg_factor (int, optional): Average factor that is used to average
            the loss. Defaults to None.
        class_weight (torch.Tensor, optional): The weight for each class with
            shape (C), C is the number of classes. Default None.

    Returns:
        torch.Tensor: The calculated loss
    none)weight	reductionNr   r   
avg_factor)F
cross_entropyfloatr   )predlabelr   r   r	   class_weightloss r   </root/Awesome-Backbones/configs/losses/cross_entropy_loss.pyr      s   r   c                 C   sT   | t j| dd }|dur||9 }|jdd}|dur | }t||||d}|S )a  Calculate the Soft CrossEntropy loss. The label can be float.

    Args:
        pred (torch.Tensor): The prediction with shape (N, C), C is the number
            of classes.
        label (torch.Tensor): The gt label of the prediction with shape (N, C).
            When using "mixup", the label can be float.
        weight (torch.Tensor, optional): Sample-wise loss weight.
        reduction (str): The method used to reduce the loss.
        avg_factor (int, optional): Average factor that is used to average
            the loss. Defaults to None.
        class_weight (torch.Tensor, optional): The weight for each class with
            shape (C), C is the number of classes. Default None.

    Returns:
        torch.Tensor: The calculated loss
    )dimNr   )r
   log_softmaxsumr   r   )r
   r   r   r   r   r	   r   r   r   r   soft_cross_entropy*   s   r   c           	      C   s   |   |  ks
J |dur|  d }||d}tj| |||dd}|dur@|  dks0J | }|   dkr@|dd}t||||d}|S )a  Calculate the binary CrossEntropy loss with logits.

    Args:
        pred (torch.Tensor): The prediction with shape (N, \*).
        label (torch.Tensor): The gt label with shape (N, \*).
        weight (torch.Tensor, optional): Element-wise weight of loss with shape
            (N, ). Defaults to None.
        reduction (str): The method used to reduce the loss.
            Options are "none", "mean" and "sum". If reduction is 'none' , loss
            is same shape as pred and label. Defaults to 'mean'.
        avg_factor (int, optional): Average factor that is used to average
            the loss. Defaults to None.
        class_weight (torch.Tensor, optional): The weight for each class with
            shape (C), C is the number of classes. Default None.
        pos_weight (torch.Tensor, optional): The positive weight for each
            class with shape (C), C is the number of classes. Default None.

    Returns:
        torch.Tensor: The calculated loss
    Nr   r   r   )r   
pos_weightr   r   r   )r   sizerepeatr
    binary_cross_entropy_with_logitsr   reshaper   )	r
   r   r   r   r	   r   r   Nr   r   r   r   binary_cross_entropyP   s(   r   c                       s>   e Zd ZdZ						d
 fdd	Z			ddd	Z  ZS )CrossEntropyLossa'  Cross entropy loss.

    Args:
        use_sigmoid (bool): Whether the prediction uses sigmoid
            of softmax. Defaults to False.
        use_soft (bool): Whether to use the soft version of CrossEntropyLoss.
            Defaults to False.
        reduction (str): The method used to reduce the loss.
            Options are "none", "mean" and "sum". Defaults to 'mean'.
        loss_weight (float):  Weight of the loss. Defaults to 1.0.
        class_weight (List[float], optional): The weight for each class with
            shape (C), C is the number of classes. Default None.
        pos_weight (List[float], optional): The positive weight for each
            class with shape (C), C is the number of classes. Only enabled in
            BCE loss when ``use_sigmoid`` is True. Default None.
    Fr         ?Nc                    sp   t t|   || _|| _| jr| jrJ d|| _|| _|| _|| _| jr+t	| _
d S | jr3t| _
d S t| _
d S )Nz8use_sigmoid and use_soft could not be set simultaneously)
superr   __init__use_sigmoiduse_softr   loss_weightr   r   r   
cls_criterionr   r   )selfr#   r$   r   r%   r   r   	__class__r   r   r"      s$   


zCrossEntropyLoss.__init__c                 K   s   |dv sJ |r
|n| j }| jd ur|| j}nd }| jd ur1| jr1|| j}	|d|	i nd }	| j| j|||f|||d| }
|
S )N)Nr   r   r   r   )r   r   r	   )r   r   
new_tensorr   r#   updater%   r&   )r'   	cls_scorer   r   r	   Zreduction_overridekwargsr   r   r   loss_clsr   r   r   forward   s,   
zCrossEntropyLoss.forward)FFr   r    NN)NNN)__name__
__module____qualname____doc__r"   r/   
__classcell__r   r   r(   r   r      s    r   )Nr   NN)Nr   NNN)torch.nnnntorch.nn.functional
functionalr
   utilsr   r   r   r   Moduler   r   r   r   r   <module>   s(   
$
(
4