o
    ϯi                     @   s   U d dl Z d dlmZ 			dde jde jded	ed
ede jfddZe j	eZ
ded< 			dde jde jded	ed
ede jfddZe j	eZded< dS )    N)
functional   noneinputstargetsalphagamma	reductionreturnc           
      C   s   |   } |  }t| }tj| |dd}|| d| d|   }|d| |  }|dkr=|| d| d|   }	|	| }|dkrG| }|S |dkrO| }|S )a  
    Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002.
    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).
        alpha: (optional) Weighting factor in range (0,1) to balance
                positive vs negative examples. Default = -1 (no weighting).
        gamma: Exponent of the modulating factor (1 - p_t) to
               balance easy vs hard examples.
        reduction: 'none' | 'mean' | 'sum'
                 'none': No reduction will be applied to the output.
                 'mean': The output will be averaged.
                 'sum': The output will be summed.
    Returns:
        Loss tensor with the reduction option applied.
    r   )r
      r   meansum)floattorchsigmoidF binary_cross_entropy_with_logitsr   r   )
r   r   r   r	   r
   pce_lossp_tlossalpha_t r   H/home/ubuntu/.local/lib/python3.10/site-packages/fvcore/nn/focal_loss.pysigmoid_focal_loss   s   
r   ztorch.jit.ScriptModulesigmoid_focal_loss_jitr   c                 C   s   |   } |  }|| d| d   }t| | }|dkr.|| d| d|   }||9 }|dkr8| }|S |dkr@| }|S )a  
    FL* described in RetinaNet paper Appendix: https://arxiv.org/abs/1708.02002.
    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).
        alpha: (optional) Weighting factor in range (0,1) to balance
                positive vs negative examples. Default = -1 (no weighting).
        gamma: Gamma parameter described in FL*. Default = 1 (no weighting).
        reduction: 'none' | 'mean' | 'sum'
                 'none': No reduction will be applied to the output.
                 'mean': The output will be averaged.
                 'sum': The output will be summed.
    Returns:
        Loss tensor with the reduction option applied.
    r   r   r   r   r   )r   r   
logsigmoidr   r   )r   r   r   r	   r
   shifted_inputsr   r   r   r   r   sigmoid_focal_loss_star7   s   r   sigmoid_focal_loss_star_jit)r   r   r   )r   r   r   )r   torch.nnr   r   Tensorr   strr   jitscriptr   __annotations__r   r    r   r   r   r   <module>   sL   

-
*