o
    oi+                     @  sd   d dl mZ d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 ddddZG dd deZdS )    )annotationsN)Tensor)Module)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SAME_DEVICEKORNIA_CHECK_SAME_SHAPEnoneimg1r   img2	reductionstrreturnc                 C  s   t |  t | t| | t| | t|dv d|  | | }t|}d| |d  }|dkr7| }|S |dkrA| }|S |dksI|du rL	 |S td	)
a  Criterion that computes the Geman-McClure loss [2].

    According to [1], we compute the Geman-McClure loss as follows:

    .. math::

        \text{WL}(x, y) = \frac{2 (x - y)^{2}}{(x - y)^{2} + 4}

    Where:
       - :math:`x` is the prediction.
       - :math:`y` is the target to be regressed to.

    Reference:
        [1] https://arxiv.org/pdf/1701.03077.pdf
        [2] Bayesian image analysis: An application to single photon emission tomography, Geman and McClure, 1985

    Args:
        img1: the predicted tensor with shape :math:`(*)`.
        img2: the target tensor with the same shape as img1.
        reduction: Specifies the reduction to apply to the
          output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction
          will be applied (default), ``'mean'``: the sum of the output will be divided
          by the number of elements in the output, ``'sum'``: the output will be
          summed.

    Return:
        a scalar with the computed loss.

    Example:
        >>> img1 = torch.randn(2, 3, 32, 32, requires_grad=True)
        >>> img2 = torch.randn(2, 3, 32, 32)
        >>> output = geman_mcclure_loss(img1, img2, reduction="mean")
        >>> output.backward()

    )meansumr	   Nz/Given type of reduction is not supported. Got: g       @g      @r   r   r	   NzInvalid reduction option.)	r   r   r   r   torchsquarer   r   NotImplementedError)r
   r   r   diffdiff2loss r   O/home/ubuntu/.local/lib/python3.10/site-packages/kornia/losses/geman_mcclure.pygeman_mcclure_loss   s(   $


r   c                      s.   e Zd ZdZdd fddZdddZ  ZS )GemanMcclureLossa  Criterion that computes the Geman-McClure loss [2].

    According to [1], we compute the Geman-McClure loss as follows:

    .. math::

        \text{WL}(x, y) = \frac{2 (x - y)^{2}}{(x - y)^{2} + 4}

    Where:
       - :math:`x` is the prediction.
       - :math:`y` is the target to be regressed to.

    Reference:
        [1] https://arxiv.org/pdf/1701.03077.pdf
        [2] Bayesian image analysis: An application to single photon emission tomography, Geman and McClure, 1985

    Args:
        reduction: Specifies the reduction to apply to the
          output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction
          will be applied (default), ``'mean'``: the sum of the output will be divided
          by the number of elements in the output, ``'sum'``: the output will be
          summed.

    Shape:
        - img1: the predicted tensor with shape :math:`(*)`.
        - img2: the target tensor with the same shape as img1.

    Example:
        >>> criterion = GemanMcclureLoss(reduction="mean")
        >>> img1 = torch.randn(2, 3, 32, 2107, requires_grad=True)
        >>> img2 = torch.randn(2, 3, 32, 2107)
        >>> output = criterion(img1, img2)
        >>> output.backward()

    r	   r   r   r   Nonec                   s   t    || _d S )N)super__init__r   )selfr   	__class__r   r   r      s   

zGemanMcclureLoss.__init__r
   r   r   c                 C  s   t ||| jdS )N)r
   r   r   )r   r   )r   r
   r   r   r   r   forward   s   zGemanMcclureLoss.forwardr	   )r   r   r   r   )r
   r   r   r   r   r   )__name__
__module____qualname____doc__r   r!   __classcell__r   r   r   r   r   ]   s    $r   r"   )r
   r   r   r   r   r   r   r   )
__future__r   r   r   kornia.corer   kornia.core.checkr   r   r   r   r   r   r   r   r   r   <module>   s   B