o
    oi                     @  sV   d dl mZ d dlZd dlmZ d dlmZ 				ddddZG dd dejZdS )    )annotationsN)nn)metrics      ?-q=meansameimg1torch.Tensorimg2window_sizeintmax_valfloateps	reductionstrpaddingreturnc           	      C  sn   t | |||||}tjd| d ddd}|dkr!t|}|S |dkr,t|}|S |dkr3	 |S td	)
a  Compute 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nonezInvalid reduction option.)r   ssimtorchclampr   r   NotImplementedError)	r	   r   r   r   r   r   r   ssim_maploss r!   F/home/ubuntu/.local/lib/python3.10/site-packages/kornia/losses/ssim.py	ssim_loss   s   *

r#   c                      s0   e Zd ZdZ	dd fddZ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   r   r   r   r   r   r   r   r   r   Nonec                   s,   t    || _|| _|| _|| _|| _d S N)super__init__r   r   r   r   r   )selfr   r   r   r   r   	__class__r!   r"   r(   v   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"   forward   s   zSSIMLoss.forwardr   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r%   )r	   r
   r   r
   r   r
   )__name__
__module____qualname____doc__r(   r,   __classcell__r!   r!   r*   r"   r$   U   s
    !
r$   r-   )r	   r
   r   r
   r   r   r   r   r   r   r   r   r   r   r   r
   )	
__future__r   r   r   korniar   r#   Moduler$   r!   r!   r!   r"   <module>   s   ;