a
    áñh×  ã                   @   s:   d dl Z d dlm  mZ de je jeeedœdd„ZdS )	é    Nç      Ð?é   Únone)ÚinputsÚtargetsÚalphaÚgammaÚ	reductionc           
      C   s�   t  | ¡}tj| |dd�}|| d| d|   }|d| |  }|dkrj|| d| d|   }	|	| }|dkr|| ¡ }n|dkrŒ| ¡ }|S )a5  
    Original implementation from https://github.com/facebookresearch/fvcore/blob/master/fvcore/nn/focal_loss.py .
    Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002.

    Arguments:
        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 or -1 for ignore. Default = 0.25
        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)ÚtorchZsigmoidÚFZ binary_cross_entropy_with_logitsr   r   )
r   r   r   r   r	   ÚpZce_lossZp_tZlossZalpha_t© r   úZ/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/ops/focal_loss.pyÚsigmoid_focal_loss   s    
ÿ
r   )r   r   r   )	r   Ztorch.nn.functionalÚnnZ
functionalr   ZTensorÚfloatÚstrr   r   r   r   r   Ú<module>   s      ûû