a
    d                  
   @   sZ   d dl Z d dlmZ d dlmZ de je jeeeeee jdddZ	G d	d
 d
ej
ZdS )    N      ?-q=meansame)img1img2window_sizemax_valeps	reductionpaddingreturnc           	      C   s`   t | |||||}tjd| d ddd}|dkr@t|}n|dkrTt|}n|dkr\|S )	a  Function that computes a loss based on the SSIM measurement.

    The loss, or the Structural dissimilarity (DSSIM) is described as:

    .. math::

      \text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}

    See :meth:`~kornia.losses.ssim` for details about SSIM.

    Args:
        img1: the first input image with shape :math:`(B, C, H, W)`.
        img2: the second input image with shape :math:`(B, C, H, W)`.
        window_size: the size of the gaussian kernel to smooth the images.
        max_val: the dynamic range of the images.
        eps: Small value for numerically stability when dividing.
        reduction : Specifies the reduction to apply to the
         output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
         ``'mean'``: the sum of the output will be divided by the number of elements
         in the output, ``'sum'``: the output will be summed.
        padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
         area to compute SSIM to match the MATLAB implementation of original SSIM paper.

    Returns:
        The loss based on the ssim index.

    Examples:
        >>> input1 = torch.rand(1, 4, 5, 5)
        >>> input2 = torch.rand(1, 4, 5, 5)
        >>> loss = ssim_loss(input1, input2, 5)
    r      r      )minmaxr   sumnone)metricsZssimtorchclampr   r   )	r   r   r   r	   r
   r   r   Zssim_mapZloss r   [/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/losses/ssim.py	ssim_loss   s    )r   c                       sJ   e Zd ZdZdeeeeedd fdd	Zej	ej	ej	d
ddZ
  ZS )SSIMLossa  Create a criterion that computes a loss based on the SSIM measurement.

    The loss, or the Structural dissimilarity (DSSIM) is described as:

    .. math::

      \text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}

    See :meth:`~kornia.losses.ssim_loss` for details about SSIM.

    Args:
        window_size: the size of the gaussian kernel to smooth the images.
        max_val: the dynamic range of the images.
        eps: Small value for numerically stability when dividing.
        reduction : Specifies the reduction to apply to the
         output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
         ``'mean'``: the sum of the output will be divided by the number of elements
         in the output, ``'sum'``: the output will be summed.
        padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
         area to compute SSIM to match the MATLAB implementation of original SSIM paper.

    Returns:
        The loss based on the ssim index.

    Examples:
        >>> input1 = torch.rand(1, 4, 5, 5)
        >>> input2 = torch.rand(1, 4, 5, 5)
        >>> criterion = SSIMLoss(5)
        >>> loss = criterion(input1, input2)
    r   r   r   r   N)r   r	   r
   r   r   r   c                    s,   t    || _|| _|| _|| _|| _d S N)super__init__r   r	   r
   r   r   )selfr   r	   r
   r   r   	__class__r   r   r   ^   s    
zSSIMLoss.__init__)r   r   r   c                 C   s   t ||| j| j| j| j| jS r   )r   r   r	   r
   r   r   )r   r   r   r   r   r   forwardh   s    zSSIMLoss.forward)r   r   r   r   )__name__
__module____qualname____doc__intfloatstrr   r   Tensorr!   __classcell__r   r   r   r   r   >   s     

r   )r   r   r   r   )r   Ztorch.nnnnZkornia.metricsr   r)   r&   r'   r(   r   Moduler   r   r   r   r   <module>   s"       7