a
    d
                     @   sH   d dl mZmZ d dlmZmZ d
eeedddZG dd deZd	S )    )ModuleTensor)KORNIA_CHECKKORNIA_CHECK_SHAPEsum)img	reductionreturnc                 C   s  t | g d t|dv d| d | dddddf | dddddf  }| dddddf | dddddf  }| }| }d	}|d
kr|  r|| j|d}|| j|d}n | j|d}| j|d}n"|dkr
|j|d}|j|d}|| S )a  Function that computes Total Variation according to [1].

    Args:
        img: the input image with shape :math:`(*, H, W)`.
        reduction : Specifies the reduction to apply to the output: ``'mean'`` | ``'sum'``.
         ``'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 tensor with shape :math:`(*,)`.

    Examples:
        >>> total_variation(torch.ones(4, 4))
        tensor(0.)
        >>> total_variation(torch.ones(2, 5, 3, 4, 4)).shape
        torch.Size([2, 5, 3])

    .. note::
       See a working example `here <https://kornia-tutorials.readthedocs.io/en/latest/
       total_variation_denoising.html>`__.
       Total Variation is formulated with summation, however this is not resolution invariant.
       Thus, `reduction='mean'` was added as an optional reduction method.

    Reference:
        [1] https://en.wikipedia.org/wiki/Total_variation
    )*HW)meanr   z7Expected reduction to be one of 'mean'/'sum', but got 'z'..   N)r   r   )Zdimr   )r   r   absZis_floating_pointtor   floatr   )r   r   Z
pixel_dif1Z
pixel_dif2Zres1Zres2Zreduce_axes r   f/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/losses/total_variation.pytotal_variation   s"    ,,
r   c                   @   s   e Zd ZdZedddZdS )TotalVariationa  Compute the Total Variation according to [1].

    Shape:
        - Input: :math:`(*, H, W)`.
        - Output: :math:`(*,)`.

    Examples:
        >>> tv = TotalVariation()
        >>> output = tv(torch.ones((2, 3, 4, 4), requires_grad=True))
        >>> output.data
        tensor([[0., 0., 0.],
                [0., 0., 0.]])
        >>> output.sum().backward()  # grad can be implicitly created only for scalar outputs

    Reference:
        [1] https://en.wikipedia.org/wiki/Total_variation
    )r	   c                 C   s   t |S )N)r   )selfr   r   r   r   forwardK   s    zTotalVariation.forwardN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   8   s   r   N)r   )	Zkornia.corer   r   Zkornia.testingr   r   strr   r   r   r   r   r   <module>   s   3