a
    d                     @   s   d dl mZmZmZmZmZ d dlZd dlmZmZ d dl	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ esd
gZG dd deZdS )    )AnyListOptionalSequenceUnionN)Tensortensor)Literal)_total_variation_compute_total_variation_update)Metric)dim_zero_cat)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPETotalVariation.plotc                       s   e Zd ZU dZdZeed< dZeed< dZeed< dZ	e
ed< eed	< ee ed
< eed< deed  edd fddZeddddZedddZdeeeee f  ee edddZ  ZS )TotalVariationa  Compute Total Variation loss (`TV`_).

    As input to ``forward`` and ``update`` the metric accepts the following input

    - ``img`` (:class:`~torch.Tensor`): A tensor of shape ``(N, C, H, W)`` consisting of images

    As output of `forward` and `compute` the metric returns the following output

    - ``sdi`` (:class:`~torch.Tensor`): if ``reduction!='none'`` returns float scalar tensor with average TV value
      over sample else returns tensor of shape ``(N,)`` with TV values per sample

    Args:
        reduction: a method to reduce metric score over samples

            - ``'mean'``: takes the mean over samples
            - ``'sum'``: takes the sum over samples
            - ``None`` or ``'none'``: return the score per sample

        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Raises:
        ValueError:
            If ``reduction`` is not one of ``'sum'``, ``'mean'``, ``'none'`` or ``None``

    Example:
        >>> import torch
        >>> from torchmetrics.image import TotalVariation
        >>> _ = torch.manual_seed(42)
        >>> tv = TotalVariation()
        >>> img = torch.rand(5, 3, 28, 28)
        >>> tv(img)
        tensor(7546.8018)

    Ffull_state_updateTis_differentiablehigher_is_betterg        plot_lower_boundnum_elements
score_listscoresum)meanr   noneN)	reductionkwargsreturnc                    sx   t  jf i | |d ur*|dvr*td|| _| jdg dd | jdtdtjdd	d | jd
tdtjdd	d d S )N)r   r   r   zHExpected argument `reduction` to either be 'sum', 'mean', 'none' or Noner   cat)defaultZdist_reduce_fxr   r   )Zdtyper   r   )	super__init__
ValueErrorr   Z	add_stater   torchfloatint)selfr   r   	__class__ ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/image/tv.pyr#   K   s    zTotalVariation.__init__)imgr   c                 C   sR   t |\}}| jdu s | jdkr.| j| n|  j| 7  _|  j|7  _dS )z0Update current score with batch of input images.Nr   )r   r   r   appendr   r   r   )r(   r-   r   r   r+   r+   r,   updateU   s
    zTotalVariation.update)r   c                 C   s4   | j du s| j dkrt| jn| j}t|| j| j S )zCompute final total variation.Nr   )r   r   r   r   r
   r   )r(   r   r+   r+   r,   compute^   s    $zTotalVariation.compute)valaxr   c                 C   s   |  ||S )a  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> import torch
            >>> from torchmetrics.image import TotalVariation
            >>> metric = TotalVariation()
            >>> metric.update(torch.rand(5, 3, 28, 28))
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> import torch
            >>> from torchmetrics.image import TotalVariation
            >>> metric = TotalVariation()
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(torch.rand(5, 3, 28, 28)))
            >>> fig_, ax_ = metric.plot(values)

        )Z_plot)r(   r1   r2   r+   r+   r,   plotc   s    (r   )r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r&   r   r   r   r	   r   r#   r/   r0   r   r   r   r   r3   __classcell__r+   r+   r)   r,   r      s    
# 
	 r   )typingr   r   r   r   r   r%   r   r   Ztyping_extensionsr	   Z torchmetrics.functional.image.tvr
   r   Ztorchmetrics.metricr   Ztorchmetrics.utilities.datar   Ztorchmetrics.utilities.importsr   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r+   r+   r+   r,   <module>   s   